Process - DPNM Lab

Download Report

Transcript Process - DPNM Lab

ITEC 502 컴퓨터 시스템 및 실습
Chapter 2-1:
Process
Mi-Jung Choi
[email protected]
DPNM Lab. Dept. of CSE, POSTECH
Contents
1. Processes
–
–
–
–
–
–
–
Process Concept
The Process Model
Process Creation
Process Termination
Process Hierarchies
Process States
Implementation of Processes
ITEC502 컴퓨터 시스템 및 실습
2
Process Concept
 An operating system executes a variety of programs:
– Batch system – jobs
– Time-shared systems – user programs or tasks
 Uses the terms job and process almost interchangeably
 Process – a program in execution; process execution must
progress in sequential fashion
 A process includes:
– program counter
– stack
– data section
ITEC502 컴퓨터 시스템 및 실습
3
Process in Memory

Current activity of a process is
represented by
– the value of program counter
– The contents of the processor’s registers

Stack
– contains temporary data
– function parameters, return address,
local variables

Data section
– contains global variables

Heap
– is a memory that is dynamically allocated
during process run time



ITEC502 컴퓨터 시스템 및 실습
Program is a passive entity
Process is a active entity
Two processes may be associated with
the same program
4
Processes: The Process Model
 Multiprogramming of four programs
 Conceptual model of 4 independent, sequential processes
 Only one program active at any instant
ITEC502 컴퓨터 시스템 및 실습
5
Process Creation (1)
Principal events that cause process creation
1. System initialization
2. Execution of a process creation system
3. User request to create a new process
4. Initiation of a batch job
ITEC502 컴퓨터 시스템 및 실습
6
Process Creation (2)
 Parent process create children processes, which, in turn
create other processes, forming a tree of processes
– via create-process system call
 Three possibilities in terms of resource sharing
– Parent and children share all resources
– Children share subset of parent’s resources
– Parent and child share no resources, child receives resources
from OS directly
 Two possibilities in terms of execution
– Parent and children execute concurrently
– Parent waits until children terminate
ITEC502 컴퓨터 시스템 및 실습
7
Process Creation (3)
 Two possibilities in terms of address space
– Child is a duplicate of parent;
• child has same program and data as the parent
– Child has a program loaded into it
 UNIX examples
– fork system call creates new process
– exec system call used after a fork to replace the
process’ memory space with a new program
ITEC502 컴퓨터 시스템 및 실습
8
fork() & exec()
 A new process is created by the fork() system call
 The new process consists of a copy of the address space
of the original process
 Both processes (parent & child) continue execution at the
instruction after the fork().
 The return code for the fork() is zero for the child process,
whereas the (nonzero) pid of the child process is returned
to the parent
 The exec() loads a binary file into memory, destroys the
memory image of the program, and starts its execution
ITEC502 컴퓨터 시스템 및 실습
9
C Program Forking – UNIX (1)
int main()
{
pid_t pid;
pid = fork();
if (pid < 0)
{
fprintf(stderr, "Fork Failed");
exit(-1);
}
else if (pid == 0)
{
execve("/bin/ls", "ls", NULL);
}
else
{
}
}
wait (NULL);
printf ("Child Complete");
exit(0);
ITEC502 컴퓨터 시스템 및 실습
/* fork another process */
/* error occurred */
/* child process */
/* parent process */
/* parent will wait for the child to complete */
10
C Program Forking – UNIX (2)
ITEC502 컴퓨터 시스템 및 실습
11
Process Creation – Win32
 Windows example
