1、调度程序选择一个新的进程运行运行就绪:运行进程用完了时间片运行进程被中断,因为一高优先级进程处于就绪状态运行阻塞:当一进程等待某一事件的发生时,如请求系统服务; 初始化I/O 且必须等待结果; 无新工作可做; 等待某一进程提供输入 (IPC)阻塞就绪:当所等待的事件发生时#include vectorstdlib.halgorithm /for sort in vectorusing namespace std;class Pro /process classpublic: char name; string status;vector ru,re,bl; /ru-running,re-rea
2、dy,bl-blocked/function declarationint helloUI();int iniQ();int showPCB();int ruTOre();int ruTObl();int blTOre();int neTOre();int ruTOex();int helloUI() /start UI cout Hello!Welcome to come back. endl;#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*# return 0;int iniQ() /initialize the process int i;Please ent
3、er processes names and their status. for(i=0; i5; i+) /15 process a to o Pro process; char nam; string sta;Please enter i nam; process.name = nam;Please enter processes status.Status contains r1(running),r2(ready) and b(blocked). sta; process.status = sta; if(sta = r1) /judge which status if(ru.size
4、()1) ru.push_back(process);yes elseError! else if(sta = r2) re.push_back(process);b bl.push_back(process);! showPCB();int showPCB()running:;ru.size(); i+) rui.name ,ready:re.size(); rei.name blocked:bl.size(); bli.name runningError in blTOreint neTOre()Please enter processes names. process.status =
5、if(ru.empty()int ruTOex()Error in ruTOex1.Error in ruTOex2.int main() int act; /choose action helloUI();Please initialize the process. iniQ(); while(1)Please select the action to take.2:running-ready3:blocked4:blocked-5:new-6:exit act; if(act = 2) ruTOre(); else if(act = 3) ruTObl(); else if(act = 4
6、) blTOre(); else if(act = 5) neTOre(); else if(act = 6) ruTOex();Error in select.endl;#拓展点:五状态模型创建进程:状态 runningready:状态 runningblocked:状态 blockedready:创建新进程:情况一 有进程正在运行情况二 无进程正在运行终止进程:实验三 进程同步和通信一、实验目的调试、修改、运行模拟程序,通过形象化的状态显示,使学生理解进程的概念,了解同步和通信的过程,掌握进程通信和同步的机制,特别是利用缓冲区进行同步和通信的过程。通过补充新功能,使学生能灵活运用相关知识,
7、培养创新能力。假定.缓冲区可以容纳8个数据;因为缓冲区是有限的,因此当其满了时生产者进程应该等待;当消费者取走一个数据后,应唤醒正在等待的生产者进程;当缓冲区空时,消费者进程应该等待;当生产者向缓冲区放入了一个数据时,应唤醒正在等待的消费者进程。这就是生产者和消费者之间的同步基础内容:编写程序使其模拟两个进程,即生产者(producer)进程和消费者(Consumer)进程工作;生产者每次产生一个数据,送入缓冲区中;消费者每次从缓冲区中取走一个数据。每次写入和读出数据时,都将读和写指针加一。当指针到达缓冲区尾,重新将指针退回起点;/*/* PROGRAM NAME: PRODUCER_CONS
8、UMER */* This program simulates two processes, producer which */* continues to produce message and put it into a buffer */* implemented by PIPE, and consumer which continues to get */* message from the buffer and use it. */* The program also demonstrates the synchronism between */* processes and use
9、s of PIPE. */stdio.h/#include #define PIPESIZE 8#define PRODUCER 0#define CONSUMER 1#define RUN 0 /* statu of process */#define WAIT 1 /* statu of process */#define READY 2 /* statu of process */#define NORMAL 0#define SLEEP 1#define AWAKE 2/Process Control block struct pcb char *name; int statu; in
10、t time; ; /* times of execution */ /Buffer struct pipetype char type; /type int writeptr; /Write pointer int readptr; /Read pointer struct pcb *pointp; /* write wait point */ struct pcb *pointc; /* read wait point */ int pipePIPESIZE; /Buffer array struct pipetype pipetb; struct pcb process2; /numbe
11、r of producer - number of consumer,buffer. count = 8:too many prodecers;=0:too many consumers int count=0; int countp=0,countr=0;main() int output,ret,i; /output-number of times, ret-present status char in2; int runp(),runc(),prn(); pipetb.type = c pipetb.writeptr = 0; pipetb.readptr = 0; pipetb.poi
12、ntp = pipetb.pointc = NULL; processPRODUCER.name = Producer0 processCONSUMER.name = Consumer0 processPRODUCER.statu = processCONSUMER.statu = READY; processPRODUCER.time = processCONSUMER.time = 0; output = 0; printf(Now starting the program!n); Press p1 to run PRODUCER1, press p2 to run PRODUCER2.n
13、 press to run CONSUMER.n /PRODUCER1-product 1 new data, PRODUCER2-product 2 new datae to exit from the program.ni1000;i+) in0=N while(in0= scanf(%s,in); if(in0!=&in0!p) /when not p,c,e continue in0= if(in0=processPRODUCER.statu=READY) /producer and ready if(in1 = 2) /producer2 int m; for(m=0;m 3) /t
14、he number of waitting producer over 4,waitqueue=4wrong! continue; if(countstatu=READY; runc(process,pipe,&pipetb,CONSUMER); countr-;countr=%dn,countr); if(countr=0) pipetb.pointc=NULL; else if(in1 = 1) /producer1 3)pipetb,PRODUCER)=SLEEP) /sleep if(ret=AWAKE) /awake if(countr=0)pipetb.pointc=NULL; i
15、f(in0=processCONSUMER.statu=READY) /consumer and ready if(ret=runc(process,pipe,&pipetb,CONSUMER)=SLEEP) /sleep pipetb.pointc = &processCONSUMER; (pipetb.pointp)- output=(output+1)%100; runp(output,process,pipe,&pipetb,PRODUCER); countp-; printf(countp=%dn,countp); if(countp=0)pipetb.pointp=NULL;processPRODUCER.statu=WAIT) /producer and waitPRODUCER is waiting, cant be scheduled.n countp+;countp=%d
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1