Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Process Description and Control

Similar presentations


Presentation on theme: "Chapter 3 Process Description and Control"— Presentation transcript:

1 Chapter 3 Process Description and Control
Operating System Chapter 3 Process Description and Control

2 Chap3 Process Description and Control
3.1 What is a Process 3.2 Process States 3.3 Process Description 3.4 Process Control 3.5 Execution of the Operating System 3.6 Unix SVR4 Process Management 3.7 Linux Process management system calls

3 3.4 Process Control 3.4.1 Modes of Execution 3.4.2 Process Creation
3.4.3 Process Switching

4 3.4.1 Modes of Execution(1/1) User mode
Less-privileged mode User programs typically execute in this mode System mode, control mode, or kernel mode More-privileged mode Kernel of the operating system Translation between the two model Processor status register (psr) and current privileged level (cpl) User mode->System mode System mode->User mode The user-mode program performs the trap instruction to switch into kernel mode.

5 3.4 Process Control 3.4.1 Modes of Execution 3.4.2 Process Creation
3.4.3 Process Switching 3.4.4 Execution of the Operating System

6 3.4.2 Process Creation(1/3) Assign a unique process identifier
Allocate space for the process Initialize process control block Set up appropriate linkages Ex: add new process to linked list used for scheduling queue Create of expand other data structures Ex: maintain an accounting file

7 3.4.2 Process Creation(2/3) Process tree for a Linux system
Processes may create other processes through appropriate system calls, such as fork Init is the root/parent of all processes executing on Linux except process 0.

8 3.4.2 Process Creation(3/3) Linux startup process
The init process is the last step in the boot procedure The init process is the last step in the boot procedure

9 3.4 Process Control 3.4.1 Modes of Execution 3.4.2 Process Creation
3.4.3 Process Switching

10 3.4.3 Process Switching(1/3) All processes begin execution in user mode, and they switch to kernel mode only when obtaining a service provided by the kernel. P137

11 3.4.3 Process Switching(2/3) Mode Switching: User Mode -> Kernel Mode 1. Interrupt Clock interrupt: process has executed for the maximum allowable time slice I/O interrupt Memory fault: Referenced virtual address is not in main memory, so it must be brought in. 2. Trap error or exception occurred, may cause process to be moved to Exit state 3. Supervisor call (System Call) such as file open A process switch may occur any time that the OS has gained control from the currently running process. The possible events that may give control to the OS include interrupt, trap and system call.

12 3.4.3 Process Switching(3/3) Steps involved in a full process switch
Save context of processor including program counter and other registers Update the process control block of the process and change the process's state that is currently in the Running state Move process control block to appropriate queue – ready; blocked; ready/suspend Select another process for execution Update the process control block of the process selected and change it state Update memory-management data structures Restore context of the selected process 12

13 Chap3 Process Description and Control
3.1 What is a Process 3.2 Process States 3.3 Process Description 3.4 Process Control 3.5 Execution of the Operating System 3.6 Unix SVR4 Process Management 3.7 Linux Process management system calls

14 3.5 Execution of the OS(1/4) 1. Nonprocess Kernel(无进程的内核)
Execute kernel outside of any process Operating system code is executed as a separate entity that operates in privileged mode Common on many older OSs

15 3.5 Execution of the OS(2/4) 2. Execution Within User Processes(在用户进程中 执行) Operating system software within context of a user process Process executes in privileged mode when executing operating system code

16 3.5 Execution of the OS(3/4)

17 3.5 Execution of the OS(4/4) 3. Process-Based Operating System(基于进程的 OS) Implement operating system as a collection of system processes Useful in multi-processor or multi-computer environment

18 Chap3 Process Description and Control
3.1 What is a Process 3.2 Process States 3.3 Process Description 3.4 Process Control 3.5 Execution of the Operating System 3.6 Unix SVR4 Process Management 3.7 Linux Process management system calls

19 3.6 Unix SVR4 Process Management(1/4)
Most of the operating system executes within the environment of a user process

20 3.6 Unix SVR4 Process Management(2/4)
UNIX Process States

21 3.6 Unix SVR4 Process Management(3/4)

22 3.6 Unix SVR4 Process Management(4/4)
UNIX Process Image(p.144)

23 Chap3 Process Description and Control
3.1 What is a Process 3.2 Process States 3.3 Process Description 3.4 Process Control 3.5 Execution of the Operating System 3.6 Unix SVR4 Process Management 3.7 Linux Process management system calls

24 3.7 Linux Process management system calls
In computing, a system call is the programmatic way in which a computer program requests a service from the kernel of the operating system it is executed on. (wikipedia) This may include hardware-related services (for example, accessing a hard disk drive), creation and execution of new processes, and communication with integral kernel services such as process scheduling. Popular system calls are open, read, write, close, wait, exec, fork, exit, and kill...

25 3.7 Linux Process management system calls

26 3.7 Linux Process management system calls
3.7.1 Process Creation 3.7.2 Process Termination

27 3.7.1 Process Creation(1/5) Creating a Process - fork()
Create a child process The child is an (almost) exact copy of the parent The new process and the old process both continue in parallel from the statement that follows the fork() Returns: To child 0 on success To parent process ID of the child process -1 on error, sets errno #include <sys/types.h> #include <unistd.h> pid_t fork(void); pid_t myid = getpid() pid_t: int in linux, pid_t: long in other systems

28 3.7.1 Process Creation(2/5) Understanding fork()
Fork is interesting (and often confusing) because it is called once but returns twice pid_t pid = fork(); if (pid == 0) { printf("hello from child\n"); } else { printf("hello from parent\n"); }

