lte__ lte__ - 6 days ago 5
C Question

C forking doesn't work

I'm trying to make a server where it listens to 2 ports and does something with the incoming data, while printing out a dot every second to let people know it's still running. I want to do this by using

fork()
. I'm trying:

p = fork();
if(p == 0){
if( getsockname( sock1, (struct sockaddr *) &name1, &length1) == -1 ) {
perror( "getting socket name" );
exit(3);
}
printf( "Socket port #%d\n", ntohs( name1.sin_port ) );

if( getsockname( sock2, (struct sockaddr *) &name2, &length2) == -1 ) {
perror( "getting socket name" );
exit(3);
}
printf( "Socket port #%d\n", ntohs( name2.sin_port ) );

}
else if(p > 0){
printf("inhere"); //
p1 = fork();
if(p1 > 0) {
while(TRUE) {
write(".");
sleep(1);
}
}
...


This doesn't even print the
inhere
string in the child process. What am I doing wrong?

Answer

The child process gets a 0 as the return value. You might call an fflush(0) right after the printf call and see if it helps.

Comments