Thread: fork problem!!!

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    20

    fork problem!!!

    First of all hello,i'm new to this forum!
    I have a prob with the fork function(or i think that i have) and i am confused.

    I have this program

    Code:
    void bypass_sigint(int sig_no)  
    {  
          if(sig_no==SIGCHLD)
          {
          	int status;
          	int pid;
          	pid=wait(&status);
    		printf("child(%d) finished with code:%d\n",pid,status);
    	  }
    
    }  
    
    int main(int args,char**array)
    {
    int fork_res;
    
    
           struct sigaction sa;  
           memset(&sa, 0, sizeof(sa)); 
           sa.sa_handler = &bypass_sigint;   
           sigaction(SIGCHLD,&sa,NULL);
    
    	for(int i=1;i<2;i++)
    	{
    
    		if((fork_res=fork())==0)
    		{
    			printf("created fork:%d\n",getpid());
    			exit(0);
    		}//end child proccess
    		else if(fork_res==-1)
    		{
    			printf("fork error\n");
    		}
    		else
    		{
    			printf("PARENT\n");
    		}//end parent proccess
    					
    	}
    
    return 0;
    }
    For is stupid in my prog but i use that to test what is going on when a create proccesses with fork multiple times(in this code is only one). The results of my prog are not the same every time i run it, not because the pid changing but look at this

    1)first time i get:

    created fork:17046
    child(17046) finished with code:0
    PARENT


    2)second time:
    PARENT
    created fork:17048

    and it goes like this, nothing standard.

    this example is simple, i'm making a server for file sharing and now i'm in fork stage of programming.when a client is connected a new proccess is created and server keeps new pid just to control it. The problem was that my signal function prints more pids than i created. Maybe my fork creates more pids but i don't know what i did wrong. I decided to make a simple program like the above and i see that something is wrong! please if you have any ideas help me! i'm confused

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    After a fork(), there are two processes running. Whichever runs first is undefined. Sometimes the child, sometimes the parent.
    In your first case, the child runs first. It terminates and displays that.
    In your second case, the parent runs first. But here it terminates before the child can terminate. So, there's never any message of the child terminating.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    20
    Thanks a lot man for your answer. I was thinking about this but i don't know how and if the signal function is called. I put a sleep function on parent for 3 secs and it prints normal things. Now it's ok.

    On my main server program i use while , so parent proccess never ends and childs are used to proccess client's

    have a look in this code

    Code:
    void bypass_sigint(int sig_no)  
    {  
          if(sig_no==SIGCHLD)
          {
          	printf("%d\n",sig_no);
          	int status;
          	int pid;
          	pid=wait(&status);
    	printf("child(%d) finished with code:%d\n",pid,status);
    	  }
    
    }  
    
    on my main programm
    
    while(1)
    {
    	server_accept();
    	if( 0 == ( fork_res = fork() ))
    	{
    	        printf("created fork: %d\n",getpid());
    			
    
    			while(1)
    			{
    				if((read(client_sockfd,&msg,sizeof(msg)))==sizeof(msg))
    			        {
                                        //here a chlid proccess a client's request
                                       
    				}
    				else
    				{
    					break;
    				}	
    											
    			}
    				
    		CLOSE_CONNECTION:
    		close(client_sockfd);
    		return 0;
    	}//end child proccess
    	else if(fork_res==-1)
    	{
    		printf("fork error\n");
    	}
    	else
    	{
    		printf("PARENT\n");
    		//sleep(1);
    		close(client_sockfd);
    		sleep(3);
    	}//end parent proccess
    					
    				
    }
    Explanation:
    Server is waiting at my accept function, when a client is connected it forks
    1)the parent (else) closes client and goes to my accept function(after a sleep(3) waitng for another client.
    2)child goes to a while loop where tries to read a standard message from client
    if not, retries or closes the connection and exits/returns.

    on a simple client's request thing goes well- one child proccess is created one chiild dies
    when the request is to send a file(from server to client) child calls a send_file function

    look what server prints on:

    a)simple request like "are you ok?":
    PARENT
    created fork: 11065
    child(11065) finished with code:0

    b)a send file request:
    PARENT
    created fork: 11089
    child(11089) finished with code:0
    created fork: 11097
    child(11097) finished with code:0
    PARENT

    the (b) it happens when the file is big(10) MB , on smaller files the result is like (a)

    My question is, it seems that it forks well but on (b) how 2 child proccesses are created?
    why? all childs are died so i have no prob with zobies but i want to count clients from pids of childs and i see more pids than i expected. Does happen something with my send_file function?(send file is a simple function which uses sendfile from <sys/sendfile.h>

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    20
    I found the problem. I tested my accept function and it returns sometimes error, this makes fork twice. One check of returned value of accept fixed my prob.
    i'm stupid sorry!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  2. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  3. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. Replies: 5
    Last Post: 11-07-2005, 11:34 PM

Tags for this Thread