answersLogoWhite

0


Best Answer

Yes. Whenever a thread needs to read or write to memory shared with concurrent threads, it must prevent other threads from writing to that memory at the same time. That is, the threads need to be synchronised. Typically, a thread will acquire a lock on that memory, releasing it when it has finished with the memory. Only one thread can acquire a lock at any given time, so if the lock is already acquired by another thread, the current thread must wait for it to be released.

However, there are often times where two or more locks need to be acquired. So long as every thread acquires locks in the exact same order this is not a problem. But if a thread acquires the locks in a different order, we can easily end up with a deadlock.

Consider the following pseudocode:

function f ()

{

acquire lock A

acquire lock B

// ...

release lock B

release lock A

}

function g ()

{

acquire lock B

acquire lock A

// ...

release lock A

release lock B

}

If f is invoked in one thread and g in another, we can easily end up with f acquiring lock A while g acquires lock B. Function f is then left in limbo waiting for function g to release lock B while g is waiting for f to release lock A. They are deadlocked because neither can progress any further.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

12y ago

Yes. "Deadlock" refers to a condition when a process stalls because each of its threads are waiting for another thread to do something or release control.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Can deadlock occur in process with multiple threads?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Computer Science

What is difference between starvation and deadlock?

Deadlock: Two processes are said to be in deadlock situation if process A holding onto resources required for process B and where as B holding onto the resources required for process A. Starvation: This is mostly happens in time sharing systems in which the process which requires less time slot is waiting for the large process to finish and to release the resources, but the large process holding the resources for long time (almost for forever) and the process that requires small time slot goes on waiting. Such situation is starvation for small process


How many seconds after a change notification process is triggered does replication between domain controllers occur?

15 seconds.


Discuss methods for handling deadlocks?

ignore the problem altogether ie. ostrich algorithm it may occur very infrequently, cost of detection/prevention etc may not be worth it.detection and recoveryavoidance by careful resource allocationprevention by structurally negating one of the four necessary conditions.Deadlock PreventionDifference from avoidance is that here, the system itself is build in such a way that there are no deadlocks. Make sure atleast one of the 4 deadlock conditions is never satisfied.This may however be even more conservative than deadlock avoidance strategy.Attacking Mutex conditionnever grant exclusive access. but this may not be possible for several resources.Attacking preemptionnot something you want to do.Attacking hold and wait conditionmake a process hold at the most 1 resource at a time.make all the requests at the beginning. All or nothing policy. If you feel, retry. eg. 2-phase lockingAttacking circular waitOrder all the resources.Make sure that the requests are issued in the correct order so that there are no cycles present in the resource graph. Resources numbered 1 ... n. Resources can be requested only in increasing order. ie. you cannot request a resource whose no is less than any you may be holding.Deadlock AvoidanceAvoid actions that may lead to a deadlock. Think of it as a state machine moving from 1 state to another as each instruction is executed.Safe StateSafe state is one whereIt is not a deadlocked stateThere is some sequence by which all requests can be satisfied.To avoid deadlocks, we try to make only those transitions that will take you from one safe state to another. We avoid transitions to unsafe state (a state that is not deadlocked, and is not safe) eg.Total # of instances of resource = 12(Max, Allocated, Still Needs)P0 (10, 5, 5) P1 (4, 2, 2) P2 (9, 2, 7) Free = 3 - SafeThe sequence is a reducible sequencethe first state is safe.What if P2 requests 1 more and is allocated 1 more instance?- results in Unsafe stateSo do not allow P2's request to be satisfied.Banker's Algorithm for Deadlock AvoidanceWhen a request is made, check to see if after the request is satisfied, there is a (atleast one!) sequence of moves that can satisfy all the requests. ie. the new state is safe. If so, satisfy the request, else make the request wait.How do you find if a state is safen process and m resources Max[n * m]Allocated[n * m]Still_Needs[n * m]Available[m]Temp[m]Done[n]while () {Temp[j]=Available[j] for all jFind an i such thata) Done[i] = Falseb) Still_Needs[i,j]


What is the exact difference between interrupt handling and exception handling?

interrupt handling is the process of handling a break or interrupt called by a program where as exception handling is for handling some exceptional conditions that'll occur when a program is running


When does a call to exec return in UNIX?

A call to the exec() family of functions in UNIX does not normally return to the calling process. This is because the call replaces the invoking process'es image, thus there is nothing to return to. If an error does occur, exec() returns -1, and sets an error value that can be interrogated, but the answer to the question is, usually, never. The normal paradigm for launching a process and getting control back, such as by the shell, is to call fork(), which splits the invoking process into two identical processes, one continuing to monitor the other. The other process then calls exec(), replacing itself. When it exits, the first process can detect that and retrieve its return value.

Related questions

How deadlock is achieved in java?

A Deadlock is a situation of indefinite waiting where the system is stuck at a particular point and would do nothing useful. For example Method A is waiting for B's input while B is waiting for C's input and C in turn is waiting for A's input. Here all the 3 methods would continue to wait because they are waiting on one another and the system is stuck. A deadlock usually occurs while using threads. Threads can lock objects on which they are processing and when multiple threads are waiting for the same object a Dead lock may occur. The use of the synchronize keyword can be used to avoid such deadlock situations.


