91av视频/亚洲h视频/操亚洲美女/外国一级黄色毛片 - 国产三级三级三级三级

  • 大小: 3KB
    文件類型: .c
    金幣: 1
    下載: 0 次
    發布日期: 2021-06-15
  • 語言: 其他
  • 標簽: 操作系統??

資源簡介

參考教材中的生產者消費者算法,創建5個進程,其中兩個進程為生產者進程,3個進程為消費者進程。一個生產者進程試圖不斷地在一個緩沖中寫入大寫字母,另一個生產者進程試圖不斷地在緩沖中寫入小寫字母。3個消費者不斷地從緩沖中讀取一個字符并輸出。為了使得程序的輸出易于看到結果,仿照的實例程序,分別在生產者和消費者進程的合適的位置加入一些隨機睡眠時間。 可選的實驗:在上面實驗的基礎上實現部分消費者有選擇地消費某些產品。例如一個消費者只消費小寫字符,一個消費者只消費大寫字母,而另一個消費者則無選擇地消費任何產品。消費者要消費的產品沒有時,消費者進程被阻塞。注意緩沖的管理。

資源截圖

代碼片段和文件信息

#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#define?maxn?15
struct?shared{
??????int?written;
?char?text[maxn];
?int?inout;
};

union?semun{
int?val;
struct?semid_ds?*buf;
unsigned?short?*arry;
};

int?Vsem(int?semidint?sem_num){
struct?sembuf?buf={sem_num+1SEM_UNDO};
if(semop(semid&buf1)==-1){
perror(“V?failed.\n“);
return?0;
}
return?1;
}

int?Psem(int?semidint?sem_num){
struct?sembuf?buf={sem_num-1SEM_UNDO};
if(semop(semid&buf1)==-1){
perror(“P?failed.\n“);
return?0;
}
return?1;
}

int?main()
{
????????//
????????key_t?key;
????????char?*??path?=?“/home/crazycat/123“;
????????key?=?ftok(path?1);
????????if(key==-1)perror(“key?error.\n“);
????????//
????????int?shmid?=?shmget(key?sizeof(struct?shared)?IPC_CREAT|0604);
????????if(shmid==-1)?perror(“shmid?error.\n“);
????????//
????????int?semid?=?semget(key?4?IPC_CREAT|0666);
????????if(semid==-1){
???????????printf(“%s\n“strerror(errno));
???????????return?0;
????????}
????????union?semun?arg;
????arg.val?=?1;
????semctl(semid?0?SETVAL?arg);??//mutex1
????arg.val=1;
????????semctl(semid?1?SETVAL?arg);??//mutex2
????????arg.val=0;
????????semctl(semid?2?SETVAL?arg);??//full_sem
????????arg.val=maxn;
????????semctl(semid?3?SETVAL?arg);??//empty_sem
????????//
????????struct?shared?*??cache;
????????if?((cache?=?(struct?shared?*)shmat(shmid?NULL?0))?==?(void?*)-1)?{
????????????perror(“shmat?error.\n“);
?????????}
????????cache->written=0;
????????cache->in=0;
????????cache->out=0;
????????//
????????pid_t?pid[5];
????????int?i;
????????for(i=0;i<5;i++){
????????????pid[i]=f

評論

共有 條評論