資源簡介
教材中對讀者寫者問題算法均有描述,但這個算法在不斷地有讀者流的情況下,寫者會被阻塞。編寫一個寫者優先解決讀者寫者問題的程序,其中讀者和寫者均是多個進程,用信號量作為同步互斥機制。
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#define?maxn?15
struct?shared{
?????????????char?*?text;
?????????????int?rnwn;
};
union?semun{
int?val;
struct?semid_ds?*buf;
unsigned?short?*arry;
};
int?Vsem(int?semid){
struct?sembuf?buf={0+1SEM_UNDO};
if(semop(semid&buf1)==-1){
perror(“semop“);
return?0;
}
return?1;
}
int?Psem(int?semid){
struct?sembuf?buf={0-1SEM_UNDO};
if(semop(semid&buf1)==-1){
perror(“semop“);
return?0;
}
return?1;
}
void?creader(int?rn_mutexint?wn_mutexint?w_firstint?readint?writestruct?shared?*?share){
????????int?k=20;
????????while(k--){
????????????????Psem(read);
????????
評論
共有 條評論