Chapter02 Processes and Threads Part1

download Chapter02 Processes and Threads Part1

of 6

Transcript of Chapter02 Processes and Threads Part1

  • 8/19/2019 Chapter02 Processes and Threads Part1

    1/13

    2/17/2016

    1

    Processes and Threads

    Chapter 2

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    Process (Definition)

    A process not only a code but includes current activity

    represented by:

    • program counter value (next instr. To run)

    • Content of processor registers

    • process stack (local variables and returning address in

    case of a call for routines, etc.)

    • data section: (like global variables)

    • A possible heap (for memory dynamically allocated

    during process time)

    Process in memory

    The Process Model (1)

    Figure 2-1. (a) Multiprogramming of four

    programs.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    •multiprogramming system, the CPU switches from process toprocess

    • each with its own flow of control (its own logical program

    counter), and each one running independently of the other

    ones.

    • Of course, only one physical program counter, so when each

    process runs, its logical program counter is loaded into the real

    program counter. When it is finished, the physical program

    counter is saved in the process’ stored logical program counter

    in memory.

  • 8/19/2019 Chapter02 Processes and Threads Part1

    2/13

    2/17/2016

    2

    The Process Model (2)

    Figure 2-1. (a) Multiprogramming of four programs.

    (b) Conceptual model of

    four independent, sequential processes. (c) Only one program is active at once.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    Process Creation

    Four principal events that cause processes to be

    created:

    1. System initialization (next slide).

    2. Execution of a process creation system call by

    a running process.

    3. A user request to create a new process.

    4. Initiation of a batch job.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    Process Creation (2)

    1. System initialization.

    When an operating system is booted, numerous processes are

    created (foreground and background processes)

    • background processes called daemons.

    • ps, task manager can be used.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

  • 8/19/2019 Chapter02 Processes and Threads Part1

    3/13

    2/17/2016

    3

    Process Creation (3)

    2. Execution of a process creation system call by

    a running process.

    • running process issues system calls to create one or more new

    processes to help it do its job.

    • creating new processes useful when the work to be done by

    several related independent interacting processes.

    • Example, if a large amount of data is being fetched over a

    network for subsequent processing, it may be convenient to

    create one process to fetch the data and put them in a shared

    buffer while a second process removes the data items and

    processes them. With a multiprocessor, job can be faster.

    Process Creation (4)

    3. A user request to create a new process.

    • interactive systems, users type or double click, a new process

    created.

    • users may have multiple windows open at once, each running

    some process. Users can select a window and interact with

    the process.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    Process Creation (5)

    4. Initiation of a batch job.• batch systems found on large mainframes.

    • users can submit batch jobs to the system.

    • operating system decides that it has the resources to run

    another job, creates a new process and runs the next job from

    the input queue in it.

    •  in all these cases, a new process is created by having an

    existing process execute a process creation system call, a

    system process invoked from the keyboard or mouse, or a

    batch-manager.

    • UNIX, there is only one system call to create a new process: fork.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

  • 8/19/2019 Chapter02 Processes and Threads Part1

    4/13

    2/17/2016

    4

    Process Creation (6)• UNIX, there is only one system call to create a new process:

    fork.

    • In Windows, in contrast, a single Win32 function call  – 

    CreateProcess. 

    • In both UNIX and Windows systems, after a process is created,

    the parent and child have their own distinct address spaces.

    If either process changes a word in its address space, the

    change is not visible to the other process.

    • In UNIX, the child’s initial address space is a copy of the

    parent’s, but there are definitely two distinct address spaces

    involved.

    • In Windows, the parent’s and child’s address spaces are

    different from the start.Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    Process Termination

    Typical conditions which terminate a process:

    1. Normal exit

    2. Error exit

    3. Fatal error

    4. Killed by another process

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    Process States (1)

    Three states a process may be in:

    1. Running (actually using the CPU at that

    instant).

    2. Ready (runnable; temporarily stopped to let

    another process run).

    3. Blocked (unable to run until some external

    event happens).

    admitted and terminated

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

  • 8/19/2019 Chapter02 Processes and Threads Part1

    5/13

    2/17/2016

    5

    Process States (2)

    Figure 2-2. A process can be in running, blocked, or ready

    state. Transitions between these states are as shown.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    New and Terminated can be considered as process states

    Process States (3)

    Figure 2-3. The lowest layer of a process-structured

    operating system handles interrupts and scheduling. Above

    that layer are sequential processes.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    Implementation of Processes (1)

    Figure 2-4. Some of the fields of a typical process table entry.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

  • 8/19/2019 Chapter02 Processes and Threads Part1

    6/13

    2/17/2016

    6

    Implementation of Processes (2)

    Figure 2-5. Skeleton of what the lowest level of the operating

    system does when an interrupt occurs.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    Modeling Multiprogramming

    Figure 2-6. CPU utilization as a function of the number of

    processes in memory.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    Process Scheduling Queues

    Job queue  – set of all processes in

    the system

    Ready queue  – set of all processes

    residing in main memory, ready and

    waiting to execute

    Device queues  – set of processes

    waiting for an I/O device

    each device has its own queue.

    Processes migrate among the

    various queues Source:Silberschatz et al, Operating SystemConcepts with Java, 8th Ed., 2010

  • 8/19/2019 Chapter02 Processes and Threads Part1

    7/13

    2/17/2016

    7

    The Classical Thread Model (1)

    Figure 2-11. (a) Three processes each with one thread.

    (b) One process with three threads.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    The Classical Thread Model (2)

    Figure 2-12. The first column lists some items shared by all

    threads in a process. The second one lists some items

    private to each thread.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    addition to items of process shared by all threads of same

    process, threads have their own items

    The Classical Thread Model (3)

    Figure 2-13. Each thread has its own

    stack.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    • A thread can be in any one of

    several states: running, blocked,ready, or terminated.

    •  A running thread currently has

    the CPU and is active.

    • a blocked thread is waiting for

    some event to unblock it (eg.,

    read from the keyboard, or waits

    for an event to unblock it)

    • A ready thread (waits for CPU).

  • 8/19/2019 Chapter02 Processes and Threads Part1

    8/13

    2/17/2016

    8

    Implementing Threads

    in User Space

    Figure 2-16. (a) A user-level threads package.

    (b) A threads package managed by the kernel.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    (a) (b)

    Managing Threads

    • Two main places to implement threads: user space and the kernel

    (can be hybrid)

    1- Put the threads package entirely in user space

    •  The kernel manages ordinary, single-threaded processes.

    • Advantages:

    • A user-level threads package can be implemented on an

    operating system that does not support threads

    • Allows each process to have its own customized scheduling

    algorithm

    • With this approach, threads are implemented by a library. The

    threads run on top of a run-time system, which is a collection of

    procedures that manage threads.

    Managing Threads (2)2. Implementing Threads in the Kernel

    • kernel knows about and manage the threads

    • kernel has a thread table (in main memory for fast access) keeps

    track of all the threads in the system.

    • When a thread wants to create a new thread or destroy an

    existing thread, it makes a kernel call to do it and updates the

    kernel thread table

    • All calls that might block a thread are implemented as system

    calls, greater cost than a call to a run-time system procedure

    •  When a thread blocks, the kernel can run either another thread

    from the same process (if one ready) or a thread from a

    different process With user-level threads

    The run-time system keeps running threads from its own process

    until the kernel takes the CPU away from it (or no ready threads

    left to run)

  • 8/19/2019 Chapter02 Processes and Threads Part1

    9/13

    2/17/2016

    9

    Synchronization

    • Concurrent access to shared data may result in data

    inconsistency

    • Maintaining data consistency requires mechanisms

    to ensure the orderly execution of cooperating

    processes

    Race Condition -Example

    • Assume account is 2000

    Process 1 (account += 1000)

    process 2 (account -= 500) ressources

    load (r1, account)

    load (r2, 1000)

    add (r1, r2)

    -------here p1 interupted context switching p2scheduled but p1 did not finsh!!!

    store (r1, account)

    load (r1, account)

    load (r2, 500)

    sub (r1, r2)

    store (r1, account)

    Switch context to p1-- 

    store (r1, account)

    • ResourcesAccount r1 r2

    2000

    2000

    1000

    3000 1000

    0 0

    2000 0

    500

    1500 500

    1500

    3000

    3000

    Race Condition

    • Race condition is when the outcome depends on theparticular order in which instructions execute

    • To guard against race condition, useSYNCHRONIZATION.Synchronize processes/ threads

    • Synchronization is the coordination of the activities of 2or more processes that usually need to execute thefollowing activities:Compete for resources in mutually exclusive manner(i.e., one at a time)Cooperate in sequencing or ordering specific events intheir individual activities

  • 8/19/2019 Chapter02 Processes and Threads Part1

    10/13

    2/17/2016

    10

    Critical Regions (1)

    Requirements to avoid race conditions:

    1. No two processes may be simultaneously inside

    their critical regions.

    2. No assumptions may be made about speeds or the

    number of CPUs.

    3. No process running outside its critical region may

    block other processes.

    4. No process should have to wait forever to enter its

    critical region.

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    Structure of a Typical Process• Another example:

    • P1 and P2 share a global variable x

    • P1: y local

    …. 

    x+=10;

    y=x;

    … 

    • P2: y local

    …. 

    x++;

    y+=x;

    …. 

    Critical sections CS

    In general with n processes, and each process has a special segment ofcode called critical section that updates shared data

    The objective: when a process is executing its CS, no other process canexecute its CS: mutual execution

    Entry section is a barrier blocking processes out when a process is in itsCS, exit releases the barrier blocking

    Solution to Critical-Section

    Problem

    1. Mutual Exclusion - If process Pi is executing in its critical section, then no

    other processes can be executing in their critical sections

    2. Progress - If no process is executing in its critical section and there exist

    some processes that wish to enter their critical section, then the

    selection of the processes that will enter the critical section next cannot

    be postponed indefinitely absence of deadlock

    3. Bounded Waiting - A bound must exist on the number of times that

    other processes are allowed to enter their critical sections after a

    process has made a request to enter its critical section and before that

    request is granted absence of starvation

    Assume that each process executes at a nonzero speed

    BUT No assumption concerning relative speed of the n processes

  • 8/19/2019 Chapter02 Processes and Threads Part1

    11/13

    2/17/2016

    11

    Mutual exclusion with Busy

    Waiting: Hardware solution1. Hardware solution: disable interrupts

    after entering its CS, the process first disable interrupts and just

    before exiting its CS enable interrupts.

    Not attractive solution:

     – Unwise to give user processes this power, suppose it turns off

    interrupts and never turn on them so end of system

     – With multiprocessor systems: disabling interrupts affects only

    the CPU which executes disable instruction (Currently running

    code would execute without preemption)

    processes in other CPUs continue running and can access to

    shared memory.

    Mutual exclusion with Busy Waiting:

    lock variables• One variable used by the processes, let us call it

    lock.

    • When it is 0 means no process is in its CS, if 1means a process is running its CS

    • This still causes race condition:

    Suppose p1 checks lock and found it 0 before settingit to 1, another process is scheduled and checks itfinds 0 so sets it to 1 and when the first process runs

    will set it to 1.

    Mutual exclusion with Busy Waiting:

    Software solution: Strict alternation• The process which gains to enter its CS will give turn to

    the other process once it exits the CS

    Entry  Entry 

    While( turn !=0); While( turn !=1);

    CRITICAL SECTION CRITICAL SECTION

    Exit Exit

    Turn=1; Turn=0;

    Non critical section Non critical section

    ----P0 ------------- - ---P1 -------------

    • Initially turn is integer variable set to 0 keeps track of whose turnto enter the CS and examine and update shared memory

    • Disadvantage: continuous testing (loop) till some value appears(busy waiting) results in CPU waste time especially when waitingtime is long

    • Also, when a process is slower than the other: Starvation problem –rule 3 notsatisfied

  • 8/19/2019 Chapter02 Processes and Threads Part1

    12/13

    2/17/2016

    12

    Mutual Exclusion with Busy Waiting:Peterson’s Solution 

    • Two process solution

    • No strict alternation

    • Combines lock variable, taking turn and warning variables ideas

    • Assume that the LOAD and STORE instructions are atomic; that is, cannotbe interrupted.

    • The two processes share two variables:

     – int turn;

     – Boolean flag[2]

    • The variable turn indicates whose turn it is to enter the critical section.

    • The flag array is used to indicate if a process is ready to enter the criticalsection. flag[i] = true implies that process Pi is ready!

    Tanenbaum & Bo,Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.

    Mutual Exclusion with Busy

    Waiting:Peterson’s Solution 

    The first rule: mutual exclusion is satisfied

    Also properties 2 and 3 are satisfied

    Synchronization Hardware –feasible

    for uni-processors and multi-

    processors• As said uni-processors can disable interrupts to

    solve critical section problem But this does nor

    work with multi-processors

    • Modern machines provide special atomic hardware

    instructions (CPU instructions)• Atomic = non-interruptible

     – Either test memory word and set value

     – Or swap contents of two memory words

    And each of these instructions is a pair of operations taken

    as one uninterruptible unit

  • 8/19/2019 Chapter02 Processes and Threads Part1

    13/13

    2/17/2016

    Semaphore• Synchronization tool that does not require busy waiting

    • Semaphore S  – integer variable

    • Two standard operations modify S: acquire() and release()

     – Originally called P() and V()

    • Less complicated

    • Can only be accessed via two indivisible (atomic) operations