linux系統編程:線程同步-訊號量(semaphore)

來源:互聯網
上載者:User

標籤:com   ret   類型   mil   sleep   sem   升級版   linu   限制   

                             線程同步-訊號量(semaphore)

生產者與消費者問題再思考

在實際生活中,僅僅要有商品。消費者就能夠消費,這沒問題。

但生產者的生產並非無限的。比如,倉庫是有限的,原材料是有限的,生產指標受消費指標限制等等。為了進一步,解決好生產者與消費者問題,引入訊號量進機制。


訊號量

訊號量(semaphore)是相互排斥量的升級版:相互排斥量的狀態為0或1。而訊號量能夠為n。

也就是說,使用相互排斥量時。最多同意一個線程進入關鍵區,而訊號量同意多個,詳細值是訊號量當前的內部值。


相關函數

sem_t       //訊號量類型sem_init(sem_t *sem, int pshared, unsigned int value);sem_wait(sem_t *sem)sem_trywaitsem_timedwaitsem_post(sem_t *sem)sem_destroy
重要的是理解:sem_wait和sem_post兩個函數。

sem_wait(sem);當sem為零時,線程堵塞。否則,sem減一,線程不堵塞。

sem_post(sem);sem加一。

此外,使用sem_init方法,對訊號量類型初始化,第二個參數。預設是0,標明用於線程之間。第三個參數指定了初始值。


單生產者與單消費者

#include <stdio.h>#include <unistd.h>#include <pthread.h>#include <semaphore.h>#define NUM 5sem_t blank_num, product_num;int i, j, k;int goods[NUM];void *producer(void *argv){while (1){sem_wait(&blank_num);goods[i] = rand() % 100 + 1;printf("produce %d\n", goods[i]);sem_post(&product_num);i = (i + 1) % NUM;sleep(rand() % 2);}}void *comsumer(void *argv){while (1){sem_wait(&product_num);printf("comsume %d\n", goods[j]);goods[j] = 0;sem_post(&blank_num);j = (j + 1) % NUM;sleep(rand() % 2);}}int main(void){i = j = k = 0;//初始化訊號量sem_init(&blank_num, 0, NUM);sem_init(&product_num, 0, 0);pthread_t pro, com;pthread_create(&com, NULL, producer, NULL);pthread_create(&pro, NULL, comsumer, NULL);pthread_join(com, NULL);pthread_join(pro, NULL);sem_destroy(&blank_num);sem_destroy(&product_num);return 0;}


多生產者與多消費者

#include <stdio.h>#include <unistd.h>#include <pthread.h>#include <semaphore.h>#define NUM 5pthread_mutex_t m1, m2;sem_t blank_num, product_num;int goods[NUM];int i, j, k;void *producer(void *argv){while (1){sem_wait(&blank_num);pthread_mutex_lock(&m1);goods[i] = rand() % 100 + 1;printf("produce %d\n", goods[i]);i = (i + 1) % NUM;pthread_mutex_unlock(&m1);sem_post(&product_num);sleep(rand() % 2);}}void *comsumer(void *argv){while (1){sem_wait(&product_num);pthread_mutex_lock(&m2);printf("comsume %d\n", goods[j]);goods[j] = 0;   //置零j = (j + 1) % NUM;pthread_mutex_unlock(&m2);sem_post(&blank_num);sleep(rand() % 2);}}int main(void){i = j = k = 0;//初始化訊號量及相互排斥量sem_init(&blank_num, 0, NUM);sem_init(&product_num, 0, 0);pthread_mutex_init(&m1, NULL);pthread_mutex_init(&m2, NULL);pthread_t pro[2], com[3];for (k = 0; k < 3; k++)pthread_create(&com[k], NULL, producer, NULL);for (k = 0; k < 2; k++)pthread_create(&pro[k], NULL, comsumer, NULL);for (k = 0; k < 3; k++)pthread_join(com[k], NULL);for (k = 0; k < 2; k++)pthread_join(pro[k], NULL);pthread_mutex_destroy(&m1);pthread_mutex_destroy(&m2);sem_destroy(&blank_num);sem_destroy(&product_num);return 0;}


     

CCPP Blog 檔案夾


linux系統編程:線程同步-訊號量(semaphore)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.