chapter 3: processes - james madison university€¦ ·  · 2011-09-15chapter 3: processes ... s =...

61
Chapter 3: Processes Wednesday, September 14, 2011

Upload: buinguyet

Post on 26-Apr-2018

218 views

Category:

Documents


4 download

TRANSCRIPT

Chapter 3: Processes

Wednesday, September 14, 2011

Themes (so far)

• Computers solve problems

• Automate computation, scientific models

• OS evolved to solve problems

• Common I/O, shared HW, multitasking, protection

• Implementations vary, but concepts persist

Wednesday, September 14, 2011

Process

• Program in execution

• Instance of a running program

• Entity scheduled for CPU time

• Combination of code, state, and resources

Wednesday, September 14, 2011

Wednesday, September 14, 2011

Wednesday, September 14, 2011

Wednesday, September 14, 2011

Wednesday, September 14, 2011

Wednesday, September 14, 2011

How is this represented?

Wednesday, September 14, 2011

Process creation

• New batch job

• Interactive logon

• Provide OS service

• Spawning

Wednesday, September 14, 2011

Process termination• Normal completion

• Time limit exceeded

• Memory unavailable

• Bounds violation

• Protection error

• Arithmetic error

• Time overrun

• I/O failure

• Invalid instruction

• Privileged instruction

• Data misuse

• Intervention

• Parent termination

• Parent request

Wednesday, September 14, 2011

New Ready

Blocked

Running Exit

Figure 3.6 Five-State Process Model

AdmitDispatch

Timeout

Release

Event

Wait

Event

Occurs

Wednesday, September 14, 2011

kernel

code+data

kernel

code+data

kernel

code+data

0x00000000

0xc0000000

0xffffffff

0x00000000

0xc0000000

0xffffffff

0x00000000

0xc0000000

0xffffffff

Wednesday, September 14, 2011

kernel

code+data

kernel

code+data

kernel

code+data

kernelready

blocked

PCB

0x00000000

0xc0000000

0xffffffff

0x00000000

0xc0000000

0xffffffff

0x00000000

0xc0000000

0xffffffff

Wednesday, September 14, 2011

kernel

code+data

kernel

code+data

kernel

code+data

kernelready

blocked

PCB

0x00000000

0xc0000000

0xffffffff

0x00000000

0xc0000000

0xffffffff

0x00000000

0xc0000000

0xffffffff

Wednesday, September 14, 2011

kernel

code+data

kernel

code+data

kernel

code+data

kernelready

blocked

PCB

0x00000000

0xc0000000

0xffffffff

0x00000000

0xc0000000

0xffffffff

0x00000000

0xc0000000

0xffffffff

Wednesday, September 14, 2011

Dispatcher

= Running = Ready

Figure 3.7 Process States for Trace of Figure 3.4

= Blocked

0 5 10 15 20 25 30 35 40 45 50

Process C

Process B

Process A

Wednesday, September 14, 2011

Wednesday, September 14, 2011

Wednesday, September 14, 2011

ready

ready

blocked

ready

blockednew

Wednesday, September 14, 2011

ready

ready

blocked

ready

blockednew

Wednesday, September 14, 2011

ready

ready

blocked

ready

blockedready

Wednesday, September 14, 2011

ready

ready

blocked

ready

suspendedready

Wednesday, September 14, 2011

Suspended process

• Not immediately available for execution

• May or may not be waiting

• Suspended by an agent

• Agent must explicitly un-suspend

Wednesday, September 14, 2011

Wednesday, September 14, 2011

Wednesday, September 14, 2011

Wednesday, September 14, 2011

Process representation

• Code

• Data

• Associated resources

Wednesday, September 14, 2011

Wednesday, September 14, 2011

Wednesday, September 14, 2011

Code: (program.c)

#include <stdio.h>#include <fcntl.h>

int main(){ int f; size_t s; char c[10];

f = open("program.c",O_RDONLY); printf ("f = %d\n",f);

s = read(f, c, 9); c[9] = '\0'; printf("c is '%s'\n",c); close(f); return 0;}

Wednesday, September 14, 2011

Code: (program.c)

#include <stdio.h>#include <fcntl.h>

int main(){ int f; size_t s; char c[10];

f = open("program.c",O_RDONLY); printf ("f = %d\n",f);

s = read(f, c, 9); c[9] = '\0'; printf("c is '%s'\n",c); close(f); return 0;}

Output:

f = 3c is ‘#include ‘

Wednesday, September 14, 2011

Code: (program.c)

#include <stdio.h>#include <fcntl.h>

int main(){ int f; size_t s; char c[10];

f = open("program.c",O_RDONLY); printf ("f = %d\n",f);

s = read(f, c, 9); c[9] = '\0'; printf("c is '%s'\n",c); close(f); return 0;}

Output:

f = 3c is ‘#include ‘

0 = stdin1 = stdout2 = stderr

Wednesday, September 14, 2011

Process location

Wednesday, September 14, 2011

Home Schedule Projects

Project 1: ELF Format

The intent of this project is to introduce you to the Executable and Linking Format (ELF) specification, which is used to define theformat of executable files for Linux, UNIX, and other OS. In addition, this will serve as an introduction to C for those of you whohave not worked with this language previously. In this project, you will write a small executable that parses and prints outinformation from the ELF header for an executable, similar to the "readelf" utility.

Background

In CS 350, you learned the basic concepts of computer organization, including the standard function calling convention (e.g., pushingarguments to the stack, adjusting the stack & base pointer registers, etc.). In many ways, the C programming language is justassembly language taken up one level of abstraction. For example, local variables in C are just aliases for an offset from the stackpointer. Here, we will review some of these concepts and introduce the ELF specification.

Typical Memory Layout

Every process in a modern OS has a linear view of memory (we will discuss this topic in more detail later in the semester).Conceptually, the program sees the following view of memory (in a 32-bit architecture):

0xffffffff +----------------------------------+ | | | kernel | | |0xc0000000 +----------------------------------+ | user stack | | | | | | | | V | | grows downward | | | | | | grows upward | | ^ | | | | | | | | user heap | +----------------------------------+ | uninitialized data segment (BSS) | +----------------------------------+ | initialized data segment | +----------------------------------+ | code segment |0x08084000 +----------------------------------+ | | | | 0 +----------------------------------+

The code segment contains the program instructions and starts at user virtual address 0x08084000, approximately 128 MB from thebottom of the address space. This value is specified in [ SysV-i386] and has no deep significance.

Variables are generally allocated in one of four spaces. The initialized data segment and unititialized data segment (also called BSS)contain global variables accessible to any part of the program. Don't be fooled by the nomenclature: All variables in both segmentsare actually initialized. Variables in the BSS are initialized to be all zero bits.

The stack typically starts immediately below the kernel (which starts at 0xc0000000), and grows downward. Variables that arecreated locally by a function are allocated on the stack. The heap is used for dynamically allocated data that typically persistsbetween function calls. Although the basic structure indicates that it grows upward, this is only partially correct. As resources are nolonger used by the program, that part of memory can be returned to the heap. Consequently, the heap is generally fragmented. The

Process location

Wednesday, September 14, 2011

Home Schedule Projects

Project 1: ELF Format

The intent of this project is to introduce you to the Executable and Linking Format (ELF) specification, which is used to define theformat of executable files for Linux, UNIX, and other OS. In addition, this will serve as an introduction to C for those of you whohave not worked with this language previously. In this project, you will write a small executable that parses and prints outinformation from the ELF header for an executable, similar to the "readelf" utility.

Background

In CS 350, you learned the basic concepts of computer organization, including the standard function calling convention (e.g., pushingarguments to the stack, adjusting the stack & base pointer registers, etc.). In many ways, the C programming language is justassembly language taken up one level of abstraction. For example, local variables in C are just aliases for an offset from the stackpointer. Here, we will review some of these concepts and introduce the ELF specification.

Typical Memory Layout

Every process in a modern OS has a linear view of memory (we will discuss this topic in more detail later in the semester).Conceptually, the program sees the following view of memory (in a 32-bit architecture):

0xffffffff +----------------------------------+ | | | kernel | | |0xc0000000 +----------------------------------+ | user stack | | | | | | | | V | | grows downward | | | | | | grows upward | | ^ | | | | | | | | user heap | +----------------------------------+ | uninitialized data segment (BSS) | +----------------------------------+ | initialized data segment | +----------------------------------+ | code segment |0x08084000 +----------------------------------+ | | | | 0 +----------------------------------+

The code segment contains the program instructions and starts at user virtual address 0x08084000, approximately 128 MB from thebottom of the address space. This value is specified in [ SysV-i386] and has no deep significance.

