進程通信之讀寫鎖_第1頁
進程通信之讀寫鎖_第2頁
進程通信之讀寫鎖_第3頁
進程通信之讀寫鎖_第4頁
進程通信之讀寫鎖_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、進程通信之讀寫鎖讀寫鎖讀寫鎖的分配規(guī)則 1. 沒有線程持有讀寫鎖進行寫,任意數(shù)量的線程可以持有該讀寫鎖用于讀 2. 只有沒有線程持有給定的讀寫鎖用于讀或者寫的時候,才能分配讀寫鎖用于寫。 如果修改數(shù)據(jù)頻繁,那么可以考慮用讀寫鎖替代互斥鎖。獲取與釋放如果對應(yīng)的讀寫鎖已由某個寫入者持有,那么阻塞pthread_rwlock_rdlock獲取讀出鎖如果對應(yīng)的讀寫鎖已由另一個寫入者持有,那就阻塞pthread_rwlock_wrlock獲取寫入鎖。pthread_rwlock_unlock用于釋放讀出鎖或者寫入鎖。 三者成功時返回0,出錯時返回正的錯誤值pthread_rwlock_tryrdlock

2、(pthread _rwlock_t *rwptr)嘗試獲取讀出鎖,如果不能馬上獲得,返回EBUSY錯誤。pthread_rwlock_trywrlock(pthread _rwlock_t *rwptr)嘗試獲取寫入鎖,如果不能馬上獲得,返回EBUSY錯誤。初始化和摧毀動態(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ù)遠大于寫的次數(shù)的情況。

4、例子: 一個寫者,多個讀者。用讀寫鎖進行協(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;運行: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) 一個線程可以被同一進程中的其他線程取消。 與之對應(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ù)和讀寫鎖文件鎖:建議性鎖,強制性鎖 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既可以施加建議性鎖,也可施加強制性鎖。 fcntl也可以對某一記錄進行上鎖,也就是所謂

15、的記錄鎖。記錄鎖分為讀取鎖(共享鎖),寫入鎖(排斥鎖,互斥鎖)。 fcntl建立記錄鎖: 下面是一個學(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、; /* 相對位移量的起點是文件的開頭 */ lock.l_start = 0; /* 相對位移量 */ lock.l_len = 0; /* 加鎖區(qū)域的長度 */ 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);不修改上面的代碼,試試寫入鎖 + 寫入鎖, 看看效果。 在不同的標簽內(nèi)運行此程序: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等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論