35
Operating Systems II. Processes Ludovic Apvrille Telecom ParisTech Eurecom, Office 223 [email protected] Fall 2011

Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

Operating Systems

II. Processes

Ludovic Apvrille

Telecom ParisTechEurecom, Office 223

[email protected]

Fall 2011

Page 2: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Definitions and basic conceptsProcess management by OSIssues

Outline

ConceptsDefinitions and basic conceptsProcess management by OSIssues

Boot sequence of an OS

Managing processes

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 2 of 35

Page 3: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Definitions and basic conceptsProcess management by OSIssues

Process Definition

Programs and processes

I One execution of a sequential program = one process

I Two executions of the same program = two processes

Computer system = set of processes

I Process = unit of work of a system

I Operating System processes

I User processes

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 3 of 35

Page 4: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Definitions and basic conceptsProcess management by OSIssues

Running a Set of Processes

I MonoprocessorI Pseudo-parallelism: 1 process running at a time

I So, either the OS or a user process is runningI Multiprocessor

I A process can be running on each processorI The OS scheduler dispatches processes on processors

I Scheduling policy

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 4 of 35

Page 5: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Definitions and basic conceptsProcess management by OSIssues

States of Processes

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 5 of 35

Page 6: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Definitions and basic conceptsProcess management by OSIssues

Process data

I Data of processes are stored in memory!

Various data of a process

I Program code = text section (static)I Current Activity

I Program counter = Processor’s registerI Next instruction to execute

I Stack: function calls are stored in a LIFO mannerI Function parametersI Return addressI Local variables

I Heap: Data section

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 6 of 35

Page 7: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Definitions and basic conceptsProcess management by OSIssues

Memory Layout of a C Program

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 7 of 35

Page 8: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Definitions and basic conceptsProcess management by OSIssues

Organization of Processes

UNIXI Process hierarchy

I A parent can possibly have many childrenI A child has exactly one parent

I Group of a process = this process + children + furtherdescendants

I All processes belong to the group of the init process (root)

WindowsI No process hierarchyI When a parent creates a child, it is given a special token

(called a handle)I A handle can be passed to other processesI 6= UNIX: processes cannot disinherit their children

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 8 of 35

Page 9: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Definitions and basic conceptsProcess management by OSIssues

UNIX: Hierarchy of Processes

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 9 of 35

Page 10: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Definitions and basic conceptsProcess management by OSIssues

Process Control Block

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 10 of 35

Page 11: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Definitions and basic conceptsProcess management by OSIssues

Switching Between Processes

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 11 of 35

Page 12: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Definitions and basic conceptsProcess management by OSIssues

Processes Data Structure

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 12 of 35

Page 13: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Definitions and basic conceptsProcess management by OSIssues

Four Important Issues Regarding Processes

1. How is it possible to launchthe first user process?

Boot sequence

2. How to manage processesfrom a programmer’s point ofview?

APIs

3. How to schedule processes(efficiently)?

Scheduling policies

4. How can processescommunicate with each others?

I Signals, shared memory,message passing, etc.

I Synchronization betweenprocesses

Attend next courses to knowmore!

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 13 of 35

Page 14: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Outline

Concepts

Boot sequence of an OS

Managing processes

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 14 of 35

Page 15: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Steps of Boot Sequence

1. Reset of the hardwareI All logic gates are reset to a known state

2. Diagnostic tests are run from the PROM Monitor

3. Bootstrap execution

4. Secondary boot loader execution

5. Starting of the OS kernel

6. User processes can execute!

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 15 of 35

Page 16: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Step 2: The PROM Monitor

Power On Self TestI System hardware is initialized

I Miniature diagnostic program: Ensures minimal operationalbase for the OS to run (memory, keyboard)

I ⇒ Does not guarantee that the hardware is fully functional!

Scan of busses

To detect hardware devices connected to the system

Loading and starting of the bootstrap

The boot block is located on a fixed position on the boot device

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 16 of 35

Page 17: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Step 3: Executing the Bootstrap

I Initialization of some peripherals and annex memories

I Loading secondary boot loader

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 17 of 35

Page 18: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Example: PC BIOS

I POST loads in RAM a program (BIOS) contained in ROM(Read Only Memory)

I When POST completes, it raises an interrupt (]19h)I Interrupt routine

I Loads into RAM a boot sector (location 7c00h)I The boot sector is searched on various devices

I CDROM, Hard disk, Network, Floppy disk, etc.

I Transfer control of the computer to the secondary boot loader

I Note that the BIOS program can be configured before theboot sequence starts

I Interrupt of devicesI Boot devices (e.g., CDROM, etc.)

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 18 of 35

Page 19: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Step 4: Secondary Bootloader

I Offers interaction with users

I Selection of the kernel to useI Modification of boot parameters

I Maintenance mode, multi-user mode, etc.

I Loads the chosen kernel, and starts itI Example of secondary boot loader

I LiloI GrubI Windows multi-boot loader

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 19 of 35

Page 20: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Step 5: Starting the OS Kernel

I Allocation of memoryI Kernel, data storage areas, I/O buffers

I Probe of devicesI The kernel builds a device tree and loads corresponding devices

I Creation of spontaneous system processesI Swapper (sched), init, pagedaemon

I Start-up scripts are executed according to run-levelI Start system services (e.g., rlogin daemon)I Windows runlevels

I Multi-user, Safe mode, Safe mode with network

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 20 of 35

Page 21: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

Outline

Concepts

Boot sequence of an OS

Managing processesOverviewfork(), vfork(), exec()And one last question!

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 21 of 35

Page 22: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

Controlling processes

I Creation of new processes

I Management of processes

I Termination of processes

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 22 of 35

