




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、嵌入式操作系统应用开发课程实验报告班 级: * 学 号: * 姓 名:* 指导老师: * 成 绩: 实验六:Linux进程编程一、目的与任务目的:了解掌握操作系统进程的特点与功能,学会借助Linux进程的功能函数进行编程。任务:利用C语言指令编写程序调用进程函数,完成相应功能。二、内容、要求与安排方式1、实验内容与要求:1)通过简单程序编写了解掌握Linux进程编程基本概念、进程关系。2)守护进程与多进程并发案例、守护进程的编写。3)编程验证孤儿进程的产生与回收。4)编程验证僵尸进程的产生与回收。2、实验安排方式:采用1人1组,上机在Linux系统下进行编程实验。三、程序清单编码头文件存放在文
2、件”ch06.h”中:#include #include #include err_exit.h#include #include #include #include #include 添加“err_exit.h”文件获取导致函数调用失败的原因;#include #include #define err_exit(MESSAGE) ( perror(MESSAGE), exit(1) ) (1) 1-1应用程序创建进程的唯一方法是通过现有进程调用folk()函数;注释:用folk创建的子进程基本上是父进程的克隆,所以他包含了父进程的许多特征;#include ch06.hint global
3、= 5;int main(void) pid_t pid; char *string = I am parent; int local = 10; printf( before fork- ); if (pid = fork() 0) /* fork调用失败 */ err_exit(fork); if (pid = 0) /* 子进程 */ string = I am child; printf(%s, my pid=%d: global=%d, local=%dn, string, getpid(), global, local); global +; else /* 父进程 */ prin
4、tf(%s, my pid=%d: global=%d, local=%dn , string, getpid(), global, local); local+; printf(At join point, %s: global=%d, local=%dn, string, global, local); exit(EXIT_SUCCESS);1-2用刚建立的子进程调用exec()执行一个新程序;#include ch06.hchar *env_init=USER=unknow,PATH=/tmp,NULL;char *path = /home/zkj/book/ch06/echoall;i
5、nt main(void) pid_t pid; if (pid=vfork() 0) err_exit (vfork error); else if (pid = 0) /* 子进程 */ if (execle(path, echoall, arg1, ARG2, (char *)0, env_init) 0) err_exit(execle error); else if (pid=vfork() 0) err_exit(vfork error); else if (pid = 0) /* 子进程 */ if (execlp(./echoall, echoall, only one arg
6、, (char *)0)= 0) /* 等待子进程 */ pr_exit(status, pid); perror(wait over); exit(EXIT_SUCCESS);(2)守护进程;#include#include#include#include/ open#include#include#include#include#include#define MAXFILE 65535volatile sig_atomic_t _running = 1;int fd;/ signal handlervoid sigterm_handler(int arg)_running = 0;int
7、main()pid_t pid;char *buf = This is a Daemon, wcdjn;/* 屏蔽一些有关控制终端操作的信号 * 防止在守护进程没有正常运转起来时,因控制终端受到干扰退出或挂起 * */signal(SIGINT, SIG_IGN);/ 终端中断signal(SIGHUP, SIG_IGN);/ 连接挂断signal(SIGQUIT, SIG_IGN);/ 终端退出signal(SIGPIPE, SIG_IGN);/ 向无读进程的管道写数据signal(SIGTTOU, SIG_IGN);/ 后台程序尝试写操作signal(SIGTTIN, SIG_IGN);/
8、 后台程序尝试读操作signal(SIGTERM, SIG_IGN);/ 终止/ test/sleep(20);/ try cmd: ./test &; kill -s SIGTERM PID/ 1 fork child process and exit father processpid = fork();if(pid 0)exit(0);/ 2 create a new sessionsetsid();/ 3 set current pathchar szPath1024;if(getcwd(szPath, sizeof(szPath) = NULL)perror(getcwd);exit
9、(1);elsechdir(szPath);printf(set current path succ %sn, szPath);/ 4 umask 0umask(0);/ 5 close useless fdint i;/for (i = 0; i MAXFILE; +i)for (i = 3; i MAXFILE; +i)close(i);/ 6 set termianl signalsignal(SIGTERM, sigterm_handler);/ open file and set rw limitif(fd = open(outfile, O_CREAT|O_WRONLY|O_APP
10、END, 0600) 0)perror(open);exit(1);printf(nDaemon begin to work., and use kill -9 PID to terminaten);/ do sth in loopwhile(_running)if (write(fd, buf, strlen(buf) != strlen(buf)perror(write);close(fd);exit(1);usleep(1000*1000);/ 1 sclose(fd);/ print dataif(fd = open(outfile, O_RDONLY) 0)perror(open);
11、exit(1);char szBuf1024 = 0;if(read(fd, szBuf, sizeof(szBuf) = -1)perror(read);exit(1);printf(read 1024 bytes:n%sn, szBuf);close(fd);return 0;(3)孤儿进程代码1 #include 2 #include 3 #include 4 #include 5 6 int main() 7 8 pid_t pid; 9 /创建一个进程10 pid = fork();11 /创建失败12 if (pid 0)13 14 perror(fork error:);15 e
12、xit(1);16 17 /子进程18 if (pid = 0)19 20 printf(I am the child process.n);21 /输出进程ID和父进程ID22 printf(pid: %dtppid:%dn,getpid(),getppid();23 printf(I will sleep five seconds.n);24 /睡眠5s,保证父进程先退出25 sleep(5);26 printf(pid: %dtppid:%dn,getpid(),getppid();27 printf(child process is exited.n);28 29 /父进程30 els
13、e31 32 printf(I am father process.n);33 /父进程睡眠1s,保证子进程输出进程id34 sleep(1);35 printf(father process is exited.n);36 37 return 0;38 (4)僵死进程代码:#include ch06.hint main(void) pid_t pid; char *message; int n; printf(fork program startn); pid = fork(); switch(pid) case -1: exit(EXIT_FAILURE); case 0: message = This is child; n = 1; break; default: message = This is parent; n = 5; break; puts(message); sleep(n-1); exit(EXIT_SUCCESS);四、实验过程(1)用folk()得到子父进程的关系;用exec()执行一个新的程序进程的不同终止状态(2) 守护进程编写执行结果(3) 孤儿进程(4)僵死进程执行结果;五、实验体会(2)守护进程:在Linux/UNIX系统引导的时候会开
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- T/CCUA 038-2024版本典藏资源智慧展陈手势交互系统技术要求
- 高中多普勒效应课件
- 高中友谊开头课件
- GIL行业市场前景及投资研究报告:输电产品放量契机
- 高一必修三红楼梦课件
- 房屋买卖合同物业管理与入住指导服务协议
- 特色小镇建设用地租赁与综合运营管理协议
- 离婚协议男方放弃抚养费支付及子女抚养权协议书
- 农业科技创新预案
- 快乐拼图:拼出快乐的每一天
- 基础课程改革试题及答案
- 塔吊前臂临近高压线处理方案
- 某卫生院员工手册
- T∕CACM 008-2018 中医药单用联合抗生素治疗常见感染性疾病临床实践指南 急性咽炎
- 消防设施操作员自测试题及答案
- 2025年上半年湖北十堰竹山招募三支一扶高校毕业生聘用为事业单位人员12人易考易错模拟试题(共500题)试卷后附参考答案
- 职业暴露的预防及处理课件
- 餐饮服务明厨亮灶建设工作方案
- 私人二手摩托车转让合同范本
- 企业形象策划服务合同范本
- 2025年家庭照护者、健康照护师岗位专业技能资格知识考试题(附答案)
评论
0/150
提交评论