21
The Structure of Processes < 단단단단단 단단단 단단단 단단단단 단단 >

The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

Embed Size (px)

Citation preview

Page 1: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

The Structure of Processes

< 단국대학교 최종무 교수님 강의노트 참조 >

Page 2: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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

Page 3: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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

Page 4: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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

Page 5: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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

Page 6: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

각 프로세스별 커널이 관리하여야 할 정보

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

Page 7: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

Task Structure

7HW context

memory context

system context

Page 8: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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

Page 9: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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

Page 10: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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

Page 11: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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.

Page 12: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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

Page 13: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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

Page 14: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

Fork() concept

Create a new process(child) that has the same context with the previous process(parent)

14

Page 15: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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)

Page 16: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

Memory image of a process (Example)

16

< 단국대 최종무 교수님 슬라이드 >

Page 17: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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...

Page 18: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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

Page 19: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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

Page 20: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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

Page 21: The Structure of Processes. What is a Process? an instance of running program Program vs process(task) Program : just a passive collection of instructions

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;}