操作系统程序进程通信实验报告详解.docx

上传人:b****1 文档编号:1066053 上传时间:2023-04-30 格式:DOCX 页数:13 大小:127KB
下载 相关 举报
操作系统程序进程通信实验报告详解.docx_第1页
第1页 / 共13页
操作系统程序进程通信实验报告详解.docx_第2页
第2页 / 共13页
操作系统程序进程通信实验报告详解.docx_第3页
第3页 / 共13页
操作系统程序进程通信实验报告详解.docx_第4页
第4页 / 共13页
操作系统程序进程通信实验报告详解.docx_第5页
第5页 / 共13页
操作系统程序进程通信实验报告详解.docx_第6页
第6页 / 共13页
操作系统程序进程通信实验报告详解.docx_第7页
第7页 / 共13页
操作系统程序进程通信实验报告详解.docx_第8页
第8页 / 共13页
操作系统程序进程通信实验报告详解.docx_第9页
第9页 / 共13页
操作系统程序进程通信实验报告详解.docx_第10页
第10页 / 共13页
操作系统程序进程通信实验报告详解.docx_第11页
第11页 / 共13页
操作系统程序进程通信实验报告详解.docx_第12页
第12页 / 共13页
操作系统程序进程通信实验报告详解.docx_第13页
第13页 / 共13页
亲,该文档总共13页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

操作系统程序进程通信实验报告详解.docx

《操作系统程序进程通信实验报告详解.docx》由会员分享,可在线阅读,更多相关《操作系统程序进程通信实验报告详解.docx(13页珍藏版)》请在冰点文库上搜索。

操作系统程序进程通信实验报告详解.docx

操作系统程序进程通信实验报告详解

 

进程通信

 

指导老师:

夏建

 

一、实验题目

进程通信上机实验(消息缓冲通信)

二、算法思想

1、在进程管理(调度)实验基础上,加入进程通信的功能,采用消息缓冲通信机制进行通信。

2、P1发送数据给P2,P2排序后发送给P3,P3接收数据并输出。

3、要发送的数据内容由操作者实时输入。

三、小组分工:

四、算法程序

1、缓冲区结构

typedefstructnode

{intid;

intsize;

chartext[100];

structnode*next;

}buftype;

2、进程控制块PCB

struct{intid;

charstatus;

intwaiter1;

intpriority;

charstack[5];

intsm;

buftype*front;

buftype*rear;

}pcb[4];

3.发送、接收、排序程序算法流程

(1)发送函数send()

 

程序:

voidsend(intrec_id,chara[],intn){

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()

 

程序:

intreceive(intrec_id,charb[])

{

buftype*p;

if(pcb[rec_id].sm<=0){

printf("nomessage!

!

\n");

return0;

}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;

return1;

}

}

(3)排序函数sort()

调用参数:

a[]待排序数组,n数据个数;

 

程序:

voidsort(chara[],intn)

{

inti,j;

chartemp;

for(i=0;i

for(j=i+1;j

if(a[j]

temp=a[i];

a[i]=a[j];

a[j]=temp;

}//if

}//for

}

}

2.修改P1、P2、P3程序

Process1() 在阻塞前插入以下操作代码

Process1() 在阻塞前插入以下操作

 

Process1程序:

process1(){

charflag;

chara[100];

intlen;

if(addr=='m')gotom;

i=1;m1=1;

a:

printf("\nprocess1printingm1=%d\n\n",m1);

printf("doyouwanttosenddata?

(y/n)");//是否发送消息

flag=getchar();

if(flag=='y'||flag=='Y'){

printf("inputdata:

");

scanf("%s",a);

len=strlen(a);

send(1,a,len);//将消息输送到缓冲队列中

printf("\n");

}

printf("process1callsponsem1!

\n");

if(p(1,1,'m')==0)

return(0);

m:

printf("\n=>process1i=%d\n\n",i);

i=i+5;

gotoa;

}

 

Process2程序:

process2(){

charflag;

charb[100];

intlen;

if(addr=='m')gotom;

if(addr=='n')goton;

i=1;

a:

printf("\nprocess2printingm1=%d\n",m1);

m2=2*m1;

printf("doyouwanttoreceivemessage?

(y/n)");//是否接收消息

getchar();

flag=getchar();

if(flag=='y'||flag=='Y')

{

if(receive(1,b)==0){//消息队列为空

getchar();

}else{//打印接收的消息,并且排序

printf("\n%s\n\n",b);

len=strlen(b);

sort(b,len);

printf("\n%s\n\n",b);

send(2,b,len);}

}

printf("process2callponsem2\n");

if(p(2,2,'m')==0)

return(0);

m:

printf("process2callvonsem1!

\n");

if(v(1,2,'n')==0)

return(0);

n:

printf("\n=>process2i=%d\n\n",i);

i=i+10;

gotoa;

}

Process3()在P3调用V操作唤醒P2前加入以下操作

Process3程序:

process3(){

charc[100];

if(addr=='m')gotom;

if(addr=='n')goton;

i=1;

a:

printf("\n=>process3i=%d\n",i);

if(i>=num)gotob;

printf("\nprocess3printing2^i=%d\n\n",m2);

m1=m2;

if(receive(2,c)){

printf("==>SORTRESULT:

%s\n\n",c);

getchar();

}

else{

printf("==>SORTRESULT:

\n\n");

getchar();

}

getchar();

printf("process3callvonsem2!

\n");

if(v(2,3,'m')==0)return(0);

m:

i++;

gotoa;

b:

if(receive(2,c)){

printf("==>result:

%s\n\n",c);

getchar();

}

else{

printf("==>result:

\n\n");

getchar();

}

printf("\nprocess3callsponsem2\n");

if(p(2,3,'n')==0)return(0);

n:

;

}

 

四、测试情况、分析

下面的是调试正确后的程序结果:

(1)发送后立刻接受:

(2)接下来三个进程

 

五、收获与体会

经过这次的实验,我收获良多,同时也让我明白,动手了,实践了就有希望,就会有希望完成任务。

刚开始做这次实验的时候,发现很没有头绪,除了老师给的PPT就没有一点线索。

只能硬着头皮去看之前做实验的程序代码,理解每个模块的作用,然后记录下来,对照之前老师给我们的讲解。

我发现通过流程图编写程序是一个很好的方法,不仅仅调理清晰,最重要的是减少很多的参数,便于程序员很快的了解每个参数、模块的功能。

之后我们根据老师给的流程图写出了程序代码。

通过这个实验,我决定以后的编程过程中,我必定会写好每一个项目的流程,以最好的效率完成既定的任务。

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2