Ⅰ 急求关于操作系统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);}