2

I am asked following question in an Interview:

1) There are two threads: T1 and T2. They are sharing one resources and to avoid deadlock using pthread_mutex for synchronizing. How you will design your code such that if any segmentation fault happen after T1 enters critical section, T2 will not be in deadlock?

//T1 Code
try 
{
    pthread_mutex_lock(somelock);
    .... 
    /// work on shared memory
    //What will happen if segfault happens here?
    .... 
    pthread_mutex_unlock(somelock);
} catch(...)
{
   pthread_mutex_unlock(somelock);
   // exception happens
}

I told I dont know the ans. Interviewer reached this situation waiting for my ans.

Is there really any design to avoid deadlock in this situation?

Above codeblock just for understanding. I read this. But its not clear Thanks in Advance.

Community
  • 1
  • 1
  • 1
    That `try`/`catch` block is not going to protect against segmentation faults. – Captain Obvlious May 01 '15 at 16:31
  • 2
    This is why you should use a [`std::lock_guard`](http://en.cppreference.com/w/cpp/thread/lock_guard). When the `lock_guard` goes out of scope the lock gets released automatically. – NathanOliver May 01 '15 at 16:32
  • 2
    Easy - ensure that the protected section is so small, and so simple, and so tested, that the segfault will not happen. – Martin James May 01 '15 at 16:32
  • Stack unwinding is about allocating objects on the stack so that however control exits the method, the object will be cleaned up automatically. That's nothing to do with a thread dying as a result of receiving a signal, because in that case the method never is exited. – John Bollinger May 01 '15 at 16:34
  • @JohnBollinger ok... may be I misinterpreted stack unwinding with this qus...... – Debdipta Ghosh May 01 '15 at 16:35
  • 1
    Maybe not. If you install a signal handler so that a segfault doesn't kill the the thread, then perhaps you could use RAII / stack unwinding to clean up. – John Bollinger May 01 '15 at 16:43
  • Signal handler as John mentioned is the only way I'd think you could do it, and I could be wrong but a lock_guard won't necessarily help either, since a segfault is neither going to invoke dtors nor throw (usually -- I've worked with some weird compilers that would throw on an access violation, but certainly not standard behavior). –  May 06 '15 at 20:18

1 Answers1

1

You could catch the signal with a signal handler and handle the resource as you please.

I could bealive that with his hint the interviewer meant using the RAII idiom - Resource Allocation Is Initialization.

But I am unsure if this applies to signals...

user1708860
  • 1,683
  • 13
  • 32
  • 1
    If a process / thread terminates as a result of receiving a signal then the stack is not unwound, exit handlers are not called, etc.. The thread simply terminates. Using RAII to address such a situation would need to involve (1) catching the signal, and (2) avoiding *abrupt* termination. Thread termination might still be the cleanest behavior, but the affected function might be induced to terminate more cleanly, perhaps by throwing an exception. – John Bollinger May 01 '15 at 16:46