版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、CO004 Projects on Operating Systems,System calls 3,Outline,Process Hierarchy exec() wait() exit(),Process Hierarchy,Process relationship: A parent process will have its parent process. Also, a child process will have its child process. This form a tree hierarchy.,Process A,Process B,Process C,Proces
2、s D,Process E,Process F,Program Execution,fork() is rather boring If a process can only duplicate itself and always runs the same program, then how can we execute other programs? We have another set of system calls to do this job. The exec() system call family.,exec(),Change program in process i.e.,
3、 launch a new program that replaces the current one Several different forms with slightly different syntax,status = execve(prog, args, env);,-1 on error. never see this if successful,name of file that should be executed,command line arguments - char* args,environment variables - char* args,Program E
4、xecution,A member of exec system call family execl(),Program: exec_example.c,$ ./exec_example_1 before execl .,Process 1234,int main(void) printf(“before execl n”); execl(“/bin/ls”, “/bin/ls”, NULL); printf(“after execl .n”); return 0; ,Program Execution,A member of exec system call family execl(),P
5、rogram: exec_example.c,$ ./exec_example_1 before execl . exec_example_1 exec_example_1.c,Process 1234,Result from “/bin/ls”,int main(void) printf(“before execl n”); execl(“/bin/ls”, “/bin/ls”, NULL); printf(“after execl .n”); return 0; ,Program Execution,A member of exec system call family execl(),P
6、rogram: exec_example.c,$ ./exec_example_1 before execl . exec_example_1 exec_example_1.c $ _,Process 1234,int main(void) printf(“before execl n”); execl(“/bin/ls”, “/bin/ls”, NULL); printf(“after execl .n”); return 0; ,Program Execution,The exec system call family is not simply a function that “invo
7、kes” a command. It is doing a very complex thing,Process 1234,int main(void) printf(“before execl n”); execl(“/bin/ls”, “/bin/ls”, NULL); printf(“after execl .n”); return 0; ,Originally, Im executing this code.,Program Execution,The exec system call family is not simply a function that “invokes” a c
8、ommand. It is doing a very complex thing,Process 1234,int main(void) printf(“before execl n”); execl(“/bin/ls”, “/bin/ls”, NULL); printf(“after execl .n”); return 0; ,file: /bin/ls,Now, I change to execute the program: /bin/ls.,Program Execution,The process is changing the code that it is executing
9、and never returns to the original code. The last two lines of codes are therefore not executed. The process that calls any one of the member of the exec system call family will throw away many things, e.g., Memory: including local variables, global variables, and allocated memory; Register value: e.
10、g., the program counter; But, the process will preserve something, such as PID; Process relationship; Running time, etc.,Program Execution,The exec system call family has 6 members and they are named as,Program Execution,Pathname vs Filename Argument list vs array,/home/henry/os/shell.c,execl(“/bin/
11、ls”, “/bin/ls”, “-l”, NULL);,execle(“/bin/ls”, array);,What you have learned?,fork() to create a child process. exec() to execute any commands. We can do a lot things based on the two system calls. For example, we want do implement a “shell”. But only fork() and exec() are not enough.,Example: imple
12、ment a system() function call,“system()” is a library function call, and it is built based on “fork()”, and exec system call family. (Thats why Ive told you that system calls are not programmer-friendly.),int main(void) printf(“before system n”); system(“ls”); printf(“after system n”); ,Program: sys
13、tem_example.c,We are now going to implement the system() function using system calls.,1 int system_ver_3150(const char *cmd_str) 2 if(cmd_str = NULL) 3 return -1; 4 5 if(fork() = 0) 6 execl(/bin/sh, /bin/sh, -c, cmd_str, NULL); 7 fprintf(stderr, 8 %s: command not foundn, 9 cmd_str); 10 exit(-1); 11
14、12 13 return 0; 14 15 16 int main(void) 17 printf(before system_ver_3150 .n); 18 system_ver_3150(/bin/ls); 19 printf(after system_ver_3150 .n); 20 return 0; 21 ,system() Implementation,$ ./system_implement_1 before system_ver_3150 . system_implement_1 system_implement_1.c after system_ver_3150 . $ _
15、,Program: system_implement_1.c,“/bin/sh” is the shell program. We actually want to execute the command: /bin/sh -c cmd_str,1 int system_ver_3150(const char *cmd_str) 2 if(cmd_str = NULL) 3 return -1; 4 5 if(fork() = 0) 6 execl(/bin/sh, /bin/sh, -c, cmd_str, NULL); 7 fprintf(stderr, 8 %s: command not
16、 foundn, 9 cmd_str); 10 exit(-1); 11 12 13 return 0; 14 15 16 int main(void) 17 printf(before system_ver_3150 .n); 18 system_ver_3150(/bin/ls); 19 printf(after system_ver_3150 .n); 20 return 0; 21 ,system() Implementation,Program: system_implement_1.c,However, when I run it for the 2nd time,$ ./syst
17、em_implement_1 before system_ver_3150 . after system_ver_3150 . system_implement_1 system_implement_1.c $ _,Whatd happened?!,1 int system_ver_3150(const char *cmd_str) 2 if(cmd_str = NULL) 3 return -1; 4 5 if(fork() = 0) 6 execl(/bin/sh, /bin/sh, -c, cmd_str, NULL); 7 fprintf(stderr, 8 %s: command n
18、ot foundn, 9 cmd_str); 10 exit(-1); 11 12 13 return 0; 14 15 16 int main(void) 17 printf(before system_ver_3150 .n); 18 system_ver_3150(/bin/ls); 19 printf(after system_ver_3150 .n); 20 return 0; 21 ,system() Implementation,Program: system_implement_1.c,Lets re-color the program!,Parent process,Chil
19、d process,Both processes,$ ./system_implement_1 before system_ver_3150 . after system_ver_3150 . system_implement_1 system_implement_1.c $ _,$ ./system_implement_1 before system_ver_3150 . after system_ver_3150 . system_implement_1 system_implement_1.c $ _,system() Implementation,$ ./system_implemen
20、t_1 before system_ver_3150 . after system_ver_3150 . system_implement_1 system_implement_1.c $ _,$ ./system_implement_1 before system_ver_3150 . system_implement_1 system_implement_1.c after system_ver_3150 . $ _,First Execution Sequence,Second Execution Sequence,Parent,fork(),Child,Parent,Parent,fo
21、rk(),Child,Parent,It is because of the OS process scheduling,system() Implementation,Dont forget that were trying to implement a system()-compatible function it is very weird to allow different execution orders. Our current problem is how to let the child to execute first? Sorrywe cant control the p
22、rocess scheduling of the OS to this extent. Then, our problem becomes How to suspend the execution of the parent process? How to wake the parent up after the child is terminated?,system() Implementation,We introduce you the wait() system call!,1 int system_ver_3150(const char *cmd_str) 2 if(cmd_str
23、= NULL) 3 return -1; 4 5 if(fork() = 0) 6 execl(/bin/sh, /bin/sh, -c, cmd_str, NULL); 7 fprintf(stderr, 8 %s: command not foundn, 9 cmd_str); 10 exit(-1); 11 12 13 return 0; 14 15 16 int main(void) 17 printf(before system_ver_3150 .n); 18 system_ver_3150(/bin/ls); 19 printf(after system_ver_3150 .n)
24、; 20 return 0; 21 ,Put something useful here!,Program: system_implement_2.c,wait(),When a process is done it can call exit(status). This is the status that echo $? can show you in the shell A parent can wait for its children (it blocks until they are done),status = waitpid(pid, ,-1 on error. otherwi
25、se, PID of process that exited,which PID to wait for; -1 means any child,exit code of process that has exited,check man page for details,system() Implementation with wait(),1 int system_ver_3150(const char *cmd_str) 2 if(cmd_str = NULL) 3 return -1; 4 5 if(fork() = 0) 6 execl(/bin/sh, /bin/sh, -c, c
26、md_str, NULL); 7 fprintf(stderr, 8 %s: command not foundn, 9 cmd_str); 10 exit(-1); 11 12 wait(NULL); 13 return 0; 14 15 16 int main(void) 17 printf(before system_ver_3150 .n); 18 system_ver_3150(/bin/ls); 19 printf(after system_ver_3150 .n); 20 return 0; 21 ,When wait() returns, the parameter can t
27、ell the parent about the current status of the child process, i.e., terminated or suspended. The parameter NULL means that “I dont care about the childs status”. Look up the man page of wait by yourself. In MINIX, the command is: “man 2 wait”,Program: system_implement_2.c,system() Implementation with wait(),
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 商渔防碰撞制度
- 员工上班规则制度
- 协会会费制度
- 小学活动室功能拓展方案
- 医院物流配送系统优化方案
- 病房健康监测设备使用方案
- 妇幼保健院楼宇节能改造方案
- 2026年大庆职业学院单招职业技能测试模拟测试卷附答案
- 2026年工贸试题-考试题库附参考答案(培优)
- 2026年时事政治测试题库附答案(b卷)
- 【《MMC-HVDC系统的仿真分析案例》1600字(论文)】
- 尼帕病毒病防治实战
- 2025年全国国家版图知识竞赛(中小学组)题库及参考答案详解
- 日照站改造工程既有投光灯塔拆除专项方案(修改版2)
- HBM高带宽存储器专题研究分析报告
- 室外消防管道穿过马路及车辆疏导施工方案
- 浙江省生态环境专业工程师职务任职资格量化评价表
- 成都市计划生育条例
- 未决清理培训
- 《课程与教学论》教案
- 2旋挖钻孔长护筒灌注桩专项施工方案
评论
0/150
提交评论