C 11 作業系統原理 生產者消費者實驗

2021-09-23 17:27:14 字數 3444 閱讀 1642

看見網上給的生產者消費者的例子是基於windows api下的程式設計,不但脫離了作業系統底層設計的初衷,還更依賴於平台。

故使用c++13標準,重寫了生產者消費者模擬程式。

首先,由於c++沒有訊號量機制,利用互斥鎖實現乙個訊號量類。**如下:

【semaphore.h】

/*

乙個訊號量類(來自網路)

*/#pragma once

#includeclass semaphore

semaphore(const semaphore&) = delete;

semaphore& operator=(const semaphore&) = delete;

void signal()

m_cv_uptr.notify_one();

} void wait()

--m_count;

}private:

std::mutex m_mutex;

std::condition_variable m_cv_uptr;

unsigned long m_count;

};

其次,定義乙個buffersimulation類,來實現緩衝區的模擬

【buffersimulation.h】

/*

使用c++11標準

寫了乙個消費者生產者——緩衝區類

e-mail:[email protected]

2023年5月26日

*/#pragma warning(disable:4996)

#pragma once

#include#include#include#include#include#include"semaphore.h"

class buffersimulation;

其實現**如下

【buffersimulation.cpp】

/*

使用c++11標準

寫了乙個消費者生產者——緩衝區類

e-mail:[email protected]

2023年5月26日

*/#include "buffersimulation.h"

buffersimulation::buffersimulation(int producernum, int consumernum, int buffersize, int delaytime, bool stepmode) :

producernum(producernum),

consumernum(consumernum),

buffersize(buffersize),

delaytime(delaytime),

stop(false),

buffersemaphore(buffersize), // 訊號量與緩衝區大小一致

buffer(buffersize),

canstop(false)

// 建立消費者

for (int j = 0; j < consumernum; j++)

}void buffersimulation::stepmode()

int tmp;

std::cin >> tmp;

if (tmp == 1) else

else

else

}} }

}buffersimulation::~buffersimulation()

for (int i = 0; i < consumernum; i++)

system("pause");

}void buffersimulation::printbuffer(int loc)

else

} std::cout << "————————————" << std::endl;

}void buffersimulation::produce(int id)

_sleep(delaytime);

buffersemaphore.wait(); // 等待有地方放

t_mutex.lock();

if (stop)

buffer[produce_ptr] = std::to_string(count);

printbuffer(produce_ptr);

produce_ptr = (produce_ptr + 1) % buffersize;

std::cout << std::this_thread::get_id()<

count++;

t_mutex.unlock(); }}

void buffersimulation::consume(int id)

_sleep(delaytime);

buffersemaphore.signal(); // 等待有東西用

t_mutex.lock();

if (stop)

std::string product = buffer[consume_ptr];

buffer[consume_ptr] = "";

printbuffer(consume_ptr);

consume_ptr = (consume_ptr + 1) % buffersize;

std::cout << std::this_thread::get_id() << " " << id << "號程序(消費者)消費:" << product << std::endl;

t_mutex.unlock();

}}

【main.cpp】

/*

基於c++11模擬生產者-消費者,緩衝佇列的實現

e-mail:[email protected]

2023年5月26日

*/#include"buffersimulation.h"

int main(int argc, char ** ar**)

下面在時間間隔模式下進行演示:

引數如下:

列印緩衝區得到:

停止執行:

自動模式下的演示略過,動態的不易演示。

c 11生產者消費者

綜合運用 c 11 中的新的基礎設施 主要是多執行緒 鎖 條件變數 來闡述乙個經典問題 生產者消費者模型,並給出完整的解決方案。include include include include include include static const int kitemrepositorysize 1...

生產者和消費者問題c 作業系統)

生產者和消費者 include include include includeusing namespace std const unsigned short size of buffer 10 緩衝區長度 unsigned short productid 0 產品號 unsigned short ...

生產者 消費者,使用C 11的版本

multi threading以及lambda是c 11的重要公升級,下面的經典的生產者 消費者的 既使用了c 11的multi threading相關的庫,又使用了lambda。中有注釋,應該比較詳細。include include include include include using na...