




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 物业酬金制服务合同范本
- 高档消费品买卖合同范本
- 高龄员工家属免责协议书
- 煤矿托管合同协议书范本
- 自动生鲜车购买合同范本
- 烘焙店加盟合同协议范本
- 深圳市拆迁协议合同模板
- 银行解除合同协议书范本
- 网店服装合伙人合同协议
- 煤矿股权出让协议书范本
- 出版专业基础知识之形式逻辑常识(初级)
- 施工管理的施工进度问题分析与解决
- 普通高中历史新课程标准试题与答案(2017年版2020年修订)
- 妊娠高血压综合征眼底病变演示课件
- “青蓝工程”结对活动记录表
- 注射用头孢比罗酯钠-临床药品应用解读
- YY 0503-2023 正式版 环氧乙烷灭菌器
- 星火英语3500(整理)
- 《色彩构成》核心课程标准
- 企业人力资源管理中的成本控制与法律风险防控
- 江苏国强产品质量证明书-
评论
0/150
提交评论