[Moscow2016] [Gym101137B] Blocking Buer(数学)

本文介绍了一种新的微架构用于并行编程中的数据交换,并通过计算最大公约数(GCD)的方法来判断是否可能发生死锁。文章提供了一个简单的C++程序实现,该程序可以确定两个线程之间的读写操作序列是否会最终导致死锁。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

New microarchitecture for parallel programming has a pipe buer to organize the data exchange between two execution threads. The buer is circular and can contain no more than l bytes. At the beginning of the program execution the buer is empty. The rst thread sometimes tries to write to the buer, and it always uses blocks of size w bytes. The second thread sometimes reads from the buer in blocks of size r bytes each. All write operations put the new data at the end of the pipe buer and all read operations take the data from the beginning of the pipe buer, so empty positions always form one continuous segment. Both write and read operations are blocking. It means that, if the rst thread tries to write the block of data to the buer but there is not enough space there, the thread stops and waits until there will be at least w empty bytes in the buer. Similarly, if the second thread tries to read the block of data from the buer but there are less than r bytes of data left, the thread stops and waits until there will be enough new data in the buer. In this particular problem, a deadlock is a situation when the rst thread is blocked because there is not enough space in the buer to t a new block of size w, and the second thread is blocked because there is not enough data in the buer to read a new block of size r. Your goal is to determine whether there exists such a sequence of reads and writes from two threads that will result in a deadlock. Note that write operations are only performed by the rst thread and read operations are only performed by the second thread.

Input

The only line of input contains three integers l, r and w (0 < l, r, w ≤ 1018 , r ≤ l, w ≤ l).

Output

If there exists a sequence of reads and writes that result in a deadlock, print DEADLOCK (without quotes) in the only line of the output. Otherwise, print OK (without quotes).

 

对r和w取个gcd,除下来,去掉那些不能达到的状态。然后,能发生死锁的条件就是r+w-2>=l(YY一下)。证明不会。

 

#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
    return b == 0 ? a : gcd(b, a % b);
}

int main() {
    #ifdef ULTMASTER
    freopen("a.in", "r", stdin);
    #endif
    long long l, r, w;
    cin >> l >> r >> w;
    long long gg = gcd(r, w);
    l /= gg; r /= gg; w /= gg;
    if (r + w - 2 >= l)
        cout << "DEADLOCK" << endl;
    else cout << "OK" << endl;
    return 0;
}

  

转载于:https://www.cnblogs.com/ultmaster/p/6144605.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值