1. /* example12-1.c 使用fork()函数创建1个新进程.*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main( )
{
pid_t pid;
pid = fork(); /* 创建新的进程*/
if(pid<0) /* 如果进程创建失败,输出错误信息并退出 */
{
printf("forkerror.\n");
exit(1);
}
if(pid==0) /* 子进程*/
printf("Thisis the child process.\n");
else /*父进程 */
printf("Thisis the parent process.\n");
return 0;
}
编译命令:gcc -o process1 process1.c
命令说明:gcc GNU的C语言编译器,-o 后是可执行文件名
运行程序:/home/by-402/process1
运行结果:This is the child process
This is the parent process
2. /* example12-2.c 下面的程序使用wait( ) 将父进程阻塞,直到子进程执行结束。*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>