Variables are generally allocated in one of four spaces. The initialized data segment and unititialized data segment (also called BSS)contain global variables accessible to any part of the program. Don't be fooled by the nomenclature: All variables in both segmentsare actually initialized. Variables in the BSS are initialized to be all zero bits.

The stack typically starts immediately below the kernel (which starts at 0xc0000000), and grows downward. Variables that arecreated locally by a function are allocated on the stack. The heap is used for dynamically allocated data that typically persistsbetween function calls. Although the basic structure indicates that it grows upward, this is only partially correct. As resources are nolonger used by the program, that part of memory can be returned to the heap. Consequently, the heap is generally fragmented. The

code + data + stack

Process location

Wednesday, September 14, 2011

Home Schedule Projects

Project 1: ELF Format

The intent of this project is to introduce you to the Executable and Linking Format (ELF) specification, which is used to define theformat of executable files for Linux, UNIX, and other OS. In addition, this will serve as an introduction to C for those of you whohave not worked with this language previously. In this project, you will write a small executable that parses and prints outinformation from the ELF header for an executable, similar to the "readelf" utility.

Background

In CS 350, you learned the basic concepts of computer organization, including the standard function calling convention (e.g., pushingarguments to the stack, adjusting the stack & base pointer registers, etc.). In many ways, the C programming language is justassembly language taken up one level of abstraction. For example, local variables in C are just aliases for an offset from the stackpointer. Here, we will review some of these concepts and introduce the ELF specification.

Typical Memory Layout

Every process in a modern OS has a linear view of memory (we will discuss this topic in more detail later in the semester).Conceptually, the program sees the following view of memory (in a 32-bit architecture):

0xffffffff +----------------------------------+ | | | kernel | | |0xc0000000 +----------------------------------+ | user stack | | | | | | | | V | | grows downward | | | | | | grows upward | | ^ | | | | | | | | user heap | +----------------------------------+ | uninitialized data segment (BSS) | +----------------------------------+ | initialized data segment | +----------------------------------+ | code segment |0x08084000 +----------------------------------+ | | | | 0 +----------------------------------+

The code segment contains the program instructions and starts at user virtual address 0x08084000, approximately 128 MB from thebottom of the address space. This value is specified in [ SysV-i386] and has no deep significance.

Variables are generally allocated in one of four spaces. The initialized data segment and unititialized data segment (also called BSS)contain global variables accessible to any part of the program. Don't be fooled by the nomenclature: All variables in both segmentsare actually initialized. Variables in the BSS are initialized to be all zero bits.

The stack typically starts immediately below the kernel (which starts at 0xc0000000), and grows downward. Variables that arecreated locally by a function are allocated on the stack. The heap is used for dynamically allocated data that typically persistsbetween function calls. Although the basic structure indicates that it grows upward, this is only partially correct. As resources are nolonger used by the program, that part of memory can be returned to the heap. Consequently, the heap is generally fragmented. The

code + data + stack

attributes (PCB)

Process location

Wednesday, September 14, 2011

Process location

Home Schedule Projects

Project 1: ELF Format

The intent of this project is to introduce you to the Executable and Linking Format (ELF) specification, which is used to define theformat of executable files for Linux, UNIX, and other OS. In addition, this will serve as an introduction to C for those of you whohave not worked with this language previously. In this project, you will write a small executable that parses and prints outinformation from the ELF header for an executable, similar to the "readelf" utility.

Background

In CS 350, you learned the basic concepts of computer organization, including the standard function calling convention (e.g., pushingarguments to the stack, adjusting the stack & base pointer registers, etc.). In many ways, the C programming language is justassembly language taken up one level of abstraction. For example, local variables in C are just aliases for an offset from the stackpointer. Here, we will review some of these concepts and introduce the ELF specification.

Typical Memory Layout

Every process in a modern OS has a linear view of memory (we will discuss this topic in more detail later in the semester).Conceptually, the program sees the following view of memory (in a 32-bit architecture):

0xffffffff +----------------------------------+ | | | kernel | | |0xc0000000 +----------------------------------+ | user stack | | | | | | | | V | | grows downward | | | | | | grows upward | | ^ | | | | | | | | user heap | +----------------------------------+ | uninitialized data segment (BSS) | +----------------------------------+ | initialized data segment | +----------------------------------+ | code segment |0x08084000 +----------------------------------+ | | | | 0 +----------------------------------+

