




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、进程通信之读写锁读写锁读写锁的分配规则 1. 没有线程持有读写锁进行写,任意数量的线程可以持有该读写锁用于读 2. 只有没有线程持有给定的读写锁用于读或者写的时候,才能分配读写锁用于写。 如果修改数据频繁,那么可以考虑用读写锁替代互斥锁。获取与释放如果对应的读写锁已由某个写入者持有,那么阻塞pthread_rwlock_rdlock获取读出锁如果对应的读写锁已由另一个写入者持有,那就阻塞pthread_rwlock_wrlock获取写入锁。pthread_rwlock_unlock用于释放读出锁或者写入锁。 三者成功时返回0,出错时返回正的错误值pthread_rwlock_tryrdlock
2、(pthread _rwlock_t *rwptr)尝试获取读出锁,如果不能马上获得,返回EBUSY错误。pthread_rwlock_trywrlock(pthread _rwlock_t *rwptr)尝试获取写入锁,如果不能马上获得,返回EBUSY错误。初始化和摧毁动态初始化 int pthread_rwlock_init(pthread_rwlock_t *rwptr, const pthread_rwlockattr_t *attr)静态初始化 PTHREAD_RWLOCK_INITIALIZER摧毁 int pthread_rwlock_destroy(pthread_rwlock
3、_t *rwptr)属性设置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)读写锁很适合读的次数远大于写的次数的情况。
4、例子: 一个写者,多个读者。用读写锁进行协调工作。 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.线程取消函数: int pthread_cancel(pthread_t tid) 一个线程可以被同一进程中的其他线程取消。 与之对应的自愿终止: 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函数和读写锁文件锁:建议性锁,强制性锁 lock结构体:Struct flockshort l_type;off_t l_start;short l_whence;off_t l_len;pid_t l_pid;flock可施加建议性锁,fcntl既可以施加建议性锁,也可施加强制性锁。 fcntl也可以对某一记录进行上锁,也就是所谓
15、的记录锁。记录锁分为读取锁(共享锁),写入锁(排斥锁,互斥锁)。 fcntl建立记录锁: 下面是一个学习的例子,参考 使用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; /* 加锁区域的长度 */ 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); /* 当前路径下创建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);不修改上面的代码,试试写入锁 + 写入锁, 看看效果。 在不同的标签内运行此程序: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锁。 现在,稍作修改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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 小学生课件教程
- 印花机挡车工上岗证考试题库及答案
- 特种同位素分离工理论学习手册练习试题及答案
- 数码印花挡车工上岗证考试题库及答案
- 育婴员岗位实习报告
- 铁路行包运输服务员(行李计划员)上岗证考试题库及答案
- 起重机械装配调试工理论学习手册练习试题及答案
- 结构件制造工岗位实习报告
- 超声波检测工安全技术操作规程
- 电线电缆金属导体剂制工理论学习手册练习试题及答案
- 辽宁省鞍山市2024-2025学年八年级下学期期末质量检测语文试卷(含答案)
- 2025年老年教育课程设计:跨学科合作教学法的探索与成效报告
- 2025教师师德师风微整改自查报告范文
- 部队特种车辆培训课件
- 【公开课】发生在肺内的气体交换课件-2024-2025学年人教版生物七年级下册
- 新闻学概论马工程课件
- 入党积极分子考试试题及答案
- 小组互评活动方案
- 酒店与硬件公司合作协议
- 工业互联网基础 课程标准
- 养老护理员心理疏导培训
评论
0/150
提交评论