UVa10145 - Lock Manager

本文介绍了数据库管理系统中锁管理器的基本概念和运作原理。锁用于控制并发事务对数据项的访问,分为共享(S)和独占(X)两种模式。锁请求包含模式、事务ID和数据项ID。如果请求不与已授予的锁冲突,则会授予锁。冲突定义为不同事务之间的独占模式锁。当请求因冲突被拒绝时,请求事务将被阻塞,其后续请求也将被忽略。

Problem C
"Lock Manager"


You are invited to be a part of the team that is developing yet another DBMS (Data Base Management System). You will be responsible for the Lock Manager.

Locks control concurrent access to data items by multiple transactions. Your DBMS is simple and uses only Shared (S) and Exclusive (X) mode locks. Each lock request contains a lock mode (S or X), a transaction identifier and a data item identifier. Multiple locks can be granted to the same data item as long as none of them conflict.

Two locks for the same data item conflict if:

  • they belong to different transactions, and
  • at least one of them is exclusive (X) mode lock.

At the earliest stages of development you are asked to write very simple lock manager that processes lock requests. The lock is granted if it does not conflict with previously granted locks for this data item. Your task is simple: locks, once granted, are never released or changed in any way. If lock request is denied due to conflict with some previously granted lock, then transaction making this request is blocked and all further requests from this transaction are ignored.

Input

The first line of the input consists of an integer indicating the number of test cases in the input. Then there is a blank line and the test cases separated by a blank line.

Each test case consists of a number of lock requests, each request on a different line. Requests have the following format:

MODE TRID ITEM

Where MODE is a single capital letter S or X denoting requested lock mode. TRID and ITEM are transaction identifier and data item identifier correspondingly. Both TRID and ITEM are integers, both are greater than zero, and both consist of at most 9 decimal digits.

There are at least one and at most 10000 requests in each test case.

The last request is followed by a line consisting of a single character '#'.

Output

Your program shall sequentially process all requests from each test case. For each request you should write one line that contains the response to the request. The following responses are allowed:
  • GRANTED - the lock request does not conflict with any previously granted locks and is granted.
  • DENIED - the lock request conflicts with some previously granted lock and is denied, thus blocking the requesting transaction.
  • IGNORED - the transaction was blocked on some request before this one.
Responses shall appear in all capital letters exactly as shown above. Print a blank line between data sets.

Sample input

1

S 1 1
S 2 2
X 10 1
S 6 123456789
S 3 3
X 2 2
S 5 6
S 3 1
S 3 2
X 987654321 123456789
X 1 4
S 6 6
S 3 5
S 2 4
X 4 5
S 2 51
#

Output for the sample input

GRANTED
GRANTED
DENIED
GRANTED
GRANTED
GRANTED
GRANTED
GRANTED
DENIED
DENIED
GRANTED
GRANTED
IGNORED
DENIED
GRANTED
IGNORED
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
using namespace std;
set<int> igno;
map<int,set<int> > rela;
map<int,int> xx;
void init(){
    igno.clear();
    rela.clear();
    xx.clear();
}
int main(){
    int ncase;
    int T = 1;
   // freopen("in","r",stdin);
    cin >> ncase;
    while(ncase--){
        init();
        char op;
        if(!T) cout<<endl;
        while(cin >> op && op!='#'){
            int trans,item;
            cin >> trans >> item;
            if(igno.find(trans)!=igno.end()){
                cout<<"IGNORED"<<endl;

            }
            else if(op=='X'){
                if((rela[item].count(trans)==rela[item].size())&&(xx[item]==0||xx[item]==trans)){
                    cout<<"GRANTED"<<endl;
                    xx[item] = trans;
                }else{
                    cout<<"DENIED"<<endl;
                    igno.insert(trans);
                }
            }else{
                if(xx[item]==trans||xx[item]==0){
                    cout<<"GRANTED"<<endl;
                    rela[item].insert(trans);
                }else{
                    cout<<"DENIED"<<endl;
                    igno.insert(trans);
                }
            }
        }
        T = 0;
    }
    return 0;
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值