Deadlock and Semaphores: Concurrency in Operating Systems

Classified in Computers

Written on in English with a size of 2.21 KB

Understanding Deadlock

A deadlock is a critical situation in which two or more computer programs, sharing the same resource, effectively prevent each other from accessing that resource. This results in both programs ceasing to function or becoming unresponsive.

Historically, the earliest computer operating systems ran only one program at a time, making all system resources available to that single program. As operating systems evolved to run multiple programs concurrently through interleaving, programs initially had to specify their resource needs in advance to avoid conflicts.

The advent of dynamic resource allocation, where programs could request further resources after execution began, introduced the problem of deadlocks. Addressing deadlocks had a major impact on the development of operating systems and the structure of databases. To prevent deadlocks, data structures and the order of resource requests were constrained.

Exploring Semaphores

In programming, particularly within Unix systems, semaphores are a fundamental technique for coordinating or synchronizing activities among multiple processes competing for the same operating system resources.

A semaphore is essentially a value stored in a designated location within the operating system's (or kernel's) memory. Each process can check and then modify this value. Depending on the semaphore's current value, a process can either proceed to use the resource or determine that it is already in use and must wait before attempting access again.

Semaphores can be binary (typically 0 or 1) or can hold additional integer values. When a process uses a semaphore, it typically checks the value and, if it is using the resource, changes the value to reflect this. This signals to subsequent semaphore users that they need to wait.

Semaphores are commonly used for two primary purposes:

  • To share a common memory space.
  • To share access to files.

Semaphores are a key technique for Interprocess Communication (IPC). The C programming language provides a comprehensive set of interfaces, often referred to as "functions," for managing semaphores.

Related entries: