这节主要讲一个系统调用:fork
pid_t fork(void);

#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#define ERR_EXIT(m) do { perror(m); exit(EXIT_FAILURE); } while(0)
int main(int argc, char *argv[])
{
signal(SIGCHLD, SIG_IGN);//这个信号量可以防止产生僵尸进程
printf("before fork pid = %d\n", getpid());
pid_t pid;
pid = fork();
if (pid == -1) //创建进程失败
ERR_EXIT("fork error");
if (pid > 0) //父进程中返回子进程的pid
{
//这是在父进程空间里执行
printf("this is parent pid=%d childpid=%d\n", getpid(), pid);
sleep(100);
}
else if (pid == 0) //返回0就是在子进程的空间里运行了
{
printf("this is child pid=%d parentpid=%d\n", getpid(), getppid());
}
return 0;
}
虽然注释很多了但是也要简单的介绍一下:
首先这是一个多组合的例子:
1、可以去掉signal信号量和sleep(100),看一下程序的运行结果,主要看相关进程号的打印。有可能父进程先结束。
2、在父进程里边加上一个sleep(1),试看一下进程号的打印,肯定会不同。
3、加上signal的目的就是为了防止僵尸进程的产生。具体会在信号量哪一节讲到。(特备注意)
原文:http://www.cnblogs.com/DamonBlog/p/4370540.html