进程创建fork()

时间:2023-03-09 03:48:06
进程创建fork()

简单进程创建例子:

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h> int main()
{
pid_t pid;
pid_t ppid;
switch(pid = fork())//子进程返回0,父进程返回子进程的进程ID
{
case -:
return -;
case :
printf ("in child pid = %d\n",getpid());
printf ("in child parent pid = %d\n",getppid());
return ;
default:
printf ("in parent child pid = %d\n",pid);
printf ("in parent pid = %d\n",getpid());
wait (&pid);
wait (&ppid);
return ;
}
}