我试图在c
中编写一个简单的SMS服务器,并且有一个生成2个进程的函数(一个用于监听,另一个用于写入),但父进程似乎没有正在运行(调试消息未被打印)尽管fork调用没有生成错误...
void connection (int sock, char username[], int *rear, int *front, msg queue_array[])
{
int n, pid;
char buffer[256], dest[64];
pid = fork();
if(pid < 0) error("ERROR on fork call");
while(1)
{
if(pid > 0)
{
bzero(buffer,256);
n = read(sock,buffer,255);
if (n < 0) error("ERROR reading from socket");
printf("Here is the full message: %s\n",buffer);
//n = write(sock,"I got your message!\n",18);
//if (n < 0) error("ERROR writing to socket");
//interpret(buffer, username);
//TEMPORARY!!!!!
msg *temp;
temp = (msg *)(malloc(sizeof(msg)));
strcpy(temp->dest, "destiny");
strcpy(temp->text, "blahblah");
insert(temp, rear, front, queue_array); //Por a mensagem na queue
}
else
{
sleep(1);
/*Verificar se a file está vazia*/
printf("hi?");
printf("yooooo: %d", *front);
if(*front != -1)
{
/*verificar se a primeira mensagem da queue é destinada a esta pessoa*/
strcpy(dest, queue_array[*front].dest);
printf("%s != destiny", dest);
if(strcmp(dest, "destiny") == 0)
{
printf("passou o teste\n");
strcpy(buffer, queue_array[*front].text);
delete(rear, front);
n = write(sock,buffer,strlen(buffer));
if (n < 0) error("ERROR writing to socket");
}
}
}
}
}
非常感谢任何帮助。