Download as pdf or txt
Download as pdf or txt
You are on page 1of 46

Chapter 3

Process Description and


Control

1
Process Image

2
Process Control Block (PCB)
 Table 3.5
 Process Identification
 Processor State Information
 Process Control Information

3
PCB

Process Control Block


• Process Identification
• Processor State Information
• Process Control Information

 Process identification
– Identifiers
• Numeric identifiers that may be stored with the process
control block include
– Identifier of this process
– Identifier of the process that created this process (parent process)
– User identifier

4
PCB

Process Control Block


• Process Identification
• Processor State Information
• Process Control Information

 Processor State Information


– User-Visible Registers
• A user-visible register is one that may be referenced by means
of the machine language that the processor executes while in
user mode.
– Control and Status Registers
These are a variety of processor registers that are employed to
control the operation of the processor.
• Program counter: Contains the address of the next instruction
to be fetched
• Condition codes: Result of the most recent arithmetic or
logical operation (e.g., sign, zero, carry, equal, overflow)
• Status information: Includes interrupt enabled/disabled flags,
execution mode

5
PCB

Process Control Block


• Process Identification
• Processor State Information
• Process Control Information

 Processor State Information


– Stack Pointers
• Each process has one or more last-in-first-out (LIFO) system
stacks associated with it. A stack is used to store parameters
and calling addresses for procedure and system calls. The
stack pointer points to the top of the stack.

6
PCB

Process Control Block


• Process Identification
• Processor State Information
• Process Control Information

 Program status word (PSW)


– contains condition codes + status information
– 예제: the EFLAGS register on Pentium machines

7
PCB

Process Control Block


• Process Identification
• Processor State Information
• Process Control Information

 Process Control Information


– Scheduling and State Information
• Process state:
– e.g., running, ready, waiting, halted
• Priority:
– One or more fields may be used to describe the scheduling priority of the
process. In some systems, several values are required (e.g., default, current,
highest-allowable)
• Scheduling-related information:
–This will depend on the scheduling algorithm used. Examples are the
amount of time that the process has been waiting and the amount of time
that the process executed the last time it was running.
• Event:
– Identity of event the process is awaiting before it can be resumed

8
PCB

Process Control Block


• Process Identification
• Processor State Information
• Process Control Information

 Process Control Information


– Data Structuring
• A process may be linked to other process in a queue, ring, or
some other structure.
• For example, all processes in a waiting state for a particular priority
level may be linked in a queue. A process may exhibit a parent-child
(creator-created) relationship with another process. The process
control block may contain pointers to other processes to support
these structures.

9
PCB

Process Control Block


• Process Identification
• Processor State Information
• Process Control Information

 Process Control Information


– Interprocess Communication
• Various flags, signals, and messages may be associated with
communication between two independent processes. Some or
all of this information may be maintained in the process
control block.
– Process Privileges
• Processes are granted privileges in terms of the memory that
may be accessed and the types of instructions that may be
executed. In addition, privileges may apply to the use of
system utilities and services.

10
PCB

Process Control Block


• Process Identification
• Processor State Information
• Process Control Information

 Process Control Information


– Memory Management
• This section may include pointers to segment and/or page
tables that describe the virtual memory assigned to this
process.
– Resource Ownership and Utilization
• Resources controlled by the process may be indicated, such as
opened files. A history of utilization of the processor or other
resources may also be included; this information may be
needed by the scheduler.

11
User processes in VM

12
3.4 Process Control
 Modes of Execution
– User mode
• Less-privileged mode
• User programs typically execute in this mode
– System mode, control mode, or kernel mode
• More-privileged mode
• Kernel of the operating system

13
Modes of Execution

14
Modes of Execution
 Reason for using Two modes
– To protect OS and OS tables

 How does the processor know in which mode it


is to be executing?
– A bit in PSW that indicates the mode of execution

 When is the mode changed?


– Call to system call
– Interrupt triggering
– 앞으로 찾아 봅시다.

15
잠깐 이해하고 가기
이해해 보자: System Call

16
잠깐 이해하고 가기
System Calls
 Application programs talk to OS via system calls
 The programmer’s functional interface to the
LINUX kernel
잠깐 이해하고 가기
System Calls

