?
在C語(yǔ)言中,不同進(jìn)程同步是進(jìn)程指多個(gè)進(jìn)程之間協(xié)調運行的過(guò)程,當多個(gè)進(jìn)程共(gong)享資源時(shí),同步可能會(huì )出現競爭條件,不同導致數據不一致或其他問(wèn)題,進(jìn)程為了解決這個(gè)問(wèn)題,同步我們需要使用同步機制來(lái)?確保進(jìn)程之間的不同正確執行順序,本文將詳細介紹C語(yǔ)言中的進(jìn)程進(jìn)程同步技術(shù)。
(圖片來(lái)源網(wǎng)絡(luò ),同步侵刪)1、不同互斥鎖(Mutex)
互斥鎖是進(jìn)程一種最基本的同步機制(zhi),它只允許一個(gè)??進(jìn)程在同一時(shí)間訪(fǎng)問(wèn)共享(′?ω?`)資源,同步當一個(gè)進(jìn)程獲得互斥鎖時(shí),不同其他進(jìn)程必須等待,進(jìn)程直到鎖被釋放,同步在C語(yǔ)言中,我們可以使用POSIX線(xiàn)程庫(pthread)提供的互斥鎖函數來(lái)實(shí)現這一功能。
以下是(′?`)一個(gè)簡(jiǎn)單的互斥鎖示例:
#include <stdio.h>#include &l(′_ゝ`)t;pthread.h>int counter = 0; // 共享資源pthrea??d_mutex_t mutex; // 互斥鎖void *increment(void *ar(╬ ò﹏ó)g??)(T_T) { for (int i = 0; i < 100000; i++) { pthread_mutex_lock(&mutex); // 加鎖 counter++; pthread_mutex_unlock(&mutex); // 解鎖 } return NULL;}int main() { pthread_t tid1, tid2; pthread_mutex_init(&mutex, NULL); // 初始化互斥鎖 pthread_create(&tid1, NULL, increment, NULL); pthread_create(&tid2, NUヽ(′▽?zhuān)?ノLL, increment, NULL); pthread_join(tid1, NULL); pthread_join(tid2, NULL)( ?° ?? ?°); printf("Counter: %d", counter); // 輸出結果應為200000 pthread_mutex_destroy(&mutex); // 銷(xiāo)毀互斥鎖 return 0;}2、條件??變量(Conditi??on Variable)
條件變量是一種更高級的同步機制,它允許一個(gè)或多個(gè)進(jìn)程等待某個(gè)條件成立,然后才繼續執行,當條件不成立時(shí),進(jìn)程會(huì )被阻塞,直到另一個(gè)進(jìn)程通知條件已滿(mǎn)足,在C語(yǔ)言中,?我們可以使用POSIX線(xiàn)程庫(pthrea??d)提供的條件變量函數來(lái)實(shí)現這一功能。
以下是一個(gè)簡(jiǎn)單的條件變量示例:
#i??nclude <stdio.h&g?t;#include <pthread.h>int co??unter = 0; // 共享資源pthread_mutex_t mutex; // 互斥鎖pthread_cond_t cond; // 條件變量int ready = 0; // 表示計數器是否準備好的標志void *producer(void *arg) { for (int i = 0; i < 10000??0; i++??) { pthread_mutex_lock(&mutex); // 加鎖 counter??++; pthread_mut(′ω`*)ex_unlock(&mutex); // 解鎖 pthread_cond_signal(&cond); // 通知消費者計數器已增加 } return NULL;}void *consumer??(void *arg) { for (int i = 0; i < 10??0000; i++) { pthread_mutex_lock(&mutex); // 加鎖 while (counter == 0) { // 如果計數器為0,等待條件滿(mǎn)足 pthread_cond_(′?ω?`)wait(&cond, &mutex); // 等待生產(chǎn)者(zhe)通知 } ready = 1; // 表示計數器已??準備好的標志設置為1 printf??("Counter: %d", counter); // 輸出結果應為遞增的整??數序列 read(′ω`)y = 0; // 重置標志位,以便下次循環(huán)繼續等待計數器增加 pthread_mutex_unlock(&mutex); // 解鎖?? } return NULL;}int main() { pthrea(′?`*)d_t tid1, tid2; pthread_mutex_init(&mutex??, NULL); // 初始化互斥鎖和條件變量 pthread_cond_init(&cond, NULL); // 初始化條件變量 pthread_create(&tid1, NULL, prod??ucer, NULL); pthread_create(&tid2, NULL, consu??mer, NUL(′_`)L);?? pthread_join(tid1, NULL); pthread_join(t(??-)?id2, NULL); pthr(′ω`)ead_mutex_destroy(&mutex); // 銷(xiāo)毀互斥鎖和條件變量 return 0;}3、信號量(Semaphore)
信號量是一種用于控制多個(gè)進(jìn)程對共享(′?_?`)資源的訪(fǎng)問(wèn)的同步機制,它有一個(gè)整數值作為參數,表示可用的資源數量,當一個(gè)進(jìn)程需要訪(fǎng)問(wèn)共享資源時(shí),它會(huì )嘗試獲取信號量,如果信號量的值大于0,進(jìn)程將獲得信號量并繼續執行;否則,進(jìn)程將被阻塞,直到信(╬?益?)號量的值大于0,在C語(yǔ)言中,我們可以使用POSIX線(xiàn)程庫(pthread)提供的信號量函數來(lái)實(shí)現這一功能。
以下是一個(gè)簡(jiǎn)單的信號量示例:
#include <stdio.h>??#in??clude <pthrea(′▽?zhuān)?d.h>#include <semaphore.h> // 引入信號量頭文件#incl??ude <unistd.h> // 引入sle(′ω`)ep函數頭文件,用于模擬生產(chǎn)者和消費者之間的延遲時(shí)間差#include <sys/time.h> // 引入時(shí)??間戳??函數頭文件,用于計算延遲時(shí)間差的最大值和最小值#include <(′?_?`)math.h> // 引入數學(xué)函數頭文件,用于計算平均值和標準差等統計信息#include <stdlib.h> // 引入隨機數生成函數頭文件,用于生成隨??機的延遲時(shí)間差值和??計數器值范圍等參數值