Find the answer to your Linux question:
Results 1 to 3 of 3
Hi guys, I have the following code: Code: int main(int argc, char *argv[]) { int pid1,pid2,i=0; pid1=fork(); i+=2; if(!pid1) i++; if(i%3) pid2=fork(); if (pid2==0) { printf("sea \n "); i-=1; } ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    4

    [Urgent]Very basic problem with fork() using c


    Hi guys,
    I have the following code:

    Code:
    int main(int argc, char *argv[]) 
    {
    	int pid1,pid2,i=0;
    
    
    	pid1=fork();
    
    	i+=2;
    	
    	if(!pid1) i++;
    	if(i%3) pid2=fork();
    	if (pid2==0)
    	{
    		printf("sea \n ");
    		i-=1;
    	}
    
    		
    	if(i>=2) printf("Mountain\n");
    }
    Please, can someone enlight me and explain WHY the output is
    sea
    mountain
    mountain
    sea ??

    Why a 4 child is forked ? From where ?!

  2. #2
    Just Joined!
    Join Date
    Aug 2012
    Posts
    4
    the if(i%3)

    if the result is "ZERO", it should be read as FALSE by the program, right ? But even if the result is == 0 its like if the condition was true.

    Can someone explain it to me ?

  3. #3
    Just Joined! krokoziabla's Avatar
    Join Date
    Sep 2012
    Location
    Russia
    Posts
    20
    I think this happens because the first child checks pid2 variable although no fork has been done.
    Code:
    int main(int argc, char *argv[])                  Parent                                                                 1st child                                                            2nd child
    {
    	int pid1,pid2,i=0;                             pid1 = uninit, pid2 = uninit, i = 0
    
    
    	pid1=fork();                                   --> child 1 forked from Parent
    
    	i+=2;                                            pid1 = positive, pid2 = uninit, i = 2                             pid1 = 0, pid2 = uninit, i = 2
    	
    	if(!pid1) i++;                                  pid1 = positive, pid2 = uninit, i = 2                             pid1 = 0, pid2 = uninit, i = 3
    	if(i%3) pid2=fork();                         --> child 2 forked from Parent                                   --> nothing happens
    	if (pid2==0)                                   pid1 = positive, pid2 = positive, i = 2                          pid1 = 0, pid2 = uninit, i = 3                                pid1 = positive, pid2 = 0, i = 2  
    	{
    		printf("sea \n ");                     does nothing                                                           prints "sea", because pid2 = uninit most likely 0      prints "sea", pid2 is 0
    		i-=1;                                    pid1 = positive, pid2 = positive, i = 2                          pid1 = 0, pid2 = uninit, i = 2                                pid1 = positive, pid2 = 0, i = 1
    	}
    
    		
    	if(i>=2) printf("Mountain\n");             prints "Mountain"                                                     prints "Mountain"                                               does nothing
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •