Ⅰ 急求關於操作系統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);}