1.

What Do We Expect?

Answer»

The child detects an EOF from its stdin, it closes the socket (assuming CONNECTION break) and exits. The server in its turn detects EOF, closes connection and exits. The parent detects EOF, makes the wait() system call and exits. What do we see instead? The socket instance in the parent process is still opened for writing and reading, though the parent never writes. The server never detects EOF and waits for more data from the client forever. The parent never sees the connection is CLOSED and hangs forever and the server hangs too. Unexpected deadlock!

You should change the client fragment as follows:

if( fork() ) { 

/* The child */

while( gets(BUFFER) }

write(s,buffer,strlen(buffer));

shutdown(s,1);

/* Break the connection

for writing, The server will detect EOF now.

Note: reading from the socket is still allowed.

The server may SEND some more data

after receiving EOF, why not? */

exit(0);

}

The child detects an EOF from its stdin, it closes the socket (assuming connection break) and exits. The server in its turn detects EOF, closes connection and exits. The parent detects EOF, makes the wait() system call and exits. What do we see instead? The socket instance in the parent process is still opened for writing and reading, though the parent never writes. The server never detects EOF and waits for more data from the client forever. The parent never sees the connection is closed and hangs forever and the server hangs too. Unexpected deadlock!

You should change the client fragment as follows:

if( fork() ) { 

/* The child */

while( gets(buffer) }

write(s,buffer,strlen(buffer));

shutdown(s,1);

/* Break the connection

for writing, The server will detect EOF now.

Note: reading from the socket is still allowed.

The server may send some more data

after receiving EOF, why not? */

exit(0);

}



Discussion

No Comment Found