您好,欢迎访问三七文档
当前位置:首页 > IT计算机/网络 > linux/Unix相关 > 实验2Linux中的进程
实验2Linux/UNIX中的进程一、实验目的与内容:1、了解Linux/UNIX中的进程创建,运行,消亡的过程和方法。2、熟悉进程相关系统调fork(),wait(),sleep(),exit()。二、实验方法及步骤:1.编写一个简单的SHELL实现的C语言程序,参考程序如下:#includestdio.hmain(){charcommand[32];char*prompt=“$”;while(printf(“%s”,prompt),gets(command)!=NULL){if(fork()==0)execlp(command,command,(char*)0);elsewait(0);}}Ctrl+Z2.编写一个简单的C语言程序实现一个进程创建、运行、消亡,参考程序如下:#includestdio.hmain(){intpid,a;a=55;pid=fork();if(pid0){perror(forkfailed);exit(1);}if(pid==0){printf(child:started,a==%d\n,a);sleep(5);a=99;printf(child:changedvalueofato%d\n,a);sleep(5);printf(childisleavingnow\n);exit(0);}else{printf(parent:fork()returned%d\n,pid);sleep(7);printf(parent:a==%d\n,na);wait(&a);sleep(1);printf(parent:childexited\n);}exit(0);}
三七文档所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
本文标题:实验2Linux中的进程
链接地址:https://www.777doc.com/doc-4947628 .html