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

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


咨詢
建站咨詢
