pid_t fork(void)
fork函数调用成功,返回两次
(1)返回值为0,代表当前进程是子进程
(2)返回值为非负数,代表当前进程是父进程
(3)调用失败,则返回-1
代码如下:
#include <stdio.h> #include <sys/types.h> #include <unistd.h> int main() { pid_t pid; printf("father: id=%d\n",getpid()); pid = fork(); if(pid > 0) { printf("this is father print, pid = %d\n",getpid()); } else if(pid == 0){ printf("this is child print,child pid = %d\n",getpid()); } return 0; }