#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main(int argc,char **agrv)
{
pid_t pid;
char * msg;
int k;
printf("Process creation study \n");
pid = fork();
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