实验二-----进程的同步详解.docVIP

  • 47
  • 0
  • 约2.68千字
  • 约 5页
  • 2016-11-12 发布于湖北
  • 举报
实验二:编程实现经典互斥和同步问题 1.实验目的加深对.实验内容 3、实验具体内容和步骤的说明 ? (1)用户进程的定义 (2)信号量的定义及初始化 (3)PV操作的定义 P操作顺序执行下述两个动作:  ①信号量的值减1,即S=S-1;  ②如果S≥0,则该进程继续执行;  如果S<0,则把该进程的状态置为阻塞态,把相应的PCB连入该信号量队列的末尾,并放弃处理机,进行等待(直至其它进程在S上执行V操作,把它释放出来为止)。V操作顺序执行下述两个动作:①S值加1,即S=S+1;②如果S>0,则该进程继续运行;如果S≤0,则释放信号量队列上的第一个PCB(即信号量指针项所指向的PCB)所对应的进程(把阻塞态改为就绪态),执行V操作的进程继续运行。#include stdio.h #include stdlib.h typedef struct node { int name; //进程ID char state;//进程状态 struct node *next; }PCB; int s=3;//资源数 PCB *stophead=NULL,*stoptail=NULL,*readyhead=NULL,*readytail=NULL;//阻塞 就绪队列头尾指针 void block(PCB *q)//无资源 尾插入阻塞队列 { q-state=B; if(stophead==NULL) { stophead=q; stoptail=q; } else { stoptail-next=q; stoptail=q; } } void wakeup()//唤醒阻塞队列头节点 尾插入就绪队列 { stophead-state=R; if(readyhead==NULL) { readyhead=stophead; readytail=stophead; stophead=stophead-next; readytail-next=NULL; } else { readytail-next=stophead; readytail=stophead; stophead=stophead-next; readytail-next=NULL; } } void p(PCB *q)//p操作 { s=s-1; if(s0)//无资源 则插入阻塞队列 block(q); else//尾插入就绪队列 { q-state=R; if(readyhead==NULL) { readyhead=q; readytail=q; } else { readytail-next=q; readytail=q; } } } int v(int b)//v操作 { PCB *q,*pre; if(readyhead==NULL)//无就绪进程 返回 { printf( 无就绪进程!\n\n); return 0; } pre=readyhead; q=readyhead; while(q-name!=b)//寻找就绪队列中v操作节点 { if(q-next==NULL)//无当前查找节点 { printf( 查无此就绪进程!\n\n); return 1; } pre=q; q=q-next; } //查找成功 if(readyhead==readytail)//就绪队列仅有一个节点 { readyhead=readytail=NULL; free(q); s=s+1; return 0; } else//就绪队列有多个节点 { if(q==readyhead) { readyhead=readyhead-next; free(q);//释放节点 } else if(q==readytail) { readytail=pre; pre-next=NULL; free(q); } else { pre-next=q-next; free(q); } s=s+1; if(s=0)//如有阻塞进程 则唤醒加入就绪队列 wakeup(); return 1; } } void show()//输出当前所有进程状态 { PCB *q; q=readyhead; printf(\n); printf(

文档评论(1)

  • 用户头像 游客 2017-12-06 14:33:59
    666

1亿VIP精品文档

相关文档