




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、進(jìn)程通信之讀寫鎖讀寫鎖讀寫鎖的分配規(guī)則 1. 沒有線程持有讀寫鎖進(jìn)行寫,任意數(shù)量的線程可以持有該讀寫鎖用于讀 2. 只有沒有線程持有給定的讀寫鎖用于讀或者寫的時(shí)候,才能分配讀寫鎖用于寫。 如果修改數(shù)據(jù)頻繁,那么可以考慮用讀寫鎖替代互斥鎖。獲取與釋放如果對(duì)應(yīng)的讀寫鎖已由某個(gè)寫入者持有,那么阻塞pthread_rwlock_rdlock獲取讀出鎖如果對(duì)應(yīng)的讀寫鎖已由另一個(gè)寫入者持有,那就阻塞pthread_rwlock_wrlock獲取寫入鎖。pthread_rwlock_unlock用于釋放讀出鎖或者寫入鎖。 三者成功時(shí)返回0,出錯(cuò)時(shí)返回正的錯(cuò)誤值pthread_rwlock_tryrdlock
2、(pthread _rwlock_t *rwptr)嘗試獲取讀出鎖,如果不能馬上獲得,返回EBUSY錯(cuò)誤。pthread_rwlock_trywrlock(pthread _rwlock_t *rwptr)嘗試獲取寫入鎖,如果不能馬上獲得,返回EBUSY錯(cuò)誤。初始化和摧毀動(dòng)態(tài)初始化 int pthread_rwlock_init(pthread_rwlock_t *rwptr, const pthread_rwlockattr_t *attr)靜態(tài)初始化 PTHREAD_RWLOCK_INITIALIZER摧毀 int pthread_rwlock_destroy(pthread_rwlock
3、_t *rwptr)屬性設(shè)置int pthread_rwlockattr_init(pthread_rwlockattr_t * attr)int pthread_rwlockattr_destroy(pthread_rwlockattr_t *attr)int pthread_rwlockattr_getpshared(const pthread_rwlockattr_t * attr,int *valptr)int pthread_rwlockattr_setpshared(pthread_rwlockattr_t * attr,int val)讀寫鎖很適合讀的次數(shù)遠(yuǎn)大于寫的次數(shù)的情況。
4、例子: 一個(gè)寫者,多個(gè)讀者。用讀寫鎖進(jìn)行協(xié)調(diào)工作。 code:#include <pthread.h>#include <stdio.h>#include <stdlib.h>#include <string.h>struct data int number; char info105;struct data *pdata = NULL; pthread_rwlock_t lock;void *read(void *arg) int id = *(int *)arg); while(1) int ret; ret = pthread_rwlock
5、_tryrdlock(&lock); if(ret != 0) continue; printf("reader %d is reading!n",id); if(pdata = NULL) printf("data is null.n"); else printf("data: number is %d, info are %s.n",pdata->number,pdata->info); pthread_rwlock_unlock(&lock); pthread_exit(0);void *write(
6、void *arg) int id = *(int *)arg); while(1) int ret; ret = pthread_rwlock_trywrlock(&lock); if(ret !=0) continue; printf("writer %d is writing!n",id); if(pdata = NULL) pdata = (struct data *)malloc(sizeof(struct data); pdata->number = 1; strcpy(pdata->info,"I love linux."
7、;); printf("finish, wrote it.n"); else printf("the pdata is not null.n"); pthread_rwlock_unlock(&lock); pthread_exit(0);int main() pthread_t reader10; pthread_t writer; int i; pthread_create(&writer,NULL,write,(void *)&i); for(i=0; i<10; i+) pthread_create(&rea
8、deri,NULL,read,(void *)&i); /pthread_create(&writer,NULL,write,NULL); if(pdata != NULL) free(pdata); getchar(); return 0;運(yùn)行:writer 1 is writing!finish, wrote it.reader 2 is reading!data: number is 1, info are I love linux.writer 1 is writing!the pdata is not null.reader 2 is reading!data: nu
9、mber is 1, info are I love linux.reader 2 is reading!data: number is 1, info are I love linux.線程取消函數(shù): int pthread_cancel(pthread_t tid) 一個(gè)線程可以被同一進(jìn)程中的其他線程取消。 與之對(duì)應(yīng)的自愿終止: void pthread_exit(void *retval)#include <stdio.h>#include <unistd.h>#include "unix98.h"#include <pthread.h&
10、gt;pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;pthread_t tid1, tid2;void *thread1(void *), *thread2(void *);int main() pthread_setconcurrency(2); pthread_create(&tid1,NULL,thread1,NULL); sleep(1); /* let tid2 get lock. */ pthread_create(&tid2,NULL,thread2,NULL); void *status; /* sto
11、re returned value from waited tid */ pthread_join(tid2,&status); if(status != PTHREAD_CANCELED) printf("thread2 status = %p n", status); pthread_join(tid1, &status); if(status != NULL) printf("thread1 status = %pn",status); /printf("rw_refcount = %d, rw_nwaitreaders
12、= %d, rw_nwaitwriter = %dn", / rwlock.rw_refcount,rwlock.rw_nwaitreaders, rwlock.rw_nwaitwriters); pthread_rwlock_troy(&rwlock); return 0;void *thread1(void *arg) pthread_rwlock_rdlock(&rwlock); printf("thread1 got a read lock.n"); sleep(3); /* let thread2 block in pthread_rwl
13、ock_wrlock */ pthread_cancel(tid2); sleep(3); pthread_rwlock_unlock(&rwlock); return NULL;void *thread2(void *arg) printf("thread2 try to obtain a write lock.n"); pthread_rwlock_wrlock(&rwlock); printf("thread2() get a write lock.n"); sleep(1); pthread_rwlock_unlock(&
14、rwlock); return NULL;./pthread_cancel thread1 got a read lock.thread2 try to obtain a write lock.thread2() get a write lock.1234fcntl函數(shù)和讀寫鎖文件鎖:建議性鎖,強(qiáng)制性鎖 lock結(jié)構(gòu)體:Struct flockshort l_type;off_t l_start;short l_whence;off_t l_len;pid_t l_pid;flock可施加建議性鎖,fcntl既可以施加建議性鎖,也可施加強(qiáng)制性鎖。 fcntl也可以對(duì)某一記錄進(jìn)行上鎖,也就是所謂
15、的記錄鎖。記錄鎖分為讀取鎖(共享鎖),寫入鎖(排斥鎖,互斥鎖)。 fcntl建立記錄鎖: 下面是一個(gè)學(xué)習(xí)的例子,參考 使用fcntl增加和釋放鎖:#include <unistd.h>#include <sys/types.h>#include <fcntl.h>#include <stdio.h>#include <sys/stat.h>#include <stdlib.h>void lock_set(int fd,int type) struct flock lock; lock.l_whence = SEEK_SET
16、; /* 相對(duì)位移量的起點(diǎn)是文件的開頭 */ lock.l_start = 0; /* 相對(duì)位移量 */ lock.l_len = 0; /* 加鎖區(qū)域的長(zhǎng)度 */ while(1) lock.l_type=type; if(fcntl(fd,F_SETLK,&lock) = 0) if(lock.l_type = F_RDLCK) printf("read lock by %dn", getpid(); else if(lock.l_type = F_WRLCK) printf("write lock by %dn", getpid(); el
17、se if(lock.l_type = F_UNLCK) printf("release lock by %dn", getpid(); return ; fcntl(fd,F_GETLK,&lock); if(lock.l_type != F_UNLCK) if(lock.l_type = F_RDLCK) printf("read lock already set by %dn",lock.l_pid); else if(lock.l_type = F_WRLCK) printf("write lock already set by
18、 %dn",lock.l_pid); printf("please enter any key to continue: "); getchar(); int main() int fd = open("path",O_RDWR | O_CREAT, 0755); /* 當(dāng)前路徑下創(chuàng)建path */ if(fd = -1) perror("open: "); exit(1); lock_set(fd,F_WRLCK); puts("wait for a secend.n"); sleep(6); lock
19、_set(fd,F_UNLCK); puts("wait for a secend.n"); sleep(1); close(fd); exit(0);不修改上面的代碼,試試寫入鎖 + 寫入鎖, 看看效果。 在不同的標(biāo)簽內(nèi)運(yùn)行此程序:write + write./writewrite lock by 2904wait for a secend.release lock by 2904wait for a secend./writewrite lock already set by 2904please enter any key to continue: write loc
20、k already set by 2904please enter any key to continue: write lock by 2905wait for a secend.release lock by 2905wait for a secend.在已有write鎖的情況下不能加上write鎖。 現(xiàn)在,稍作修改lock_set(fd,F_WRLCK);->lock_set(fd,F_RDLCK);write + read./write write lock by 3005wait for a secend.release lock by 3005wait for a secend./read write lock already set by 3005please enter any key to continue: write lock already set by 3005please enter any key to continue: write lock already set by 3005please enter any key to continue: read lock by 3006wait for a secend.release lock by 300
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 工作務(wù)實(shí)活動(dòng)方案
- 小學(xué)閱讀評(píng)比活動(dòng)方案
- 山西開學(xué)第一課活動(dòng)方案
- 工地宣講活動(dòng)方案
- 少兒戶外詩歌會(huì)活動(dòng)方案
- 展廳游戲活動(dòng)方案
- 工會(huì)國(guó)慶朗誦活動(dòng)方案
- 小店電信活動(dòng)方案
- 少先隊(duì)義賣活動(dòng)方案
- 小學(xué)課程育人活動(dòng)方案
- MAM6090空壓 機(jī)微電腦控制器說明書
- 北師大版八年級(jí)數(shù)學(xué)下冊(cè)??碱}專練專題18平行四邊形中的周長(zhǎng)和面積問題(原卷版+解析)
- 探監(jiān)證明樣本范文(篇一)
- 2023-2024學(xué)年年北京市懷柔區(qū)八年級(jí)初二(下)期末道德與法治試卷(含答案)
- 山東省濟(jì)寧市曲阜市2023-2024學(xué)年七年級(jí)下學(xué)期期末數(shù)學(xué)試題
- 2024年廣西中考地理+生物試題
- 瑜伽教練聘用勞動(dòng)合同
- 校本課題研究活動(dòng)記錄
- 體育指導(dǎo)員協(xié)會(huì)籌備
- 馬克思主義基本原理-2023版-課后習(xí)題答案
- MOOC 光纖通信-南京郵電大學(xué) 中國(guó)大學(xué)慕課答案
評(píng)論
0/150
提交評(píng)論