top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is use of fork() function in C?

+1 vote
644 views
What is use of fork() function in C?
posted May 2, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+1 vote

In short fork function create a child or we can say creates a duplicate process or clone the process, which shares the execution stack. The difference between the parent and the child is the return value of the function. In child process fork() returns 0 and in parent it returns the child pid.

Example

pid_t pid = fork();

if(pid == 0) {
    printf("this is a child: my new unique pid is %d\n", getpid());
} else {
    printf("this is the parent: my pid is %d and I have a child with pid %d \n", getpid(), pid);
}
answer May 2, 2016 by Rajan Paswan
Can you explain me the program??
Similar Questions
+7 votes
#include<stdio.h>

int &fun()
{
   static int x;
   return x;
}   

int main()
{
   fun() = 10;
   printf(" %d ", fun());

   return 0;
}

It is fine with c++ compiler while giving error with c compiler.

...