收藏 分销(赏)

进程管理实验二.doc

上传人:a199****6536 文档编号:3859948 上传时间:2024-07-22 格式:DOC 页数:12 大小:66KB 下载积分:8 金币
下载 相关 举报
进程管理实验二.doc_第1页
第1页 / 共12页
进程管理实验二.doc_第2页
第2页 / 共12页


点击查看更多>>
资源描述
#include<stdio.h> main() { int p,x; p=fork(); if(p>0) { x=fork(); if(x>0) printf("father\n"); else printf("child2"); } else printf("child1"); } 实验步骤及输出结果: [test@localhost 桌面]$ gcc -o 1 1.c [test@localhost 桌面]$ ./1 father [test@localhost 桌面]$ child2child1 2、运行以下程序,分析程序执行过程中产生的进程情况。 #include <stdio.h> main() {  int p,x;   p=fork();   if (p>0)     fork();   else{       fork();       fork();     }    sleep(15);   } 实验步骤: 编译连接 gcc –o forktree forktree.c 后台运行 ./forktree & 使用 pstree –h 查看进程树 实验步骤及输出结果: [test@localhost 桌面]$ gcc -o forktree forktree.c [test@localhost 桌面]$ ./forktree & [1] 3952 [test@localhost 桌面]$ pstree -h init─┬─NetworkManager─┬─dhclient │ └─{NetworkManager} ├─abrtd ├─acpid ├─atd ├─auditd───{auditd} ├─automount───4*[{automount}] ├─avahi-daemon───avahi-daemon ├─bonobo-activati───{bonobo-activat} ├─clock-applet ├─console-kit-dae───63*[{console-kit-da}] ├─crond ├─2*[dbus-daemon───{dbus-daemon}] ├─2*[dbus-launch] ├─devkit-power-da ├─gconf-im-settin ├─gconfd-2 ├─gdm-binary───gdm-simple-slav─┬─Xorg │ └─gdm-session-wor───gnome-session─┬─abrt-ap+ │ ├─bluetoo+ │ ├─gdu-not+ │ ├─gnome-p+ │ ├─gnome-p+ │ ├─gnome-v+ │ ├─gpk-upd+ │ ├─metacit+ │ ├─nautilus │ ├─polkit-+ │ ├─restore+ │ ├─rhsm-co+ │ └─{gnome-+ ├─gdm-user-switch ├─gedit ├─gnome-keyring-d───2*[{gnome-keyring-}] ├─gnome-screensav ├─gnome-settings-───{gnome-settings} ├─gnome-terminal─┬─bash───pstree │ ├─gnome-pty-helpe │ └─{gnome-terminal} ├─gnote ├─gvfs-afc-volume───{gvfs-afc-volum} ├─gvfs-gdu-volume ├─gvfs-gphoto2-vo ├─gvfsd ├─gvfsd-burn ├─gvfsd-computer ├─gvfsd-metadata ├─gvfsd-trash ├─hald───hald-runner─┬─hald-addon-acpi │ └─hald-addon-inpu ├─ibus-x11 ├─im-settings-dae───ibus-daemon─┬─ibus-engine-pin │ ├─ibus-gconf │ ├─python │ └─{ibus-daemon} ├─loop ├─master─┬─pickup │ └─qmgr ├─5*[mingetty] ├─modem-manager ├─notification-ar ├─notification-da ├─polkitd ├─pulseaudio─┬─gconf-helper │ └─2*[{pulseaudio}] ├─rhsmcertd ├─rpc.idmapd ├─rpc.mountd ├─rpc.rquotad ├─rpc.statd ├─rpcbind ├─rsyslogd───2*[{rsyslogd}] ├─rtkit-daemon───2*[{rtkit-daemon}] ├─seahorse-daemon ├─sshd ├─trashapplet ├─udevd───2*[udevd] ├─udisks-daemon───udisks-daemon ├─wnck-applet ├─wpa_supplicant └─xinetd [1]+ Done ./forktree 3、运行程序,分析运行结果。 #include <stdio.h> main() {  int p,x,ppid,pid;   x=0;   p=fork();   if(p>0)     { printf("parent output x=%d\n",++x);       ppid=getpid();       printf("Thi id number of parent is:ppid=%d\n",ppid);      }     else     { printf("child output x=%d\n",++x);       pid=getpid();       printf("Thi id number of child is:pid=%d\n",pid); } }  实验步骤及输出结果: [test@localhost 桌面]$ gcc -o 3 3.c [test@localhost 桌面]$ ./3 child output x=1 Thi id number of child is:pid=4070 parent output x=1 Thi id number of parent is:ppid=4069 注释:fork创建进程的时候子进程与父进程共享了代码区,子进程复制父进程的数据区,所以,两个进程的数据互不影响东都是1. 4、loop.c #include <stdio.h> main() {   while(1){  }; { 编译gcc loop.c_o ioop 后台运行./loop&(可多次使用命令) 多次使用ps命令查看进程状态 实验步骤输出结果: [test@localhost 桌面]$ gcc loop.c –o loop [test@localhost 桌面]$ ./loop & [1]3770 [test@localhost 桌面]$ps PID TTY TIME CMD 3572 PTS/0 00:00:00 bash 3770 pst’0 00:00:08 loop 3771 pst/0 00:00:00 ps [test@localhost 桌面]$ps PID TTY TIME CMD 3572 PTS/0 00:00:00 bash 3770 pst’0 00:00:32 loop 3773 pst/0 00:00:00 ps [test@localhost 桌面]$ps PID TTY TIME CMD 3572 PTS/0 00:00:00 bash 3770 pst’0 00:00:37 loop 3774 pst/0 00:00:00 ps (二)进程控制 1、设计一程序,父进程创建一子进程,子进程的功能是输出“hello world!”,使用execl()加载子进程的程序。 程序代码如下: execl.c程序: #include<stdio.h> #include<stdlib.h> #include<unistd.h> main() { int p; p=fork(); if(p>0) printf("father is running!\n"); else { printf("child is running!\n"); execl("./hello","hello",0); printf("child is running!\n"); } } hello.c程序: #include<stdio.h> main() { printf("hello world!\n"); } 输出结果: [test@localhost 桌面]$ gcc -o 5 5.c [test@localhost 桌面]$ ./5 father is running! [test@localhost 桌面]$ child is running! child is running! 2、运行以下程序,分析程序执行结果。 #include<stdlib.h> #include <stdio.h> main() { int p;    p=fork();   if (p>0)      printf("this is parent ");   else{       printf("this is child first\n”);       printf("this is child second ”);       _exit(0);      }      } 实验步骤及结果: [test@localhost 桌面]$ gcc -o 6 6.c [test@localhost 桌面]$ ./6 hello world! 注释:exit(0)表示进程正常终止。 (三)、进程的互斥 1、利用cat  to_be_locked.txt 查看下面程序的输出结果。 #include<stdio.h> #include<unistd.h> main() { int p1,p2,i; int *fp; fp=fopen("to_be_locked.txt","w+"); if(fp==NULL) { printf("Fail to create file"); exit(-1); } while((p1=fork())==-1); /*创建子进程p1*/ if(p1==0) { lockf(*fp,1,0); /*加锁*/ for(i=0;i<10;i++) fprintf(fp,"daughter%d\n",i); lockf(*fp,0,0); /*解锁*/ } else { while((p2=fork())==-1); /*创建子进程p2*/ if(p2==0) { lockf(*fp,1,0); /*加锁*/ for(i=0;i<10;i++) fprintf(fp,"son%d\n",i); lockf(*fp,0,0); /*解锁*/ } else { wait(NULL); lockf(*fp,1,0); /*加锁*/ for(i=0;i<10;i++) fprintf(fp,"parent%d\n",i); lockf(*fp,0,0); /*解锁*/ } } fclose(fp); } 执行结果: 实验步骤及结果:(该程序运行出错,注释lockf函数后才可正常运行) [test@localhost 桌面]$ gcc –o 4 4.c [test@localhost 桌面]$./4 [test@localhost 桌面]$cat to_be_lockde.txt daughter0 daughter1 daughter2 daughter3 daughter4 daughter5 daughter6 daughter7 daughter8 daughter9 parent0 parent1 parent2 parent3 parent4 parent5 parent6 parent7 parent8 parent9 son0 son1 son2 son3 son4 son5 son6 son7 son8 son9 注释:程序开始定义了文件读写指针用于打开指定的文件。当文件不存在的时候则自动创建,然后创建了一个进程p1,p1 获得处理机执行,给文件读写指针加锁,这样,即使p1失去处理机,其他获得处理机的进程也无法访问文件指针指向的文件。当p1再次获得处理机后继续执行直到进程p1解释并解锁。 2、按以下步骤分析下面的程序: (1)查看程序执行的结果并估计程序执行所需要时间。 (2)将程序中所有的lockf函数加上注释,再观察程序执行的结果和估算程序执行所需的时间。 (3)分析这两次执行的结果与时间的区别。 #include <stdio.h> #include <unistd.h> main( ) {   int p1,p2,i;   p1=fork( );      /*创建子进程p1*/   if (p1==0)    {      lockf(1,1,0);      /*加锁,这里第一个参数为stdout(标准输出设备的描述符)*/      for(i=0;i<10;i++)       {         printf("child1=%d\n",i);         sleep(1);        }      lockf(1,0,0);                    /*解锁*/    }  else    {      p2=fork();          /*创建子进程p2*/     if (p2==0)       {        lockf(1,1,0);       /*加锁*/        for(i=0;i<10;i++)        {        printf("child2= %d\n",i);        sleep(1);        }        lockf(1,0,0);           /*解锁*/       }     else       {         lockf(1,1,0);         /*加锁*/         for(i=0;i<10;i++)           {              printf(" parent %d\n",i);             sleep(1);          }        lockf(1,0,0);         /*解锁*/       }     } } 实验结果及步骤: [test@localhost 桌面]$ gcc -o 11 11.c [test@localhost 桌面]$ ./11 parent 0 parent 1 parent 2 parent 3 parent 4 parent 5 parent 6 parent 7 parent 8 parent 9 child2= 0 [test@localhost 桌面]$ child2= 1 child2= 2 child2= 3 child2= 4 child2= 5 child2= 6 child2= 7 child2= 8 child2= 9 child1=0 child1=1 child1=2 child1=3 child1=4 child1=5 child1=6 child1=7 child1=8 child1=9 加注释的步骤及执行结果(把11.c里面的lockf函数全部注释重新执行): [test@localhost 桌面]$gcc –o 11 11.c [test@localhost 桌面]$./11 Parent0 Child1=0 Child2=0 Parent1 Child1=1 Child2=1 Parent2 Child1=2 Child2=2 Parent3 Child1=3 Child2=3 Parent4 Child1=4 Child2=4 Parent5 Child1=5 Child2=5 Parent6 Child1=6 Child2=6 Parent7 Child1=7 Child2=7 Parent8 Child1=8 Child2=8 Parent9 Child1=9 Child2=9 注释:没有加注释时程序运行时间大约是30秒,注释后的程序运行时间大约是10秒。 注释掉所有lockf()函数后,循环输出语句块没有被加锁。而当前进程失去处理机后,获得处理机的其他进程也可以继续输出。 3、通过活动,使学生养成博览群书的好习惯。 B比率分析法和比较分析法不能测算出各因素的影响程度。√ C采用约当产量比例法,分配原材料费用与分配加工费用所用的完工率都是一致的。X C采用直接分配法分配辅助生产费用时,应考虑各辅助生产车间之间相互提供产品或劳务的情况。错 C产品的实际生产成本包括废品损失和停工损失。√ C成本报表是对外报告的会计报表。× C成本分析的首要程序是发现问题、分析原因。× C成本会计的对象是指成本核算。× C成本计算的辅助方法一般应与基本方法结合使用而不单独使用。√ C成本计算方法中的最基本的方法是分步法。X D当车间生产多种产品时,“废品损失”、“停工损失”的借方余额,月末均直接记入该产品的产品成本 中。× D定额法是为了简化成本计算而采用的一种成本计算方法。× F“废品损失”账户月末没有余额。√ F废品损失是指在生产过程中发现和入库后发现的不可修复废品的生产成本和可修复废品的修复费用。X F分步法的一个重要特点是各步骤之间要进行成本结转。(√) G各月末在产品数量变化不大的产品,可不计算月末在产品成本。错 G工资费用就是成本项目。(×) G归集在基本生产车间的制造费用最后均应分配计入产品成本中。对 J计算计时工资费用,应以考勤记录中的工作时间记录为依据。(√) J简化的分批法就是不计算在产品成本的分批法。(×) J简化分批法是不分批计算在产品成本的方法。对 J加班加点工资既可能是直接计人费用,又可能是间接计人费用。√ J接生产工艺过程的特点,工业企业的生产可分为大量生产、成批生产和单件生产三种,X K可修复废品是指技术上可以修复使用的废品。错 K可修复废品是指经过修理可以使用,而不管修复费用在经济上是否合算的废品。X P品种法只适用于大量大批的单步骤生产的企业。× Q企业的制造费用一定要通过“制造费用”科目核算。X Q企业职工的医药费、医务部门、职工浴室等部门职工的工资,均应通过“应付工资”科目核算。X S生产车间耗用的材料,全部计入“直接材料”成本项目。X S适应生产特点和管理要求,采用适当的成本计算方法,是成本核算的基础工作。(×) W完工产品费用等于月初在产品费用加本月生产费用减月末在产品费用。对 Y“预提费用”可能出现借方余额,其性质属于资产,实际上是待摊费用。对 Y引起资产和负债同时减少的支出是费用性支出。X Y以应付票据去偿付购买材料的费用,是成本性支出。X Y原材料分工序一次投入与原材料在每道工序陆续投入,其完工率的计算方法是完全一致的。X Y运用连环替代法进行分析,即使随意改变各构成因素的替换顺序,各因素的影响结果加总后仍等于指标的总差异,因此更换各因索替换顺序,不会影响分析的结果。(×) Z在产品品种规格繁多的情况下,应该采用分类法计算产品成本。对 Z直接生产费用就是直接计人费用。X Z逐步结转分步法也称为计列半成品分步法。√ A按年度计划分配率分配制造费用,“制造费用”账户月末(可能有月末余额/可能有借方余额/可能有贷方余额/可能无月末余额)。 A按年度计划分配率分配制造费用的方法适用于(季节性生产企业)
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服