程序間通訊機制01 訊號量

2021-08-15 02:20:59 字數 1275 閱讀 1635

它是乙個特殊變數,只允許對它進行等待(wait)和傳送訊號(signal)這兩種操作。最簡單的訊號量是只能取值0和1的變數,即二進位制訊號量。可以取多個正整數值的訊號量被稱為通用訊號量。

(1)原型:

int

semget(key_t key, int num_sems, int sem_flags);

(2)作用:建立乙個新訊號量或取得乙個已有訊號量的鍵。

(1)原型:

int

semget(int sem_id, struct sembuf *sem_ops, size_t num_sem_ops);

(2)作用:用於改變訊號量的值。

(1)原型:

int

semctl(int sem_id, int sem_num, int command...);

(2)作用:用來直接控制訊號量資訊。

#include 

#include

#include

#include

#include

#include

void *thread_function(void *arg);

sem_t bin_sem;

#define work_size 1024

char work_area[work_size];

int main()

res = pthread_create(&a_thread, null, thread_function, null);

if (res != 0)

printf("input some text. enter 'end' to finish.\n");

while (strncmp("end", work_area, 3) != 0)

printf("\nwaiting for thread to finish...\n");

res = pthread_join(a_thread, &thread_result);

if (res != 0)

printf("thread joind\n");

sem_destroy(&bin_sem);

exit(exit_success);

return0;}

void *thread_function(void *arg)

pthread_exit(null);

}

程序間通訊 訊號量

ipc識別符號和關鍵字 在終端輸入ipcs,可以看到目前系統中所有的ipc資訊 第一列的key就是ipc的關鍵字,第二列是ipc的識別符號。ftok 函式用於獲得乙個ipc的關鍵字,其函式原型是 key t ftok const char pathname,int proj id 下面是乙個訊號量的...

程序間通訊 訊號量

system ipc中,對於每乙個新建的訊號量 訊息佇列 共享記憶體,都有乙個在整個系統中唯一的識別符號。每個標識也都有唯一對應的關鍵字,關鍵字的資料型別為ket t 在終端輸入命令 ipcs 可以看到目前系統中所有的ipc資訊 共享記憶體段 鍵 shmid 擁有者 許可權 位元組 nattch 狀...

程序間通訊 訊號量

訊號量不是ipc 機構,它只是乙個計數器用於不同程序之間或同一程序不同執行緒之間的同步,型別 二元訊號 值為0或 1,1說明有可用資源,0說明此時資源占用,其他程序需等待。計數訊號量 表示可用資源數量。計數訊號量集 由乙個或多個訊號量組成的集合,每乙個都是計數訊號量。訊號量資料結構 include ...