新聞中心
在Linux系統(tǒng)中,如果多個(gè)進(jìn)程同時(shí)訪問(wèn)共享資源,就有可能出現(xiàn)競(jìng)爭(zhēng)條件,導(dǎo)致程序崩潰、死鎖等問(wèn)題。為了解決這個(gè)問(wèn)題,Linux內(nèi)核提供了進(jìn)程間鎖機(jī)制,可以保證程序的穩(wěn)定運(yùn)行。

從策劃到設(shè)計(jì)制作,每一步都追求做到細(xì)膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶提供成都網(wǎng)站建設(shè)、成都做網(wǎng)站、網(wǎng)站策劃、網(wǎng)頁(yè)設(shè)計(jì)、域名申請(qǐng)、雅安服務(wù)器托管、網(wǎng)絡(luò)營(yíng)銷、VI設(shè)計(jì)、 網(wǎng)站改版、漏洞修補(bǔ)等服務(wù)。為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造優(yōu)易品牌,攜手廣大客戶,共同發(fā)展進(jìn)步。
一、什么是進(jìn)程間鎖
進(jìn)程間鎖是Linux操作系統(tǒng)提供的多進(jìn)程通信機(jī)制之一,它是一種鎖機(jī)制,通過(guò)鎖來(lái)控制進(jìn)程對(duì)共享資源的訪問(wèn)。進(jìn)程間鎖有兩種:互斥鎖和讀寫(xiě)鎖。
互斥鎖:是最常見(jiàn)的進(jìn)程間鎖,保證在同一時(shí)刻只有一個(gè)進(jìn)程能夠訪問(wèn)共享資源。如果一個(gè)進(jìn)程在使用共享資源時(shí)占用了鎖,那么其他進(jìn)程就必須等待鎖的釋放才能訪問(wèn)共享資源。
讀寫(xiě)鎖:是一種特殊的互斥鎖,用于控制讀寫(xiě)操作的并發(fā)性??梢詫⒁粋€(gè)共享資源分為讀取和寫(xiě)入兩個(gè)階段,多個(gè)進(jìn)程可以同時(shí)讀取該共享資源,但只有一個(gè)進(jìn)程能夠進(jìn)行寫(xiě)入操作。
進(jìn)程間鎖一般是在內(nèi)核空間實(shí)現(xiàn)的,可以通過(guò)系統(tǒng)調(diào)用函數(shù)進(jìn)行調(diào)用。Linux內(nèi)核提供了下面這些函數(shù)用于操作進(jìn)程間鎖:
1. pthread_mutex_init 初始化互斥鎖
2. pthread_mutex_lock 加鎖
3. pthread_mutex_trylock 嘗試加鎖
4. pthread_mutex_unlock 解鎖
5. pthread_rwlock_init 初始化讀寫(xiě)鎖
6. pthread_rwlock_rdlock 加讀鎖
7. pthread_rwlock_wrlock 加寫(xiě)鎖
8. pthread_rwlock_unlock 解鎖
9. pthread_mutex_destroy 銷毀互斥鎖
10. pthread_rwlock_destroy 銷毀讀寫(xiě)鎖
二、進(jìn)程間鎖的作用
進(jìn)程間鎖可以解決多個(gè)進(jìn)程同時(shí)訪問(wèn)共享資源的問(wèn)題,保證程序的正常運(yùn)行。假如沒(méi)有進(jìn)程間鎖機(jī)制,如果多個(gè)進(jìn)程同時(shí)訪問(wèn)同一塊共享資源,就可能出現(xiàn)競(jìng)爭(zhēng)條件,導(dǎo)致數(shù)據(jù)的不一致或者程序的死鎖。有了進(jìn)程間鎖,就可以確保只有一個(gè)進(jìn)程能夠訪問(wèn)共享資源,避免了這些潛在的問(wèn)題,保證了程序的穩(wěn)定性。
進(jìn)程間鎖可以應(yīng)用于多種情況,例如:
1、多個(gè)進(jìn)程同時(shí)讀取或?qū)懭胍粋€(gè)文件時(shí),需要使用讀寫(xiě)鎖機(jī)制。
2、多個(gè)進(jìn)程同時(shí)對(duì)一塊共享內(nèi)存進(jìn)行讀寫(xiě)時(shí),需要使用互斥鎖機(jī)制。
3、多個(gè)進(jìn)程同時(shí)使用網(wǎng)絡(luò)服務(wù)、數(shù)據(jù)庫(kù)等公共服務(wù)時(shí),需要使用互斥鎖或讀寫(xiě)鎖機(jī)制。
三、進(jìn)程間鎖的注意事項(xiàng)
1. 鎖的范圍:鎖的粒度必須控制在最小范圍內(nèi),避免一段不必要的代碼也被鎖住,從而影響程序的性能。
2. 鎖的粒度:讀寫(xiě)鎖適合讀操作比較多的情況,互斥鎖適合寫(xiě)操作比較頻繁的情況。
3. 鎖的調(diào)用次數(shù):鎖的調(diào)用次數(shù)越少,程序的效率就越高。
4. 死鎖:死鎖是指多個(gè)進(jìn)程之間相互等待對(duì)方的資源卻無(wú)法釋放自己的資源,從而導(dǎo)致程序崩潰。為了避免死鎖,需要規(guī)劃好進(jìn)程之間的資源請(qǐng)求順序,盡量避免循環(huán)依賴的情況。
五、
進(jìn)程間鎖是保證程序穩(wěn)定運(yùn)行的重要機(jī)制之一,它能夠在多個(gè)進(jìn)程同時(shí)訪問(wèn)同一共享資源時(shí)起到控制的作用,保證多個(gè)進(jìn)程能夠有序地訪問(wèn)共享資源,避免了數(shù)據(jù)的不一致和程序崩潰的問(wèn)題。在使用進(jìn)程間鎖時(shí),需要注意鎖的范圍和粒度,減少其調(diào)用次數(shù),避免死鎖的發(fā)生。
相關(guān)問(wèn)題拓展閱讀:
- Linux系統(tǒng)內(nèi)核首次加入鎖定功能
- Linux多進(jìn)程和線程同步的幾種方式
Linux系統(tǒng)內(nèi)核首次加入鎖定功能
Linux之父林納斯·托瓦茲(Linus Torvalds)上周六宣布在新版Linux系統(tǒng)內(nèi)晌亂核中首滾謹(jǐn)明次加入鎖定功能。
這項(xiàng)名為“l(fā)ockdown”的Linux內(nèi)核新安全功能將作為L(zhǎng)(Linux安全模塊)出現(xiàn)在即將發(fā)布的Linux 5.4版本當(dāng)中。
該功能默認(rèn)情況下處于關(guān)閉狀態(tài),由于存在破壞現(xiàn)有系統(tǒng)的風(fēng)險(xiǎn),因此用戶可選使用。這項(xiàng)新功能的主要目的是通過(guò)防止root帳戶與內(nèi)核代碼進(jìn)行交互來(lái)加強(qiáng)用戶態(tài)進(jìn)程與內(nèi)核代碼之間的鴻溝。
啟用后,新的“鎖定”功能將限制Linux某些內(nèi)核功能,即使對(duì)于root用戶也大告是如此,這使得受到破壞的root帳戶更難于破壞其余的系統(tǒng)內(nèi)核。
托瓦茲表示:“啟用后,各種內(nèi)核功能都受到限制。 ” 這包括限制對(duì)內(nèi)核功能的訪問(wèn),這些功能可能允許通過(guò)用戶級(jí)進(jìn)程提供的代碼執(zhí)行任意代碼;阻止進(jìn)程寫(xiě)入或讀取/ dev / mem和/ dev / kmem內(nèi)存;阻止對(duì)打開(kāi)/ dev / port的訪問(wèn),以防止原始端口訪問(wèn);加強(qiáng)內(nèi)核模塊簽名等。
Linux是一種自由和開(kāi)放源碼的類UNIX 操作系統(tǒng)。該操作系統(tǒng)的內(nèi)核由林納斯·托瓦茲在1991年10月5日首次發(fā)布。在加上用戶空間的應(yīng)用程序之后,成為 Linux 操作系統(tǒng)。Linux也是最著名的自由軟件和開(kāi)放源代碼軟件。只要遵循GNU 通用公共許可證(GPL),任何個(gè)人和機(jī)構(gòu)都可以自由地使用Linux 的所有底層源代碼,也可以自由地修改和再發(fā)布。
Linux多進(jìn)程和線程同步的幾種方式
Linux 線程同步的橘螞笑三種方法
線程的更大特點(diǎn)是資源的共享性,但資源共享中的同步問(wèn)題是多線程編程的難點(diǎn)。linux下提供了多種方式來(lái)處理線程同步,最常用的是互斥鎖、條件變量和信號(hào)量。
一、互斥鎖(mutex)
通過(guò)鎖機(jī)制實(shí)現(xiàn)線程間的同步。
初始化鎖。在Linux下,線程的互斥量數(shù)據(jù)類型是pthread_mutex_t。在使用前,要對(duì)它進(jìn)行初始化。
靜態(tài)分配:pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
動(dòng)態(tài)分配:int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutex_attr_t *mutexattr);
加鎖。對(duì)共享資源的訪問(wèn),要對(duì)互斥量進(jìn)行加鎖,如果互斥量已經(jīng)上了鎖,調(diào)用線程會(huì)阻塞,直到互斥量被解鎖。
int pthread_mutex_lock(pthread_mutex *mutex);
int pthread_mutex_trylock(pthread_mutex_t *mutex);
解鎖。在完成了對(duì)共享資源的訪問(wèn)后,要對(duì)互斥量進(jìn)行解鎖。
int pthread_mutex_unlock(pthread_mutex_t *mutex);
銷毀鎖。鎖在是使用完成后,需要進(jìn)行銷毀以釋放資源。
int pthread_mutex_destroy(pthread_mutex *mutex);
view plain copy
#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;
}
view plain copy
#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主要是用來(lái)保證pthread_cond_wait的并發(fā)性
pthread_mutex_lock(&mtx);
while (head == NULL)
{
//這個(gè)while要特別說(shuō)明一下,單個(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è)流程是比較清楚的
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)額外的說(shuō)明,它是從外部終止子線程,子線程會(huì)在最近的取消點(diǎn),退出
//線程,而在我們的代碼里,最近的取消點(diǎn)肯定就是pthread_cond_wait()了。
pthread_cancel(tid);
pthread_join(tid, NULL);
printf(“All done — exiting/n”);
return 0;
}
三、信號(hào)量(sem)
如同進(jìn)程一樣,線程也可以通過(guò)信號(hào)量來(lái)實(shí)現(xiàn)通信,雖然是輕量級(jí)的。信號(hào)量函數(shù)的名字都以”sem_”打頭。線程使用的基本信號(hào)量函數(shù)有四個(gè)。
信號(hào)量初始化。
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。
等待信號(hào)量。給信號(hào)量減1,然后等待直到信號(hào)量的值大于0。
int sem_wait(sem_t *sem);
釋放信號(hào)量。信號(hào)量值加1。并通知其他等待線程。
int sem_post(sem_t *sem);
銷毀信號(hào)量。我們用完信號(hào)量后都它進(jìn)行清理。歸還占有的一切資源。
int sem_destroy(sem_t *sem);
view plain copy
#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;
}
成都網(wǎng)站設(shè)計(jì)制作選創(chuàng)新互聯(lián),專業(yè)網(wǎng)站建設(shè)公司。
成都創(chuàng)新互聯(lián)10余年專注成都高端網(wǎng)站建設(shè)定制開(kāi)發(fā)服務(wù),為客戶提供專業(yè)的成都網(wǎng)站制作,成都網(wǎng)頁(yè)設(shè)計(jì),成都網(wǎng)站設(shè)計(jì)服務(wù);成都創(chuàng)新互聯(lián)服務(wù)內(nèi)容包含成都網(wǎng)站建設(shè),小程序開(kāi)發(fā),營(yíng)銷網(wǎng)站建設(shè),網(wǎng)站改版,服務(wù)器托管租用等互聯(lián)網(wǎng)服務(wù)。
網(wǎng)頁(yè)標(biāo)題:Linux進(jìn)程間鎖:保證程序穩(wěn)定運(yùn)行的重要機(jī)制 (進(jìn)程間鎖 linux)
本文URL:http://m.5511xx.com/article/dpjcgps.html


咨詢
建站咨詢
