The document discusses concurrency, critical regions, and mutual exclusion in operating systems, highlighting issues like race conditions, deadlocks, and resource starvation. It explains mechanisms for process synchronization, including semaphores, message passing, and various mutual exclusion techniques such as Peterson’s solution and the producer-consumer problem. Additionally, it covers inter-process communication and specific problems like the dining philosophers problem, emphasizing the importance of managing shared resources effectively.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
9 views34 pages
Unit 3 and Unit 4
The document discusses concurrency, critical regions, and mutual exclusion in operating systems, highlighting issues like race conditions, deadlocks, and resource starvation. It explains mechanisms for process synchronization, including semaphores, message passing, and various mutual exclusion techniques such as Peterson’s solution and the producer-consumer problem. Additionally, it covers inter-process communication and specific problems like the dining philosophers problem, emphasizing the importance of managing shared resources effectively.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34
Concurrency
Concurrency is the execution of the multiple
instruction sequences at the same time. It happens in the operating system when there are several process running in parallel. The running process always communicate with each other through shared memory or message passing. Concurrency results in sharing of resources result in problems like deadlocks and resources starvation. Race Condition • Improper order of execution of processes may occur Race Condition. • Example :- Process A and B both access the shared memory and tries to change the value of variable. Critical Region & Mutual Exclusion • When a process is accessing a shared variable, the process is said to be in the a critical region.
• No two processes can be in the same critical
region at the same time. This is called Mutual Exclusion. Semaphores Semaphores are integer variables that are used to solve the critical section problem by using two atomic operations, wait and signal that are used for process synchronization. Semaphore is simply a variable which is non-negative. It is used to achieve process synchronization. It helps in achieving Mutual Exclusion. Semaphores Semaphores are of two types: Binary Semaphore – This is also known as mutex lock. It can have only two values – 0 and 1. Its value is initialized to 1. It is used to implement the solution of critical section problem with multiple processes.
Counting Semaphore – Its value can range
over an unrestricted domain. It is used to control access to a resource that has multiple instances. Pipes Pipe is a connection between two processes, such that the standard output from one process becomes the standard input of the other process. Shared Memory • Communication between processes using shared memory requires processes to share some variable Message passing Message passing allows multiple processes to read and write data to the message queue without being connected to each other. Messages are stored on the queue. Message queues are quite useful for interprocess communication and are used by most operating systems. Two primitives are used in message passing SEND and RECEIVE. Signals A signal is a software generated interrupt that is sent to a process. There are fix set of signals that can be sent to a process. signal are identified by integers. SIGABRT Process abort signal. SIGALRM Alarm clock. SIGFPE Erroneous arithmetic operation. SIGHUP Hangup. SIGILL Illegal instruction. SIGINT Terminal interrupt signal. Inter Process Communication Inter Process Communication (IPC) is a mechanism which allows processes to communicate with each other and synchronize their actions. The communication between these processes can be seen as a method of co- operation between them. Processes can communicate with each other through both :- shared memory and message passing. Inter Process Communication Mechanisms of Mutual Exclusion • Disabling Interrupt :- Perhaps the most obvious way of achieving mutual exclusion is to allow a process to disable interrupts before it enters its critical section and then enable interrupts after it leaves its critical section. By disabling interrupts the CPU will be unable to switch processes. Mechanisms of Mutual Exclusion • Disadvantages :- • Not recommended to give powers to user process to turn on and off the interrupt. Process may never turn off the interrupt. • Not recommended for multiprocessor system having more than two processors. Disabling interrupts affects only the CPU that executed disable instruction. Other CPUs will continue accessing the shared object. Mechanisms of Mutual Exclusion • Shared Lock Variable :- A simple variable having value 0 or 1. Before entering into the critical region a process checks the value of variable. If the value of variable is 0 then a process is allowed to enter the critical region by setting the value of variable to 1. When a process is completed then it will set the value of variable to 0 and leaves the critical region. Mechanisms of Mutual Exclusion • Disadvantage :- • When P1 finds lock variable = 0 then it tries to set lock variable = 1. But before setting value to 1 context switch occurs. So now P2 finds lock variable = 0 then it sets value to 1 and enters into the critical region. Meanwhile P1 comes back. P1 does not read the lock variable value again. It will read the previously stored value of lock variable i.e 0. Hence P1 also enters into the critical region. Now two processes P1 and P2 are in critical region which violates the rule of Mutual Exclusion. Mechanisms of Mutual Exclusion • Test and Set Lock (TSL) :- Test and Set Lock (TSL) is a synchronization mechanism. • It uses a test and set instruction to provide the synchronization among the processes executing concurrently. • If one process is currently executing a test-and- set, no other process is allowed to begin another test-and-set until the first process test-and-set is finished. Mechanisms of Mutual Exclusion • Disadvantage : • If one process is currently executing a test- and-set, no other process is allowed to begin another test-and-set until the first process test-and-set is finished. Lower priority process is executing in critical region but higher priority process wants to be in the critical region so higher priority processes will suffer. Mechanisms of Mutual Exclusion Turn Variable or Strict Alteration :- Turn Variable or Strict Alternation Approach is the software mechanism implemented at user mode. It is a busy waiting solution which can be implemented only for two processes. In this approach, A turn variable is used which is actually a lock. This approach can only be used for only two processes. Mechanisms of Mutual Exclusion • Disadvantage :- • When turn variable is 0, process P1 enters into the critical region. When process P1 exits critical region the turn variable is 1 and process P2 will get chance to enter into critical region. When P2 exits critical region then turn variable is 0 and now process P1 will again get a chance to enter into the critical region. But P1 does not want to enter into the critical region at this moment. On the otherside process P2 wants to enter into the critical region but cannot do so because turn variable is 0. Mechanisms of Mutual Exclusion • Peterson’s Solution :- Use to allow process enter into the critical region turn by turn. #define FALSE 0 #define TRUE 1 #define N 2 //no of process int turn; //whose turn is ? int interested[N]; void enter_region(int process) { int other; //no of other process other = 1-process; interested [process] = TRUE; //process is interested turn = process; //set flag; while (turn == process && interested [other] == TRUE);// wait } void leave_region (int process) { interested [process] = FALSE; }// process leaves critical region #define FALSE 0 #define TRUE 1 #define N 2 //no of process int turn; //whose turn is ? int interested[N];
void enter_region(int process)
{ int other; //no of other process Other = 1-process; interested [process] = TRUE; //process is interested turn = process; //set flag; while (turn == process && interested [other] == TRUE);// wait } void leave_region (int process) { interested [process] = FALSE; // process leaves critical region } Sleep and Wakeup Call • In Interprocess Communication (IPC) two system call are used namely Sleep and Wakeup. • Sleep :- If any process tries to enter into the critical region but not able to enter it then it goes to sleep state. • Wakeup :- When a process exits from critical region exits it generates wakeup call for the process who shows interest to enter into the critical region. Producer Consumer Problem • Producer Consumer problem is also known as bounded buffer problem. Two processes share a common buffer. • Producer :- Puts information in the buffer • Consumer :- Takes information out of the buffer Producer Consumer Problem • Share buffer has 3 possibilities :-
• 1. Buffer is Full
• 2. Buffer is Empty
• 3. Buffer is partially Full/Empty
Producer Consumer Problem • 1. Buffer is Full :- • Producer will go to sleep state • Consumer will go to wakeup state • 2. Buffer is Empty :- • Producer will go to wakeup state • Consumer will go to sleep state • 3. Buffer is Full :- • Producer will go to wakeup state • Consumer will go to wakeup state Producer Consumer Problem Solution to Producer :- Producer will go the sleep when buffer is full. When consumer removes data from the buffer it notifies Wakeup call to the producer.
Solution to Consumer :- Consumer will go to sleep
when buffer is empty. When Producer adds data into the buffer it notifies Wakeup call to the consumer. Producer Consumer Problem using Sleep and Wakeup Producer Consumer Problem using Semaphore A semaphore is a variable which is used to control the access of shared multiple processes.
Two functions are used :
insert_item remove_item • Properties to be followed for insert_item and remove_item 1. Mutual exclusive access to buffer :- Only one the function should execute at a time 2. No buffer overflow 3. No buffer underflow 4. No busy waiting void produce (void) { int item; while (true) { item = produce_item(item); insert_item(item); } } void consume (void) { int item; while (true) { item = remove_item(item); remove_item(item); } } Reader Writer Problem • If one process is reading a data other processes can read data into the memory by maintaining the readers count. If one process is writing a data no other process can read or write into the memory. Dining Philosopher problem • The dining philosophers problem states that there are 5 philosophers sharing a circular table and they eat and think alternatively. There is a bowl of rice for each of the philosophers and 5 chopsticks. A philosopher needs both their right and left chopstick to eat. A hungry philosopher may only eat if there are both chopsticks available. Otherwise a philosopher puts down their chopstick and begin thinking again. • Either the philosopher can be in thinking, hungry or eating mode. Solution is when any philosopher starts eating the chopstick is locked and neighbor philosopher cant access it.