1、一個讀取設置信號量集合的示例程序
code:
#include<stdio.h> #include<sys/types.h> #include<sys/sem.h> #include<errno.h> #define MAX_SEMAPHORES 5 int main() { union semun // 參數聯合體 { int val; struct semid_ds * buf; unsigned short * array; //for GETALL and SETALL } arg; int semid=semget(IPC_PRIVATE,MAX_SEMAPHORES,IPC_CREAT|0666); //創建信號量集合 if(semid!=-1){ unsigned short sem_array[MAX_SEMAPHORES]; int i; for(i=0;i<MAX_SEMAPHORES;i++) sem_array[i]=i+1; arg.array=sem_array; int ret=semctl(semid,0,SETALL,arg); //設置信號量-全部 if(ret==-1) printf("failed %d/n",errno); unsigned short sem_read_array[MAX_SEMAPHORES]; arg.array=sem_read_array; ret=semctl(semid,0,GETALL,arg); //讀取信號量-全部 if(ret==-1) printf("failed %d/n",errno); for(i=0;i<MAX_SEMAPHORES;i++) printf("Semaphore %d, value %d/n",i,sem_read_array[i]); for(i=0;i<MAX_SEMAPHORES;i++) { ret=semctl(semid,i,GETVAL); //讀取信號量-單個 printf("Semaphore %d, value %d/n",i,ret); } ret=semctl(semid,0,IPC_RMID); //刪除信號量集合 } else printf("Could not allocate semaphore %d/n",errno); return 0; }
結果:
root@ubuntu:/code/chap9# ./run3 Semaphore 0, value 1 Semaphore 1, value 2 Semaphore 2, value 3 Semaphore 3, value 4 Semaphore 4, value 5 Semaphore 0, value 1 Semaphore 1, value 2 Semaphore 2, value 3 Semaphore 3, value 4 Semaphore 4, value 5
2、使用信號量 實現生產者-消費者問題
生產者:
root@ubuntu:/code/chap9# cat producer.c #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/sem.h> #include<errno.h> int sem_id; void init() //建立信號量集合 { key_t key; int ret; key=ftok(".",'s'); sem_id=semget(key,2,IPC_CREAT|0644); union semun { int val; struct semid_ds * buf; unsigned short * array; }arg; unsigned short sem_array[2]={0,100}; arg.array=sem_array; ret=semctl(sem_id,0,SETALL,arg); //初始化信號量,2個信號量,produt、space if(ret==-1) printf("setall failed %d/n",errno); printf("product init is %d/n",semctl(sem_id,0,GETVAL)); printf("space init is %d/n",semctl(sem_id,1,GETVAL)); } int main() { //操作兩個信號量使用的結構體 struct sembuf sop[2]; sop[0].sem_num=0; //執行加1操作,每生產一個產品,對 product 加1 sop[0].sem_op=1; sop[0].sem_flg=0; sop[1].sem_num=1; //執行減1操作,每生產一個產品,對 space 減1 sop[1].sem_op=-1; sop[1].sem_flg=0; init(); printf("this is productor/n"); int count=1; while(1) { printf("stage %d/nbefore produce:/n",count++); printf("product is %d/n",semctl(sem_id,0,GETVAL)); printf("space is %d/n",semctl(sem_id,1,GETVAL)); semop(sem_id,(struct sembuf*)&sop[1],1); //get the space printf("now producing.../n"); semop(sem_id,(struct sembuf*)&sop[0],1); //put the product printf("after produce:/n"); printf("product is %d/n",semctl(sem_id,0,GETVAL)); printf("space is %d/n/n",semctl(sem_id,1,GETVAL)); sleep(4); } return 0; }
消費者:
root@ubuntu:/code/chap9# cat customer.c #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<sys/types.h> #include<sys/ipc.h> #include<sys/sem.h> #include<errno.h> int sem_id; void init() { key_t key; key=ftok(".",'s'); sem_id=semget(key,2,IPC_CREAT|0644); } int main() { init(); struct sembuf sop[2]; sop[0].sem_num=0; sop[0].sem_op=-1; sop[0].sem_flg=0; sop[1].sem_num=1; sop[1].sem_op=1; sop[1].sem_flg=0; printf("this is customer/n"); int count=1; while(1) { printf("stage %d:/nbefore sonsume:/n",count++); printf("product is %d/n",semctl(sem_id,0,GETVAL)); printf("space is %d/n",semctl(sem_id,1,GETVAL)); semop(sem_id,(struct sembuf*)&sop[0],1); //get the product printf("now consuming.../n"); semop(sem_id,(struct sembuf*)&sop[1],1); //increase the space printf("after consume/n"); printf("product is %d/n",semctl(sem_id,0,GETVAL)); printf("space is %d/n/n",semctl(sem_id,1,GETVAL)); sleep(3); } }
stage 3 before produce: product is 2 space is 98 now producing... after produce: product is 3 space is 97 stage 4 before produce: product is 3 space is 97 now producing... after produce: product is 4 space is 96
stage 2: before sonsume: product is 9 space is 91 now consuming... after consume product is 8 space is 92 stage 3: before sonsume: product is 9 space is 91 now consuming... after consume product is 8 space is 92
在Java中,Semaphore和CountDownLatch都是用於線程同步的工具類,但它們的應用場景不同。Semaphore用於控制同時訪問某些資源的線程數量,而CountDownLatch用於等待一組線程完成他們的操作。 如果我們想要
在現代的多線程編程中,Semaphore和CountDownLatch是兩個非常常見和重要的工具類,它們都可以用來實現多線程間的同步和互斥,提高程序的併發性能和效率。本文將詳細介紹Java中的Semaphore和CountDownLatch
虛擬線程(Virtual Threads)是 Java 21 所有新特性中最爲吸引人的內容,它可以大大來簡化和增強Java應用的併發性。但是,隨着這些變化而來的是如何最好地管理此吞吐量的問題。本文,就讓我們看一下開發人員在使用虛擬線程時,應
用於衡量算法佔用內存空間隨着數據量變大時的增長趨勢。這個概念與時 間複雜度非常類似,只需將“運行時間”替換爲“佔用內存空間”。 算法在運行過程中使用的內存空間主要包括以下幾種。 ‧ 輸入空間:用於存儲算法的輸入數據。
【關鍵詞】{{linux安裝}} 【提問】{{question}} 在Linux操作系統的安裝過程中,如何選擇合適的發行版,並確保安裝過程順利進行?此外,對於不同硬件配置的用戶,有哪些特定的安裝注意事項和優化策略? 【文章】{{Linux操
有時,Python發佈的程序需要被打包爲一個文件夾、甚至一個文件發佈。 目前(2020)最佳的策略是使用pyinstaller。 pyinstaller不僅支持打包整個運行環境到一個可執行文件,而且還支持加密。 但唯一的問題是,必須依賴
安裝 Python 3.8 或更高版本 https://phoenixnap.com/kb/how-to-install-python-3-ubuntu # 查看是否已經安裝了python python --version #更新包 sud
前言 linux下安裝nginx比較繁瑣,遇到內網部署環境更是麻煩,所以研究了下nginx綠色免安裝版的部署包製作,開箱即用,特此記錄分享,一下操作在centos8環境下安裝,如果需要其他內核系統的安裝(Debian/Ubuntu等),請在
核心板參數: 尺寸: 55mm*45mm 連接器高度: 1.5mm CPU: RK3588S 主頻: 四核 Cortex-A55, Quad-core ARM Cortex-A76,Neon and FPU, 2.4GHZ 內存:
執行步驟: 1. 備份 mv -f /etc/apt/sources.list /etc/apt/sources.list.bak 2. 執行替換爲阿里源 cat > /etc/apt/sources.list<< EOF deb
本文分享自華爲雲社區《Ascend C 自定義PRelu算子》,作者: jackwangcumt。 1 PRelu算子概述 PReLU是 Parametric Rectified Linear Unit的縮寫,首次由何凱明團隊提出,和Le
本文分享自華爲雲社區《基於STM32的智慧農業管理系統設計與實現》,作者: DS小龍哥。 一、前言 1.1 項目介紹 【1】項目功能 隨着全球農業現代化進程的加快,以及物聯網、人工智能等先進技術的發展與應用,智慧農業已經成爲現代農業發
aws ec默認用戶名 在aws中每個發佈版的默認用戶名不一樣,記錄一下。 每個 Linux 實例都使用默認的 Linux 系統用戶帳戶啓動。默認用戶名由您在啓動實例時指定的 AMI 確定。 對於 Amazon Linux 2 或 Amaz
Ubuntu 和 Debian 文件系統的源碼在“ iTOP-3588 開發板 \01_ 【 iTOP-RK3588 開發板】基 礎資料 \05_iTOP-RK3588 開發板 Linux 源碼 \02_Debian 和
MobaXterm 軟件網盤下載路徑:“iTOP-3588 開發板\02_【iTOP-RK3588 開發板】開發資 料\04_iTOP-3588 開發板所需 PC 軟件(工具)\02-MobaXterm”。 打開