System and device programming——R&W with semaphore

Readers Precedence

when both readers and writers wait,favor readers and writers can write only after all readers read.Using semaphore me1 to let writer need to wait one more signal.e.g.when 

writer w1 enters and no readers,w1 can write,then have both writers and readers are waiting. Reader r1 can continue till wait(w),but writer w2 can‘t enter because it needs w1

completes to release semaphore(me1).After w1 finishes writing,it releases semaphore w first,then reader r1 gets immediately. Then reader r1 increases counter,and release semaphore me later which can let next reader increases counter,in this way till last reader

finishes reading and decreases(nr==0) counter,semaphore w can only be released.Only after this,can writer gets semaphore w and writes.

nr=0(number of readers currently reading)

int(w)=int(me)=int(me1)=1

Reader                  Writer

wait(me);                wait(me1)

nr++;                   wait(w)

if(nr==1) wait(w)          ...

signal(me)                //writing

...                     ...

//reading                  signal(w)

...                    signal(me1)

wait(me)

nr--;

if(nr==0) signal(w)

signal(me)

Writers Precedence

The use of me2 is same with previous case

nr=nw=0;init(r)=init(w)=init(me)=init(me1)=init(me2)=1

Reader                               Writer

wait(me2)                             wait(me1)

wait(r)                              nw++

//needs to see whether there‘s writer            if(nw==1) wait(r)

//waiting,previous only first reader needs to check signal(me1)

//and checks only writing                  wait(w)

wait(me)                            .

nr++                               .

if(nr==1) wait(w)                    //Writing

signal(me)                           .

signal(r)                            .

signal(me2)                            signal(w)

//Reading                            wait(me1)

wait(me)                                nw--

nr--                                 if(nw==0)signal(r)

if(nr==0) signal(w)                     signal(me1)

signal(me2)

when no precedence for both readers and writers,but first in,first served,what to do?

maybe list

System and device programming——R&W with semaphore

时间: 2024-08-26 01:20:47

System and device programming——R&W with semaphore的相关文章

System and device programming lab3

pthread_cond_wait(buf->notfull,&buf->lock); notfull is a pointer,so don't need to use &. if use int *p,needs to allocate space,but use int p[10],don't need use gdb to detect segmentation fault, which usually due to malloc /*pthread_cond_init

System and Device Programming------lab5(paging and swapping)

CR0:has a control flag PG,If 1, enable paging and use the CR3 register, else disable paging CR2:store the address the program attempted to access. CR3:used to store the address of the top-level structure,enable the processor to translate linear addre

C语言中文件打开模式(r/w/a/r+/w+/a+/rb/wb/ab/rb+/wb+/ab+)浅析

C语言文件打开模式浅析 在C语言的文件操作语法中,打开文件文件有以下12种模式,如下图: 打开模式  只可以读   只可以写  读写兼备 文本模式 r w a r+ w+ a+ 二进制模式 rb wb ab  rb+ (r+b)   wb+ (w+b)   ab+ (a+b)  其中,二进制模式与文本模式操作相似,只不过是以二进制流的形式读写而已,下面以文本模式为例分析: 1."r" 模式: 1.1 打开文件进行“只读”操作,即只能从文件读取内容. 1.2 若欲操作的文件不存在,则打开

nagios 监控 mysql 读写比 R/W

#!/usr/bin/python2.7 # -*- coding:utf-8 -*- from optparse import OptionParser import commands,sys,jastme,re from datetime import datetime """     Nagios plugin to report the mysql R/W Ratio     author jastme """ parser = Opti

Python文件打开方式详解——r,w,a,r+,w+,a+

r,r+,w,w+,a,a+作用 r 只读 不创建 r+ 读写 w 新建只写 二者都会将文件清空 w+ 新建读写 a 附加写方式打开,不可读 a+ 附加读写方式打开 w+与r+区别: r+ 可读可写,若文件不存在,报错 w+ 可读可写,若文件不存在,创建 r+与a+区别: fd = open("1.txt",'w+') fd.write('123') fd = open("1.txt",'r+') fd.write('456') fd = open("1.

SmartSql = Dapper + MyBatis + Cache(Memory | Redis) + ZooKeeper + R/W Splitting + ......

SmartSql Why 拥抱 跨平台 DotNet Core,是时候了. 高性能.高生产力,超轻量级的ORM.156kb (Dapper:168kb) So SmartSql TargetFrameworks: .NETFramework 4.6 & .NETStandard 2.0 SmartSql = SmartSql = MyBatis + Cache(Memory | Redis) + ZooKeeper + R/W Splitting +Dynamic Repository + ..

python 文件操作联系 r+ w+ a+ 的理解

突然来一句:“慨然有经略四方之志” 文件操作三部曲:1.先用open打开 2.再写关闭  3.再回到中间写操作     为啥要刚打开就关闭 那是很容易望,所以先写上... 基本格式 f = open("file_name", "r", encoding="utf-8")    都得有才标准呐 ! f.read() 如果不写读几位,它会一直读下去 文件指针也就从头跟着下去,而且他是不会自己回来滴  所以要用f.seek()  把指针找回来并放到想去

System and Device Programming------questions solved about device

(1) br------ 2 root root 0,21 Feb 12 15:40 /dev/dsk15crw-rw-- 2 root root 7,21 Mar 7 09:29 /dev/rdsk15because the two special files propably refer to same partition,sothey refer to to same device,they are just two files of samedevice,which stands for

r w x 权限

文件: r:可获取文件的数据: w: 可修改文件的数据: x:可将此文件运行为进程: 目录: r:可使用ls命令获取其下的所有文件列表: w: 可修改此目录下的文件列表:即创建或删除文件: x: 可cd至此目录中,且可使用ls -l来获取所有文件的详细属性信息: