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