15
ACKNOWLEDGEMENT We have no words to express our deepest gratitude to ALLAH who blessed and enabled us to complete this project. Its great privilege to mention the feelings of obligations towards our affectionate parents and department, who prayed, encouraged and inspired us for higher education and are supporting financially and morally throughout our studies. We find it very difficult to select the appropriate words to express our gratitude to our teacher Prof. Umar and his useful encouragement and dynamic supervision. Course Instructor: -----------------------------------

Operating System Process Synchronization

Embed Size (px)

Citation preview

Page 1: Operating System Process Synchronization

ACKNOWLEDGEMENT

We have no words to express our deepest gratitude to ALLAH who

blessed and enabled us to complete this project. Its great privilege to

mention the feelings of obligations towards our affectionate parents

and department, who prayed, encouraged and inspired us for higher

education and are supporting financially and morally throughout our

studies.

We find it very difficult to select the appropriate words to express our

gratitude to our teacher Prof. Umar and his useful encouragement and

dynamic supervision.

Course Instructor:

-----------------------------------

Page 2: Operating System Process Synchronization

DECLARATION

We hereby declare that this project report entitled “Process Synchronization” is written by us

and is our own effort and that no part has been copied and taken from any other source.

Submitted by Signature with Date

Haziq Naeem (107) ----------------------------------------

Muhammad Adeel (83) ----------------------------------------

Page 3: Operating System Process Synchronization

TABLE OF CONTENTS

Sr# Contents Page#

1 Introduction 1

2 Synchronization 2

2.1 Critical Section Problem 2

2.2 Locks 4

2.3 Petersons Solution 5

2.4 Semaphores 5

2.5 Major Problems of Synchronization 6

3 Windows vs Linux Synchronization 8

4 Activity Chart 9

5 Conclusion 9

6 References 9

Page 4: Operating System Process Synchronization

FIGURES TABLE

Fig# Contents Page#

1 Synchronization 1

2 Synchronization 2

3 Critical Section Problem 3

4 Locks 4

5 Petersons Solution 5

6 Major Problems of Synchronization 7

7 Major Problems of Synchronization 7

8 Major Problems of Synchronization 8

9 Activity Chart 9

Page 5: Operating System Process Synchronization

1. Introduction

Synchronization is the mutual understanding between two processes or more.

In simple words synchronization means sharing system resources. i.e. Track of a train, they are

mutually synchronized with each other.

2. Synchronization

Access to two or more processes to share data can result in data inconsistency. for this a

mechanism is used called “Synchronization”.

Synchronization is the mutual understanding between two processes or more.

In simple words synchronization means sharing system resources. i.e. Track of a train, they are

mutually synchronized with each other.

Page 6: Operating System Process Synchronization

(Figure 1)

Example:

Synchronizing Traffic

In a one way tunnel the tunnel can only be used when no oncoming traffic is present and when

the traffic is already flowing in the right way.

Page 7: Operating System Process Synchronization

(Figure 2)

Problems:

Deadlock: Two or more than two process waiting for an event

Starvation: Indefinite blocking. Process that required resource which is never

allocated.

2.1. Critical Section Problem

Only one process executed at a time in critical section. Only one process is allowed to start or

execute in its critical section. So every process 1st request permission to entering in critica l

section.

Three section involved in critical section:

i. Entering Section

ii. Exit Section

iii. Remainder Section

If one process started working, other processes will have to wait until first will finished.

(Figure 3)

We discuss the critical section problem now we will discuss the solution of critical section

problem.

Solutions of Critical Section Problem:

Page 8: Operating System Process Synchronization

Three kinds of solutions involved in critical section problem

i. Mutual Exclusion

ii. Progress

iii. Bounded Waiting

i. Mutual Exclusion

One processes execute no other process will interfere in it.

For example, if a process named A1 and other Process named A2. If A1 executes in its critica l

section, then A2 not allowed to interfere in it.

ii. Progress

If no process executed in its critical section, then those process only requested to execute in

its critical section which are in remainder section and the rest are placed in queue.

iii. Bounded Waiting

When a process makes request or a process request granted to enter in a critical section then a

limit or bound exists on the number of times that the other processes are allowed to enter its

critical section.

For example, a process AB requested to enter in its critical section bounded waiting restricted

that this process enter only two times in its critical section after request made and before request

granted to its critical section

Another example of bounded waiting is the limits of downloading on websites

2.2. Locks

Lock means to prevent someone to do something. Any solution to critical section problem

requires some form of lock.

Acquire: Before entering its critical section

Release: After exits the critical section

A software tool is built for this known as “Mutex Lock”. Also known as spin lock.

Page 9: Operating System Process Synchronization

(Figure 4)

Synchronization Hardware:

Many systems provide the support of hardware for critical section.

