當前位置:首頁 » 操作系統 » linux加鎖

linux加鎖

發布時間: 2022-07-11 01:02:46

linux線程同步的互斥鎖(mutex)到底怎麼用的》謝謝

互斥鎖(mutex) 通過鎖機制實現線程間的同步。

1、初始化鎖。在Linux下,線程的互斥量數據類型是pthread_mutex_t。在使用前,要對它進行初始化。

2、靜態分配:pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;

3、動態分配:int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutex_attr_t *mutexattr);

4、加鎖。對共享資源的訪問,要對互斥量進行加鎖,如果互斥量已經上了鎖,調用線程會阻塞,直到互斥量被解鎖。

intpthread_mutex_lock(pthread_mutex*mutex);
intpthread_mutex_trylock(pthread_mutex_t*mutex);
解鎖。在完成了對共享資源的訪問後,要對互斥量進行解鎖。
intpthread_mutex_unlock(pthread_mutex_t*mutex);
銷毀鎖。鎖在是使用完成後,需要進行銷毀以釋放資源。
intpthread_mutex_destroy(pthread_mutex*mutex);
#include<cstdio>
#include<cstdlib>
#include<unistd.h>
#include<pthread.h>
#include"iostream"
usingnamespacestd;
pthread_mutex_tmutex=PTHREAD_MUTEX_INITIALIZER;
inttmp;
void*thread(void*arg)
{
cout<<"threadidis"<<pthread_self()<<endl;
pthread_mutex_lock(&mutex);
tmp=12;
cout<<"Nowais"<<tmp<<endl;
pthread_mutex_unlock(&mutex);
returnNULL;
}
intmain()
{
pthread_tid;
cout<<"mainthreadidis"<<pthread_self()<<endl;
tmp=3;
cout<<"Inmainfunctmp="<<tmp<<endl;
if(!pthread_create(&id,NULL,thread,NULL))
{
cout<<"Createthreadsuccess!"<<endl;
}
else
{
cout<<"Createthreadfailed!"<<endl;
}
pthread_join(id,NULL);
pthread_mutex_destroy(&mutex);
return0;
}
//編譯:g++-othreadtestthread.cpp-lpthread

⑵ linux里的文件夾上有個鎖頭的圖標,表示什麼

Linux中帶鎖的文件夾圖標表示沒有許可權訪問該文件夾。取消linux文件上面的小鎖頭方法:


1、首先點擊任務欄上面的≥,或者是在桌面空白處不要點擊任何軟體。


⑶ linux中,"加鎖/解鎖"處理追加後對線程a和b執行過程有什麼改變

如果a和b使用同一個鎖,那麼加鎖解鎖過程會使線程a和線程b在共同操作的數據上保持互斥性,即每次只有一個線程對共同的數據進行操作;如果不使用同一個鎖,那就沒什麼關系

⑷ linux怎麼把文件同時進行讀寫鎖

讀寫鎖與互斥量類似,不過讀寫鎖的並行性更高。
讀寫鎖可以有三種狀態:(1)讀模式加鎖;(2)寫模式加鎖;(3)不加鎖。
在寫加鎖狀態時,在解鎖之前,所有試圖對這個鎖加鎖的線程都會被阻塞。在讀加鎖狀態時,所有試圖以讀模式對它進行加鎖的線程都可以得到訪問許可權。但是如果線程希望以寫模式加鎖,它必須阻塞,直至所有的線程釋放讀鎖。
讀寫鎖很適合於對數據結構讀的次數遠大於寫的情況。

相關函數:
int pthread_rwlock_init(pthread_rwlock_t *restrict rwlock, const pthread_rwlockattr_t *restrict attr);
int pthread_rwlock_destroy(pthread_rwlock_t *rwlock) // 成功則返回0,失敗則返回錯誤代碼
int pthread_rwlock_rdlock(pthread_rwlock_t *restrict rwlock) ;//讀模式加鎖
int pthread_rwlock_wrlock(pthread_rwlock_t *restrict rwlock);//寫模式加鎖
int pthread_rwlock_unlock(pthread_rwlock_t *restrick rwlock);
int pthread_rwlock_tryrdlock(pthread_rwlock_t *restrict rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *restrict rwlock);
int pthread_rwlock_trywrlock(pthread_rwlock_t *restrict rwlock);
相關示例:讀者寫者問題,這也是一個很經典的多線程題目,題目大意:有一個寫者多個讀者,多個讀者可以同時讀文件,但寫者在寫文件時不允許有讀者在讀取文件,同樣有讀者讀文件時
#include <stdio.h>
#include <pthread.h>