The code segment contains the program instructions and starts at user virtual address 0x08084000, approximately 128 MB from thebottom of the address space. This value is specified in [ SysV-i386] and has no deep significance.

Variables are generally allocated in one of four spaces. The initialized data segment and unititialized data segment (also called BSS)contain global variables accessible to any part of the program. Don't be fooled by the nomenclature: All variables in both segmentsare actually initialized. Variables in the BSS are initialized to be all zero bits.

The stack typically starts immediately below the kernel (which starts at 0xc0000000), and grows downward. Variables that arecreated locally by a function are allocated on the stack. The heap is used for dynamically allocated data that typically persistsbetween function calls. Although the basic structure indicates that it grows upward, this is only partially correct. As resources are nolonger used by the program, that part of memory can be returned to the heap. Consequently, the heap is generally fragmented. The

Memory

Storage

Wednesday, September 14, 2011

Process location

Home Schedule Projects

Project 1: ELF Format

The intent of this project is to introduce you to the Executable and Linking Format (ELF) specification, which is used to define theformat of executable files for Linux, UNIX, and other OS. In addition, this will serve as an introduction to C for those of you whohave not worked with this language previously. In this project, you will write a small executable that parses and prints outinformation from the ELF header for an executable, similar to the "readelf" utility.

Background

In CS 350, you learned the basic concepts of computer organization, including the standard function calling convention (e.g., pushingarguments to the stack, adjusting the stack & base pointer registers, etc.). In many ways, the C programming language is justassembly language taken up one level of abstraction. For example, local variables in C are just aliases for an offset from the stackpointer. Here, we will review some of these concepts and introduce the ELF specification.

Typical Memory Layout

Every process in a modern OS has a linear view of memory (we will discuss this topic in more detail later in the semester).Conceptually, the program sees the following view of memory (in a 32-bit architecture):

0xffffffff +----------------------------------+ | | | kernel | | |0xc0000000 +----------------------------------+ | user stack | | | | | | | | V | | grows downward | | | | | | grows upward | | ^ | | | | | | | | user heap | +----------------------------------+ | uninitialized data segment (BSS) | +----------------------------------+ | initialized data segment | +----------------------------------+ | code segment |0x08084000 +----------------------------------+ | | | | 0 +----------------------------------+

The code segment contains the program instructions and starts at user virtual address 0x08084000, approximately 128 MB from thebottom of the address space. This value is specified in [ SysV-i386] and has no deep significance.

Variables are generally allocated in one of four spaces. The initialized data segment and unititialized data segment (also called BSS)contain global variables accessible to any part of the program. Don't be fooled by the nomenclature: All variables in both segmentsare actually initialized. Variables in the BSS are initialized to be all zero bits.

The stack typically starts immediately below the kernel (which starts at 0xc0000000), and grows downward. Variables that arecreated locally by a function are allocated on the stack. The heap is used for dynamically allocated data that typically persistsbetween function calls. Although the basic structure indicates that it grows upward, this is only partially correct. As resources are nolonger used by the program, that part of memory can be returned to the heap. Consequently, the heap is generally fragmented. The

Memory

Storage

Wednesday, September 14, 2011

Process location

Home Schedule Projects

Project 1: ELF Format

The intent of this project is to introduce you to the Executable and Linking Format (ELF) specification, which is used to define theformat of executable files for Linux, UNIX, and other OS. In addition, this will serve as an introduction to C for those of you whohave not worked with this language previously. In this project, you will write a small executable that parses and prints outinformation from the ELF header for an executable, similar to the "readelf" utility.

Background

In CS 350, you learned the basic concepts of computer organization, including the standard function calling convention (e.g., pushingarguments to the stack, adjusting the stack & base pointer registers, etc.). In many ways, the C programming language is justassembly language taken up one level of abstraction. For example, local variables in C are just aliases for an offset from the stackpointer. Here, we will review some of these concepts and introduce the ELF specification.

Typical Memory Layout

Every process in a modern OS has a linear view of memory (we will discuss this topic in more detail later in the semester).Conceptually, the program sees the following view of memory (in a 32-bit architecture):