Uniprocessors can disable interrupts:

System clock kept updated by interrupts

Modern machines are non-interrupt able:

Allow us to test and modify content of a word or to swap contents of two words.

2.3. Peterson’s Solution

It is a software based solution to critical section problem.

Provides good algorithm description of solving.

Describe the complexities in designing software (three critical section solutions).

Could not be work on modern non-interrupt able machines.

The solution is for two processes. Load and store instructions are non-interrupt able.

Page 10: Operating System Process Synchronization

(Figure 5)

2.4. Semaphores

Semaphores is a variable that is used to manage or control access to a common resource by

multiple processes in a similar or concurrent system.

It is basically a synchronization tool. Two operations are involved in it. Wait and Signal

represented as P() and V().

Wait: increasing the value of its arguments as soon as it will become non minus

Signal: decreasing the value of its arguments as an individual operation

Properties:

It is very simple

Works with many processes

Each critical section has different semaphores

Can have same critical section with same semaphores

Types of semaphores:

i. Counting Semaphores

ii. Binary Semaphores

i. Binary Semaphores

It is a type of semaphores used to implement bounded concurrency

The value of binary semaphores can only range from 0 to 1.

ii. Counting Semaphores

A type of semaphores which is also known as mutex. It is used to implement mutual exclus ion

value of counting semaphores over an unrestricted range.

For example Bank account

2.5. Major Problems of Synchronization

There are three major problems of synchronization

Page 11: Operating System Process Synchronization

i. Bound Buffer Problems

ii. Reader-Writer Problem

iii. Dinning Philosophers Problem

i. Bound Buffer Problem

In bound buffer there are two concepts involved. Producer and consumer

This problem explains these two processes which share common buffer used as a queue.

Producer produce the data take it into the buffer and again started Whereas Consumer delete

or remove data one by one If buffer is full then more data can’t be stored and consumer not

able to remove data from an empty buffer.

The solution for producer is to discard data or go to sleep if the buffer is full. If consumer

removes something into the buffer, then notifies the producer will start again to fill the buffer.

Now for consumer, if consumer finds buffer empty then it can go to sleep. When producer puts

any data in buffer the consumer awakes.

The solution of this problem is semaphores and improper solution will result as a deadlock.

ii. Readers-Writers Problem

Readers: Don’t perform any updates only read the data

Writers: Performing both reading and writing

Problem is that multiple readers to read the data but only one writer can access data at that

time.

iii. Dining Philosophers Problems

Invented by E.W. Dijkstra.

Problem:

Imagine there are five philosophers who only eat and think. They are sitting on a circular table

to eat spaghetti and have only five chopsticks to eat. Each philosopher thins and when he felt

hungry he picks up chopsticks and eat spaghetti, after finish eating he puts down the chopsticks

and starts to think again.

Page 12: Operating System Process Synchronization

(Figure 6)

Analysis of problem:

(Figure 7)

Solution:

Page 13: Operating System Process Synchronization

(Figure 8)

3. Windows vs Linux Synchronization

Windows Linux

Uses interrupts to protect access to

global resources on unipolar systems

Disables interrupts to implement

short critical sections

Provides dispatcher objects which

may act as semaphores

Provides semaphores

Uses spin locks on multiprocessor

systems

Provides spin locks

4. Activity Chart

Page 14: Operating System Process Synchronization

(Figure 9)

5. Conclusion

Synchronization is a mechanism for the mutual understanding between two or more processes.

It Includes critical section problems, locks, Peterson’s solution, semaphores and major

problems of synchronization.

As very thing has positive and negative sights, synchronization has more positive ones.

It is a major technique / mechanism used in the operating systems. It gives access to prevent

from deadlocks and starvation, due to this mechanism the data integrity and consistency

increases, it collaborates data and improve the efficiency of the work.

6. References

Critical Section:

http://web.stanford.edu/class/cs140/cgi-bin/lecture.php?topic=locks

https://cseweb.ucsd.edu/classes/fa05/cse120/lectures/120- l5.pdf

Semaphores:

https://en.wikipedia.org/wiki/Semaphore_(programming)

http://www.personal.kent.edu/~rmuhamma/OpSystems/Myos/semaphore.htm

https://en.wikibooks.org/wiki/Operating_System_Design/Processes/Semaphores

Dining Philosophers Problems:

https://www.cs.mtu.edu/~shene/NSF-3/e-Book/MUTEX/TM-example-philos-1.html

Locks:

http://cse.stfx.ca/~mlin/cs375/lectures/syn3.ppt

Imp of Sync:

http://users.csc.calpoly.edu/~fkurfess/Courses/COMP-346/W00/Slides/05-ProcSync.ppt

Page 15: Operating System Process Synchronization

=======================