Skip to main content

Operating Systems | Concept of shared memory and critical section

While printing your document opened in Adobe Acrobat Reader, if you gave a same command for document opened in Microsoft Word! How will it be?

These are the real time situations where two processes competing together for same resources due to improper process synchronization.

An unstructured access approval to these resources may lead to a abnormal output or a breach in information security or even it may cause for data loss. In the above case which might be a bunch of over-written papers coming out of the printer connected to that system.




Shared Memory and Critical Section

Resources or memory spaces where different processes have access to read, write or update at same time are known as shared memory.

And the piece of program which tries to access the shared memory is known as Critical Section.
Representation of two processes accessing shared memory or shared resource

There are situations where an operating system need to handle multiple request from critical sections of various processes in a machine, in order to maintain data consistency in resources or shared memories and most importantly for a secure trade-off across various activities under that particular operating system.



  • Let a shares memory location var has to be accessed by two different processes P1 and P2 to increment and decrement respectively at an instant as shown.

At assembly level the code snippets for var+ + for P1 and var- - for P2 will be as follows.

HLL
var++

Assembly
1) MOV  var, WR0
2) INC    WR0
3) MOV  WR0, var
HLL
var- -

Assembly
1) MOV  var, WR1
2) DEC    WR1
3) MOV  WR1, var


In each of these cases

  • First instruction copies the value in var to some register.
  • Second instruction increases(for P1) or decreases(for P2) the value in the register.
  • Third one updates the var with new value in the register

Let P1.3 represent the third instruction of first process and see how various cases where an operating system with preemptive scheduling algorithm performs on var

Here final value of var is 5 or 4 or 6 depending on order of sequence of execution.

Getting an inconsistent or multiple final result on applying same operations is not a reliable result.

Such a situation where order of execution of instruction determines, the final result is called race condition in operating system.

The only way to resolve race condition is denial of other processes from accessing critical shared data the until completion of execution of current process over this shared memory.

In order to solve the data inconsistency with an efficient and to control the race which determines this inconsistent final result there are various methods of process synchronization will be discussed in upcoming articles.

Comments

Popular posts from this blog

Operating Systems | Scheduling Algorithms : Round Robin

| FCFS | SJF | SRTS | Round Robin | LJF | Priority Scheduling | HRRN | Round RobinFeatures : Most popular algorithm of allPractically implementableImplementable with basic data structures like queueExtremely lesser starvationOptimum efficiency can be set by controlling time quantumThe round robin algorithm will continuously switch between processes if a process in CPU (under execution) exceeds a time limit set by OS called time quantum.Flow Chart :Scheduler allocated process to for execution.CPU starts monitoring the execution time right after from allocation.If the process completes its execution before exceeding time quantum, OS forwards the process to termination state.else, the processes gets preempted once the time quantum limit exceeded and if the process finished at this moment, OS moves the process to termination state, else it moves to ready queue and iterates over the whole process listed above.Example : Consider the following table of processes and calculate complet…

Operating Systems | Concept of Process

Hard disk drive of the system in called primary memory and secondary memory is Random Access Memory(RAM). Either a program written in High Level Language(HLL) or a executable code generated by the sequence of works done by pre-processor, compiler and assembler, resides in secondary memory of the system. To start an execution, operating system allocates some space in the main memory, for the program to be executed and loads the program in the secondary memory to the allocated space. The piece of work which is loaded by the operating system to the main memory in order to execute program is called processEvery program loaded by operating system will create a focus boundary(or process body), a partitioned memory area where all memory requirements for the execution of program is satisfied.Variable which will not change its value through out life time of process is called static variable and variables which are globally accessible in a process known as global variable.Heap area is reserved f…

Operating Systems | Scheduling Algorithms : SJF

| FCFS | SJF | SRTS | Round Robin | LJF | Priority Scheduling | HRRN | Shortest Job First(SJF)SJF algorithm schedules the shortest job(low burst time) available on main memory at the time of context switching in ready state . It can be either preemptive or non-preemptive depending on the nature of scheduler.Since the SJF select the job which is shorted among available jobs on main memory, Non-preemptive SJF algorithm will have larger waiting time if a job with longer burst time get scheduled first. In short SJF is also vulnerable to Convoy EffectTo find shortest process among min-heap would be the best data structure to be usedExample : Consider the following table of processes and calculate completion time, turn around time and waiting time using SJF algorithm with the following assumptions. No preemption.No I/O request will be there from any process.Arrival time is relative to CPU on time.Process NumberATBT103222324442View Answer

The execution of processes can be visualized …