新聞中心
Semaphore是一種用于保護對共享資源的訪問的技術,在多任務或多線程環(huán)境中使用。Linux系統(tǒng)提供了一組函數(shù)來支持Semaphore,也就是sem_init、sem_wt、sem_post和sem_destroy等函數(shù)。本文將深入探究這組函數(shù)的使用方法和注意事項。

陵水黎族ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
1. Semaphore的定義
Semaphore是一種計數(shù)器,用于在多個進程之間共享資源。在Linux系統(tǒng)中,Semaphore被定義為結構體sem_t。Semaphore的值表示可用資源的數(shù)量。如果在一個進程中使用某個資源,則Semaphore的計數(shù)器將減少。當Semaphore值為0時,表示沒有可用資源,進程必須等待資源變得可用。
Semaphores可以使用兩種方式創(chuàng)建。之一種方式是在運行時創(chuàng)建,使用sem_init函數(shù)來創(chuàng)建。第二種方式是在系統(tǒng)啟動時創(chuàng)建,然后存儲在共享內(nèi)存中。Linux使用IPCs來實現(xiàn)這種類型的semaphores。
2. Semaphore函數(shù)
2.1 sem_init函數(shù)
sem_init函數(shù)用于初始化Semaphore,它需要三個參數(shù)。之一個參數(shù)是一個sem_t結構體指針。第二個參數(shù)是指定Semaphore的進程間共享的方式,可以是PTHREAD_PROCESS_PRIVATE或者PTHREAD_PROCESS_SHARED。第三個參數(shù)是Semaphore的值,表示初始資源數(shù)量。
函數(shù)原型為:
int sem_init(sem_t *sem, int pshared, unsigned int value);
2.2 sem_wt函數(shù)
sem_wt函數(shù)用于在Semaphore減少之前等待Semaphore值變得大于0。當Semaphore的值為0時,這個函數(shù)將一直等待。一旦Semaphore值大于0,則函數(shù)將Semaphore的值減1,并返回。
函數(shù)原型為:
int sem_wt(sem_t *sem);
2.3 sem_post函數(shù)
sem_post函數(shù)用于將Semaphore的值加1,并喚醒等待的進程或線程。 Semaphore的值可能增加后,調(diào)用sem_post函數(shù)將返回。
函數(shù)原型為:
int sem_post(sem_t *sem);
2.4 sem_destroy函數(shù)
sem_destroy函數(shù)用于釋放Semaphore資源。此函數(shù)需要一個sem_t結構體指針作為參數(shù)。
函數(shù)原型為:
int sem_destroy(sem_t *sem);
3. 使用Semaphore函數(shù)的注意事項
1)在使用Semaphore的過程中,需要確保在進程或線程間的修改Semaphore值是互斥的,避免訪問同一資源。
2)在使用Semaphore時,需要思考如何設置初始值,避免因賦給一個錯誤的初始值而導致的死鎖。
3)需要考慮多個Semaphore同時使用時可能存在的死鎖情況。
4)在多線程環(huán)境中使用Semaphore時,需要注意線程安全問題,如果線程使用了相同的Semaphore,則需要對Semaphore進行保護。
5)在進程退出前,需要調(diào)用sem_destroy函數(shù)釋放Semaphore資源。
4.
Semaphore是一種用于在多線程或多任務環(huán)境享資源的技術。在Linux操作系統(tǒng)中,提供了一組函數(shù)用于支持Semaphore,包括sem_init、sem_wt、sem_post和sem_destroy等函數(shù)。在使用Semaphore的過程中,需要注意安全性和死鎖等問題,確保資源共享的正確性和穩(wěn)定性。
成都網(wǎng)站建設公司-創(chuàng)新互聯(lián)為您提供網(wǎng)站建設、網(wǎng)站制作、網(wǎng)頁設計及定制高端網(wǎng)站建設服務!
linux 多進程信號同步問題
朋友你好:希望能幫到你。互相學習。
線程的更大特點是資源的共享性,但資源共享中的同步問題是多線程編程的難點。清敏linux下提供了多種方式來處理線程裂做同步,最常用的是互斥鎖、條件變量和信號量。
1)互斥鎖(mutex)
通過鎖機制實現(xiàn)線程間的同步。同一時刻只允許一個肆正衡線程執(zhí)行一個關鍵部分的代碼。
int pthread_mutex_init(pthread_mutex_t *mutex,const pthread_mutex_attr_t *mutexattr);
int pthread_mutex_lock(pthread_mutex *mutex);
int pthread_mutex_destroy(pthread_mutex *mutex);
int pthread_mutex_unlock(pthread_mutex *
(1)先初始化鎖init()或靜態(tài)賦值pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIER
attr_t有:
PTHREAD_MUTEX_TIMED_NP:其余線程等待隊列
PTHREAD_MUTEX_RECURSIVE_NP:嵌套鎖,允許線程多次加鎖,不同線程,解鎖后重新競爭
PTHREAD_MUTEX_ERRORCHECK_NP:檢錯,與一同,線程請求已用鎖,返回EDEADLK;
PTHREAD_MUTEX_ADAPTIVE_NP:適應鎖,解鎖后重新競爭
(2)加鎖,lock,trylock,lock阻塞等待鎖,trylock立即返回EBUSY
(3)解鎖,unlock需滿足是加鎖狀態(tài),且由加鎖線程解鎖
(4)清除鎖,destroy(此時鎖必需unlock,否則返回EBUSY,//Linux下互斥鎖不占用內(nèi)存資源
示例代碼
#include
#include
#include
#include
#include “iostream”
using namespace std;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int tmp;
void* thread(void *arg)
{
cout
#include
#include “stdlib.h”
#include “unistd.h”
pthread_mutex_t mutex;
pthread_cond_t cond;
void hander(void *arg)
{
free(arg);
(void)pthread_mutex_unlock(&mutex);
}
void *thread1(void *arg)
{
pthread_cleanup_push(hander, &mutex);
while(1)
{
printf(“thread1 is running\n”);
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond,&mutex);
printf(“thread1 applied the condition\n”);
pthread_mutex_unlock(&mutex);
sleep(4);
}
pthread_cleanup_pop(0);
}
void *thread2(void *arg)
{
while(1)
{
printf(“thread2 is running\n”);
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond,&mutex);
printf(“thread2 applied the condition\n”);
pthread_mutex_unlock(&mutex);
sleep(1);
}
}
int main()
{
pthread_t thid1,thid2;
printf(“condition variable study!\n”);
pthread_mutex_init(&mutex,NULL);
pthread_cond_init(&cond,NULL);
pthread_create(&thid1,NULL,thread1,NULL);
pthread_create(&thid2,NULL,thread2,NULL);
sleep(1);
do
{
pthread_cond_signal(&cond);
}while(1);
sleep(20);
pthread_exit(0);
return 0;
}
示例程序2:
#include
#include
#include “stdio.h”
#include “stdlib.h”
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
struct node
{
int n_number;
struct node *n_next;
} *head = NULL;
/**/
static void cleanup_handler(void *arg)
{
printf(“Cleanup handler of second thread./n”);
free(arg);
(void)pthread_mutex_unlock(&mtx);
}
static void *thread_func(void *arg)
{
struct node *p = NULL;
pthread_cleanup_push(cleanup_handler, p);
while (1)
{
//這個mutex主要是用來保證pthread_cond_wait的并發(fā)性
pthread_mutex_lock(&mtx);
while (head == NULL)
{
//這個while要特別說明一下,單個pthread_cond_wait功能很完善,為何
//這里要有一個while (head == NULL)呢?因為pthread_cond_wait里的線
//程可能會被意外喚醒,如果這個時候head != NULL,則不是我們想要的情況。
//這個時候,應該讓線程繼續(xù)進入pthread_cond_wait
// pthread_cond_wait會先解除之前的pthread_mutex_lock鎖定的mtx,
//然后阻塞在等待對列里休眠,直到再次被喚醒(大多數(shù)情況下是等待的條件成立
//而被喚醒,喚醒后,該進程會先鎖定先pthread_mutex_lock(&mtx);,再讀取資源
//用這個流程是比較清楚的/*block–>unlock–>wait() return–>lock*/
pthread_cond_wait(&cond, &mtx);
p = head;
head = head->n_next;
printf(“Got %d from front of queue/n”, p->n_number);
free(p);
}
pthread_mutex_unlock(&mtx); //臨界區(qū)數(shù)據(jù)操作完畢,釋放互斥鎖
}
pthread_cleanup_pop(0);
return 0;
}
int main(void)
{
pthread_t tid;
int i;
struct node *p;
//子線程會一直等待資源,類似生產(chǎn)者和消費者,但是這里的消費者可以是多個消費者,而
//不僅僅支持普通的單個消費者,這個模型雖然簡單,但是很強大
pthread_create(&tid, NULL, thread_func, NULL);
sleep(1);
for (i = 0; i n_number = i;
pthread_mutex_lock(&mtx); //需要操作head這個臨界資源,先加鎖,
p->n_next = head;
head = p;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx); //解鎖
sleep(1);
}
printf(“thread 1 wanna end the line.So cancel thread 2./n”);
//關于pthread_cancel,有一點額外的說明,它是從外部終止子線程,子線程會在最近的取消點,退出
//線程,而在我們的代碼里,最近的取消點肯定就是pthread_cond_wait()了。
pthread_cancel(tid);
pthread_join(tid, NULL);
printf(“All done — exiting/n”);
return 0;
}
3)信號量
如同進程一樣,線程也可以通過信號量來實現(xiàn)通信,雖然是輕量級的。
信號量函數(shù)的名字都以”sem_”打頭。線程使用的基本信號量函數(shù)有四個。
#include
int sem_init (sem_t *sem , int pshared, unsigned int value);
這是對由sem指定的信號量進行初始化,設置好它的共享選項(linux 只支持為0,即表示它是當前進程的局部信號量),然后給它一個初始值VALUE。
兩個原子操作函數(shù):
int sem_wait(sem_t *sem);
int sem_post(sem_t *sem);
這兩個函數(shù)都要用一個由sem_init調(diào)用初始化的信號量對象的指針做參數(shù)。
sem_post:給信號量的值加1;
sem_wait:給信號量減1;對一個值為0的信號量調(diào)用sem_wait,這個函數(shù)將會等待直到有其它線程使它不再是0為止。
int sem_destroy(sem_t *sem);
這個函數(shù)的作用是再我們用完信號量后都它進行清理。歸還自己占有的一切資源。
示例代碼:
#include
#include
#include
#include
#include
#include
#define return_if_fail(p) if((p) == 0){printf (“:func error!/n”, __func__);return;}
typedef struct _PrivInfo
{
sem_t s1;
sem_t s2;
time_t end_time;
}PrivInfo;
static void info_init (PrivInfo* thiz);
static void info_destroy (PrivInfo* thiz);
static void* pthread_func_1 (PrivInfo* thiz);
static void* pthread_func_2 (PrivInfo* thiz);
int main (int argc, char** argv)
{
pthread_t pt_1 = 0;
pthread_t pt_2 = 0;
int ret = 0;
PrivInfo* thiz = NULL;
thiz = (PrivInfo* )malloc (sizeof (PrivInfo));
if (thiz == NULL)
{
printf (“: Failed to malloc priv./n”);
return -1;
}
info_init (thiz);
ret = pthread_create (&pt_1, NULL, (void*)pthread_func_1, thiz);
if (ret != 0)
{
perror (“pthread_1_create:”);
}
ret = pthread_create (&pt_2, NULL, (void*)pthread_func_2, thiz);
if (ret != 0)
{
perror (“pthread_2_create:”);
}
pthread_join (pt_1, NULL);
pthread_join (pt_2, NULL);
info_destroy (thiz);
return 0;
}
static void info_init (PrivInfo* thiz)
{
return_if_fail (thiz != NULL);
thiz->end_time = time(NULL) + 10;
sem_init (&thiz->s1, 0, 1);
sem_init (&thiz->s2, 0, 0);
return;
}
static void info_destroy (PrivInfo* thiz)
{
return_if_fail (thiz != NULL);
sem_destroy (&thiz->s1);
sem_destroy (&thiz->s2);
free (thiz);
thiz = NULL;
return;
}
static void* pthread_func_1 (PrivInfo* thiz)
{
return_if_fail (thiz != NULL);
while (time(NULL) end_time)
{
sem_wait (&thiz->s2);
printf (“pthread1: pthread1 get the lock./n”);
sem_post (&thiz->s1);
printf (“pthread1: pthread1 unlock/n”);
sleep (1);
}
return;
}
static void* pthread_func_2 (PrivInfo* thiz)
{
return_if_fail (thiz != NULL);
while (time (NULL) end_time)
{
sem_wait (&thiz->s1);
printf (“pthread2: pthread2 get the unlock./n”);
sem_post (&thiz->s2);
printf (“pthread2: pthread2 unlock./n”);
sleep (1);
}
return;
}
通 過執(zhí)行結果后,可以看出,會先執(zhí)行線程二的函數(shù),然后再執(zhí)行線程一的函數(shù)。它們兩就實現(xiàn)了同步
創(chuàng)新互聯(lián)-老牌IDC、云計算及IT信息化服務領域的服務供應商,業(yè)務涵蓋IDC(互聯(lián)網(wǎng)數(shù)據(jù)中心)服務、云計算服務、IT信息化、AI算力租賃平臺(智算云),軟件開發(fā),網(wǎng)站建設,咨詢熱線:028-86922220
文章標題:深入探究LinuxSemaphore函數(shù)(linuxsemaphore函數(shù))
文章分享:http://www.dlmjj.cn/article/ccddegc.html


咨詢
建站咨詢
