導航:首頁 > 編程語言 > pv操作代碼

pv操作代碼

發布時間:2025-05-05 15:50:38

Ⅰ 急求關於操作系統PV操作的程序代碼C或C++都可以,在線等啊!!!!!!!!!!

完整程序要花不少時間調試的,我這里給你一個實現PV操作的程序吧,5個人搶3個位子,這個你們上課應該會講的,通過幾個pv操作就可以實現,只要把我這里寫的pv操作的代碼用上就可以了。程序是在linux系統下調試的。pv操作分別對應下面的up和down(我們老師習慣用這種稱呼,於是就在代碼中也這么用了)
#include <stdio.h>#include <signal.h>#include <unistd.h>#include <sys/types.h>#include <sys/stat.h>#include <sys/mman.h>#include <sys/times.h>#include <fcntl.h>#include <stdlib.h>#include <sys/sem.h> //include header file for semaphore #define FILE_SIZE 11#define NO_PROC 10 int DelayCount = 0; int readerID = 0;int writerID = 0; char* shared_buffer; int Delay100ms = 0; int* shared_buffer2;////////Variables for semaphoreoperations/////int semid;// the followings are not necessary to definehere, but just for conveniencekey_t semkey;struct sembuf operation[1];short sarray[2];int ret_val;/////////////////////////////////////////////// void init_sem(){ int*rc; //getkey semkey=ftok("/dev/null",12); if( semkey == (key_t) -1 ) { printf("main:ftok() failed\n"); exit(-1); } printf("semkeygenerated\n"); //getsemaphore set semid= semget( semkey, 2, 0666 | IPC_CREAT | IPC_EXCL ); if( semid == -1 ) { printf("main: semget()failed\n"); exit(-1); } //setthe two semaphores to 1; sarray[0]=1; sarray[1]=1; ret_val=semctl(semid,1, SETALL, sarray); if(ret_val==-1) { printf("main:semctlerror\n"); exit(-1); } printf("ddddd\n"); //setreader count rc=(int*)(&shared_buffer[FILE_SIZE]); //printf("%d\n",*shared_buffer2); *rc=0; printf("RC:%d\n",*rc);} void down(int i) //i=0, 1{ operation[0].sem_num=i; operation[0].sem_op=-1; operation[0].sem_flg=0; ret_val=semop(semid,operation,1);} void up(int i){ operation[0].sem_num=i; operation[0].sem_op=1; operation[0].sem_flg=0; ret_val=semop(semid,operation,1);}

閱讀全文

與pv操作代碼相關的資料

熱點內容
網路中常用的傳輸介質 瀏覽:518
文件如何使用 瀏覽:322
同步推密碼找回 瀏覽:865
樂高怎麼才能用電腦編程序 瀏覽:65
本機qq文件為什麼找不到 瀏覽:264
安卓qq空間免升級 瀏覽:490
linux如何刪除模塊驅動程序 瀏覽:193
at89c51c程序 瀏覽:329
怎麼創建word大綱文件 瀏覽:622
裊裊朗誦文件生成器 瀏覽:626
1054件文件是多少gb 瀏覽:371
高州禁養區內能養豬多少頭的文件 瀏覽:927
win8ico文件 瀏覽:949
仁和數控怎麼編程 瀏覽:381
項目文件夾圖片 瀏覽:87
怎麼在東芝電視安裝app 瀏覽:954
plc顯示數字怎麼編程 瀏覽:439
如何辨別假網站 瀏覽:711
寬頻用別人的賬號密碼 瀏覽:556
新app如何佔有市場 瀏覽:42

友情鏈接