lec3_processes.pptx

Embed Size (px)

Citation preview

  • 7/26/2019 lec3_processes.pptx

    1/46

    Operating SystemsECE344

    Ding Yuan

    Lecture 3: Processes

  • 7/26/2019 lec3_processes.pptx

    2/46

    Ding Yuan, ECE344 OperatingSystem

    2

    Processes

    This lecture starts a class segment that coversprocesses, threads, and synchroniation

    These topics are perhaps the most important in this class

    You can rest assured that they !ill "e covered in the e#ams

    Today$s topics are processes and process management

    %hat are the units o& e#ecution'

    (o! are those units o& e#ecution represented in the OS'

    %hat are the possi"le e#ecution states o& a process'

    (o! does a process move &rom one state to another'

  • 7/26/2019 lec3_processes.pptx

    3/46

    )sers, Programs

    )sers have accounts on the system

    )sers launch programs

    *any users may launch the same program

    One user may launch many instances o& thesame program

    Then !hat is a process'

    Ding Yuan, ECE344 OperatingSystem

    3

  • 7/26/2019 lec3_processes.pptx

    4/46

    Ding Yuan, ECE344 OperatingSystem

    4

    The Process

    The process is the OS a"straction &or e#ecution

    +t is the unit o& e#ecution

    +t is the unit o& scheduling

    +t is the dynamic e#ecution conte#t o& a program

    process is sometimes called a-o"or a tas.ora se/uential process

    0eal li&e analogy'

  • 7/26/2019 lec3_processes.pptx

    5/46

    nalogy1 ro"ot ta.ingECE344

    Program1 steps &or attending the lecture

    Step 1 !al. to 5

    Step 21 6nd a seat

    Step 31 listen 7or sleep8

    Process1 attending the lecture

    ction

    You are all in the middle o& a process

    Ding Yuan, ECE344 OperatingSystem

    9

  • 7/26/2019 lec3_processes.pptx

    6/46

    *acOS e#ample1 ctivity monitor

    Ding Yuan, ECE344 OperatingSystem

    :

  • 7/26/2019 lec3_processes.pptx

    7/46

    ;inu# e#ample1 ps

    Ding Yuan, ECE344 OperatingSystem

    1 STTcolumn indicates e#ecution state

  • 7/26/2019 lec3_processes.pptx

    10/46

    Buestions

    %hat state do you thin. a process is in mosto& the time'

    or a uniprocessor machine, ho! manyprocesses can "e in running state'

    ene6t o& multicore'

    Ding Yuan, ECE344 OperatingSystem

    5

  • 7/26/2019 lec3_processes.pptx

    11/46

    Ding Yuan, ECE344 OperatingSystem

    Process State raph

    Fe! 0eady

    0unning

    %aiting

    Terminated

    CreateProcess

    ProcessE#it

    +AO, Pageault,etc@

    +AO Done

    ScheduleProcess

    )nscheduleProcess

  • 7/26/2019 lec3_processes.pptx

    12/46

    Ding Yuan, ECE344 OperatingSystem

    2

    Process Components

    Process State

    ne!, ready, running, !aiting, terminatedG

    Program Counter

    the address o& the ne#t instruction to "e e#ecuted &orthis processG

    CP) 0egisters

    inde# registers, stac. pointers, general purposeregistersG

    CP) Scheduling +n&ormation

    process priorityG

  • 7/26/2019 lec3_processes.pptx

    13/46

    Ding Yuan, ECE344 OperatingSystem

    3

    Process Components7cont@8

    *emory *anagement +n&ormation

    "aseAlimit in&ormation, virtualHphysical mapping, etc

    ccounting +n&ormation time limits, process num"erG o!ner

    +AO Status +n&ormation

    list o& +AO devices allocated to the processG

    n ddress Space

    memory space visi"le to one process

  • 7/26/2019 lec3_processes.pptx

    14/46

    Fo! ho! a"out this'

    Fo! simultaneously start t!o instances o& this

    program *yval 9

    *yval :

    %hat !ill the outputs "e'

    Ding Yuan, ECE344 OperatingSystem

    4

    intmyval;intmain(int argc, char *argv[]){ myval = atoi(argv[1]);while (1)

    printf(myval is %, loc !"%l"#n$, myval, (long) myval);&

  • 7/26/2019 lec3_processes.pptx

    15/46

    Ding Yuan, ECE344 OperatingSystem

    9

  • 7/26/2019 lec3_processes.pptx

    16/46

    +nstances o& Programs

    The address !as al!ays the same

    ut the values !ere diIerent

    +mplications'

    The programs aren$t seeing each other ut they thin. they$re using the same address

    Conclusions

    addresses are not the =physical memory>

    (o!'

    *emory mapping

    %hat is the "ene6t'Ding Yuan, ECE344 Operating

    System:

  • 7/26/2019 lec3_processes.pptx

    17/46

    Ding Yuan, ECE344 OperatingSystem

    into the process$ address space

    +nitialies hard!are conte#t and args &or the ne! program

    Places the PC onto the ready /ueue

    Fote1 +t does notcreate a ne! process

    %hat does it mean &or e#ec to return'

    %hat does it mean &or e#ec to return !ith an error'

  • 7/26/2019 lec3_processes.pptx

    39/46

    Ding Yuan, ECE344 OperatingSystem3

    Process Creation1 )ni#738

    &or.78 is used to create a ne! process, e#ec is used toload a program into the address space

    %hy does %indo!s have CreateProcess !hile )ni# uses&or.Ae#ec'

    Comparing &or.78 and CreateProcess78' %hich is more convenient to use'

    %hich is more e]cient'

    %hat happens i& you run =e#ec csh> in your shell'

    %hat happens i& you run =e#ec ls> in your shell' Try it@

    &or.78 can return an error@ %hy might this happen'

    Cannot create child process 7return to parent8@

  • 7/26/2019 lec3_processes.pptx

    40/46

    Ding Yuan, ECE344 OperatingSystem45

    Process Termination

    ll good processes must come to an end@ ut ho!'

    )ni#1 e#it7int status8,%indo!s1 E#itProcess7int status8

    Essentially, &ree resources and terminate

    Terminate all threads 7ne#t lecture8

    Close open 6les, net!or. connections

    llocated memory 7and * pages out on dis.8

    0emove PC &rom .ernel data structures, delete

    Fote that a process does not needto clean up itsel&

    %hy does the OS have to do it'

  • 7/26/2019 lec3_processes.pptx

    41/46

    Physical memory70*8

    CopyOn%rite

    Lazycopy

    Ding Yuan, ECE344 OperatingSystem4

    c [ 35

    " [ 25

    a [ 5

    c [ 35

    " [ 25

    a [ 5

    5 25 35

    Proc@ $s address space Proc@ $s address space

    &or.78

  • 7/26/2019 lec3_processes.pptx

    42/46

    Physical memory70*8

    CopyOn%rite

    Lazycopy

    Ding Yuan, ECE344 OperatingSystem42

    c [ 35

    " [ 25

    a [ 5

    c [ 45

    " [ 25

    a [ 5

    5 25 35

    Proc@ $s address space Proc@ $s address space

    &or.78

    45

  • 7/26/2019 lec3_processes.pptx

    43/46

    Process Termination

    %hen e#it78 is called on )ni#1

    Threads are terminated 7ne#t lec@8

    Open 6les, net!or. connections are closed

    ddress space is deallocated ut the PC still remains in the Process Ta"le

    Only a parent can remove the PC

    Thus completely terminate the process 7called

    reap8

    Died "ut not yet reaped process is called aom"ie

    Ding Yuan, ECE344 OperatingSystem43

  • 7/26/2019 lec3_processes.pptx

    44/46

    Ding Yuan, ECE344 OperatingSystem44

    !ait78 a second\

    O&ten it is convenient to pause until a child processhas 6nished

    Thin. o& e#ecuting commands in a shell

    )se !ait787%aitorSingleO"-ect8

    Suspends the current process until a child process ends

    !aitpid78 suspends until the speci6ed child process ends

    )ni#1 Every process must "e reaped "y a parent

    %hat happens i& a parent process e#its "e&ore a child'

    %hat do you thin. a =om"ie> process is'

  • 7/26/2019 lec3_processes.pptx

    45/46

    Ding Yuan, ECE344 OperatingSystem49

    )ni# Shells

    while (1) {char *cm = rea.comman();int chil.pi = for-();if (chil.pi == !) {

    Manipulate STDIN/!T/"## $le descriptors %or pipes&redirection& etc'

    e"ec(cm);panic(e"ec faile$);

    & else {waitpi(chil.pi);

    &&

  • 7/26/2019 lec3_processes.pptx

    46/46

    Ding Yuan, ECE344 Operating

    Process Summary

    %hat are the units o& e#ecution' Processes

    (o! are those units o& e#ecution represented' Process Control loc.s 7PCs8

    (o! is !or. scheduled in the CP)' Process states, process /ueues, conte#t s!itches

    %hat are the possi"le e#ecution states o& a process' 0unning, ready, !aiting

    (o! does a process move &rom one state to another' Scheduling, +AO, creation, termination

    (o! are processes created' CreateProcess 7%indo!s8, &or.Ae#ec 7)ni#8