What is a Process?
Download
Report
Transcript What is a Process?
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)
argc, argv
env. variables
stack
heap
Uninitialized data [bss]
(initialized to 0 by exec)
Initialized data
text (code)
<user space>
User level context
4
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
memory context
system context
HW context
7
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
System level Context
Process text, data, user stack, and shared memory
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
int
char
glob = 6;
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) {
glob++; var++;
} else
sleep(2);
}
/* child */
/* parent */
printf(“pid = %d, glob = %d, var = %d\n”, getpid(), glob, var);
exit (0);
Source : Adv. programming in the UNIX Env., pgm 8.1)
15
Memory image of a process (Example)
< 단국대 최종무 교수님 슬라이드>
16
Before fork()
task_struct
Segment
(vm_area_struct)
…
movl %eax, [glob]
addl %eax, 1
movl [glob], %eax
...
text
pid = 11
var, pid
stack
glob, buf
data
17
After fork()
memory
task_struct
Segment
(vm_area_struct)
glob, buf
data
pid = 11
text
var, pid
stack
task_struct
Segment
(vm_area_struct)
glob, buf
pid = 12
data
var, pid
stack
18
Fork with COW (Copy-On-Write)
after “glob++” operation
after fork with COW
Segment
task_struct (vm_area_struct)
pid = 11
memory
task_struct
text
Segment
(vm_area_struct)
pid = 11
stack
Segment
task_struct (vm_area_struct)
data
text
stack
Segment
task_struct (vm_area_struct)
pid = 12
data
pid = 12
data
19
exec concept
• Replace memory context with new binary program (loader) and execute
task_struct
Segment
(vm_area_struct)
data
pid = 11
text
a.out
header
stack
text
data
text
bss
stack
data
stack
20
exec example
int main()
{
printf("before exec\n");
execl("exec_example", "exec_example", 0);
printf("after exec\n");
return 0;
}
6 syntaxes for exec()
execl(), execv(), execlp(), execvp(), execle(),
execve() : will be covered in next lectures
21