(操作系统) C++ 进程同步

实验二 进程同步

一、实验名称:进程同步

二、实验目的与要求

1. 通过本次实验,使学生了解进程同步和互斥的实现方法,加深对进程同步和互斥概念的理解。

三、实验内容

(一)售票员和司机合作问题

问题描述:设公共汽车上,司机和售票员的活动分别是:

司机的活动:  启动车辆;

                  正常行车;

                  到站停车;

售票员的活动:关车门;

                  售票;

                  开车门;

编写程序实现这两个活动的同步关系。

源代码为:

#include <iostream>
#include <thread>
#include <vector>
#include <queue>
#include <ctime>
#include <mutex>
#include <windows.h>
#include <condition_variable>
using namespace std;
class Semaphore{
private:
mutex mut;
condition_variable condition;
int count;
public:
Semaphore(int count=0) : count(count) {};
//P操作
void P(){
unique_lock<mutex> unique(mut);
--count;
if(count<0)
condition.wait(unique);
} 
//V操作
void V(){
unique_lock<mutex> unique(mut);
++count;
if(count<=0)
condition.notify_one();
}  
}; 
Semaphore s1(0);//同步信号量S1表示是否允许驾驶员启动汽车
Semaphore s2(0); //同步信号量S2表示是否允许售票员开门
void drive()
{
while(true)
{
s1.P();
cout<<"汽车行驶中…."<<endl;
Sleep(1);
s2.V();
}
} 
void conductor()
{
while(true)
{
cout<<"关车门!"<<endl;
s1.V();
cout<<"现在开始售票…."<<endl;
Sleep(1);
s2.P();
cout<<"汽车到站,打开车门,乘客上下车。"<<endl;
}
}
int main() {
    thread p1(drive),p2(conductor);
    p1.join();
    p2.join();
    return 0;
}

执行结果为:

(二)进步同步关系问题:现有5个操作ABCDE,操作C必须在操作AB完成后执行,操作E必须在操作CD完成后执行,请编程实现5者之间的同步关系。

源代码为:

#include <iostream>
#include <thread>
#include <vector>
#include <queue>
#include <ctime>
#include <mutex>
#include <windows.h>
#include <condition_variable>
using namespace std;

class Semaphore{
private:
mutex mut;
condition_variable condition;
int count;
public:
Semaphore(int count=0) : count(count){};
//P操作
void P(string x){
unique_lock<mutex> unique(mut);
--count;
if(count<0)
{
condition.wait(unique);
cout<<x<<"等待执行…."<<endl; 
}
} 
//V操作
void V(){
unique_lock<mutex> unique(mut);
++count;
if(count<=0)
condition.notify_one();
}  
}; 
Semaphore Sa(0),Sb(0),Sc(0),Sd(0);//信号量定义
void process_p1()
{

while(1)
{
cout<<"A正在执行"<<endl;
Sa.V();
}
                              
}
void process_p2()
{
while(1)
{
cout<<"B正在执行"<<endl;
Sb.V();
}
 
}
void process_p3()
{
while(1)
{
Sa.P("A");
Sb.P("B");
cout<<"C正在执行"<<endl;
Sc.V();
}

}
void process_p4()
{
while(1)
{
cout<<"D正在执行"<<endl;
Sd.V();
}

}
void process_p5()
{
while(1)
{
Sc.P("C");
Sd.P("D");
cout<<"E正在执行"<<endl;
}

}

int main() {
    thread p1(process_p1),p2(process_p2),p3(process_p3),p4(process_p4),p5(process_p5);
p1.join();
p2.join();
p3.join();
p4.join();
p5.join();                                                                                                                                                                                                                                                                                                                        
    return 0;
}

执行结果为:

(三)展览馆问题:某展览馆矩形现代画展,展览馆内可以同时接纳2000人参观,参观者的票分为学生票和普通票,要求:

10≤(普通票-学生票)≤400

2)展览馆出入口每次只能有1人进出。

请编程实现进程之间的关系。

源代码为:

#include <iostream>
#include <thread>
#include <vector>
#include <queue>
#include <ctime>
#include <mutex>
#include <windows.h>
#include <condition_variable>
using namespace std;

class Semaphore{
private:
mutex mut;
condition_variable condition;
int count;
public:
Semaphore(int count=0) : count(count){};
//P操作
void P(){
unique_lock<mutex> unique(mut);
--count;
if(count<0)
{
condition.wait(unique); 
}
} 
//V操作
void V(){
unique_lock<mutex> unique(mut);
++count;
if(count<=0)
condition.notify_one();
}  
}; 
Semaphore t1(400);//普通票初值
Semaphore t2(0);//学生票初值
Semaphore empty(2000),m(1);
void Common()
{
while(1)
{
   empty.P();
   t1.P();
   m.P();
   cout<<"普通人进馆"<<endl;
   m.V();
   Sleep(3);
   m.P();
   cout<<"普通人出馆"<<endl;
   m.V();
   t2.V();
   empty.V();
}
                              
}
void Student()
{
while(1)
{
   empty.P();
   t2.P();
   m.P();
   cout<<"学生进馆"<<endl;
   m.V();
   Sleep(3);
   m.P();
   cout<<"学生出馆"<<endl;
   m.V();
   t1.V();
   empty.V();
}
 
}
int main() {
    thread  common(Common),student(Student);
common.join();
student.join();
return 0;
}

执行结果为:

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值