Application code

User
fget() Standard Library Function
fscanf() Library Buffer

read() System Call


Kernel
System Buffer (buffer cache)
sys_read()

Hardware (terminal, file, … ) H/W


잠깐 이해하고 가기
System calls & Library functions

 System calls
– Well defined entry points directly into the kernel
– Look like C functions which can be called from a
user’s program
• Just need to include the appropriate header
Ex) read(), write()

 Library functions
– The library function is often more elaborate than
the system call, and usually invokes the system
call
Ex) fprintf() fscanf()
잠깐 이해하고 가기
User mode vs. Kernel mode
앞으로 살펴 볼 것들…
 Process creation

 Process switching
– 언제 process switching이 발생하는가?
– Process switch가 발생할 때, OS가 할 일은?
– Process switch와 Mode switch의 차이는?

21
PCB

Process Creation Step


• Process Identification
• Processor State Information
• Process Control Information

1. Assign a unique process identifier


2. Allocate space for the process
– Program + data + user stack + PCB
3. Initialize process control block
– Process ID
– Processor state info
• initialized with zero
• except Program counter & stack pointer
– Process control info
• initialized with default value
4. Set up appropriate linkages
– Ex: add new process to linked list used for scheduling queue
5. Create or expand other data structures
– Ex: maintain an accounting file

22
PCB

Process Creation Step


• Process Identification
• Processor State Information
• Process Control Information

1. Assign a unique process identifier


2. Allocate space for the process
– Program + data + user stack + PCB
3. Initialize process control block
– Process ID
– Processor state info
• initialized with zero
• except Program counter & stack pointer
– Process control info
• initialized with default value
4. Set up appropriate linkages
– Ex: add new process to linked list used for scheduling queue
5. Create or expand other data structures
– Ex: maintain an accounting file

23
Control to OS…
 Possible events that may give control to the OS
– Interrupt
– Trap
– Supervisor call

24
Process Switching

When to Switch a Process (1)


• Interrupt
• Trap
• Supervisor call

 Interrupt
– External to the execution of the current instruction

– 종류:
(1) Clock interrupt
• process has executed for the maximum allowable time slice
(2) I/O interrupt
• When I/O action has occurred
(3) Memory fault
• memory address is in virtual memory so it must be brought
into main memory

25
Process Switching

When to Switch a Process (2)


• Interrupt
• Trap
• Supervisor call

 Trap
– Associated with the execution of the current
instruction
– error or exception occurred
– may cause process to be moved to Exit state

 Supervisor call (system call)


– Explicit request
– such as file open

26
Clock interrupt가 발생하면…
 Processor에 대한 control  OS
– OS는 현재 running process A를 ready 상태로 바꾸고
– Ready process 중 하나를 선택하여 (process B) running
상태로 바꾼 후…
– Processor 에 대한 control을 Process B에게 양도…
 즉, process switching 발생
 자세히 살펴보면…

27
Process Switching Step
1. Save context of processor including program counter
and other registers
2. Update the process control block of the process that is
currently in the Running state
– Running  Ready or Blocked or Ready/Suspend
3. Move process control block to appropriate queue –
ready; blocked; ready/suspend
4. Select another process for execution
5. Update the process control block of the process
selected (Ready  Running)
6. Update memory-management data structures
7. Restore context of the selected process

28
그런데…
 Interrupt가 발생하면 항상 process switching이 발생해
야 하나?
– 즉, interrupt 발생하면
– 현재 running process의 state를 block/ready로 바꿔야 하나?

 Interrupt 처리가 매우 간단한 작업이라면…


– OS가 잠깐 control을 갖고
– 간단한 코드를 수행할 때…
• 예) reset flag; check for an error condition; return;
– 현재 running process의 상태를 어떻게 할 것인가?
• Ready 또는 block으로 변환 OR
• 그대로 running으로 유지

29
Reading…
Mode Switching

If an interrupt is pending, the processor does the following :

1. It sets the program counter to the starting address of an interrupt


handler program.
2. It switches from user mode to kernel mode so that the interrupt
processing code may include privileged instructions.

… However, in most operating systems, the occurrence of an interrupt


