791 views
1 votes
1 votes
void forkexample() 
{ 
    // child process because return value zero 
    if (fork() == 0) 
        printf("Hello from Child!\n"); 
  
    // parent process because return value non-zero. 
    else
        printf("Hello from Parent!\n"); 
} 
int main() 
{ 
    forkexample(); 
    return 0; 
} 

 

 

 

Please someone explain how things are working?

1 Answer

1 votes
1 votes
OUTPUT:

Hello from child!

Hello from parent!

or

Hello from parent!

Hello from child!

Because we dont know wheather parent executed first or child.

Related questions

3 votes
3 votes
2 answers
1
iarnav asked Oct 18, 2018
1,874 views
A process executes the following segment of code :for(i = 1; i <= n; i++) fork (); fork ();The number of new processes created is
1 votes
1 votes
2 answers
3
junaid ahmad asked Jan 1, 2019
1,455 views
int main(void) { int var1=100; int pid; if(pid==fork()) var1=200; fork(); printf("%d",var1); return 0; }what could be the output ?a)100 100 200 200b)200 200 200 200c)none...