Docsity

Thread Synchronization and Communication in Operating Systems, Study notes of Electrical and Electronics Engineering

Toggle sidebar
bg1
Introduction to Processes and Threads
Readings: Tanenbaum, 2.1-2.2.4.
Sample questions that this topic addresses:
What is a process and why is it a useful concept?
What is a thread and why is it useful?
What is the difference between a process and a thread? Why two
concepts?
Uniprogramming and multiprogramming
Process state versus thread state
Examples from OSes
Keeping track of processes and threads
Running multiple threads on a single CPU
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52

Partial preview of the text

Download Thread Synchronization and Communication in Operating Systems and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Introduction to Processes and Threads

Readings:

Tanenbaum, 2.1-2.2.4.

Sample questions that this topic addresses: What is a process and why is it a useful concept?What is a thread and why is it useful?What is the difference between a process and a thread? Why twoconcepts?Uniprogramming and multiprogrammingProcess state versus thread stateExamples from OSesKeeping track of processes and threadsRunning multiple threads on a single CPU

Processes We want to run multiple jobs at the same time on a system. Historically, itwas to make better use of the CPU.^ With many things happening at once in a system, need some way ofseparating them all out cleanly so that they are protected from eachother and appear to be independent activities. That is a

process

Important concept: decomposition. Given hard problem, chopit up into several simpler problems that can be solvedseparately.

What is a process?

An intuitive definition is just a running piece of code alongwith all the things that the code can affect or be affected by.Key aspect of a process: Processes protected from each other.Each process is given the illusion that it has protected state.What constitutes process state?

Is a process the same as a program?

^ Thread status:

o^ Show a state diagram showing the state transitions betweendifferent status of a thread.

-^ Created, Runnable, Running, Blocked, Done(Zombie), Done

^ Thread State kept in

thread control block

o^ Some state shared by all threads in a process: global variables,address space, open file handles.

o^ Some state "private" to each thread each thread has its owncopy: program counter, registers, execution stack (What is theexecution stack?). o^ Note that there is no enforced protection between threads in thesame process. A poorly-coded thread can overwrite the stack ofanother thread, for example, trashing it.

Example OSes ^ MS/DOS, MacOS: 1 address space, one thread per address space ^ Traditional Unix: multiple address spaces, one thread/address space ^ Mach, Solaris, VMS, HPUX, NT: multiple address spaces, multiplethreads per address space

Deterministic: input state alone determines results.Reproducible.Can stop and restart with no bad effects (only time varies). Example: aprogram that sums the integers from 1 to 100. Cooperating threads/processes: Machine must model the social structures of the people that use it. Peoplecooperate, so machine must support that cooperation. Cooperation meansshared state, e.g. a single file system shared among multiple userprocesses.Cooperating threads are those that communicate via share state.

Problem with cooperating threads:^ •

Results may be non-deterministic • They may not be easy to reproduce. Debugging can be difficult

Example:^ •

one process writes ABC'' to the terminal, • another process writesCBA'' to the same terminal.

What are the potential outputs on the terminal? Are these cooperatingprocesses? How?When discussing concurrent threads, a single CPU vs. multiple CPU doesnot matter. Both lead to equally unpredictable results in the aboveexample, assuming that, with a single CPU, the context-switch amongthreads can occur at any time.^ •

A given thread runs on only one processor at a time.

If the results of execution depend on the order of execution of threads, wehave a

race condition.

Don't know what will happen; depends on which one goes fastest. What ifthey happen at EXACTLY the same time, with multiple CPUs?

Can't tell

anything without more information. Could end up with A=3! Atomic operations: Before we can say ANYTHING about parallel threads, we must know thatsome operation is atomic, i.e. that it either happens in its entirety withoutinterruption, or not at all. Cannot be interrupted in the middle. Key point

: If you don't have an atomic operation, you can't make one.

Fortunately, the hardware guys give us atomic ops.

References and assignments are atomic in almost all systems. A=B willalways get a good value for B, will always set a good value for A (but notnecessarily true if A and B are arrays or records). Example

Suppose,

initially

A^ =

2,^

B^ =^

Thread

A^ =^

B;

if^ (A

==^

B)^ cout

<<^

“they

are equal”;

else “they

are not equal”;

Thread

B^ =^

A+1;

Can^ you^ predict

what

will

be^ printed

by^ Thread

Can^ you^ state

possible

final

values

of^ A^ and

B?

if^ (savings

>^ 100)

savings

=^ savings

-^ 100;

Thread

if^ (savings

>^ 100)

savings

=^ savings

-^ 100;

Is it possible that both people end up withdrawing $100? Is bankguaranteed to detect the problem? Example

3:^

Computerized

milk

buying:

Too much

milk

problem

Anne

Bob

//^ look

in^ fridge if^ (no

milk)// go^ to^ Kroger Buy^ Milk Put^ milk in^ fridge

//^ look

in^ fridge if^ (no

milk)// go^ to^ Kroger Buy^ Milk Put^ milk in^ fridge

The^ following

execution

is^ possible:

What is needed to address the above problems? If you have any atomic operation, we need to be able generate

higher-level

atomic operations

and make parallel programs work correctly. This is the

approach we'll take in this class. Example

2 revisited:

from entering the critical section). Thus, only 1 thread accessesand updates the savings variable at a time. • vacation property:

If a thread is outside the critical section

(i.e., on vacation), other threads should not be prevented fromgoing into the critical section. • no speed assumption:

No assumptions should be made about

relative speeds of threads or the number of CPUs. • A thread

should not have to have to wait forever to enter the

critical section.

In the banking example, we want the two lines of code in the threads toform a critical section. Solution to the critical section problem: Locks A^ lock

-^ prevent someone from doing something,e.g., before shopping, leave a note on the fridge

Three elements of locking

0.^ must aqcuire lock before doing something (e.g. entering criticalsection, accessing shared data0.^ must release lock (unlock) when done0.^ must wait to acquire if someone else has it locked

Example

3 revisited: In general, wherever shared variables are manipulated, you need to worryabout using locks to make a set of statements atomic (i.e., turn them into acritical section) How to implement locks?:

Does this work in the computerized world?At this point, before reading on, you should try to improve the solutionand make it correct.

Some possible solutions that you may come up with: Too-much-milk:

Solution

Attempt

What if we use two notes and make Anne and Bob hang around to makesure they enter the critical section?

Ar e C

Anne ritical section requirements satisfied?

Bob

NoteAnne

=^ true; while(NoteBob)

loop;

if^ (no

milk)Buy Milk; NoteAnne

=^ false;

NoteBob

=^ true; while

(NoteAnne)loop; if^ (no

milk)Buy Milk; NoteBob

=^ false;