#define Read_Num 2

pthread_rwlock_t lock;

class Data
{
public:
Data(int i, float f): I(i),F(f)
{}
private:
int I;
float F;

};

Data *pdata = NULL;

void *read(void * arg)
{
int id = (int)arg;
while(true)
{

pthread_rwlock_rdlock(&lock);
printf(" reader %d is reading data!\n", id);
if(data == NULL)
{
printf("data is NULL\n");
}
else
{
printf("data: I = %d, F = %f \n", pdata->I, pdata->F);
}
pthread_rwlock_unlock(&lock);
}

pthread_exit(0);

}

void *write()
{
while(true)
{
pthread_rwlock_wrlock(&lock);
printf(" writer is writind data!\n");
if(pdata == NULL)
{
pdata = new Data(1, 1.1);
printf("Writer is writing data: %d, %f\n", pdata->I, pdata->F);
}
else
{
delete pdata;
pdata = NULL;
printf("writer free the data!");
}

pthread_rwlock_unlock(&lock);
}
pthread_exit(0);
}

void main()
{
pthread_t reader[Read_Num];
pthread_t writer;

for(int i = 0;i<Read_Num;i++)
{
pthread_create(&read[i],NULL,read,(void *)i);
}

pthread_create(writer, NULL, write, NULL);

sleep(1);
return 0;
}

⑸ linux下SVN怎樣對文件夾加鎖

可以考慮這樣的方法:
一、
其實SVN加鎖會在.SVN(
隱藏文件
)中生成一個名字叫lock的文件(無後綴),查找所有的,手工刪除。然後再嘗試更新,系統可能會提示某個.base文件無法訪問。找到它,把相關的文件或其所在的目錄刪除

⑹ linux:加鎖與加密的有什麼區別或者聯系

我的理解是:
比如一張紙上寫了123456789這些數字,是機密內容,別人想得到。加鎖就是把那張紙放到保險箱里,加密就是把這些數據按一定的演算法轉換成別的如這張紙上的內容轉換成751236948,別人拿到了也沒什麼用。然後把紙看成是磁碟。

⑺ linux 定時任務,如何加鎖

好像沒有加鎖的功能,但是你可以在你的腳本中寫一個判斷,如果有相同進程,就不執行

⑻ linux shell中的加鎖

flock的選項-n等同於--nonblock,表示非阻塞模式。man flock可以看到詳細說明:
Fail (with an exit code of 1) rather than wait if the lock cannot be immediately acquired.
翻譯過來就是,若不能立即獲得文件鎖,就直接出錯返回1而不是繼續等待(默認flock是會繼續等待直到獲得文件鎖)。

3就是fd(即文件描述符)。

文件鎖只是用於防止多個程序同時訪問一個文件的沖突情況。
Shell自動分配fd的話,原本就不會使用已被佔用的fd,而是自動遞增。
這里顯式指定fd為3(3<>mylockfile),是為了flock有操作的對象。只有unlock後文件才能被其他程序訪問,這樣就可以保證文件訪問不沖突。

⑼ 為什麼在linux上對某個文件加鎖還是可以rm掉

因為加鎖只是防止修改文件,並不能防止刪除文件。

熱點內容
linux下ntp伺服器搭建 發布:2024-09-08 08:26:46 瀏覽:742
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:171
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:778
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:100
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:208
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995
加上www不能訪問 發布:2024-09-08 06:39:52 瀏覽:811
銀行支付密碼器怎麼用 發布:2024-09-08 06:39:52 瀏覽:513
蘋果手機清理瀏覽器緩存怎麼清理緩存 發布:2024-09-08 06:31:32 瀏覽:554