C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-08-2009, 08:45 PM   #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
xmariux is offline   Reply With Quote
Old 03-09-2009, 05:07 AM   #2
Registered User
 
Join Date: Oct 2008
Posts: 452
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.
EVOEx is offline   Reply With Quote
Old 03-09-2009, 07:44 AM   #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>
xmariux is offline   Reply With Quote
Old 03-09-2009, 08:16 AM   #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!!!!
xmariux is offline   Reply With Quote
Reply

Tags
fork

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Someone having same problem with Code Block? ofayto C++ Programming 1 07-12-2007 08:38 AM
A question related to strcmp meili100 C++ Programming 6 07-07-2007 02:51 PM
WS_POPUP, continuation of old problem blurrymadness Windows Programming 1 04-20-2007 06:54 PM
Laptop Problem Boomba Tech Board 1 03-07-2006 06:24 PM
Sorting problem.. well actually more of a string problem fatdunky C Programming 5 11-07-2005 11:34 PM


All times are GMT -6. The time now is 08:55 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22