does not necessarily mean a process switch. It is possible that, after
the interrupt handler has executed, the currently running process will
resume execution. In that case, all that is necessary is to save the
processor state information when the interrupt occurs and restore that
information when control is returned to the program that was running.
Typically, the saving and restoring functions are performed in hardware.
30
Simple
Interrupt
Processing

여기서..잠깐…
CHAP 1을 다시 돌아보면…

31
또는…
 System call 호출 시에도…
– User process에서 함수 호출 시…
– 잠깐 OS가 control을 갖게 되는데…
– 이 경우에도 현재 user process의 state를 변환? 또는 그대로?

2013-02 Computer Programming2


32
Process switch vs. Mode switch
 Process switch
 Mode switch

Process
Process
A
A

OS CPU Process switch

Process
B Interrupt occurs

33
Process switch vs. Mode switch
 Process switch
 Mode switch

Process
Process
A OS
A

OS CPU Process switch

Process
B

34
Process switch vs. Mode switch
 Process switch
 Mode switch

Process
Process
A OS
B
A

OS CPU Process switch

Process
B

35
Mode Switching (1)
 When interrupt issues, the processor does…
– Set the program counter to the starting address of an
interrupt handler program
– What happens next?
• Process switching?

 Switch from user mode to kernel mode, so that the


interrupt processing code may include privileged instruction

 Mode switch
– may occur without changing the state of the process that is
currently in the Running state..
– 현재 RUNNUNG 상태의 프로세스를 그대로 RUNNING 상태
로 두고…

36
Mode Switching (2)
 First instruction of the interrupt handler code…
– Context of the process that has been interrupted is saved
into that PCB of the interrupted program
– 다시 말하면…Processor state information is saved to be
able to resume the program that was interrupted
 After interrupt has executed…
– Currently running process will resume execution
– All information are restored when control is returned to the
program…

 In most OS, the occurrence of an interrupt does


not necessarily mean a process switch  mode
switch만 발생
37
Process switch vs. Mode switch
 Process switch
 Mode switch

Process
Process
A
A

OS CPU Mode switch

Process
B Interrupt occurs

38
Process switch vs. Mode switch
 Process switch
 Mode switch

Process
Process
A
A

OS CPU Mode switch


(kernel mode)
Process
B

39
Process switch vs. Mode switch
 반드시 process switch가 발생하는 event는?
– 현재 running Process가 더 진행하지 못하는 경우…
• Clock interrupt,
• Memory Fault
• Trap (치명적 오류로 인해 process가 더 진행 못하는 경우)
• System call (I/O관련 호출처럼 바로 service가 종료되지 못하는
경우는 process switch 발생)

 어떤 것이 cost가 더 드는 operation?
– Process switch
– Mode switch

40
3.5 Execution of the Operating System
 그렇다면 OS는 무엇인가?
– 같은 개념의 Process? 또는 예외적인 무엇?

 OS를 보는 3가지 관점
– Non-process Kernel (프로세스 절대 아님)
– Execution within User process
– Process-based Operating System (일반 프로세스)

41
OS 관점

Execution of the Operating System


• Non-process kernel
• within User process
• process-based OS

 Non-process Kernel
– Execute kernel outside of any process
– Operating system code is executed as a separate entity that
operates in privileged mode
– Running process is interrupted or issues a supervisor call, the
mode context of this process is saved and control is passed to
the kernel
– Older OS model
– Concept of process is considered to apply only to user program

42
OS 관점

Execution of the Operating System


• Non-process kernel
• within User process
• process-based OS

 Execution Within User Processes


– Common with OS on small machines (PCs, workstation)
– Operating system software within context of a user process
– Process executes in privileged mode when executing operating
system code
– When an interrupt, trap, supervisor call occurs, the processor is
place in kernel mode and control is to the OS.
– Process switch is not performed, a mode switch within the same
process
– User cannot tamper with OS routines, even though they are
executing in user’s process environment

– Dispatcher may not execute in the current process…

43
dispatcher

44
OS codes & data

Operating System executes


within User’s space 45
OS 관점

Execution of the Operating System


• Non-process kernel
• within User process
• process-based OS

 Process-Based Operating System


– Implement operating system as a collection of system processes
– use of modular OS design
– Useful in multi-processor or multi-computer environment

46

You might also like