实验三-进程通信.doc

上传人:F****n 文档编号:101729985 上传时间:2019-09-29 格式:DOC 页数:12 大小:142KB
返回 下载 相关 举报
实验三-进程通信.doc_第1页
第1页 / 共12页
实验三-进程通信.doc_第2页
第2页 / 共12页
实验三-进程通信.doc_第3页
第3页 / 共12页
实验三-进程通信.doc_第4页
第4页 / 共12页
实验三-进程通信.doc_第5页
第5页 / 共12页
点击查看更多>>
资源描述

《实验三-进程通信.doc》由会员分享,可在线阅读,更多相关《实验三-进程通信.doc(12页珍藏版)》请在金锄头文库上搜索。

1、实验三 进程通信一. 实验学时与类型学时:2,课外学时:自定实验类型:设计性实验二. 实验目的了解Linux的软中断、管道、消息队列、共享存储区等进程间通信方式。三. 实验内容1. 软中断通信机制(1) 请编写一个程序:循环输出“how are you?”,在按下Ctrl+C后中断显示,输出“Byebye!”后退出程序。#include#includeint k=1;void int_func(int sig) /软中断处理函数 k=0; Int main() signal(SIGINT,int_func);/预置软中断信号处理函数 While(k=1) Printf(“how are you

2、?n”); Printf(“byebye!”);(2) 使用信号机制实现父子进程同步,父进程先输出A,然后子进程输出B。#include#includeint k=1;void func(int sig) k=0; main() int pid; pid=fork(); if(pid0) printf(“An”); kill(pid,12); else if(pid=0) signal(12,func); while(k=1) sleep(1); printf(“Bn”); 2. 管道机制(1) 父子进程通过管道传送一串字符。要求:子进程随机从键盘输入一串字符,通过管道发给父进程,父进程从管道

3、中将消息读出并显示出来。#include#includemain() int pid, fd2 ; char outpipe50, inpipe50; pipe(fd); pid=fork(); if (pid=0) Printf(“please input some message:n”); Fgets(inpipe,sizeof(inpipe),stdin);write(fd1,inpipe,50); else if (pid0); wait(0); Printf(“father get this message:n”); read(fd0,outpipe,50); printf(“%sn

4、”,outpipe); (2)父子进程通过管道互相发送字符串。要求:子进程向父进程通过管道发送”I am child.”,父进程回送”I am father.”,父子进程将各自收到的字符串显示在屏幕上。#inlcude#include#includemain() int pid, fd2 ; char str150, str250; pipe(fd); pid=fork();if (pid=0) strcpy(str1,”Im child”); write(fd1,str1,strlen(str1);Sleep(1); read(fd0,str2,50); printf(“Child rece

5、ived: %sn”,str2); else if (pid0) read(fd0,str1,50); printf(“Parent received:%sn”,str1); strcpy(str2,”Im father.”); write(fd1,str2,strlen(str2); 3. 消息队列机制(1) 父进程及其子进程通过一条消息队列互相传送数据。#include#include#include#includeint msgqid,qid;struct msg long mtype; char mtext256;pmsg;cancelqueue() msgctl(msgqid,IPC

6、_RMID,0); exit(0);main() int pid; Pid=fork(); If (pid0) msgqid=msgget(75, 0777);printf(“msg id: %dn”,msgqid);pmsg.mtype=1;*(int *)pmsg.mtext)=getpid();msgsnd(msgqid,&pmsg,sizeof(int),0);msgrcv(msgqid,&pmsg,256,getpid(),0);printf(“A:receive msg from %dn,*(int *)pmsg.mtext);Else if(pid=0) signal(2,can

7、celqueue);msgqid=msgget(75, 0777|IPC_CREAT);while(1) msgrcv(msgqid,&pmsg,256,1,0);qid=*(int *)pmsg.mtext;printf(“B:receive msg from %dn”,qid);pmsg.mtype=qid;*(int *)pmsg.mtext)=getpid();msgsnd(msgqid,&pmsg,sizeof(int),0); (2) 非父子进程之间实通过一条消息队列互相传递数据。A进程:#include#include#include#includestruct msg long

8、 mtype; char mtext256;pmsg;main() int msgqid,pid;msgqid=msgget(75, 0777);printf(“msg id: %dn”,msgqid);pmsg.mtype=1;*(int *)pmsg.mtext)=getpid();msgsnd(msgqid,&pmsg,sizeof(int),0);msgrcv(msgqid,&pmsg,256,getpid(),0);printf(“A:receive msg from %dn”,*(int *)pmsg.mtext);B进程:#include#include#include#incl

9、udeint msgqid,pid;struct msg long mtype; char mtext256; pmsg;cancelqueue() msgctl(msgqid,IPC_RMID,0); exit(0);main() signal(2,cancelqueue);msgqid=msgget(75, 0777|IPC_CREAT);while(1) msgrcv(msgqid,&pmsg,256,1,0);pid=*(int *)pmsg.mtext;printf(“B:receive msg from %dn”,pid);pmsg.mtype=pid;*(int *)pmsg.m

10、text)=getpid();msgsnd(msgqid,&pmsg,sizeof(int),0); 4. 共享内存机制(1) 编程实现基于共享内存的进程间通信。要求:进程A通过共享内存将自己的进程号传递给进程B。A:#include#includemain()int shmid; int *va; shmid=shmget(22,sizeof(*va),0666|IPC_CREAT); va=(int *)shmat(shmid, 0,0); *va=getpid(); printf(“get As pid:%dn”,*va);shmdt(va); B:#include#includemai

11、n() int shmid; int *va; shmid=shmget(22,sizeof(*va),0666|IPC_CREAT); va=(int*)shmat(shmid, 0,0); printf(“As pid is :%dn”,*va); shmdt(va); shmctl(shmid,IPC_RMID,0);(2) 若要通过共享内存实现进程A与进程B互送进程号,可怎样编程实现?A:#include#includemain()int shmid; int *va; shmid=shmget(22,sizeof(*va),0666|IPC_CREAT); va=(int *)shm

12、at(shmid, 0,0); *va=getpid();printf(“get As pid:%dn”,*va);Sleep(2);printf(“Bs pid is:%dn”,*va)shmdt(va) B:#include#includemain() int shmid; int *va; shmid=shmget(22,sizeof(*va),0666|IPC_CREAT); va=(int*)shmat(shmid, 0,0); printf(“As pid is :%sn”,*va); *va=getpid(); printf(“get Bs pid:%dn”,*va); shmdt(va); shmctl(shmid,IPC_RMID,0);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号