Page 23: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

Starting and Terminating a Process

A C program starts its execution with a call to:

i n t main ( i n t argc , char ∗ a rgv [ ] )

Termination

I Normal terminationI Return from mainI Call to exit() (or exit())

I Abnormal terminationI Call to abort()I Allocated resources exceededI Cascading terminationI . . .

#inc l u d e < s t d l i b . h>vo id e x i t ( i n t s t a t u s )

#inc l u d e <un i s t d . h>vo id e x i t ( i n t s t a t u s )

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 23 of 35

Page 24: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

Life of a C Program

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 24 of 35

Page 25: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

Call to exit()

1. Process resources are freedI File descriptors

I Shared memory segments

I Semaphores

I Messages queues

I ...

2. The process becomes a zombie process

I Entry in the process allocation table is conserved

I Children’s parent is set to the init process

I All children executing in foreground are terminated

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 25 of 35

Page 26: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

UNIX: Creation of a New Process with fork()

I A running process calls fork()

fork()

#inc l u d e <s y s / t yp e s . h>#inc l u d e <un i s t d . h>

p i d t f o r k ( vo id ) ;

I The new process is a child processI The function is called once but returns twice:

I 0 is returned to the child processI The pid of the child is return to the parent process

I For more information on fork()

$man f o r k

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 26 of 35

Page 27: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

fork(): Code Example

Code#include <sys/types.h>

int glob = 6;

int main(void) {

pid_t pid;

int var = 66;

pid = fork();

if (pid == 0) { /* Child */

glob ++; var ++;

} else {

sleep(1);

}

printf("process pid = %d glob=%d var=%d\n", pid, glob, var);

exit(0);

}

Guess what happens when executing this!

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 27 of 35

Page 28: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

fork(): Drawback and Solution

Drawback: Created child is a clone of its parent

Recopy of data space, heap, stack, etc.⇒ Not very efficient

A few solutions...I fork() under Linux: Copy-On-Write technique

I Memory pages shared by parent and childI Memory pages set to read-only for bothI Copy of the memory when a write operation is performed

I vfork()I Parent process is suspended until the child process makes a

call to exec() or to exit() (Linux)

I exec()I Replaces the current process image with a new process image

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 28 of 35

Page 29: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

vfork() and exec(): Code Example

Code#include <sys/types.h>

#include <unistd.h>

int glob = 6;

int main(void) {

int var = 66;

pid_t pid = fork();

if (pid == 0) { /* Child */

if (execl("/bin/sh", "sh", "-c", "/bin/ls", NULL) <0) {

exit(127); /* error */

}

} else {

sleep(1);

}

printf("process pid = %d glob=%d var=%d\n", pid, glob, var);

exit(0);

}

Guess what happens when executing this!Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 29 of 35

Page 30: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

Linux: Excerpt of The Manual of fork()

fork()

fork creates a child process that differs from the parent processonly in its PID and PPID, and in the fact that resource utilizationsare set to 0. File locks and pending signals are not inherited.Under Linux, fork is implemented using copy-on-write pages, so theonly penalty incurred by fork is the time and memory required toduplicate the parent’s page tables, and to create a unique taskstructure for the child.

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 30 of 35

Page 31: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

Linux: Excerpt of The Manual of vfork()

vfork()

vfork, just like fork(2), creates a child process of the callingprocess.vfork() is a special case of clone(2). It is used to create newprocesses without copying the page tables of the parent process. Itmay be useful in performance sensitive applications where a childwill be created which then immediately issues an execve.vfork() differs from fork in that the parent is suspended until thechild makes a call to execve(2) or exit(2). The child shares allmemory with its parent, including the stack, until execve is issuedby the child. The child must not return from the current functionor call exit, but may call exit().

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 31 of 35

Page 32: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

Solaris: Excerpt of The Manual of fork() and vfork()

fork()

The fork() and fork1() functions create a new process. The newprocess (child process) is an exact copy of the calling process(parent process).

vfork()

The vfork() function creates new processes without fully copyingthe address space of the old process. This function is useful ininstances where the purpose of a fork(2) operation would be tocreate a new system context for an execve() operation (seeexec(2)).

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 32 of 35

Page 33: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

POSIX: Excerpt of The Manual of vfork()

vfork()

The vfork() function has the same effect as fork(), except that thebehavior is undefined if the process created by vfork() eithermodifies any data other than a variable of type pid t used to storethe return value from vfork(), or returns from the function in whichvfork() was called, or calls any other function before successfullycalling exit() or one of the exec family of functions.

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 33 of 35

Page 34: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

vfork(): Another Code example

Code#include <sys/types.h>

int glob = 6;

int main(void) {

pid_t pid;

int var = 66;

pid = vfork(); // previously was fork

if (pid == 0) { /* Child */

glob ++; var ++;

} else { sleep(1);}

printf("process pid = %d glob=%d var=%d\n", pid, glob, var);

exit(0);

}

Executionprocess pid = 0 glob=7 var=67

process pid = 7262 glob=7 var=67

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 34 of 35

Page 35: Operating Systems II. Processessoc.eurecom.fr/OS/docs/slideProcesses.pdf · I Process = unit of work of a system ... Ludovic Apvrille Operating Systems - II. Processes - Fall 2011

ConceptsBoot sequence of an OS

Managing processes

Overviewfork(), vfork(), exec()And one last question!

Questions

When I need some information on a function provided by anOperating System, what should I do?

I Use google? wikipedia? Other sites?

I Use local manual pages?

Why?

Local manual pages

$man −s2 f o r k$ i n f o f o r k

Ludovic Apvrille Operating Systems - II. Processes - Fall 2011 35 of 35