– CreateProcess() function is used, which is similar to
fork
– creates a new child process
– requires loading a specified program into the address
space of the child process at process creation
– expects no fewer than ten parameters
ITEC502 컴퓨터 시스템 및 실습
12
C Program Forking – Wind32 (1)
void _tmain( int argc, TCHAR *argv[] )
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process
if( !CreateProcess(
NULL,
“c:\\Windows\\system32\mspaint.exe”,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi )
)
ITEC502 컴퓨터 시스템 및 실습
// No module name (use command line)
// program name
// Process handle not inheritable
// Thread handle not inheritable
// Set handle inheritance to FALSE
// No creation flags
// Use parent's environment block
// Use parent's starting directory
// Pointer to STARTUPINFO structure
// Pointer to PROCESS_INFORMATION structure
13
C Program Forking – Win32 (2)
{
}
printf( "CreateProcess failed (%d)\n", GetLastError() );
return;
// Wait until child process exits
WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles
}
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
ITEC502 컴퓨터 시스템 및 실습
14
Process Termination (1)
Conditions which terminate processes
1. Normal exit (voluntary)
2. Error exit (voluntary)
3. Fatal error (involuntary)
4. Killed by another process (involuntary)
ITEC502 컴퓨터 시스템 및 실습
15
Process Termination (2)
 Process executes last statement and asks the operating
system to delete it (exit)
– Status value returned from child to parent (via wait)
– Process’ resources are deallocated by operating system
• Memory, open files, I/O buffers
 Parent may terminate execution of children processes
(abort) with various reason
– Child has exceeded allocated resources
– Task assigned to child is no longer required
 If parent is exiting
• Some operating systems do not allow child to continue if its parent
terminates
– All children terminated - cascading termination
ITEC502 컴퓨터 시스템 및 실습
16
Process Hierarchies
 Parent creates a child process, child processes can
create its own process
 Forms a hierarchy
– UNIX calls this a "process group"
 Windows has no concept of process hierarchy
– all processes are created equal
ITEC502 컴퓨터 시스템 및 실습
17
Process States (1): 3 states
 Possible process states
– running
– blocked
– ready
 Transitions between states shown
ITEC502 컴퓨터 시스템 및 실습
18
Process States (2)
 Lowest layer of process-structured OS
– handles interrupts, scheduling
 Above that layer are sequential processes
ITEC502 컴퓨터 시스템 및 실습
19
Process States – 5 states
 As a process executes, it changes state
–
–
–
–
new: The process is being created
running: Instructions are being executed
waiting: The process is waiting for some event to occur
ready: The process is waiting to be assigned to a processor
(CPU)
– terminated: The process has finished execution
 The state of a process is defined in part by the current
activity of that process
ITEC502 컴퓨터 시스템 및 실습
20
Diagram of Process States
 It is important to realize that only one process can be
running on any processor at any instant
 Many processes may be ready and waiting states
ITEC502 컴퓨터 시스템 및 실습
21
Process Control Block (PCB) (1)
 Each process is represented in the operating
system by a process control block (PCB)
 Information associated with each process
–
–
–
–
–
–
–
Process state
Program counter
CPU registers
CPU scheduling information
Memory-management information
Accounting information
I/O status information
ITEC502 컴퓨터 시스템 및 실습
22
Process Control Block (PCB) (2)

Process state
– New, ready, running, waiting, terminated

Program counter
– The address of the next instruction to be executed
for this process

CPU registers
– Accumulators, index registers, stack pointers, and
general-purpose registers, etc.

CPU scheduling information
– Process priority, pointers to scheduling queue, etc.

Memory-management information
– Value of base and limit registers, the page tables
or segment tables, etc.

Accounting information
– The amount of CPU and real time used, time limits,
account number, job and process number

I/O status information
– List of I/O devices allocated, open files, etc.
ITEC502 컴퓨터 시스템 및 실습
23
CPU Switch From Process to Process
ITEC502 컴퓨터 시스템 및 실습
24
Implementation of Processes (1)
Fields of a process table entry
ITEC502 컴퓨터 시스템 및 실습
25
Implementation of Processes (2)
Skeleton of what lowest level of OS does when an interrupt occurs
ITEC502 컴퓨터 시스템 및 실습
26
Summary
 A process is a program in execution
 As a process executes, the process may be in
one of the 3 states or 5 states:
– running, blocked, ready
– new, ready, running, waiting, terminated
 Each process is represented in the OS by its PCB
 OS provides a mechanism for parent process to
create a child process
ITEC502 컴퓨터 시스템 및 실습
27
Review
1. Processes
–
–
–
–
–
–
–
Process Concept
The Process Model
Process Creation
Process Termination
Process Hierarchies
Process States
Implementation of Processes
ITEC502 컴퓨터 시스템 및 실습
28