What is the purpose of fork()?

前端 未结 15 1268
悲哀的现实
悲哀的现实 2020-12-12 11:11

In many programs and man pages of Linux, I have seen code using fork(). Why do we need to use fork() and what is its purpose?

15条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-12 11:33

    Fork() is used to create new processes as every body has written.

    Here is my code that creates processes in the form of binary tree.......It will ask to scan the number of levels upto which you want to create processes in binary tree

    #include 
    #include 
    #include   
    int main() 
    {
    int t1,t2,p,i,n,ab;
    p=getpid();                
    printf("enter the number of levels\n");fflush(stdout);
    scanf("%d",&n);                
    printf("root %d\n",p);fflush(stdout);
    for(i=1;i

    OUTPUT

      enter the number of levels
      3
      root 20665
      child pid 20670   parent pid 20665
      child pid 20669   parent pid 20665
      child pid 20672   parent pid 20670
      child pid 20671   parent pid 20670
      child pid 20674   parent pid 20669
      child pid 20673   parent pid 20669
    

提交回复
热议问题