Debdipta Ghosh
Debdipta Ghosh

Reputation: 23

segfault in critical section - avoiding deadlock

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.

Upvotes: 2

Views: 910

Answers (1)

user1708860
user1708860

Reputation: 1753

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...

Upvotes: 1

Related Questions