《操作系统》——解决并发进程中与时间有关的错误

    在多道程序设计的系统中同时存在着许多进程。他们可能同时装入主存,等待处理器的调度,这就形成的并发进程。对于单核处理器来说,并发进程并不是多个进程同时占用处理器同时执行,而是同时装入主存,至于进程什么时候被执行,这要看进程的调度策略啦! 谈到并发进程,我就想到进程的并发会产生许多错误,这些错误在我们在设计系统或者编写软件时都是尽量要避免的。 

    那么进程的并发执行为什么会产生错误那?归根到底是并发进程访问共享变量的事,当多个进程访问共享变量的过程中,就有可能会产生与时间有关的错误,或者是死锁。

    一、导图分析


   二、导图分析

   说到进程访问共享资源,我觉得首先先明白进程的同步与进程的互斥问题。

   1、进程的互斥:指当有若干个进程都要使用某一共享资源时,任何时候最多只允许一个进程去使用共享资源,其他要使用的进程必须等待,知道该资源的占用者释放了该资源。

   2、进程的同步:在并发进程之间存在一种制约关系,一个进程的执行依赖另一个进程的消息,当一个进程没有得到另一个进程的消息时应该等待,知道消息到达后才被唤醒。

   3、与时间有关的错误:多个进程共同执行时,交替的访问了共享变量,但是有一个进程由于自身原因或者外界原因被中断了后,稍后又接着执行,最后导致运行结果出错。

   例如:

   某小区有两扇门,其中一扇门是入口,另一扇门是出口,用一个计数器count显示在小区的人数,当有人进入时,由进程PIN实现计数加一,当有人退出时,由进程POUT减一。这两个进程如下

<span style="font-family:KaiTi_GB2312;font-size:18px;">begin
     count: interge                                                               
     count: =0
cobegin
process PIN 
     R1: integer;
begin
     R1:=count;
     R1:=R1+1;
     count: =R1;
end ;

process POUT 
     R2: integer;
begin
     R2:= count;
     R2:=R2-1;
     count:=R2;
end;
coend;
end;</span>
    假定某个时候的计数值count=n,这时有一个人要进入,正好有一个同时要出去,于是进程PIN和POUT同时都要执行,如果进程PIN和POUT都没有被打断,那么各自完成了count+1和count-1操作,count还是n,但是若果被打断了PIN操作,看下图



这样执行过后,结果会变成n+1,这就是与时间有关的错误的实例。

    解决办法:采用PV操作,引进PV操作会引进一个叫临界区的名词,临界区是指并发进程中与共享变量有关的程序段。相关临界区是指并发进程中设计到相同变量的那些临界区。PV操作的原理是保证一个进程在临界区执行时,不让另一个进程进入相关临界区执行,即个进程对共享变量的访问是互斥的,这就不会造成与时间有关的错误。对于上面表格的一个例子来说,当进程PIN被打断后,POU不能访问共享变量count,直到PIN进程结束后才让POUT访问,这样最后的结果就正确了。

   三、小结

    解决并发进程之间的错误关键就是解决共享变量的访问方式,当多个进程都想访问共享变量时,我们一定要管理好各个进程的使用规律,不然的话程序就会出错。采用PV操作,让相关进程互斥的进入各自的临界区执行,这样就解决了并发进程间与时间有关的错误。好了,并发进程访问共享变量时,还会产生死锁,要想看死锁的形成原因及解决办法,请关注我的下一篇博客!

  • 15
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 27
    评论