0xffffffff +----------------------------------+ | | | kernel | | |0xc0000000 +----------------------------------+ | user stack | | | | | | | | V | | grows downward | | | | | | grows upward | | ^ | | | | | | | | user heap | +----------------------------------+ | uninitialized data segment (BSS) | +----------------------------------+ | initialized data segment | +----------------------------------+ | code segment |0x08084000 +----------------------------------+ | | | | 0 +----------------------------------+

The code segment contains the program instructions and starts at user virtual address 0x08084000, approximately 128 MB from thebottom of the address space. This value is specified in [ SysV-i386] and has no deep significance.

Variables are generally allocated in one of four spaces. The initialized data segment and unititialized data segment (also called BSS)contain global variables accessible to any part of the program. Don't be fooled by the nomenclature: All variables in both segmentsare actually initialized. Variables in the BSS are initialized to be all zero bits.

The stack typically starts immediately below the kernel (which starts at 0xc0000000), and grows downward. Variables that arecreated locally by a function are allocated on the stack. The heap is used for dynamically allocated data that typically persistsbetween function calls. Although the basic structure indicates that it grows upward, this is only partially correct. As resources are nolonger used by the program, that part of memory can be returned to the heap. Consequently, the heap is generally fragmented. The

Memory

Storage

Wednesday, September 14, 2011

Process attributes

• Process identification

• PID, PPID, UID

• Processor state information

• User-visible registers, control/status registers (PC, etc.), stack pointers

• Process control information

• Scheduling (state, priority, events), pointers to other PCB, IPC, privileges, memory mgmt., resources

Wednesday, September 14, 2011

Wednesday, September 14, 2011

Wednesday, September 14, 2011

proc

intWednesday, September 14, 2011

proc

int hdlr

intWednesday, September 14, 2011

proc

int hdlr kernel

int irtWednesday, September 14, 2011

proc

int hdlr kernel

proc

int intirtWednesday, September 14, 2011

proc

int hdlr kernel

proc

int hdlr

int int intirtWednesday, September 14, 2011

Wednesday, September 14, 2011

Process creation

1. Assign unique PID

2. Allocate space

3. Initialize PCB

4. Set linkages

5. Create/expand data structures

Wednesday, September 14, 2011

Process switching

• When?

• Interrupt, trap, supervisor (system) call

• How?

• Mode switch vs. context switch

Wednesday, September 14, 2011

kernel

code+data

CPUgen reg ctl reg

vm reg

kernel

code+data

stack

PCB PCB

stack

stack

code

stack

code

pc

PCB PCB

Wednesday, September 14, 2011

kernel

code+data

CPUgen reg ctl reg

vm reg

kernel

code+data

stack

PCB PCB

stack

stack

code

stack

code

pc

PCB PCB

Wednesday, September 14, 2011

kernel

code+data

CPUgen reg ctl reg

vm reg

kernel

code+data

stack

PCB PCB

stack

stack

code

stack

code

pc

PCB PCB

INT ==> Mode switch

Wednesday, September 14, 2011

kernel

code+data

CPUgen reg ctl reg

vm reg

kernel

code+data

PCB PCB

stackstack

stack

code

stack

code

pc

PCB PCB

Wednesday, September 14, 2011

kernel

code+data

CPUgen reg ctl reg

vm reg

kernel

code+data

PCB PCB

stackstack

stack

code

stack

code

pc

PCB PCB

Wednesday, September 14, 2011

kernel

code+data

CPUgen reg ctl reg

vm reg

kernel

code+data

PCB PCB

stackstack

stack

code

stack

code

pc

PCB PCB

Wednesday, September 14, 2011

kernel

code+data

CPUgen reg ctl reg

vm reg

kernel

code+data

PCB PCB

stack stack

stack

code

stack

code

pc

PCB PCB

Wednesday, September 14, 2011

kernel

code+data

CPUgen reg ctl reg

vm reg

kernel

code+data

PCB PCB

stack stack

stack

code

stack

code

Control registers• PC• Kernel stack pointer• User stack pointer• PSW (x86 EFLAGS)• CR0, CR1, CR2, CR3 (for VM)

pc

PCB PCB

Wednesday, September 14, 2011

Context switch1. Save CPU context

2. Update PCB of running process

3. Move PCB to queue

4. Select next process

5. Update PCB of new process

6. Update memory management

7. Restore context of selected process

Wednesday, September 14, 2011

Wednesday, September 14, 2011