QOJ.ac

QOJ

Límite de tiempo: 1.0 s Límite de memoria: 512 MB Puntuación total: 100 Hackeable ✓

#16719. 阻塞缓冲区

Estadísticas

一种用于并行编程的新型微架构使用管道缓冲区来组织两个执行线程之间的数据交换。该缓冲区是循环的,最多可容纳 $l$ 字节。在程序执行开始时,缓冲区为空。第一个线程有时会尝试向缓冲区写入数据,且每次写入的数据块大小均为 $w$ 字节。第二个线程有时会从缓冲区读取数据,每次读取的数据块大小均为 $r$ 字节。所有写操作都将新数据放入管道缓冲区的末尾,而所有读操作都从管道缓冲区的开头取出数据,因此空闲位置总是形成一个连续的段。

写操作和读操作都是阻塞的。这意味着,如果第一个线程尝试向缓冲区写入数据块,但缓冲区中没有足够的空间,该线程就会停止并等待,直到缓冲区中至少有 $w$ 个空闲字节。类似地,如果第二个线程尝试从缓冲区读取数据块,但剩余的数据少于 $r$ 字节,该线程就会停止并等待,直到缓冲区中有足够的新数据。

在本题中,死锁是指以下情况:第一个线程因缓冲区中没有足够的空间容纳大小为 $w$ 的新数据块而被阻塞,同时第二个线程因缓冲区中没有足够的数据来读取大小为 $r$ 的新数据块而被阻塞。

你的目标是确定是否存在这样一种来自两个线程的读写操作序列,从而导致死锁。请注意,写操作仅由第一个线程执行,读操作仅由第二个线程执行。

输入格式

输入的唯一一行包含三个整数 $l$,$r$ 和 $w$($0 < l, r, w \le 10^{18}$,$r \le l$,$w \le l$)。

输出格式

如果存在导致死锁的读写序列,则在输出的唯一一行中打印 "DEADLOCK"(不带引号)。否则,打印 "OK"(不带引号)。

样例

输入样例 1

5 3 4

输出样例 1

DEADLOCK

输入样例 2

5 2 3

输出样例 2

OK

说明

在第一个样例中,导致死锁的一种可能方式如下:

  1. 第一个线程向缓冲区写入一个大小为 4 的数据块。
  2. 第二个线程读取一个大小为 3 的数据块。缓冲区中还剩 1 字节的数据。
  3. 第一个线程写入一个大小为 4 的数据块。此时缓冲区已满。
  4. 第二个线程读取一个大小为 3 的数据块。缓冲区中还剩 2 字节的数据。
  5. 第二个线程尝试读取一个大小为 3 的数据块,但缓冲区中没有足够的数据,因此该线程被阻塞。
  6. 第一个线程尝试写入一个大小为 4 的数据块,但缓冲区中没有足够的空闲空间,因此该线程也被阻塞。死锁刚刚发生。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.