What is false deadlock?

the coordinator conculde incorrectly that a deadlock exist and kills some process --------------------------- Detecting a non existent deadlock in distributed system has been referred as false deadlock and it may occur due to communication delay.. ---->Ashok Paranjothi


Is it possible to have a deadlock involving only a single process?

no deadlock can only occur when processes access shared memory and the following conditions must be met: -mutual exclusion -hold and wait -no preemption -circular wait. As soon as any of the 4 conditions fail,no deadlock. For example there cannot be circular wait for the case of one process.Who is waiting for who?The single process have access to all the available ressources.


What is difference between starvation and deadlock?

Deadlock: Two processes are said to be in deadlock situation if process A holding onto resources required for process B and where as B holding onto the resources required for process A. Starvation: This is mostly happens in time sharing systems in which the process which requires less time slot is waiting for the large process to finish and to release the resources, but the large process holding the resources for long time (almost for forever) and the process that requires small time slot goes on waiting. Such situation is starvation for small process


Is it possible to occur deadlock in one process during rsource allocation?

No. This follows directly from the hold-and-wait condition. Raj Gopal Mishra (India)


What are the differences between deadlock avoidance and deadlock prevention in operating systems?

Deadlock Prevention: o Preventing deadlocks by constraining how requests for resources can be made in the system and how they are handled (system design). o The goal is to ensure that at least one of the necessary conditions for deadlock can never hold. * Deadlock Avoidance: o The system dynamically considers every request and decides whether it is safe to grant it at this point, o The system requires additional apriori information regarding the overall potential use of each resource for each process. o Allows more concurrency. Similar to the difference between a traffic light and a police officer directing traffic. * Deadlock deduction:- Often, neither avoidance nor deadlock prevention may be used. Instead deadlock detection and process restart are used by employing an algorithm that tracks resource allocation and process states, and rolls back and restarts one or more of the processes in order to remove the deadlock. Detecting a deadlock that has already occurred is easily possible since the resources that each process has locked and/or currently requested are known to the resource scheduler or OS. Detecting the possibility of a deadlock before it occurs is much more difficult and is, in fact, generally undecidable, because the halting problem can be rephrased as a deadlock scenario. However, in specific environments, using specific means of locking resources, deadlock detection may be decidable. In the general case, it is not possible to distinguish between algorithms that are merely waiting for a very unlikely set of circumstances to occur and algorithms that will never finish because of deadlock. Deadlock detection techniques include, but is not limited to, Model checking. This approach constructs a Finite State-model on which it performs a progress analysis and finds all possible terminal sets in the model. These then each represent a deadlock.


Define deadlock in operating system?

In order for deadlock to occur, four conditions must be true.Mutual exclusion - Each resource is either currently allocated to exactly one process or it is available. (Two processes cannot simultaneously control the same resource or be in their critical section).Hold and Wait - processes currently holding resources can request new resourcesNo preemption - Once a process holds a resource, it cannot be taken away by another process or the kernel.Circular wait - Each process is waiting to obtain a resource which is held by another process.


What is the difference between deadlock and a race condition?

Race ConditionsA race condition occurs when two threads access a shared variable at the same time. The first thread reads the variable, and the second thread reads the same value from the variable. Then the first thread and second thread perform their operations on the value, and they race to see which thread can write the value last to the shared variable. The value of the thread that writes its value last is preserved, because the thread is writing over the value that the previous thread wrote.DeadlocksA deadlock occurs when two threads each lock a different variable at the same time and then try to lock the variable that the other thread already locked. As a result, each thread stops executing and waits for the other thread to release the variable. Because each thread is holding the variable that the other thread wants, nothing occurs, and the threads remain deadlocked.


What are the problems that can occur if you do not implement locking properly?

The main - and most important - problem that can come from buggy locking code is that the data you're trying to establish a lock on may end up being accessed and edited by multiple threads/processes at the same time. This would, of course, defeat the purpose of trying to lock out data. Other problems could include things like two threads trying to access some data at the same time, but each thinks the other has a lock on it. Which would give us a nice little deadlock to deal with.


When a screwdriver is being used to put a screw into a wall where does friction occur?

The threads of the screw as the threads are turned by the screwdriver .


What is the differences between a process and a thread in Linux?

A single process can have multiple threads that share global data and address space with other threads running in the same process, and therefore can operate on the same data set easily. Processes do not share address space and a different mechanism must be used if they are to share data.If we consider running a word processing program to be a process, then the auto-save and spell check features that occur in the background are different threads of that process which are all operating on the same data set (your document). processIn computing, a process is an instance of a computer program that is being sequentially executed[1] by a computer system that has the ability to run several computer programs concurrently. Thread A single process may contain several executable programs (threads) that work together as a coherent whole. One thread might, for example, handle error signals, another might send a message about the error to the user, while a third thread is executing the actual task of the...


How does the bonding process occur?

There are many types of bonds so that depends on the type, however, to put it in astronomically simplified terms, the bonding process occurs when electrons are shared between multiple atoms.