fork函數測試

 


#include <stdio.h>.net

#include <sys/types.h>get

#include <unistd.h>it


int main(int argc,char **agrv)io

{class

    pid_t pid;test

    char * msg;gc

    int k;error


    printf("Process creation study \n");di


    pid = fork();process


    switch(pid)

    {

         case 0:

             msg = "child process is running";

             k=3;

             break;


         case -1:

             msg = "error msg..";

             break;


         default:

             msg = "Parent process is running ";

             k=5;

             break;

    }

    while(k>0)

    {

          puts(msg);

          sleep(1);

          k--;

    }

}

執行結果:

[root@localhost pfork]# ./forktest2.bin 

Process creation study 

child process is running

Parent process is running 

child process is running

Parent process is running 

child process is running

Parent process is running 

Parent process is running 

Parent process is running 

相關文章
相關標籤/搜索