僵局 - 转让计划 [英] Deadlock - Transfer Program

查看:113
本文介绍了僵局 - 转让计划的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么这个程序可以有死锁

I want to know why this program can have a deadlock

void transfer(int from, into to, double amount) {
     sem_t *sem_from, *sem_to;
     sem_from=get_sem(from); //function that obtains the semaphore from bank account argument
     sem_to=get_sem(to);
     sem_wait(sem_from);
     sem_wait(sem_to);
     withdraw(from, amount); 
     deposit(to, amount); 
     sem_post(sem_to);
     sem_post(sem_from);
}

感谢。

推荐答案

说,有同时运行功能的两个实例。第一个是从一个账户转移一定量的帐户B,而第二个是从帐户B,转一些其他的量如果碰巧一审获得A的锁,并在同一时间二审获得占A. B的锁,死锁的发生。

Say that there are two instances of the function running concurrently. The first one is transferring some amount from account A to account B, while the second is transferring some other amount from account B to account A. If it happens that the first instance acquires the lock of A and in the same time the second instance acquires the lock of B, a deadlock happens.

这篇关于僵局 - 转让计划的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