模拟进程管理 #include <stdio.h> #include <malloc.h> //Can only be used in independent situation; //#define getmem(type) (type*)malloc(sizeof(type)) #define buffersize 5 int processnum=0;//the num of processes struct pcb { /* 定义进程控制块PCB */ int flag;//flag=1 denote producer;flag=2 denote consumer; int numlabel; char product; char state; struct pcb* processlink; }*exe=NULL,*over=NULL; typedef struct pcb PCB; PCB* readyhead=NULL,* readytail=NULL; PCB* consumerhead=NULL,* consumertail=NULL; PCB* producerhead=NULL,* producertail=NULL; //产品数量 int productnum=0; int full=0,empty=buffersize;// semaphore char buffer[buffersize];//缓冲区 int bufferpoint=0;//缓冲区指针 void linkqueue(PCB* process,PCB** tail); PCB* getq(PCB* head,PCB** tail); bool hasElement(PCB* pro); void display(PCB* p); void linklist(PCB* p,PCB* listhead); void freelink(PCB* linkhead); bool processproc(); bool waitempty(); bool waitfull(); void signalempty(); void signalfull(); void producerrun(); void comsuerrun(); bool hasElement(PCB* pro); void linklist(PCB* p,PCB* listhead) { PCB* cursor=listhead; while(cursor->processlink!=NULL){ cursor=cursor->processlink; } cursor->processlink=p; } void freelink(PCB* linkhead) { PCB* p; while(linkhead!=NULL){ p=linkhead; linkhead=linkhead->processlink; free(p); } } void linkqueue(PCB* process,PCB** tail) { if((*tail)!=NULL){ (*tail)->processlink=process; (*tail)=process; } else{ printf("队列未初始化!"); } } PCB* getq(PCB* head,PCB** tail) { PCB* p; p=head->processlink; if(p!=NULL){ head->processlink=p->processlink; p->processlink=NULL; if( head->processlink ==NULL ) (*tail)=head; } else return NULL; return p; } bool processproc() { int i,f,num; char ch; PCB* p=NULL; PCB** p1=NULL; printf("\n 请输入希望产生的进程个数?"); scanf("%d",&num); getchar(); // if(num>=100){ // printf("您怎么要产生这么多进程!Demands Denied!"); // return false; // } for(i=0;i<num;i++){ printf("\n 请输入您要产生的进程:输入1为生产者进程;输入2为消费者进程\n"); scanf("%d",&f); getchar(); p=(PCB*)malloc(sizeof(PCB)) ; if( !p) { printf("内存分配失败"); return false; } p->flag=f; processnum++; p->numlabel=processnum; p->state='w'; p->processlink=NULL; if(p->flag==1){ printf("您要产生的进程是生产者,它是第%d个进程。请您输入您要该进程产生的字符!\n",processnum); scanf("%c",&ch); getchar(); p->product=ch; productnum++; printf("您要该进程产生的字符是%c \n",p->product); } else { printf("您要产生的进程是消费者,它是第%d个进程。\n",p->numlabel); } linkqueue(p,&readytail); } return true; } bool waitempty() { if(empty<=0) { printf("进程%d:缓冲区存数,缓冲区满,该进程进入生产者等待队列\n",exe->numlabel); linkqueue(exe,&producertail); return false; } else{ empty--; return true; } } void signalempty() { PCB* p; if(hasElement(producerhead)){ p=getq(producerhead,&producertail); linkqueue(p,&readytail); printf("等待的生产者进程进入就绪队列,它的进程号是%d\n",p->numlabel); } empty++; } bool waitfull() { if(full<=0) { printf("进程%d:缓冲区取数,缓冲区空,该进程进入消费者等待队列\n",exe->numlabel); linkqueue(exe,&consumertail); return false; } else{ full--; return true;} } void signalfull() { PCB* p; if(hasElement(consumerhead)){ p=getq(consumerhead,&consumertail); linkqueue(p,&readytail); printf("等待的消费者进程进入就绪队列,它的进程号是%d\n",p->numlabel); } full++; } void producerrun() { if(!waitempty()) return; printf("进程%d开始向缓冲区存数%c\n",exe->numlabel,exe->product); buffer[bufferpoint]=exe->product; bufferpoint++; printf("进程%d向缓冲区存数操作结束\n",exe->numlabel); signalfull(); linklist(exe,over); } void comsuerrun() { if(!waitfull()) return; printf("进程%d开始向缓冲区取数\n",exe->numlabel); exe->product=buffer[bufferpoint-1]; bufferpoint--; printf("进程%d向缓冲区取数操作结束,取数是%c\n",exe->numlabel,exe->product); signalempty(); linklist(exe,over); } void display(PCB* p) { p=p->processlink; while(p!=NULL){ printf("进程%d,它是一个",p->numlabel); p->flag==1? printf("生产者\n"):printf("消费者\n"); p=p->processlink; } } bool hasElement(PCB* pro) { if(pro->processlink==NULL) return false; else return true; } void main() { char terminate; bool element; printf("你想开始程序吗?(y/n)"); scanf("%c",&terminate); getchar(); //Queue initialize; readyhead=(PCB*)malloc(sizeof(PCB)); if(readyhead==NULL) return; readytail=readyhead; readyhead->flag=3; readyhead->numlabel=processnum; readyhead->state='w'; readyhead->processlink=NULL; consumerhead=(PCB*)malloc(sizeof(PCB)); if(consumerhead==NULL) return; consumertail=consumerhead; consumerhead->processlink=NULL; consumerhead->flag=4; consumerhead->numlabel=processnum; consumerhead->state='w'; consumerhead->processlink=NULL; producerhead=(PCB*)malloc(sizeof(PCB)); if(producerhead==NULL) return; producertail=producerhead; producerhead->processlink=NULL; producerhead->flag=5; producerhead->numlabel=processnum; producerhead->state='w'; producerhead->processlink=NULL; over=(PCB*)malloc(sizeof(PCB)); if(over==NULL) return; over->processlink=NULL; while(terminate=='y') { if(!processproc()) break; element=hasElement(readyhead); while(element){ exe=getq(readyhead,&readytail); printf("进程%d申请运行,它是一个",exe->numlabel); exe->flag==1? printf("生产者\n"):printf("消费者\n"); if(exe->flag==1) producerrun(); else comsuerrun(); element=hasElement(readyhead); } printf("就绪队列没有进程\n"); if(hasElement(consumerhead)) { printf("消费者等待队列进程:\n"); display(consumerhead); } else { printf("消费者等待队列没有进程\n"); } if(hasElement(producerhead)) { printf("生产者等待队列进程:\n"); display(producerhead); } else { printf("生产者等待队列没有进程\n"); } printf("你想继续吗?(press 'y' for on)"); scanf("%c",&terminate); getchar(); } printf("\n\n 进程模拟完成.\n"); //Free the room; freelink(over); over=NULL; freelink(readyhead); readyhead=NULL; readytail=NULL; freelink(consumerhead); consumerhead=NULL; consumertail=NULL; freelink(producerhead); producerhead=NULL; producertail=NULL; getchar(); }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 27
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值