Hello, I'm trying to implement a version of a bucketSort (kinda) server/client, but I'm having a VERY hard time on making the server behave correctly, when talking to the children, after it forks.
The server is kinda big (300+ lines), so I won't post it here, but here's what I'm doing.
1)create a pipe using
int pipefd[2];
if(pipe(pipefd)==-1){
perror("pipe");
exit(EXIT_FAILURE);
}
2)all the socket(),bind(),listen() stuff
3)the main loop, now, here's the catch I think
int done = 0;
.
.
.
while(done!=1){
clLen = sizeof(clAddr);
if((clientsd=accept(serversd,(struct sockaddr *)&clAddr,&clLen)) == -1){
perror("accept");
close(serversd);
exit(EXIT_FAILURE);
}
if((cpid = fork()) == -1){
perror("fork");
close(clientsd);
close(serversd);
exit(EXIT_FAILURE);
}
if(cpid==0){
.
.
//It keeps reading for comands on a variable named cmd (this works)
if(strncmp(cmd,"STOP",4)==0){
write(pipefd[1],"1",1);
_exit(0);
}
}else{
close(clientsd);
read(pipefd[0],&tmp,1);
done=atoi(tmp);
}
}
}
Now, if I start this server and have only one connection, and this connection goes DIRECTLY sending a STOP signal, it works, but, if this connection does something else, or there's another connection (let's say a total of 9 connection for instance), then I have to send 9 diferent STOPs signals to actually kill the server. Where I want just to have to send 1 instead. Also, I want that if the client send some data, that the server acnowledges as being the last data it will ever recieve, to send a done to the main server and continue with all the rest (showing the data it recieved for instance). I thought that by using pipes and read/write on the pipe this would work, but as far as I could see, the read() function is a blocking function, and the main server keeps waiting for something to read, and this could be making some trouble?
I'm really out of ideias here, and really newbie when it comes to fork() and signals processing.
Sorry for the long post, does anyone knows what can possible be wrong? I can post the complete code somewhere over the net if somebody want's to help me.
Also, another question, is there any good way of debuging fork() programs?
Thanks a lot.