创建进程进程管理_第1页
创建进程进程管理_第2页
创建进程进程管理_第3页
创建进程进程管理_第4页
创建进程进程管理_第5页
免费预览已结束,剩余3页可下载查看

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、实验题目进程管理小组合作否姓名班级学号一、实验目的1、掌握linux中进程的创建及撤消,理解进程的生命周期。2、 理解进程冋步工作的原理,掌握 linux中wait()、exit()、sleep(实现进程的同步。二.实验环境Windows xp VMware、Linux 系统三、实验内容与步骤(1) 编写三个程实现进程的创建及撤消。要求分别调用fork()、 vfork()实现进程的创建,调用exit()终止进程,调用exec(为进程指定 新的运行程序。(2) 调试并分析结果。对观察到的内容做详细记录分析,并写出实验报告。(3) 编写两个程实现进程的同步。要求分别调用fork()、sleep(

2、) wait()、exit()实现两个进程的同步。(4) 调试并分析结果。对观察到的内容做详细记录分析,并写出实验报告四、实验过程与分析1利用fork()函数创建进程用fork()函数创建进程时,语句调用系列如下:Pit_t fork(void); fork()函数是一个单调用双返回的函数,也就是说,该函数有父进程调用,执行时,在父进程中返回子进程标识,在子进程中返回0。fork()调用 后,子进程是父进程的一个复制,都是从fork()调用语句开始执行。#in clude<stdio.h>#i nclude<sys/types.h>#i ncludev un istd.

3、h>int glob=3;int main(v oid)pid_t pid;i nt loc=3;printf("before fork():glob=%d,loc=%d. n",glob,loc);if(pid=fork()<0)prin tf("fork() error. n");exit(0);else if(pid=0)glob+;loc-;printf("child process changes glob and loc:n");elseprin tf("pare nt process does n&

4、#39;t cha nge the glob and loc:n");prin tf("glob=%d,loc=%dn",glob,loc);exit(0);ludes td icinf ludp<sy s / type- § , h>jfinq ludQ<un i s td .k>ii I g lob=3;i n t nu in(vo id)(phl_t pid; in I loc-3;pr intfC*t>e fore for k( ):y lub"%d»loc=%d . Vi* .glob loc&

5、gt;i f (p id=forJct )G) I pr inlfC fQTk() error * n'>: ex i (0):e W# i t( pid=(B glob+ ; lac彳 pr in tf (T ch i Id pfoce i i常 e, g lub and loc; n *;hc I s<pr intf( *procti s due in ' Ithe glob and lac j n'pr i ntf( rg lub = tjiil, loc=J%dn" loh , luc):fx in运行结果如下:rootlccalbost

6、ldv| ./J5-1 .exebefore fork();glob=3lnc=3.ch i Id prorei; s changes g Inb and Im;g lob=4,1dc=2parenI jiruces docsn11 iliane the glob jnd loc : Klob=3 P Juc=3I rc?Q t 劄 dm Ihos t Idwj #结果分析:程序运行的情况可能有3种结果:(1) 若运行到fork()时,出现错误,则输出“ fork() error. ” ,程序 故障终止。(2) 由于程序运行时产生两个进程,由mai n()函数产生一个父 进程以及程序执行中通过

7、调用fork()函数产生一个子进程。当执行到 fork()后形成两个并发进程。根据系统调度情况,可能产生两种运行结果为:第一种:ruol'lora I hus I I dw/) . /15-1 .ext?be fore fork():glob=3,loc=3«rhi l(1 proce s s 匚hanes g Inh and lociglob=4,loc-2puren i proct ss Joe sn t ch<ii)ge the £ lob 胡nd lac:glob=3,loc=3root14»lbo»t Ichv片第二种:Befor

8、e fork():glob=3,loc=3Pare nt process doeshcha nges the glob and loc Glob=3,loc=3Child process cha nges glob and locGlob=4,loc=22给进程指定一个新的运行程序的函数exec()一个进程的调用exec()函数来运行一新程序。之后该进程的代码 段、数据段和堆栈段被新程序所代替,新程序从自己的main()函数开始进行。#in clude<stdio.h>#i nclude<sys/types.h>#i ncludev un istd.h>#in c

9、lude<sys/wait.h>char * en v_list="USER二root","PATH二/tmp",NULL;int main(v oid)pid_t pid;if(pid=fork()<0)prin tf("fork error' n");exit(0);else if(pid=0)if(execle("/root/ldw/pri nt1.exe","pri nt1","arg1","arg2",(char *)0

10、,e nv_list)<0)prin tf("execle error' n");exit(0);if(waitpid(pid,NULL,0)<0)prin tf("WAIT ERROR'n");exit(0);if(pid=fork()<0)prin tf("fork error' n");exit(0);else if(pid=0)if(execlp("pri nt1.exe","pri nt1","arg1",(char *)0

11、)<0) prin tf("execle error' n");exit(0);exit(0);chaF * *nvJTi iiJ = "CSER=ioor/PATlk/mpT .NLLLiin t its in( vo id) pi,Lt pid: if<(pid'fork( )X0)Ipr intf('fork errorn');i 1(0) 1T丄c I i f(p idz _0)ic 1e ' ' i root/ ldv?pr i n I 1 ve ' F p r inti* r arg

12、1 r*arg2T »( /h j r *0 henv 1 $寻卄芝0p r intf('亡e r roinT) ex i l( 0) ii i >i uh i tp id( p id NULL hU.1) pr intf( TMA1T ERRRn')i eit i I (fi);i f<(pid = fork( )X0)pr intf( fork errorn'):fx i 1(0)|l|运行结果是:rooiloca Jhost Idw# b/sy5-40: pr in L1 argfl): argl ar2 : arg2 USER=roo tP

13、ATH=/ tup结果分析:程序首先调用execlp (),传递的参数有可执行程序的路径名、程序所需的参数及程序执行时的环境变量。故执行15-4.exe的执行结果为 arg0:print1(程序),arg1:arg1,arg2:arg2(参数), USER二root,PATH二/tmp(环境变量)之后,子进程终止。3父子进程同步编写两个程实现进程的同步。要求分别调用fork()、sleep。、wait()、 exit()实现两个进程的同步#in clude<stdio.h>mai n()int pid1;if(pid1=fork()if(fork()pri ntf("pa

14、re nt's con text. n ”);printf("parent is waiting the child1 terminate. n"); wait(0);printf("parent is waiting the child2 terminate. n"); wait(0):pri ntf("pare nt termi nates. n");exit(O);elseprintf("child2's context. n"); sleep(5);printf("child2 t

15、erminate. n"); exit(0); elseif(pid1=0)prin tf("child1's con text. n ”); sleep(10);prin tf("child1 term in ate. n"); exit(0);if( forkO)I1pr int Tpar亡nt、con lex H Xn');pr in t f(F paren t i si I ithe ch i Id I te rmina 1e . n' ) iwit(0):pr in t(C pa ren t i$ ii ing the

16、ch i Id2 t e r mi nd t e . Xn'); 'Aa i t (0):pr io t f< 'parent te rrruna ie s . n*>:ex i i(0);pr in t f(* ch i Id2 s con tex I. n'): s leept 5:pr in Lf( *ch i 1d2 ierrnna te . n* ): ex i i( U);Ie 1 se(i f(pidl=O>pr in t T ch i IdT s con tex I. Xn' );-一 p ' A 一 ” _ 一

17、运行结果是:IIhos t Ldv# 1S-fi .ex<?ch i Jtt I ' s c-dii let ch i Ld2'e con1e丸 t pa rrn I $ con icm t y i, iiiiig: Ll)c childl icrininu ic ch i liJ2 te r minu ic pa ren 1 i s wa i 1 in l he cti i Id2 le rmiina e .ch i Id I I e r minaIe .pa ren I I e r mi ria I e s r rwitlDea lhosi ldwl结果分析:首先有父

18、进程创建一个子进程,创建成功后,再创建第 二个子进程,之后三个进程按随机的顺序运行,运行的顺序不同,其 结果是不同的。其运行的一种结果是:roo lloca Lho;; L Ldw# > /15-6 * extch i Id I s contcxi.ch ji ld2's comex i par 亡coniex 11parent is l t ing the ch i Id 1 terminatc . chiLd2 te rmite .parent is i( i ng lhe ch i ld2 lermina le ch i Id I te rmiha Lf parent te

19、rmina le s.(TDOtlQCalhO3t Idw#对以上的运行结果进行分析:子进程1先运行,输出childl 'context, 之后调用sleep (10)等待;子进程2运行,输出child2 'co ntext,之 后调用slee(5)等待;之后父进程运行,输出pare nt is wait ing the childl terminate.,之后调用wait(0)等待;子进程2运行,输出child2 terminate, 之后调用exit (0)终止;之后父进程运行,输出pare nt is waiti ng the child2 terminate.,之后调用

20、wait (0)等待;子进程1运行,输出child1 termi nate,之后调用 exit (0)终止;之后父进程运行,输出pare ntterminate,之后调用exit (0)终止,至此程序完全结束。4.自己编写一个程序输出父进程、子进程 a、b、c#in clude<stdio.h> #i nclude<sys/types.h> #i ncludevu ni std.h> int mai n()int pid; if(pid=fork() if(fork()pri ntf("pare nt process is n"); pri ntf("An");elsepri ntf("child1 process is n ”);pri ntf("Bn");elseif(pid=0)prin tf("child2 process is n ”);prin tf("Cn");#include<s td io.h>#i

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论