Upload
gerald-hawkins
View
222
Download
1
Tags:
Embed Size (px)
Citation preview
The Structure of Processes
< 단국대학교 최종무 교수님 강의노트 참조 >
What is a Process?
an instance of running program
Program vs process(task) Program : just a passive collection of instructions
high-level program vs. binary program Process : actual execution of the instructions Several processes may be associated with one program
In addition to program code, necessary resources (memory, CPU, etc) are allocated to process
2
Process in detail
Program in execution having its own memory space (text, data, stack,..) One program may have many processes Independent of each other (protection) Scheduling entity Executed in CPU (having registers) Competing for system resources pid, context, priority, allocation resources
3
Memory image of a process
Segment layout Text : program code Data : global variables Stack : local variables, parameters Heap : dynamically allocated space
(eg. Malloc)
4
argc, argvenv. variables
stack
heap
Uninitialized data [bss](initialized to 0 by exec)
Initialized data
text (code)
<user space>User level context
Multi-tasking
Running multiple processes in a system Requirements
Which to run? (scheduling) Which memory to allocate? (virtual memory) Maintain process information (pid, state, context, …)
uni-processor vs multi-processor
5
각 프로세스별 커널이 관리하여야 할 정보
task_struct Task Identification : process id, (real/effective) user-id, group-id, … Task State Task relationship : pointer to parent, siblings Scheduling information : policy, priority, counter Signal information: received signal , signal handler Memory information : virtual memory information, mm_struct File information : file descriptor tables (files_struct, fs_struct) Thread structure : CPU information(register context)
Task 가 어디까지 실행했는지 기억 (PC,SP, general purpose register, 등 )
Time information : start time, CPU time Inter-Process Communication information : signal (handler), … Executable format : Resource limit 6
Task Structure
7HW context
memory context
system context
Process States and Transitions
Task running (ready, running) Waiting (interruptible, uninterruptible)
waiting for an event or resource Stopped (task_stopped: by receiving a signal, task_traced: by debugger) Zombie
Most task structures are freed. Will be dead after wait() is called.
8
Linux Scheduler
select the most deserving process to run out of all of the runnable processes
use simple priority based scheduling algorithm Context switch
after choosing a new process to run, it saves the state of the current process (the processor specific registers and other context) being saved in the process’s task_struct data structure.
It then restores the state of the new to run and gives control of the system to that process.
Schduling information policy(normal/realtime,round-robin/FIFO), priority, counter
9
Process Context
User-level (memory) Context Process text, data, user stack, and shared memory
System level Context task structures
Hardware(register) Context Program counter (PC), process status (PS) register stack pointer (SP), general-purpose registers
10
CPU execution mode place restrictions on the operations that can be performed by the process
currently running in the CPU
Kernel mode When the CPU is in kernel mode, it is assumed to be executing trusted software,
and thus it can execute any instructions and reference any memory addresses (i.e., locations in memory).
The kernel (which is the core of the operating system and has complete control over everything that occurs in the system) is trusted software, but all other programs are considered untrusted software.
User mode It is a non-privileged mode in which each process (i.e., a running instance of a
program) starts out. It is non-privileged in that it is forbidden for processes in this mode to access those portions of memory (i.e., RAM) that have been allocated to the kernel or to other programs.
Layout of System Memory Physical address space
impossible for two processes to execute concurrently if their set of generated addresses overlapped.
Virtual address space Allows many processes to share finite amount of physical
memory Each process uses the same virtual addresses but
reference different physical addresses Requires mechanism for translating virtual address to
physical address
12
Regions
Region (segment) : vm_area_struct Contiguous area of virtual address space of a
process that can be treated as a distinct object to be shared or protected.
Virtual address space of a process is divided into logical regions Text : a set of instructions Data : (initialized & uninitialized) data variables Stack : data structures local to a subroutine
13
Fork() concept
Create a new process(child) that has the same context with the previous process(parent)
14
fork() example
15
int glob = 6;char buf[] = “a write to stdout\n”;
int main(void){ int var;
pid_t pid;
var = 88;write(STDOUT_FILENO, buf, sizeof(buf)-1);printf(“before fork\n”);
if ((pid = fork()) == 0) { /* child */glob++; var++;
} elsesleep(2); /* parent */
printf(“pid = %d, glob = %d, var = %d\n”, getpid(), glob, var);
exit (0);} Source : Adv. programming in the UNIX Env., pgm 8.1)
Memory image of a process (Example)
16
< 단국대 최종무 교수님 슬라이드 >
Before fork()
17
text
stack
data
Segment(vm_area_struct)task_struct
pid = 11
glob, buf
var, pid
…movl %eax, [glob]addl %eax, 1movl [glob], %eax...
After fork()
18
memory
text
stack
data
Segment(vm_area_struct)task_struct
pid = 11
Segment(vm_area_struct)
task_struct
pid = 12
stack
data
glob, buf
var, pid
var, pid
glob, buf
Fork with COW (Copy-On-Write)
19
after fork with COW after “glob++” operation memory
text
stack
data
Segment(vm_area_struct)task_struct
pid = 11
Segment(vm_area_struct)task_struct
pid = 12
text
stack
data
Segment(vm_area_struct)
task_struct
pid = 11
Segment(vm_area_struct)task_struct
pid = 12
data
exec concept
20
header
text
data
bss
stack
a.out
text
stack
data
Segment(vm_area_struct)task_struct
pid = 11
stack
data
text
• Replace memory context with new binary program (loader) and execute
exec example
6 syntaxes for exec() execl(), execv(), execlp(), execvp(), execle(),
execve() : will be covered in next lectures
21
int main(){ printf("before exec\n"); execl("exec_example", "exec_example", 0); printf("after exec\n"); return 0;}