两道面试题解答之一 Trading Server Deadlocks

本文介绍了一种检测交易服务器中潜在死锁问题的方法。通过分析不同的执行路径及其锁获取顺序,该方案能找出可能导致死锁的执行路径组合。

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

https://bridgewater.interviewstreet.com/challenges/

第一题是关于判断死锁问题的

Trading Server Deadlocks

A few months ago, we were running a new release of our trade server in QA and experienced a hang. The system was accepting connections from clients, but no longer processing any transactions. This system is critical to many of our major trading operations and so, the issue was a big deal to fix before we released to production.

The trade server supports many different execution paths which take out various locks on shared resources.  Upon investigation, we found that the system had experienced a deadlock between two of those execution paths. To ensure we fixed the problem holistically (rather than just the deadlock case that we caught), we wanted to examine all the execution paths in the system and determine all cases that were constructed in a way that they can cause deadlocks.

We'll provide you a set of execution paths. Each is written as an ordered list of lock acquisitions. Your job is to write a program which enumerate all the subsets of sets of execution paths that could result in a deadlock if executed in parallel.

Your input will be a file with one execution path per line. The name will be first, separated by a space, then a comma separated list of the locks in order they are acquired. For instance:

    path1 L1,L2,L3
    path2 L2,L1,L3
    path3 L4,L5,L6
    path4 L5,L4,L6

Your output should be each subset of execution paths which can deadlock, one per line. Sort the paths alphabelitcaly and order the sets alphabetically based on their lexicographic representation.

    path1,path2
    path3,path4

For more information about deadlocks, checkout the wikipedia page.


package bridgewater;

import java.io.*;
import java.util.*;
import java.util.Map.Entry;

public class DeadLockSolution {
	
	private List<String> inputs = new ArrayList<String>();
	
	public DeadLockSolution(){
	}
	
	public static void main(String[] args) throws Exception {
		BufferedReader br = null;
		DeadLockSolution solution = new DeadLockSolution();
		try {
			InputStream inputStream = new FileInputStream("input.txt");
			Reader reader = new InputStreamReader(inputStream);
			br = new BufferedReader(reader);
			
//			br = new BufferedReader(new InputStreamReader(System.in));
			
			String input = br.readLine();
			while(input != null){
				solution.inputs.add(input);
				input = br.readLine();
			}
			solution.process(solution.inputs);
			
		} finally {
			if (br != null)
				br.close();
		}
	}

	public void process(List<String> inputs) {
		LinkedHashMap<String, ArrayList<String>> map = new LinkedHashMap<String, ArrayList<String>>();
		
		for (String string : inputs) {
			String[] splits = string.split(" ");
			if(splits.length > 1){
				String[] tmp = splits[1].split(",");
				ArrayList<String> locks = new ArrayList<String>();
				locks.addAll(Arrays.asList(tmp));
				map.put(splits[0], locks);
			}
		}
//		print(map);
		
		LinkedHashSet<ArrayList<String>> ret = new LinkedHashSet<ArrayList<String>>();
		int entryIndex = 0;
		for (Entry<String, ArrayList<String>> entry : map.entrySet()) {
			ArrayList<String> value = entry.getValue();
			int i = 0;
			do{
				int otherEntryIndex = 0;
				for(Entry<String, ArrayList<String>> otherEntry : map.entrySet()){
					if(otherEntryIndex>entryIndex && !entry.equals(otherEntry)){
						ArrayList<String> otherValue = otherEntry.getValue();
						if(otherValue.contains(value.get(i))){
							int FirstHitIndex = otherValue.indexOf(value.get(i));
							if((i+1)%value.size() == 0){		// Special case, when looping around!
								if(FirstHitIndex+1<otherValue.size() && otherValue.get(FirstHitIndex+1).equals(value.get((i+1)%value.size()))){
									ArrayList<String> conflicts = new ArrayList<String>();
									conflicts.add(entry.getKey());
									conflicts.add(otherEntry.getKey());
									ret.add(conflicts);
								}
							}else{
								if(FirstHitIndex-1>=0 && otherValue.get(FirstHitIndex-1).equals(value.get((i+1)%value.size()))){
									ArrayList<String> conflicts = new ArrayList<String>();
									conflicts.add(entry.getKey());
									conflicts.add(otherEntry.getKey());
									ret.add(conflicts);
								}
							}
						}
					}
					otherEntryIndex++;
				}
				i++;
				i %= value.size();
			}while(i != 0);
			entryIndex++;
		}
		
//		System.out.println(ret);
		for (ArrayList<String> al : ret) {
			System.out.println(al.get(0)+","+al.get(1));
		}
	}
	
	public void print(Map<String, ArrayList<String>> map){
		for (Entry<String, ArrayList<String>> entry : map.entrySet()) {
			 String key = entry.getKey();
			 ArrayList<String> value = entry.getValue();
		    System.out.println(key + "->" + value);
		}
		
		
//		Iterator it = map.entrySet().iterator();
//	    while (it.hasNext()) {
//	        Map.Entry pairs = (Map.Entry)it.next();
//	        System.out.println(pairs.getKey() + " = " + pairs.getValue());
//	    }
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值