Thread: Child dying after first line of code

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    5

    Child dying after first line of code

    Hi there!

    I'm writing a little program that needs to fork 2 times and then the child has to run some code. It should be quite an easy task, but my child seems to die right after the first line of code it runs..

    Code:
    int j = 0;
            for (j = 0; j < nrOfClients; j++) // let's assume nrOfClients = 2
    	{
    		int iPid = fork();
    
    		switch ( iPid ) {
    			case 0 :
    					clientTCP();
    			case -1:
    				perror("Error while forking");
    				exit(1);
    			//default:
    				//printf("Child %d created \n", j);
    			}
    	}
    	sleep(30000);
    Code:
    void clientTCP(void)
    {
    	printf("prints this \n");
    	int sock;
    	if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1)
    	{
    		printf("but doesn't come here");
    		perror("Error creating socket");
    		exit(1);
    	}
    
    	printf("or here.");
    	int k;
    	for (k = 0; k < nrOfRequests; k++)
    	{
    		char messageReceived[nrOfBytes];
    		write(sock, messageOut, nrOfBytes);
    
    		printf("clientTCP 2");
    		int bytesReceived;
    		bzero(&messageReceived, sizeof(messageReceived));
    		if ((bytesReceived = read(sock, messageReceived, nrOfBytes)) == 0)
    		{
    			fprintf(stderr, "The server has terminated the connection. \n");
    			close(sock);
    			exit(0);
    		}
    
    		printf("clientTCP 3");
    		printf("Child %d received %d bytes \n", getpid(), bytesReceived);
    	}
    It's probably a stupid mistake, but I'd appreciate it if someone can help me get further.
    Last edited by Xandux; 03-07-2010 at 03:59 PM.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    If that first bit of code is in main(), main() has nothing to do after the fork and so exits. When main exits, the parent process is done (altho the child will still be running).

    You need to use waitpid() to wait for the child to exit.

    Code:
    	int i;
    	pid_t pid = fork();
    
    	if (!pid) {
    		for (i=0;i<10;i++) {
    			puts("hello world");
    			sleep(1);
    		}
                    return 0;
    	} else waitpid(pid);
    
    	puts("done");
    Try it with and without the else clause.
    Last edited by MK27; 03-07-2010 at 03:57 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    Ah oops, There's a sleep(30000) beneath it atm.. so child's should have more than enough time. After some more testing I did the following:
    - Removed all code from clientcp except for the sock and printf's.

    Now I found out that the child actually goes into all the cases on the fork.. So it does case 0, then case -1 and even case default when i uncomment it..
    How can a child do this?

    (edited main post to show the sleep(30000))
    Last edited by Xandux; 03-07-2010 at 04:00 PM.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    5
    Damn, I totally forgot the connect() call after the socket() call. Apparently the program won't run unless there is a connect before a write. Makes sense, but the way the program reacts to it just doesn't..

    The problem with the child going into all cases after the fork was an easy fix too, I had to exit the child after the childTCP was done..

    Everything fixed now, thanks for the reply .

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Xandux View Post
    Damn, I totally forgot the connect() call after the socket() call. Apparently the program won't run unless there is a connect before a write. Makes sense, but the way the program reacts to it just doesn't..

    The problem with the child going into all cases after the fork was an easy fix too, I had to exit the child after the childTCP was done..

    Everything fixed now, thanks for the reply .
    From the write() manual, at errors:
    EPIPE fd is connected to a pipe or socket whose reading end is closed.
    When this happens the writing process will also receive a SIG‐
    PIPE signal. (Thus, the write return value is seen only if the
    program catches, blocks or ignores this signal.)
    So I expect your child receives a SIGPIPE signal, of which the default action is to kill the process. If you would have handled this signal, everything would have worked as expected.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  2. detecting numbers in a code of line
    By aama100 in forum C++ Programming
    Replies: 7
    Last Post: 01-23-2008, 10:24 AM
  3. Need the missing line of code
    By mayhem in forum C Programming
    Replies: 3
    Last Post: 06-20-2005, 04:21 PM
  4. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM