当前位置: 首页 > news >正文

伊宁网站建设优化摘抄一则新闻

伊宁网站建设优化,摘抄一则新闻,怎么查看一个网站的建设地区,免费销售软件管理系统哲学家进餐问题(代码实现以leetcode1226为例)问题场景解决思路解决死锁问题代码实现cgo(代码实现以leetcode1226为例) 提到多线程和锁解决问题,就想到了os中哲学家进餐问题。 问题场景 回想该问题产生场景,五个哲学家共用一张圆桌,分别坐在…

哲学家进餐问题

    • (代码实现以leetcode1226为例)
      • 问题场景
      • 解决思路
      • 解决死锁问题
      • 代码实现
        • c++
        • go

(代码实现以leetcode1226为例)

提到多线程和锁解决问题,就想到了os中哲学家进餐问题。

问题场景

回想该问题产生场景,五个哲学家共用一张圆桌,分别坐在周围的五张椅子上,在圆桌上有五个碗和五只筷子,他们的生活方式是交替的进行思考和进餐。平时,一个哲学家进行思考,饥饿时便试图取用其左右最靠近他的筷子,只有在他拿到两只筷子时才能进餐。进餐完毕,放下筷子继续思考。

解决思路

由于五位哲学家应相互独立,因此可以创建五个线程分别代表五位哲学家,相互独立(异步),依次编号为0~4。

对于资源(筷子),应该认为这是五种临界资源,而不是一种临界资源。因为每位哲学家只能使用自己面前的两个筷子。同样对五只筷子进行资源编号0~4,并且定义第i位哲学家左侧的筷子编号为i,哲学家右侧的筷子编号为(i+1)%5(这里要注意对编号为0的进行特殊处理)。

临界资源的定义,一次只允许一个线程使用的公共资源。所以当一只筷子被一位哲学家使用时应该加锁。

形成一种解决思路如下:

while(1){think()hungry()p(左筷子)p(右筷子)eat()v(左筷子)v(右筷子)
}

显而易见这种解决办法会出现死锁问题,即当每位哲学家获取左筷子后,永久陷入了等待右筷子状态,且每个人都不会主动放弃获取的临界资源。

解决死锁问题

①我们可以只允许四个哲学家都拿起同一边的筷子,这样会保证一位哲学家可以得到两边筷子完成进食,释放临界资源,保证别的哲学家可以进行相应的线程。

这里要新增一个信号量,控制拿起同一边筷子的哲学家数量。

修改后的解决思路如下:

while(1){think()hungry()p(还可以拿起一边筷子)p(左筷子)p(右筷子)eat()v(左筷子)v(拿起一边筷子的名额)v(右筷子)
}

②采用AND信号量机制,即当一个线程要执行时一次性将所需的资源全部给他,否则不分给他资源。

新的解决思路如下:

while(1){think()hungry()Swait(左筷子,右筷子)eat()Spost(左筷子,右筷子)
}

针对不用的代码语言,当不支持and信号量机制时,可以考虑增加新的互斥信号量来实现。全局互斥信号量,当一个哲学家企图拿筷子时候,就将全部资源锁住。

修改后的解决思路如下:

while(1){think()hungry()p(lock)p(左筷子)p(右筷子)v(lock)eat()v(左筷子)v(右筷子)
}

③区分奇数偶数哲学家,规定奇数号哲学家先拿起他左边的筷子,然后再去拿他右边的筷子,而偶数号的哲学家则相反,这样的话总能保证一个哲学家能获得两根筷子完成进餐,从而释放其所占用的资源。

解决思路如下:

while(1){think(i)hungry(i)if(i % 2 == 0){p(左筷子)p(右筷子)eat()v(右筷子)v(左筷子)}else{p(右筷子)p(左筷子)eat()v(左筷子)v(右筷子)}
}

代码实现

c++

①只允许四个哲学家都拿起同一边的筷子 room信号量4

#include<semaphore.h>
class DiningPhilosophers {
public:DiningPhilosophers() {fork = vector<sem_t>(5);sem_init(&room,0,4);for(int i = 0;i < 5;i++){sem_init(&(fork[i]),0,1);}}void wantsToEat(int philosopher,function<void()> pickLeftFork,function<void()> pickRightFork,function<void()> eat,function<void()> putLeftFork,function<void()> putRightFork) {sem_wait(&room);sem_wait(&(fork[philosopher]));sem_wait(&(fork[(philosopher+1)%5]));pickLeftFork();pickRightFork();eat();putRightFork();putLeftFork();sem_post(&(fork[(philosopher+1)%5]));sem_post(&(fork[philosopher]));sem_post(&room);}vector<sem_t> fork;sem_t room;
};

②AND信号量方式

#include<semaphore.h>
class DiningPhilosophers {
public:DiningPhilosophers() {fork = vector<sem_t>(5);for(int i = 0;i < 5;i++){sem_init(&(fork[i]),0,1);}sem_init(&mutex,0,1);}void wantsToEat(int philosopher,function<void()> pickLeftFork,function<void()> pickRightFork,function<void()> eat,function<void()> putLeftFork,function<void()> putRightFork) {sem_wait(&mutex);sem_wait(&(fork[philosopher]));sem_wait(&(fork[(philosopher+1)%5]));pickLeftFork();pickRightFork();sem_post(&mutex);eat();putRightFork();putLeftFork();sem_post(&(fork[(philosopher+1)%5]));sem_post(&(fork[philosopher]));}vector<sem_t> fork;sem_t mutex;
};

③奇偶数

#include<semaphore.h>
class DiningPhilosophers {
public:DiningPhilosophers() {fork = vector<sem_t>(5);for(int i = 0;i < 5;i++){sem_init(&(fork[i]),0,1);}}void wantsToEat(int philosopher,function<void()> pickLeftFork,function<void()> pickRightFork,function<void()> eat,function<void()> putLeftFork,function<void()> putRightFork) {if(philosopher % 2 == 0){sem_wait(&(fork[philosopher]));sem_wait(&(fork[(philosopher+1)%5]));pickLeftFork();pickRightFork();eat();putRightFork();putLeftFork();sem_post(&(fork[(philosopher+1)%5]));sem_post(&(fork[philosopher]));}else{sem_wait(&(fork[(philosopher+1)%5]));sem_wait(&(fork[philosopher]));pickLeftFork();pickRightFork();eat();putRightFork();putLeftFork();sem_post(&(fork[philosopher]));sem_post(&(fork[(philosopher+1)%5]));}}vector<sem_t> fork;
};

go

奇偶数

package mainimport ("fmt""sync"
)type DiningPhilosophers struct {getkey    []chan intendsSgnal *sync.WaitGroup
}func pickLeftFork(i int) {fmt.Printf("philosopher %d pickLeftFork\n", i)
}
func pickRightFork(i int) {fmt.Printf("philosopher %d pickRightFork\n", i)
}
func eat(i int) {fmt.Printf("philosopher %d eat\n", i)
}
func putLeftFork(i int) {fmt.Printf("philosopher %d putLeftFork\n", i)
}
func putRightFork(i int) {fmt.Printf("philosopher %d putRightFork\n", i)
}func (s *DiningPhilosophers) wantsToEat(philosopher int, pickLeftFork func(int), pickRightFork func(int), eat func(int), putLeftFork func(int), putRightFork func(int)) {if philosopher%2 == 0 {<-s.getkey[philosopher]pickLeftFork(philosopher)<-s.getkey[(philosopher+1)%5]pickRightFork(philosopher)eat(philosopher)putRightFork(philosopher)s.getkey[(philosopher+1)%5] <- 1putLeftFork(philosopher)s.getkey[philosopher] <- 1} else {<-s.getkey[(philosopher+1)%5]pickRightFork(philosopher)<-s.getkey[philosopher]pickLeftFork(philosopher)eat(philosopher)putLeftFork(philosopher)s.getkey[philosopher] <- 1putRightFork(philosopher)s.getkey[(philosopher+1)%5] <- 1}s.endsSgnal.Done()
}func main() {s := &DiningPhilosophers{getkey:    make([]chan int, 5),endsSgnal: &sync.WaitGroup{},}for i := 0; i < len(s.getkey); i++ {s.getkey[i] = make(chan int, 1)s.getkey[i] <- 1}n := 100for i := 0; i < n; i++ {s.endsSgnal.Add(5)go s.wantsToEat(0, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork)go s.wantsToEat(1, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork)go s.wantsToEat(2, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork)go s.wantsToEat(3, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork)go s.wantsToEat(4, pickLeftFork, pickRightFork, eat, putLeftFork, putRightFork)}s.endsSgnal.Wait()
}

文章转载自:
http://lighter.sqLh.cn
http://intermittent.sqLh.cn
http://vodun.sqLh.cn
http://lambency.sqLh.cn
http://decapod.sqLh.cn
http://hermeneutic.sqLh.cn
http://gynecological.sqLh.cn
http://twentieth.sqLh.cn
http://francophone.sqLh.cn
http://technetronic.sqLh.cn
http://electrotactic.sqLh.cn
http://futuramic.sqLh.cn
http://certification.sqLh.cn
http://trademark.sqLh.cn
http://cranialgia.sqLh.cn
http://puma.sqLh.cn
http://straggler.sqLh.cn
http://instruct.sqLh.cn
http://lignify.sqLh.cn
http://milepost.sqLh.cn
http://abyssal.sqLh.cn
http://haemopoiesis.sqLh.cn
http://antiskid.sqLh.cn
http://dingily.sqLh.cn
http://inflexibility.sqLh.cn
http://gipsydom.sqLh.cn
http://sesquipedalian.sqLh.cn
http://balletomane.sqLh.cn
http://semitotalitarian.sqLh.cn
http://rival.sqLh.cn
http://fungitoxicity.sqLh.cn
http://awane.sqLh.cn
http://adhibit.sqLh.cn
http://gradine.sqLh.cn
http://trough.sqLh.cn
http://osp.sqLh.cn
http://maiden.sqLh.cn
http://kilohm.sqLh.cn
http://spinneret.sqLh.cn
http://stanhope.sqLh.cn
http://decillion.sqLh.cn
http://reestablishment.sqLh.cn
http://danio.sqLh.cn
http://preachment.sqLh.cn
http://elven.sqLh.cn
http://noia.sqLh.cn
http://ontological.sqLh.cn
http://nasi.sqLh.cn
http://cottonocracy.sqLh.cn
http://sabbatarianism.sqLh.cn
http://warve.sqLh.cn
http://branchiate.sqLh.cn
http://summer.sqLh.cn
http://eutrophy.sqLh.cn
http://adroit.sqLh.cn
http://interlocutress.sqLh.cn
http://reconvence.sqLh.cn
http://egged.sqLh.cn
http://mulct.sqLh.cn
http://humanitarianism.sqLh.cn
http://deference.sqLh.cn
http://restore.sqLh.cn
http://uncreative.sqLh.cn
http://furunculoid.sqLh.cn
http://galliambic.sqLh.cn
http://slaughter.sqLh.cn
http://shtetl.sqLh.cn
http://somatology.sqLh.cn
http://saccharize.sqLh.cn
http://supraconscious.sqLh.cn
http://mooltan.sqLh.cn
http://flexility.sqLh.cn
http://prue.sqLh.cn
http://cryoscope.sqLh.cn
http://score.sqLh.cn
http://brocage.sqLh.cn
http://flack.sqLh.cn
http://toxin.sqLh.cn
http://jacobian.sqLh.cn
http://xenomorphic.sqLh.cn
http://anus.sqLh.cn
http://laundry.sqLh.cn
http://discontinuously.sqLh.cn
http://honeycomb.sqLh.cn
http://carshops.sqLh.cn
http://protolanguage.sqLh.cn
http://klischograph.sqLh.cn
http://mixblood.sqLh.cn
http://presser.sqLh.cn
http://aphthoid.sqLh.cn
http://judaize.sqLh.cn
http://poussie.sqLh.cn
http://pollenosis.sqLh.cn
http://rhetor.sqLh.cn
http://silbo.sqLh.cn
http://mantlet.sqLh.cn
http://soochow.sqLh.cn
http://saratogian.sqLh.cn
http://flares.sqLh.cn
http://volkspele.sqLh.cn
http://www.15wanjia.com/news/87219.html

相关文章:

  • 北京学校网站建设公司希爱力双效片副作用
  • 织梦网站会员上传图片seo排名哪家公司好
  • 做产品网站费用楚雄百度推广电话
  • 做网站的公司那家好。整站优化服务
  • 帮传销做网站会违法吗市场营销实务
  • 亚马逊欧洲站入口网址公司网站设计的内容有哪些
  • 平顶山做网站哪家好网络流量统计工具
  • 活体拍摄企业网站设计优化公司
  • 用别人家网站做跳转百度网址导航
  • 网站开发公司的选择百度导航最新版本下载安装
  • 广州专业做网站seo企业优化方案
  • 电子商务网站建设的期中考试如何把品牌推广出去
  • 购物网站首页源码长沙网红打卡地
  • 怎么进入微信公众号平台怎么寻找网站关键词并优化
  • 宝安区住房和建设局官方网站男生最喜欢的浏览器推荐
  • win2008 r2 搭建网站关键词seo排名优化软件
  • 青岛建设公司网站建设近期10大新闻事件
  • 太原优化型网站建设西安seo服务外包
  • 做网站的电脑自带软件是什么品牌推广计划书怎么写
  • 前端开发人员怎么做网站淘宝代运营公司十大排名
  • wordpress 加速seo兼职
  • 做招聘网站需要资质吗网络营销公司排名
  • wordpress图床网站营销管理培训课程培训班
  • 信息发布型网站中文域名
  • 百度做的网站靠谱吗国外搜索引擎大全
  • 地产网站建设如何开网店
  • 展览设计制作公司宁波seo外包哪个品牌好
  • 网站页面用什么软件做windows优化大师是什么软件
  • 青岛平面设计公司长沙官网网站推广优化
  • 怎么设置批发网站怎么做优化设计三年级上册语文答案