C ++中使用信号量的作家首选项

时间:2018-12-14 11:54:37

标签: c++ multithreading semaphore

我正在尝试编写作家的喜好代码,以防止作家排队时出现饥饿,而读者由于优先级而忽略了它。用于检查有多少读者已阅读的计数器受信号量(readerCount)保护,尝试信号量用于指示读者正在尝试输入(psembufT),而资源信号量(psembufF)。

每当我尝试读取时,我都需要在一个“终端”窗口中写入一个文本文件(已编写代码),并在另一个窗口中从另一个窗口读取该文件,但出现 Segmentation fault [core dumped] 错误。

#include <iostream>
#include <fstream>
#include <string>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/shm.h>
#include <inttypes.h>
using namespace std;

#define SHM_KEY 9876                                    
#define SEMKEY  1234
struct sembuf vsembufR, psembufR, vsembufW, psembufW; 
struct sembuf vsembufF, psembufF, vsembufT, psembufT; 

int main()
{
union semun{   
    int val;
    struct semid_ds *buf;
    ushort myArray[0];
} arg;

string op;
ifstream myFile;                                 // makes an ifstream object to read from myFile

int shmid = shmget(SHM_KEY, 256, 0777|IPC_CREAT);
int *readerCount = (int*)shmat(shmid, 0, 0);    
int semid = semget(SEMKEY, 2, 0777|IPC_CREAT);  // Creates two semaphores
int pause;
readerCount = 0;

psembufR.sem_num=0;                          // init reader mutex members
psembufR.sem_op=-1;
psembufR.sem_flg=SEM_UNDO;
vsembufR.sem_num=0;
vsembufR.sem_op=1;
vsembufR.sem_flg=SEM_UNDO;

psembufF.sem_num=1;                          // resource
psembufF.sem_op=-1;
psembufF.sem_flg=SEM_UNDO;
vsembufF.sem_num=1;
vsembufF.sem_op=1;
vsembufF.sem_flg=SEM_UNDO;

psembufW.sem_num=0;                         // writer
psembufW.sem_op=-1;
psembufW.sem_flg=SEM_UNDO;
vsembufW.sem_num=0;
vsembufW.sem_op=1;
vsembufW.sem_flg=SEM_UNDO;

psembufT.sem_num=1;                         
psembufT.sem_op=-1;
psembufT.sem_flg=SEM_UNDO;
vsembufT.sem_num=1;
vsembufT.sem_op=1;
vsembufT.sem_flg=SEM_UNDO;

arg.val = 1;
semctl(semid, 0, SETVAL, arg);                
semctl(semid, 1, SETVAL, arg);

 while(1){
    cout << "Reader1:\n";
    pause = getchar();                      
    semop(semid, &psembufT, 1);             
    semop(semid, &psembufR, 1);             
    cout << "count inc" << endl;    
    *readerCount++;
    if(*readerCount == 1)                   // is this first reader
        semop(semid, &psembufF, 1);         // lok resource from writers if 1st reader
    semop(semid, &vsembufR, 1);             // unlock reader mutex (for other readers)
    semop(semid, &vsembufT, 1);             // unlock try mutex (done accessing file)
    // Critical Section
    myFile.open ("myFile.txt", ios::out | ios::app); // ::app appends the myFile (new line)                                           
    if(myFile.is_open()){        
        while(getline(myFile, op)){
            cout << op << endl;             // reads
        }
        myFile.close();
    }
    semop(semid, &psembufR, 1);             // lock reader mutex (avoid race)
    *readerCount--;
    if(*readerCount == 0)                   // is this the last reader
        semop(semid, &vsembufF, 1);         // unlock resource
    semop(semid, &vsembufR, 1);             // unlock reader mutex
}    

我认为这与我声明readerCount的方式有关,但我无法弄清楚。

1 个答案:

答案 0 :(得分:1)

readerCount = 0;应该是*readerCount = 0;

readerCountint*,因此当您执行readerCount = 0;时,您将指针设置为指向地址零。以后尝试更新该地址的值时,很可能会崩溃。