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

陵水黎族ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
1. Semaphore的定義
Semaphore是一種計(jì)數(shù)器,用于在多個(gè)進(jìn)程之間共享資源。在Linux系統(tǒng)中,Semaphore被定義為結(jié)構(gòu)體sem_t。Semaphore的值表示可用資源的數(shù)量。如果在一個(gè)進(jìn)程中使用某個(gè)資源,則Semaphore的計(jì)數(shù)器將減少。當(dāng)Semaphore值為0時(shí),表示沒有可用資源,進(jìn)程必須等待資源變得可用。
Semaphores可以使用兩種方式創(chuàng)建。之一種方式是在運(yùn)行時(shí)創(chuàng)建,使用sem_init函數(shù)來創(chuàng)建。第二種方式是在系統(tǒng)啟動(dòng)時(shí)創(chuàng)建,然后存儲(chǔ)在共享內(nèi)存中。Linux使用IPCs來實(shí)現(xiàn)這種類型的semaphores。
2. Semaphore函數(shù)
2.1 sem_init函數(shù)
sem_init函數(shù)用于初始化Semaphore,它需要三個(gè)參數(shù)。之一個(gè)參數(shù)是一個(gè)sem_t結(jié)構(gòu)體指針。第二個(gè)參數(shù)是指定Semaphore的進(jìn)程間共享的方式,可以是PTHREAD_PROCESS_PRIVATE或者PTHREAD_PROCESS_SHARED。第三個(gè)參數(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。當(dāng)Semaphore的值為0時(shí),這個(gè)函數(shù)將一直等待。一旦Semaphore值大于0,則函數(shù)將Semaphore的值減1,并返回。
函數(shù)原型為:
int sem_wt(sem_t *sem);
2.3 sem_post函數(shù)
sem_post函數(shù)用于將Semaphore的值加1,并喚醒等待的進(jìn)程或線程。 Semaphore的值可能增加后,調(diào)用sem_post函數(shù)將返回。
函數(shù)原型為:
int sem_post(sem_t *sem);
2.4 sem_destroy函數(shù)
sem_destroy函數(shù)用于釋放Semaphore資源。此函數(shù)需要一個(gè)sem_t結(jié)構(gòu)體指針作為參數(shù)。
函數(shù)原型為:
int sem_destroy(sem_t *sem);
3. 使用Semaphore函數(shù)的注意事項(xiàng)
1)在使用Semaphore的過程中,需要確保在進(jìn)程或線程間的修改Semaphore值是互斥的,避免訪問同一資源。
2)在使用Semaphore時(shí),需要思考如何設(shè)置初始值,避免因賦給一個(gè)錯(cuò)誤的初始值而導(dǎo)致的死鎖。
3)需要考慮多個(gè)Semaphore同時(shí)使用時(shí)可能存在的死鎖情況。
4)在多線程環(huán)境中使用Semaphore時(shí),需要注意線程安全問題,如果線程使用了相同的Semaphore,則需要對(duì)Semaphore進(jìn)行保護(hù)。
5)在進(jìn)程退出前,需要調(diào)用sem_destroy函數(shù)釋放Semaphore資源。
4.
Semaphore是一種用于在多線程或多任務(wù)環(huán)境享資源的技術(shù)。在Linux操作系統(tǒng)中,提供了一組函數(shù)用于支持Semaphore,包括sem_init、sem_wt、sem_post和sem_destroy等函數(shù)。在使用Semaphore的過程中,需要注意安全性和死鎖等問題,確保資源共享的正確性和穩(wěn)定性。
成都網(wǎng)站建設(shè)公司-創(chuàng)新互聯(lián)為您提供網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)及定制高端網(wǎng)站建設(shè)服務(wù)!
linux 多進(jìn)程信號(hào)同步問題
朋友你好:希望能幫到你?;ハ鄬W(xué)習(xí)。
線程的更大特點(diǎn)是資源的共享性,但資源共享中的同步問題是多線程編程的難點(diǎn)。清敏linux下提供了多種方式來處理線程裂做同步,最常用的是互斥鎖、條件變量和信號(hào)量。
1)互斥鎖(mutex)
通過鎖機(jī)制實(shí)現(xiàn)線程間的同步。同一時(shí)刻只允許一個(gè)肆正衡線程執(zhí)行一個(gè)關(guān)鍵部分的代碼。
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:其余線程等待隊(duì)列
PTHREAD_MUTEX_RECURSIVE_NP:嵌套鎖,允許線程多次加鎖,不同線程,解鎖后重新競(jìng)爭(zhēng)
PTHREAD_MUTEX_ERRORCHECK_NP:檢錯(cuò),與一同,線程請(qǐng)求已用鎖,返回EDEADLK;
PTHREAD_MUTEX_ADAPTIVE_NP:適應(yīng)鎖,解鎖后重新競(jìng)爭(zhēng)
(2)加鎖,lock,trylock,lock阻塞等待鎖,trylock立即返回EBUSY
(3)解鎖,unlock需滿足是加鎖狀態(tài),且由加鎖線程解鎖
(4)清除鎖,destroy(此時(shí)鎖必需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)
{
//這個(gè)mutex主要是用來保證pthread_cond_wait的并發(fā)性
pthread_mutex_lock(&mtx);
while (head == NULL)
{
//這個(gè)while要特別說明一下,單個(gè)pthread_cond_wait功能很完善,為何
//這里要有一個(gè)while (head == NULL)呢?因?yàn)閜thread_cond_wait里的線
//程可能會(huì)被意外喚醒,如果這個(gè)時(shí)候head != NULL,則不是我們想要的情況。
//這個(gè)時(shí)候,應(yīng)該讓線程繼續(xù)進(jìn)入pthread_cond_wait
// pthread_cond_wait會(huì)先解除之前的pthread_mutex_lock鎖定的mtx,
//然后阻塞在等待對(duì)列里休眠,直到再次被喚醒(大多數(shù)情況下是等待的條件成立
//而被喚醒,喚醒后,該進(jìn)程會(huì)先鎖定先pthread_mutex_lock(&mtx);,再讀取資源
//用這個(gè)流程是比較清楚的/*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;
//子線程會(huì)一直等待資源,類似生產(chǎn)者和消費(fèi)者,但是這里的消費(fèi)者可以是多個(gè)消費(fèi)者,而
//不僅僅支持普通的單個(gè)消費(fèi)者,這個(gè)模型雖然簡(jiǎn)單,但是很強(qiáng)大
pthread_create(&tid, NULL, thread_func, NULL);
sleep(1);
for (i = 0; i n_number = i;
pthread_mutex_lock(&mtx); //需要操作head這個(gè)臨界資源,先加鎖,
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”);
//關(guān)于pthread_cancel,有一點(diǎn)額外的說明,它是從外部終止子線程,子線程會(huì)在最近的取消點(diǎn),退出
//線程,而在我們的代碼里,最近的取消點(diǎn)肯定就是pthread_cond_wait()了。
pthread_cancel(tid);
pthread_join(tid, NULL);
printf(“All done — exiting/n”);
return 0;
}
3)信號(hào)量
如同進(jìn)程一樣,線程也可以通過信號(hào)量來實(shí)現(xiàn)通信,雖然是輕量級(jí)的。
信號(hào)量函數(shù)的名字都以”sem_”打頭。線程使用的基本信號(hào)量函數(shù)有四個(gè)。
#include
int sem_init (sem_t *sem , int pshared, unsigned int value);
這是對(duì)由sem指定的信號(hào)量進(jìn)行初始化,設(shè)置好它的共享選項(xiàng)(linux 只支持為0,即表示它是當(dāng)前進(jìn)程的局部信號(hào)量),然后給它一個(gè)初始值VALUE。
兩個(gè)原子操作函數(shù):
int sem_wait(sem_t *sem);
int sem_post(sem_t *sem);
這兩個(gè)函數(shù)都要用一個(gè)由sem_init調(diào)用初始化的信號(hào)量對(duì)象的指針做參數(shù)。
sem_post:給信號(hào)量的值加1;
sem_wait:給信號(hào)量減1;對(duì)一個(gè)值為0的信號(hào)量調(diào)用sem_wait,這個(gè)函數(shù)將會(huì)等待直到有其它線程使它不再是0為止。
int sem_destroy(sem_t *sem);
這個(gè)函數(shù)的作用是再我們用完信號(hào)量后都它進(jìn)行清理。歸還自己占有的一切資源。
示例代碼:
#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í)行結(jié)果后,可以看出,會(huì)先執(zhí)行線程二的函數(shù),然后再執(zhí)行線程一的函數(shù)。它們兩就實(shí)現(xiàn)了同步
創(chuàng)新互聯(lián)-老牌IDC、云計(jì)算及IT信息化服務(wù)領(lǐng)域的服務(wù)供應(yīng)商,業(yè)務(wù)涵蓋IDC(互聯(lián)網(wǎng)數(shù)據(jù)中心)服務(wù)、云計(jì)算服務(wù)、IT信息化、AI算力租賃平臺(tái)(智算云),軟件開發(fā),網(wǎng)站建設(shè),咨詢熱線:028-86922220
網(wǎng)頁(yè)名稱:深入探究LinuxSemaphore函數(shù)(linuxsemaphore函數(shù))
當(dāng)前地址:http://m.5511xx.com/article/ccddegc.html


咨詢
建站咨詢
