- 1、本文档共15页,可阅读全部内容。
- 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
操作系统程序进程通信实验报告
进 程 通 信
指导老师: 夏 建
一、实验题目
进程通信上机实验(消息缓冲通信)
二、算法思想
1、在进程管理(调度)实验基础上,加入进程通信的功能,采用消息缓冲通信机制进行通信。
2、P1发送数据给P2,P2排序后发送给P3,P3接收数据并输出。
要发送的数据内容由操作者实时输入。
三、小组分工:
四、算法程序
缓冲区结构
typedef struct node
{int id;
int size;
char text[100];
struct node *next;
}buftype;
进程控制块PCB
struct {int id;
char status;
int waiter1;
int priority;
char stack[5];
int sm;
buftype *front;
buftype *rear;
}pcb[4];
3 .发送、接收、排序程序算法流程
(1)发送函数 send()
程序:
void send(int rec_id,char a[],int n){
buftype *p;
p=(buftype *)malloc(sizeof(buftype));
p-id=rec_id; //接收消息进程id
p-size=n; //消息的大小
strcpy(p-text,a); //将消息拷贝到节点之中
p-next=0;
if(pcb[rec_id].sm=0)//如果消息缓冲队列空..就将p放在链首
{
pcb[rec_id].front=p;
}
else
pcb[rec_id].rear-next=p;//如果不空..放在队尾..rear指针后移一位
pcb[rec_id].rear=p;//队尾指针指向新加入的缓冲区
pcb[rec_id].sm++; //接收进程的消息个数+1
}
(2)接收函数 receive()
程序:
int receive(int rec_id,char b[])
{
buftype *p;
if(pcb[rec_id].sm=0){
printf(no message!!\n);
return 0;
}else{
p=pcb[rec_id].front;
pcb[rec_id].sm--;
pcb[rec_id].front=pcb[rec_id].front-next;//取出来后..front指针后移一位
strcpy(b,p-text);
free(p);
if(pcb[rec_id].front==NULL)
pcb[rec_id].rear=NULL;
return 1;
}
}
(3)排序函数 sort()
调用参数:a[ ] 待排序数组,n 数据个数;
程序:
void sort(char a[],int n)
{
int i,j;
char temp;
for(i=0;in;i++){
for(j=i+1;jn;j++){
if(a[j]a[i]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}//if
}//for
}
}
2.修改P1、P2、P3程序
Process1() 在阻塞前插入以下操作process1(){
char flag;
char a[100];
int len;
if(addr==m) goto m;
i=1;m1=1;
a: printf(\n process1 printing m1=%d\n\n,m1);
printf(do you want to send data ?(y/n)); //是否发送消息
flag=getchar();
if(flag==y||flag==Y){
printf(input data:);
scanf(%s,a);
len=strlen(a);
send(1,a,len); //将消息输送到缓冲队列中
printf(\n);
}
printf(process1 calls p on sem1!\n);
if(p(1,1,m)==0)
return (0);
m: printf(\n=process1 i=%d\n\n,i);
i=i+5;
goto a;
}
Process2程序:
process2(){
char fl
文档评论(0)