fork()
pid_t fork();
返回值:新创建进程的进程ID,0表示子进程,其他大于0的数表示父进程,负数表示失败
#include
#include
#include
#include
int main()
{
pid_t pid; pid = getpid(); printf("parentpid = %d\n",pid); pid = fork(); if(pid<0){ printf("fork error\n"); return 0; } if(0==pid){ printf("this is child, pid = %d\n",getpid()); sleep(20); }else{ printf("this is parent, pid = %d,child pid=%d \n",getpid(),pid); sleep(20); } return 0; }