1、实验内容与目的
(1)通过编写多进程程序,使读者熟练掌握 fork()、exec()、wait()和 waitpid()等函数的使用,进一步理解在 Linux中多进程编程的步骤。
(2)通过编写一个完整的守护进程,使读者掌握守护进程编写和调试的方法,并且进一步熟悉如何编写多进程
程序。
2、基本原理与方法(原理、流程图)
(1)multi_proc_wrong.c程序同时创建两个子进程,两个进程之间是存在竞争关系的,因此会产生有执行顺序不确定的结果。

multi_proc.c程序则是首先创建子进程1,之后再创建子进程2,如此以来可以有效避免两个进程之间的竞争关系,执行的顺序也固然不会产生变化。
(2)守护进程是在后台运行不受终端控制的进程(如输入、输出等),一般的网络服务都是以守护进程的方式运行。
该程序中守护进程产生一个子进程,该子进程暂停 10s,然后自动退出,并由守护进程收集子进程退出的消息。在这里,子进程和守护进程的退出消息都在系统日志文件中输出。从而能够让我们更加了解进程的运行过程。

3、实验结果
(1)


(2)查看系统日志文件


守护进程daemon_proc一直都有在运行
4、结果分析
多进程并发执行必然会产生竞争资源的关系,导致在执行的顺序上可能会发生变化。先后创建进程或设置休眠时间可以有效避免进程之间的冲突,达到预想的执行效果。
守护进程是运行在后台的不受终端的进程,一般用于输入或是输出,只有在终端关闭的时候才会结束运行。
/* multi_proc_wrong.c */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void)
{
pid_t child1, child2, child;
/*创建两个子进程*/
child1 = fork();
child2 = fork();
/*子进程 1 的出错处理*/
if (child1 == -1)
{
printf("Child1 fork error\n");
exit(1);
}
else if (child1 == 0) /*在子进程 1 中调用 execlp()函数*/
{
printf("In child1: execute 'ls -l'\n");
if (execlp("ls", "ls","-l", NULL)<0)
{
printf("Child1 execlp error\n");
}
}
if (child2 == -1) /*子进程 2 的出错处理*/
{
printf("Child2 fork error\n");
exit(1);
}
else if( child2 == 0 ) /*在子进程 2 中使其暂停 5s*/
{
printf("In child2: sleep for 5 seconds and then exit\n");
sleep(5);
exit(0);
}
else /*在父进程中等待两个子进程的退出*/
{
printf("In father process:\n");
child = waitpid(child1, NULL, 0); /* 阻塞式等待 */
if (child == child1)
{
printf("Get child1 exit code\n");
}
else
{
printf("Error occured!\n");
}
do
{
child =waitpid(child2, NULL, WNOHANG);/* 非阻塞式等待 */
if (child == 0)
{
printf("The child2 process has not exited!\n");
sleep(1);
}
} while (child == 0);
if (child == child2)
{
printf("Get child2 exit code\n");
}
else
{
printf("Error occured!\n");
}
}
exit(0);
}
/*multi_proc.c */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
int main(void)
{
pid_t child1, child2, child;
/*创建两个子进程*/
child1 = fork();
/*子进程 1 的出错处理*/
if (child1 == -1)
{
printf("Child1 fork error\n");
exit(1);
}
else if (child1 == 0) /*在子进程 1 中调用 execlp()函数*/
{
printf("In child1: execute 'ls -l'\n");
if (execlp("ls", "ls", "-l", NULL) < 0)
{
printf("Child1 execlp error\n");
}
}
else /*在父进程中再创建进程 2,然后等待两个子进程的退出*/
{
child2 = fork();
if (child2 == -1) /*子进程 2 的出错处理*/
{
printf("Child2 fork error\n");
exit(1);
}
else if(child2 == 0) /*在子进程 2 中使其暂停 5s*/
{
printf("In child2: sleep for 5 seconds and then exit\n");
sleep(5);
exit(0);
}
printf("In father process:\n");
child = waitpid(child1, NULL, 0); /* 阻塞式等待 */
if (child == child1)
{
printf("Get child1 exit code\n");
}
else
{
printf("Error occured!\n");
}
do
{
child = waitpid(child2, NULL, WNOHANG ); /* 非阻塞式等待 */
if (child == 0)
{
printf("The child2 process has not exited!\n");
sleep(1);
}
} while (child == 0);
if (child == child2)
{
printf("Get child2 exit code\n");
}
else
{
printf("Error occured!\n");
}
}
exit(0);
}
/* daemon_proc.c */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <syslog.h>
int main(void)
{
pid_t child1,child2;
int i;
/*创建子进程 1*/
child1 = fork();
if (child1 == 1)
{
perror("child1 fork");
exit(1);
}
else if (child1 > 0)
{
exit(0); /* 父进程退出*/
}
/*打开日志服务*/
openlog("daemon_proc_info", LOG_PID, LOG_DAEMON);
/*以下几步是编写守护进程的常规步骤*/
setsid();
chdir("/");
umask(0);
for(i = 0; i < getdtablesize(); i++)
{
close(i);
}
/*创建子进程 2*/
child2 = fork();
if (child2 == 1)
{
perror("child2 fork");
exit(1);
}
else if (child2 == 0)
{ /* 进程 child2 */
/*在日志中写入字符串*/
syslog(LOG_INFO, " child2 will sleep for 10s "); /*查看系统日志文件:tail /var/log/syslog*/
sleep(10);
syslog(LOG_INFO, " child2 is going to exit! ");
exit(0);
}
else
{ /* 进程 child1*/
waitpid(child2, NULL, 0);
syslog(LOG_INFO, " child1 noticed that child2 has exited ");
/*关闭日志服务*/
closelog();
while(1)
{
sleep(10);
}
}
}
本文详述了Linux环境下,通过fork()、exec()、wait()和waitpid()实现多进程编程,以及如何编写和调试守护进程。通过示例程序分析了进程间的竞争关系,演示了守护进程在后台运行及收集子进程退出信息的过程,强调了控制进程执行顺序以避免冲突的重要性。
1808

被折叠的 条评论
为什么被折叠?



