Mutexes

면접 대비

시간 제한2초메모리 제한512 MB

요약
함수 호출과 뮤텍스 acquire, release, access 명령으로 이루어진 프로그램을 실행 순서대로 시뮬레이션하면서 가장 먼저 발생하는 corruption, deadlock, error를 찾는다.
난이도

보통10점 중 5점

유형
시뮬레이션, 재귀, 해시맵, 구현
정답자
아직 제출이 없습니다

문제

Anna loves coding multithreaded backend services. In such a service, multiple threads may sometimes need to read and write the same data structures in memory. To ensure all threads have a consistent view of a single datastructure, one can use so-called mutexes to protect access to it.

A mutex is an object that threads can acquire and release. When a mutex is acquired, the mutex cannot be acquired again until it is released -- even by the same thread! If a thread attempts to acquire a mutex it has already acquired, the thread will deadlock, waiting for itself to release the mutex.

Anna has written a program that consists of a number of functions. Each function consists of a list of commands that execute sequentially when the function is called. The commands are each one of:

  • acquire the mutex named XX
  • release the mutex named XX
  • access a data structure protected by the mutex XX
  • call another function

Anna is not sure if she implemented her mutexes correctly, and she wants your help to verify that three properties hold. Assuming a function main is called at the beginning of the program, you should check that:

  • whenever a data structure protected by a mutex XX is to be accessed, the mutex XX is currently acquired,
  • whenever a mutex is to be acquired, the program has not already acquired it (in order to avoid a deadlock),
  • whenever a mutex is to be released, the program is currently holding it.

입력

The first line of the input contains an integer NN, the number of functions. This is followed by a description of all the NN functions.

The description of a function starts with an integer MM and a string XX, meaning that there is a function named XX with MM commands. This is followed by MM lines, each containing a command. The commands will be of the form:

  • acquire X -- the mutex named XX is acquired
  • release X -- the mutex named XX is released
  • access X -- a data structure that must be protected by the mutex XX is accessed
  • call F -- the function called FF is called

All functions and mutexes have names between 1−101-10 characters in length containing only characters a-z. No two functions will have the same name, and there will always be a function called main.

It is guaranteed that there is no infinite recursion: a function will never call itself, either directly or through a chain of other functions.

출력

If the program is free from errors, output a-ok.

Otherwise, output the first error that occurs during execution. Specifically:

  • if a data structure is accessed without the correct mutex being acquired, output corruption,
  • if a deadlock occurs, output deadlock,
  • if a mutex is released without being acquired, output error.

제한

Let LL denote the total number of instructions among all functions.

  • L≤50,000L \le 50\\,000

예제4

  1. 예제 1

    입력
    3
    5 main
    acquire mutex
    call foo
    acquire mutex
    call foo
    acquire mutex
    2 foo
    access mutex
    release mutex
    1 bar
    release mutex
    
    예상 출력
    a-ok
    
  2. 예제 2

    입력
    3
    2 f
    acquire x
    call g
    2 g
    access x
    acquire y
    2 main
    call f
    call g
    
    예상 출력
    deadlock
    
  3. 예제 3

    입력
    1
    3 main
    release x
    acquire x
    access x
    
    예상 출력
    error
    
  4. 예제 4

    입력
    1
    3 main
    access x
    acquire x
    release x
    
    예상 출력
    corruption