linux fork()

#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>

int main ()
{
   pid_t pid;
   printf("fork!/n");
   pid=fork();

   if (pid < 0)         
    printf("error in fork!/n");
   else if (pid == 0)
    printf("i am the child process,  my process id is %d/n",getpid());
   else
    printf("i am the parent process, my process id is %d/n",getpid());
          
   return 0;
}

 

結果:

fork!
i am the child process, my process id is 4286
i am the parent process, my process id is 4285

 

 

 

#include <unistd.h>
#include <sys/types.h>
#include <stdio.h>

int main ()
{
   pid_t pid;
   printf("fork!");
   pid=fork();

   if (pid < 0)         
    printf("error in fork!/n");
   else if (pid == 0)
    printf("i am the child process,  my process id is %d/n",getpid());
   else
    printf("i am the parent process, my process id is %d/n",getpid());
          
   return 0;
}

 

結果:

fork!i am the child process, my process id is 4286
fork!i am the parent process, my process id is 4285

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章