09_实验6 编写多进程程序

  • 格式:doc
  • 大小:138.50 KB
  • 文档页数:9

实验步骤: 1、/* daemon_proc.c */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <syslog.h>
宁德师范学院计算机系
实验报告
(2015 —2016 学年 第 二 学期)
课程名称 实验名称 专 年 组 业 级 号与技术(专升本) 15 级 09______
成员 1 学号 B2015102320 成员 1 姓名 陈朋朋 成员 2 学号 B2015102323 指导教师 实验日期 黄进华 2016-5-16 成员 2 姓名 林振贤
}
write(fd, buf, strlen(buf) + 1); close(fd); sleep(10); }
exit(0); } 3、
实验结果分析
实验总结(包括过程总结、心得体会及实验改进意见等) :
通过编写多进程程序,使我们更加熟练掌握 fork、exec、wait、waitpid 等函数的使用, 进一步理解在 Linux 中多进程编程的步骤。
int main(void) { pid_t child1,child2; int i; child1 = fork(); if (child1 == { perror("child1 fork"); exit(1); 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); } child2 = fork(); if (child2 == { perror("child2 fork"); exit(1); } else if (child2 == 0) { syslog(LOG_INFO, " child2 will sleep for 10s "); sleep(10); syslog(LOG_INFO, " child2 is going to exit! "); exit(0); } 1)
else { waitpid(child2, NULL, 0); syslog(LOG_INFO, " child1 noticed that child2 has exited "); closelog(); while(1) { sleep(10); } } } 2、/* dameon.c 创建守护进程实例 */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h>
实验目的与要求:
通过编写多进程程序,使大家熟练掌握 fork、exec、wait、waitpid 等函数的使用, 进一步理解在 Linux 中多进程编程的步骤。
实验设备(环境) : 每组一台安装 VMware Workstation 的计算机。 实验内容:
该实验有 3 个进程,其中一个为父进程,其余两个是该父进程创建的子进程,其中一个 子进程运行“ls -l”指令,另一个子进程在暂停 5s 之后异常退出,父进程并不阻塞自己, 并等待子进程的退出信息,待收集到该信息,父进程就返回。
int main() { pid_t pid; int char i, fd; *buf = "This is a Daemon\n";
pid = fork(); if (pid < 0) { printf("Error fork\n"); exit(1); } else if (pid > 0) { exit(0); }
指导教师评语:
成绩评定 教师签字 2016 年 黄进华 5 月 29 日
备注:
注:1、报告内的项目或设置,可根据实际情况加以补充和调整 2、教师批改学生实验报告应在学生提交实验报告 10 日内
openlog("demo_update", LOG_PID, LOG_DAEMON); setsid(); chdir("/"); umask(0); for(i = 0; i < getdtablesize(); i++) { close(i); } while(1) { if ((fd = open("/tmp/daemon.log", O_CREAT|O_WRONLY|O_APPEND, 0600)) < 0) { printf("Open file error\n"); exit(1);