操作系统上机实验报告-进程通信(消息,软中断,管道)进程管理(并发运行).doc_第1页
操作系统上机实验报告-进程通信(消息,软中断,管道)进程管理(并发运行).doc_第2页
操作系统上机实验报告-进程通信(消息,软中断,管道)进程管理(并发运行).doc_第3页
操作系统上机实验报告-进程通信(消息,软中断,管道)进程管理(并发运行).doc_第4页
操作系统上机实验报告-进程通信(消息,软中断,管道)进程管理(并发运行).doc_第5页
免费预览已结束,剩余7页可下载查看

下载本文档

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

文档简介

操作系统上机实验报告 实验1、【进程管理】父进程创建两个子进程,各自输出一个字符,通过多次运行的结果分析进程并发运行的特点。 源代码:#include main() int p1,p2; while(p1=fork()=-1); if(p1=0) putchar(b); else while(p2=fork()=-1); if(p2=0) putchar(c); else putchar(a); 运行结果:bacswufelinux: ./abcaswufelinux: ./abcaswufelinux: ./abcaswufelinux: ./acabswufelinux: ./abcaswufelinux: ./abacswufelinux: ./a分析:多数结果为bca,偶尔有bac,cab出现。程序并发执行时依照时间片轮转,父子进程执行时有一定的随机性,出现了不同结果。实验2、【进程管理】父进程创建一个子进程,在父子进程中分别输出各自的进程标识号,分析运行结果。 源代码:#include main()int p1,p2;while(p1=fork()=-1);if(p1=fork()=0) printf(-this is child processn); printf(-my pid(child) is %dn,getpid(); printf(-my parent pid is %dn,getppid(); else printf(this is parent processn); printf(my pid(parent) is %dn,getpid(); printf(my child pid is %dn,p1); 运行结果:swufelinux: ./wsjthis is parent processmy pid(parent) is 8633my child pid is 8636-this is child process-my pid(child) is 8635-my parent pid is 8634swufelinux: ./wsj-this is child process-my pid(child) is 8630-my parent pid is 8627this is parent processmy pid(parent) is 8627my child pid is 8630swufelinux: ./wsj-this is child process-my pid(child) is 8629-my parent pid is 8628this is parent processmy pid(parent) is 8628my child pid is 8629swufelinux: ./wsjthis is parent processmy pid(parent) is 8634my child pid is 8635-this is child process-my pid(child) is 8636-my parent pid is 1分析:还是由于时间片轮转的关系,可能父进程先执行,或者子进程先执行。父进程先执行后会自动消除,此时其子进程托管给一号进程,即出现子进程输出“-my parent pid is 1”的情况。实验3、【进程通信管道】创建管道,父进程与两个子进程通过管道传递数据。 源代码:#include #include #include #include main()int i,r,p1,p2,fd2;char buf50,s50;pipe(fd); while(p1=fork()=-1); if(p1=0) lockf(fd1,1,0); sprintf(buf,child process p1 is sending messages!n); printf(child process p1!n); write(fd1,buf,50); sleep(5); lockf(fd1,0,0); exit(0); else while(p2=fork()=-1); if (p2=0) lockf(fd1,1,0); sprintf(buf,child process p2 is sending messagesn); printf(child process p2!n); write(fd1,buf,50); sleep(5); lockf(fd1,0,0); exit(0); wait(0); if(r=read(fd0,s,50)=-1) printf(cant read pipen); else printf(%sn,s); wait(0); if(r=read(fd0,s,50)=-1) printf(cant read pipen); else printf(%sn,s); exit(0); 实验结果:swufelinux: ./bbcchild process p1!child process p2!child process p1 is sending messages!child process p2 is sending messages分析:第一个子进程锁定写入端,并将数据写入管道,并睡眠等待,解锁。随后第二个子进程执行第一个进程的操作。最后父进程的两个 wait()函数,读取子进程的数据,并将它显示出来。 实验4、【进程通信软中断】不断输入字符串,把其中的小写字母转换为大写字母后输出,直到按键CtrlC、CtrlZ或Kill终止,并显示软中断信号的名称和编号,参考课件。 源代码:#include#include#includevoid stop();main()char str10;signal(SIGINT,&stop); for(;) printf(ns=);scanf(%s,str); if (strcmp(str,ok)=0) kill(getpid(),SIGINT); void stop(int signumber)printf(n process is killed by %d!nn,signumber);exit(0);运行结果:s=43ts=fdgd s=ok process is killed by 2!s= process is killed by 2! 分析:第一个结果的前两次输入都未成功,因为设定的中断字符为“ok”,第三次由设定的输入ok而中断,并且显示中断命令的软中断号为2;第二个结果中断为在键盘上输入“ctrl+c”命令,效果等同于输入“ok”字符。软中断的发送与接受是可以分开的,在死循环中发送软中断信号仍旧可以被循环外的软中断处理语句捕捉。 实验5、【进程通信软中断】不断输入字符串,当输入为“intermit”、 “stop”、 “quit”时,向自身发软中断信号SIGINT、SIGTSTP、SIGTERM,退出执行,参考课件。 源代码:#include#include#includevoid terminate(int signumber);main(void) char buf100; int i; signal(SIGINT,&terminate); signal(SIGTSTP,&terminate); signal(SIGTERM,&terminate); printf(nPid of this process :%d.nn,getpid(); for(;) printf(Input is :); scanf(%s,buf); if(strcmp(buf,intermit)=0) kill(getpid(),SIGINT); else if(strcmp(buf,stop)=0) raise(SIGTSTP); else if(strcmp(buf,quit)=0) raise(SIGTERM); else printf(Your input is :%snn,buf); return 0;void terminate(int signumber ) if (signumber=2) printf(catch signal SIGNIT,signumber is %dnn,signumber); else if (signumber=20) printf(catch signal SIGTSTP,signumber is %dnn,signumber); else if (signumber=15) printf(catch signal SIGTERM,signumber is %dnn,signumber); exit(0);运行结果:Pid of this process :8810.Input is :54gte Your input is : 54gte Input is :quitcatch signal SIGTERM,signumber is 15Pid of this process :8816Input is :stopcatch signal SIGTSTP,signumber is 20Pid of this process :8823Input is :intermitcatch signal SIGNIT,signumber is 2分析:首先输出执行程序的进程编号,进入死循环,输入一般字符串原样输出。当输入intermit、quit等设定的字符时发送软中断信号,被循环外的软中断相应语句捕捉,输出相应软中断代码后程序结束。 实验6、【进程通信消息】创建三个子进程,其中两个调用client.c ,另一个调用server.c,相互之间发送消息。 源代码:client:#include #include #include #include #include #define MSGKEY 75struct msgform long mtype; char mtext256;main (int argc, char *argv) struct msgform msg; int msgqid,pid,*pint; msgqid =msgget (MSGKEY,0777|IPC_CREAT); printf(msgqid = %dnn,msgqid); pid =getpid(); pint =(int*) msg.mtext; *pint =pid; msg.mtype =1; msgsnd (msgqid,&msg,sizeof(int),0); msgrcv(msgqid,&msg,256,pid,0); pint =(int *)msg.mtext; pid = (int) *pint; printf(Client %s: receive from Server process %d|n ,argv1,pid);server:#include #include #include #include #include #define MSGKEY 75struct msgform long mtype; char mtext256;msg;int msgqid;main () int i,pid,*pint; extern cleanup (); for (i =0;i20; i+) signal (i,cleanup); msgqid =msgget (MSGKEY, 0777|IPC_CREAT); for (;) msgrcv (msgqid,&msg,256,1,0); pint = (int*) msg.mtext; pid =*pint; printf (server: receive from pid %dn,pid); msg.mtype =pid ; pint= (int*) msg.mtext; *pint =getpid(); msgsnd (msgqid,&msg,sizeof (int ),0);cleanup()msgctl (msgqid ,IPC_RMID,0);exit(0);msg:#include #include #include #include main() int pid1,pid2,pid3; pid1 = vfork(); if (pid1 = 0) printf(nClient1 process %4d :n,getpid();execlp(/home/swufe/client,client,1,NULL); else pid2 = vfork(); if (pid2 = 0) printf(Client2 process %4d:n,getpid();execlp(/home/student/swufe,client,2,NULL); else pid3 = vfork(); if (pid3 = 0) printf(Server3 process %4d:n,getpid(); execlp(/home/swufe/server,server,NULL); elseprintf(nThis is Parent process %4d ! n,getpid(); wait(0); wait(0); wait(0); 运行结果:Client1 process2211:msgqid=360448Client2 process 2212msgqid=360448Server3 pro

温馨提示

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

评论

0/150

提交评论