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> /* 注意包含该头文件*/
int main()
{
pid_t pid;
int status, i = -1;
pid = fork(); /* 创建新的进程*/
if(pid<0) /* 如果进程创建失败,输出错误信息并退出 */
{
printf("fork error.\n");
exit(1);
}
if(pid==0) /* 子进程 */
{
printf("This is the childprocess.\n");
}
else /* pid=子进程的pid 且>0,父进程*/
{
sleep(1); /* 暂停进程的执行,直到括号内的时间(秒) */
printf("This is the parentprocess.\n");
printf("Waiting for child process ... \n");
if(pid !=wait(&status)) /* 子进程结束后应向status地址返回自己的进程号 */
{
printf("wait error.\n");
exit(1);
}
if(~WIFEXITED(status)) /* p102,宏:若子进程是正常终止,它返回1个非零值。 */
{
i = WEXITSTATUS(status); /* 宏:若WIFEXITED非零,它返回子进程的退出码。*/
}
printf("child's pid =%d\n", pid); /* 输出子进程标识符 */
printf("exit status =%d\n", i); /* 输出子进程结束状态值 */
}
return 0;
}
编译命令:gcc -o process2 process2.c
运行程序:/home/by-402/process2
运行结果:
This is the child process
This is the parent process
Waiting for child process…
child’s pid = 4375
exit status=0
3. /*example12-10.c 下面的程序获取并输出子进程和父进程的信息。*/
#include<stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
int status;
pid = fork(); /* 创建新的进程*/
if(pid<0) /* 如果进程创建失败,输出错误信息并退出 */
{
printf("fork error.\n");
exit(1);
}
if(pid==0) /* 子进程 */
{
printf("ChildProcess:\n");
printf("pid = %d\n",getpid()); /* 获取当前进程的标识符 */
printf("ppid = %d\n",getppid()); /* 获取父进程的标识符 */
printf("gid = %d\n",getpgrp()); /* 获取进程的组标识符 */
exit(1);
}
else /* 父进程 */
{
printf("ParentProcess:\n");
printf("pid = %d\n",getpid()); /* 获取当前进程的标识符 */
printf("ppid = %d\n",getppid()); /* 获取父进程的标识符 */
printf("gid = %d\n",getpgrp()); /* 获取进程的组标识符 */
if(pid != wait(&status)) /* 等待进程结束 */
{
printf("wait error.\n");
exit(1);
}
}
return 0;
}
运行结果:
child process:
pid=2171
pid=2170
gid=2170
parent process:
pid=2170
ppid=2067
gid=2170
4. /*example12-4 下面的程序在子进程中使用 exec( )函数执行 1 个 Linux 命令并设定命令输出的格式。*/
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
int status;
pid = vfork(); /* 使用vfork函数创建新进程 */
if(pid<0) /*如果进程创建失败,输出错误信息并退出 */
{
printf("vfork error.\n");
exit(1);
}
if(pid==0) /* 新进程 */
{
execlp("ps","ps", "-o", "pid,ppid,comm", NULL); /* 路径,命令,输出及格式 */
}
else /*父进程 */
{
if(pid != wait(&status)) /* 等待进程结束 */
{
printf("wait error.\n");
exit(1);
}
printf(" Done! \n");
}
return 0;
}
运行结果:
PID PPID COMMAND
2070 2069 bash
5114 2070 example4
5115 5114 ps
Done!
5./* example12-5.c 在子进程中使用exec( ) 函数调用自定义的可执行文件。*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
int main()
{
pid_t pid;
int status;
char *arrArg[] ={"test", "A", "B", "C", NULL};
char *arrEnv[] ={"env1=100", "env2=yanyb", NULL};
pid = vfork(); /* 使用vfork函数创建新进程*/
if(pid<0) /* 如果进程创建失败,输出错误信息并退出 */
{
printf("vfork error.\n");
exit(1);
}
if(pid==0) /* 新进程 */
{
execve ("/home/by-402/process2",arrArg, arrEnv);
/* 可以用不同的可执行程序代替红字部分 */
}
else /* 父进程 */
{
if(pid != wait(&status)) /* 等待进程结束 */
{
printf("wait error.\n");
exit(1);
}
}
return 0;
}
运行结果:
This is the child process
This is the parent process /* 取决于所选择的可执行程序。 */