29 3.7.1 Process Creation(3/5) Both processes start with same state
Each has private copy Including shared output file descriptor Relative ordering of their print statements (and so variable manipulations) is undefined void fork1() { int x = 1; pid_t pid = fork(); if (pid == 0) { printf("Child has x = %d\n", ++x); } else { printf("Parent has x = %d\n", --x); } printf("Bye from process %d with x = %d\n", getpid(), x);

30 3.7.1 Process Creation(4/5) Three consecutive forks #define bork fork
void forkbork() { bork(); bork(); bork(); printf(”borked\n"); }

31 3.7.1 Process Creation(5/5) Three consecutive forks void fork3() {
printf("L0\n"); fork(); printf("L1\n"); printf("L2\n"); printf("Bye\n"); }

32 3.7 Linux Process management system calls
3.7.1 Process Creation 3.7.2 Process Termination

33 3.7.2 Process Termination(1/11)
Voluntary termination Normal exit End of main() Error exit exit(2) Involuntary termination Fatal error Divide by 0, core dump / seg fault Killed by another process kill procID, end task

34 3.7.2 Process Termination(2/11)
exit() Example void exit(int status) Exits a process Normally return with status 0 atexit() Registers functions to be executed upon exit void cleanup(void) { printf("cleaning up\n"); } void fork4() { atexit(cleanup); fork(); exit(0);

35 3.7.2 Process Termination(3/11)
Zombie Process A process that has completed execution (via the exit system call) but still has an entry in the process table. Living corpse, half alive and half dead

36 3.7.2 Process Termination(4/11)
Zombie Process Reaping Performed by parent on terminated child (using wait or waitpid) Parent is given exit status information Kernel discards process What if parent doesn't reap? If any parent terminates without reaping a child, then child will be reaped by init process (pid == 1) So, only need explicit reaping in long-running processes e.g., shells and servers

37 3.7.2 Process Termination(5/11)
Zombie Example void fork5() { if (fork() == 0) { /* Child */ printf("Terminating Child, PID = %d\n", getpid()); exit(0); } else { printf("Running Parent, PID = %d\n", getpid()); while (1); /* Infinite loop */ } 在 Unix 进程模型中,父进程和其所产生的子进程是异步运行的,所以如果子进程在结束后,会留下一些信息需要父进程使用 wait/waitpid 来接收。而如果父进程太忙了,没有调用 wait/waitpid 的话,子进程就会变成僵尸进程。

38 3.7.2 Process Termination(6/11)
Orphan Process In a Unix-like operating system any orphaned process will be immediately adopted by the special init system process: the kernel sets the parent to init. This operation is called re-parenting and occurs automatically. Even though technically the process has the "init" process as its parent, it is still called an orphan process since the process that originally created it no longer exists.

39 3.7.2 Process Termination(7/11)
Orphan Example void fork6() { if (fork() == 0) { /* Child */ printf("Running Child, PID = %d\n", getpid()); while (1) ; /* Infinite loop */ } else { printf("Terminating Parent, PID = %d\n", getpid()); exit(0); } Child process still active even though parent has terminated. Must kill explicitly, or else will keep running indefinitely.

40 3.7.2 Process Termination(8/11)
Waiting for a child to finish – wait() Suspend calling process until child has finished Allow parent to reap child Returns: Process ID of terminated child on success -1 on error, sets errno Parameters: status: status information set by wait and evaluated using specific macros defined for wait #include <sys/types.h> #include <wait.h> pid_t wait(int *status); 进程一旦调用了wait(), 就立即阻塞自己,由wait自动分析是否有当前进程的某个子进程已经退出,如果让它找到了一个已经变成僵尸的子进程,wait就会收集这个子进程的信息,并把它彻底销毁后返回;如果没有找到这样一个子进程,wait就会一直阻塞在这里,直到一个子进程结束或者该进程接到了一个指定的信号为止。

41 3.7.2 Process Termination(9/11)
wait() example void fork7() { int child_status; if (fork() == 0) { printf("HC: hello from child\n"); } else { printf("HP: hello from parent\n"); wait(&child_status); printf("CT: child has terminated\n"); printf("Bye\n"); exit(0);

42 3.7.2 Process Termination(10/11)
Waiting for a child to finish – waitpid() Suspend calling process until child specified by pid has finished Returns: Process ID of terminated child on success 0 if WNOHANG and no child available, sets errno -1 on error, sets errno Parameters: status: status information set by wait and evaluated using specific macros defined for wait. #include <sys/types.h> #include <wait.h> pid_t waitpid(pid_t pid, int *status, int options); waitpid()的作用和wait()一样,但它并不一定要等待第一个终止的子进程,它还有若干选项,如可提供一个非阻塞版本的wait()功能等。实际上wait()函数只是waitpid()函数的一个特例,在Linux内部实现wait()函数时直接调用的就是waitpid()函数。 wait()函数的实现: static inline pid_t wait(int *wait_stat) { return waitpid(-1,wait_stat,0); }

43 3.7.2 Process Termination(11/11)
waitpid() example void fork8() { pid_t pc,pr; if ( (pc=fork())== 0) { sleep(5); exit(0); } else { do { pr=waitpid(pc,NULL,WNOHANG); if (pr==0) { printf("The child process has not exited \n"); sleep(1); } }while(pr==0); if(pr==pc) printf("Get child exit code: %d\n ",pr); else printf("Some error occured. \n");

44 Homework Review Questions: 3.5 3.10 Problems: 3.1 3.3

45 Questions: Next class, I will ask the one who is luck enough to describe it:

46 Questions:


Download ppt "Chapter 3 Process Description and Control"

Similar presentations


Ads by Google