1 / 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 high-level program vs. binary program Process : actual execution of the instructions

corbettm
Download Presentation

The Structure of Processes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Structure of Processes <단국대학교 최종무 교수님 강의노트 참조>

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

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

  4. Memory image of a process argc, argv env. variables stack heap Uninitialized data [bss] (initialized to 0 by exec) Initialized data text (code) <user space> User level context • Segment layout • Text : program code • Data : global variables • Stack : local variables, parameters • Heap : dynamically allocated space (eg. Malloc)

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

  6. 각 프로세스별 커널이 관리하여야 할 정보 • 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

  7. memory context Task Structure system context HW context

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

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

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

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

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

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

  14. Fork() concept Create a new process(child) that has the same context with the previous process(parent)

  15. fork() example 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++; } else sleep(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)

  16. Memory image of a process (Example) < 단국대 최종무 교수님 슬라이드>

  17. Before fork() … movl %eax, [glob] addl %eax, 1 movl [glob], %eax ... Segment (vm_area_struct) task_struct text pid = 11 var, pid stack glob, buf data

  18. After fork() memory glob, buf Segment (vm_area_struct) data task_struct pid = 11 text var, pid stack Segment (vm_area_struct) task_struct glob, buf pid = 12 data var, pid stack

  19. Fork with COW (Copy-On-Write) after fork with COWafter “glob++” operation memory Segment (vm_area_struct) Segment (vm_area_struct) task_struct task_struct data pid = 11 text pid = 11 text stack stack Segment (vm_area_struct) Segment (vm_area_struct) task_struct task_struct pid = 12 pid = 12 data data

  20. a.out header text data bss stack exec concept • Replace memory context with new binary program (loader) and execute Segment (vm_area_struct) data task_struct pid = 11 text stack text data stack

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

More Related