Linux訊號量實踐(2)

來源:互聯網
上載者:User

標籤:linux   ipc   處理序間通訊   訊號量   同步與互斥   

訊號量API綜合實踐

//實踐1:封裝PV原語union mySemUn{    int              val;    // Value for SETVAL//    struct semid_ds *buf;    // Buffer for IPC_STAT, IPC_SET//    unsigned short  *array;  // Array for GETALL, SETALL//    struct seminfo  *__buf;  // Buffer for IPC_INFO (Linux-specific)//};//封裝V操作int sem_V(int semid){    struct sembuf buf = {0,+1,0};    return semop(semid,&buf,1);}//封裝P操作int sem_P(int semid){    struct sembuf buf = {0,-1,0};;    return semop(semid,&buf,1);}int main(){    //建立或擷取訊號量    int semid = semget(0x12345670,1,0666|IPC_CREAT);    if (semid == -1)    {        err_exit("semget error");    }    //設定訊號量的值    int valueForSetSem = 0;    cout << "Please input the value to set sem: ";    cin >> valueForSetSem;    union mySemUn setValue;    setValue.val = valueForSetSem;    //設定訊號量    if (semctl(semid,0,SETVAL,setValue) != 0)    {        err_exit("semctl SETVAL error");    }    //進入臨界區    sem_P(semid);    cout << "Hello Xiaofang!" << endl;    //退出臨界區    sem_V(semid);    return 0;}

//實踐2:線程與訊號量綜合實踐union mySemUn{    int              val;    // Value for SETVAL//    struct semid_ds *buf;    // Buffer for IPC_STAT, IPC_SET//    unsigned short  *array;  // Array for GETALL, SETALL//    struct seminfo  *__buf;  // Buffer for IPC_INFO (Linux-specific)//};//封裝V操作int sem_V(int semid){    struct sembuf buf = {0,+1,0};    return semop(semid,&buf,1);}//封裝P操作int sem_P(int semid){    struct sembuf buf = {0,-1,0};;    return semop(semid,&buf,1);}unsigned long long int count = 0;void *threadForAddCount(void *arg){    int semid = *static_cast<int *>(arg);    for (;;)    {        sem_P(semid);        ++ count;        sem_V(semid);    }    pthread_exit(NULL);}void *threadForPrintCount(void *arg){    for (;;)    {        cout << "count = " << count << endl;        sleep(1);    }    pthread_exit(NULL);}int main(){    //建立或擷取訊號量    int semid = semget(0x12345670,1,0666|IPC_CREAT);    if (semid == -1)    {        err_exit("semget error");    }    //設定訊號量的值    int valueForSetSem = 0;    cout << "Please input the value to set sem: ";    cin >> valueForSetSem;    union mySemUn setValue;    setValue.val = valueForSetSem;    //設定訊號量    if (semctl(semid,0,SETVAL,setValue) != 0)    {        err_exit("semctl SETVAL error");    }    pthread_t addThread[2];    pthread_t printThread;    //建立線程    for (int i = 0; i < 2; ++i)    {        pthread_create(&addThread[i],NULL,threadForAddCount,&semid);    }    pthread_create(&printThread,NULL,threadForPrintCount,NULL);    //等待線程終止    for (int i = 0; i < 2; ++i)    {        pthread_join(addThread[i],NULL);    }    pthread_join(printThread,NULL);    return 0;}void err_exit(std::string str){    perror(str.c_str());    exit(EXIT_FAILURE);}

附-Makefile
CC = g++ CPPFLAGS = -Wall -g -pthreadBIN = mainSOURCES = $(BIN.=.cpp).PHONY: clean all all: $(BIN)$(BIN): $(SOURCES)clean:    -rm -rf $(BIN) bin/ obj/ core


Linux訊號量實踐(2)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.