logo资料库

了解与进程控制相关的各种系统调用,加深对进程的状态变迁与进程调度的理解.doc

第1页 / 共4页
第2页 / 共4页
第3页 / 共4页
第4页 / 共4页
资料共4页,全文预览结束
中国矿业大学计算机学院实验报告 课程名称 计算机组成与体系结构 实验名称 基本运算器实验 班级 信安 06-3 班 姓名__ 学号_08063719_实验日期__2009/6/2__ 实验报告要求:1.实验目的 2.实验内容 3.实验步骤 6.实验体会 4.运行结果 5.流程图 一、实验目的 了解与进程控制相关的各种系统调用,加深对进程的状态变迁与进程调度的 理解 二、实验要求 1.掌握进程的创建 fork 系统调用的原理。 2.掌握 exec 系统调用的原理。 3.掌握 exit 系统调用的原理。 4.掌握 wait 系统调用的原理。 三、实验步骤 sh.out sh.c –调试 1、gcc –o 2、vi 3、./sh.out 执行 sh.c –编译 四、实验程序及结果 1.Fork #include int main() { int pid; printf("Just 1 process now.\n"); printf("Calling fork()…\n"); pid=fork(); if(pid == 0) /*子进程 */ printf("I’m the child.\n"); else if (pid > 0) /*父进程*/ printf("I’m the parent.\n"); else printf("fork failed.\n"); printf("Program end.\n"); } 结果:
[s01@localhost ~]$ ./sh2.out Just 1 process now. Calling fork( ). . . I’m the child. exec error: No such file or directory I’m the parent. programe end. 2. exec 调用 #include #include #include int main() { int pid; printf("Just 1 process now.\n"); printf("Calling fork(). . . \n"); pid=fork(); if { (pid == 0) printf("I’m the child. \n"); execl("/bin/ls","-1","Fork.c",0); perror("exec error"); exit(1); (pid > 0) printf("I’m the parent.\n "); /*父进程 */ } else if else /* 子进程*/ /*①子进程终止*/ /*pid<0,fork 失败 */ printf("fork failed.\n"); wait(0); printf ("programe end.\n"); /*②父进程等待进程终止*/ return(0); }
3. wait 调用 #include #include #include #include int main() { int i,j,status; i= fork(); if( i == 0) { execlp("ysh", "ysh", "c", "expr.sh 1st 2st 3rd 4th",NULL); /*子进程 */ /*执行失败,错误返回*/ perror("exec error"); exit(5); } wait (& status); printf("status returned : %x \n",status); printf("that is %x in exit (). \n",status>>8); /*父进程接收子进程返回状态度*/ } 结果: [root@localhost s01]# vi wait.c [root@localhost s01]# gcc -o wait.out wait.c [root@localhost s01]# ./wait.out exec error: No such file or directory status returned : 500 that is 5 in exit (). 五、实验体会 本次实验让我学会了进程的创建,fork 系统调用,exec 系统调用,exit 系 统调用,wait 系统调用的原理。执行文件时需要对.out 文件,编辑的时候在 vi 编辑器里对.c 文件操作。 原程序的错误: 1、 shi.c:9: 错误:程序中有游离的 ‘\211’: 表示程序中的‘;’等符号不是英文法输入 2、/tmp/ccsRaRun.o: In function `main':shi.c:(.text+0x61)对‘print’未定义的引用 :shi.c:(.text+0x6d):对‘print’未定义的引用: 程序中的 pirintf 少打了 f (print)
以前的实验都是把原代码敲进去看结果就行了,类似于验证性的实验,让我 除了对实验流程更熟练外没有什么收获,这次通过修改调试不完整的实验程序, 我仔细分析了程序的编写,每个语句的含义,我感觉收获更大。 教师评价 优 良 中 及格 不及格 教师签名 日期
分享到:
收藏