Download presentation
Presentation is loading. Please wait.
Published byGloria Rich Modified over 8 years ago
1
Lecture 5 Process, Thread and Task September 22, 2015 Kyu Ho Park
2
Computer Engineering Research Laboratory vi fork.c 2
3
Computer Engineering Research Laboratory 3
5
4GB 3GB Stack text data heap
6
Computer Engineering Research Laboratory 6 Process Concept An operating system executes a variety of programs: Batch system – jobs Time-shared systems – user programs or tasks Process – a program in execution; process execution must progress in sequential fashion A process includes: program counter stack data section
7
7 Process in Memory Stack Heap BSS Data Text SP PC
8
Virtualization 8 Processes are provided with 2 virtualizations: Virtualized Processor Virtualized Memory
9
Computer Engineering Research Laboratory 9 Process State As a process executes, it changes state new: The process is being created running: Instructions are being executed waiting: The process is waiting for some event to occur ready: The process is waiting to be assigned to a process terminated: The process has finished execution
10
Computer Engineering Research Laboratory 10 Diagram of Process State terminated running ready new admitted waiting interrupt scheduler dispatch I/O or event waitI/O or event completion exit
11
Computer Engineering Research Laboratory 11 Process Control Block (PCB) Information associated with each process Process state Program counter CPU registers CPU scheduling information Memory-management information Accounting information I/O status information
12
12 Process management Registers Program counter Program status word Stack pointer Process state Time when process started CPU time used Children ’ s CPU time Time of next alarm Message queue pointers Pending signal bits Process id Various flag bits Memory management Pointer to text segment Pointer to data segment Pointer to bss segment Exit status Signal status Process id Parent process Process group Real uid Effective Real gid Effective gid Bit maps for signals Various flag bits Files management UMASK mask Root directory Working directory File descriptors Effective uid Effective gid System call parameters Various flag bits Some of the fields of the MINIX process table Process Control Block(PCB)
13
File table fd segment table page table memory task_struct eip sp eflag s eax … cs … CPU specific state of this task Swap or a.out disk memory context system context hardware context
14
Computer Engineering Research Laboratory 14/3 9 Process Descriptor What each process is doing!! This Section (Processor Descriptor) is subdivided into Process State Identifying a Process Relationships Among Processes How process are organized Process Resource Limits Process Descriptor Process Switching Creating Processes Destroying Processes
15
Computer Engineering Research Laboratory 15/3 9 Process Descriptor Overview of the Linux Process Descriptor ( struct task_struct ) This seminar (chapter 3) focus on Process State TASK_RUNNING TASK_INTERRUPTIBLE TASK_UNINTERRUPTIBLE TASK_STOPPED TASK_TRACED TASK_ZOMBIE EXIT_DEAD Process parent/child Relationship Process Descriptor Process Switching Creating Processes Destroying Processes
16
Computer Engineering Research Laboratory Task List Representation of a process: A process descriptor of the type struct task_struct // 1.7KBytes on a 32-bit machine Task list: A circular doubly linked list of task_struct 16
17
17 Threads 17 So far a process is a single thread of execution. The single thread of control allows the process to perform only one task. The user cannot simultaneously type in characters and run the spell checker with the same process. Therefore modern OSs have extended the process concept to allow a process to have multiple threads of execution.
18
Computer Engineering Research Laboratory 18 Program counter Thread Process Computer (a) (b) (a)Three processes each with one thread. (b)One process with three threads. Process and Threads
19
Computer Engineering Research Laboratory 19 Thread Usage[Tanenbaum] A word processor with three threads
20
Computer Engineering Research Laboratory Linux Implementation of Threads In the Linux, each thread has a unique task_struct. Linux implements all threads as standard processes. A thread in Linux is just a process that shares certain resources( such as an address space). 20
21
21 A Thread Stack Heap BSS Data Text SP PC. open files. Registers Resources
22
22 Sharing of threads open files. SP PC. Registers Resources Stack1 Heap BSS Data Stack2 Text SP PC. Registers Thread1 Thread2
23
23 CPU Switch From Process to Process P 0P 0 Save state into PCB0 reload from PCB1 Save to PCB1 Reload from PCB0 OSP 1P 1 idle
24
Computer Engineering Research Laboratory 24 Operations on Processes: Process Creation Parent process create children processes, which, in turn create other processes, forming a tree of processes Possibilities of Resource sharing Parent and children share all resources Children share subset of parent’s resources Parent and child share no resources Possibilities of Execution Parent and children execute concurrently Parent waits until children terminate
25
Computer Engineering Research Laboratory 25 Process Creation (Cont.) Possibilities of Address space Child duplicate of parent Child has a program loaded into it UNIX examples fork system call creates a new process exec system call used after a fork to replace the process’ memory space with a new program
26
Computer Engineering Research Laboratory fork() 26 Initial Process fork() Returns a new pid(child process) Returns zero Original Process Continues New Process:Child
27
27 fork() Stack Heap BSS Data Text. fork( ). SP PC. open files. Registers Resources pid=1000 ID Stack Heap BSS Data Text. fork( ). SP PC. open files. Registers Resources pid=1001 ID Stack Heap BSS Data Text. fork( ). SP PC. open files. Registers Resources pid=1000 ID Parent Child
28
Computer Engineering Research Laboratory fork() 28
29
Computer Engineering Research Laboratory forkOut 29
30
Computer Engineering Research Laboratory fork() 30 pid=999; fork() pid=1000; parent child pid=1234;pid=1452;pid=1345; running ready
31
Computer Engineering Research Laboratory fork() int main(){ int i; for(i=0; i<10; i++){ printf(“Process_id=%d, i=%d\n”, getpid(), i); if(i==5){ printf(“Process_id=%d: fork() to start\n”,getpid()); int forkValue=fork(); printf(“forkValue=%d\n”, forkValue); } 31
32
Computer Engineering Research Laboratory fork() 32
33
Computer Engineering Research Laboratory fork( ) output 33
34
Computer Engineering Research Laboratory fork()
35
Computer Engineering Research Laboratory fork() output
36
Computer Engineering Research Laboratory Process creation
37
Computer Engineering Research Laboratory
38
Thread creation
39
Computer Engineering Research Laboratory Thread creation
40
Creation of a process 40 fork() system call : It creates a new process by duplicating an existing one. The process that calls fork() is the parent, and the new process is the child. pid = fork(); in the parent process, pid is the child process ID and in the child process, pid=0; execve() system call : It creates a new address space and load a new program into it. int execve(const char *filename, char *const argv[], char *const envp[]); argv: command line argument; envp: path name, etc;
41
wait exit() exec() fork() parent resumes child fork() - exec()-wait()
42
Computer Engineering Research Laboratory forkWait
43
Computer Engineering Research Laboratory fork()-wait() output
44
Computer Engineering Research Laboratory forkWaitExeclp
45
Computer Engineering Research Laboratory output
46
Computer Engineering Research Laboratory 46 Process Termination Process executes last statement and asks the operating system to delete it (exit) Output data from child to parent (via wait) Process’ resources are deallocated by operating system Parent may terminate execution of children processes (abort) Child has exceeded allocated resources Task assigned to child is no longer required If parent is exiting Some operating system do not allow child to continue if its parent terminates All children terminated - cascading termination
47
Computer Engineering Research Laboratory 47
48
Computer Engineering Research Laboratory Duplicating a process image #include pid_t fork(void); 48
49
Computer Engineering Research Laboratory Replacing a process image #include char **environ; int execl(const char *path, const char *arg0, …,(char *)0); int execlp(const char *file, const char *arg0,…,(char *)0); int execle(const char *path, const char *arg0,…,(char *)0,char *const envp[]); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); int execve(const char *path, char *const argv[],char *const envp[]); 49
50
Computer Engineering Research Laboratory Examples of exe*() #include char *const ls_argv[]={“ls”,”-l”,0}; char *const ls_envp[]={“PATH=bin:/usr/bin”,”TERM=console”,0}; execl(“/bin/ls”,”ls”,”-l”,0); execlp(“ls”,”ls”,”-l”,0); execle(“/bin/ls”,”ls”,”-l”,0,ls_envp); execv(“/bin/ps”,ls_argv); execvp(“ls”,ls_argv); execve(“/bin/ls”, ls_argv, ls_envp); 50
51
Computer Engineering Research Laboratory Waiting for a process #include pid_t wait(int *stat_loc); The parent process executing wait(), pauses until its child process stops. The call returns the pid of the child process 51
52
Computer Engineering Research Laboratory If(pid !=0){ int stat; pid_t pid_child; pid_child = wait(&stat); printf(“Child has finished,pid_child=%d\n”,pid_child); if(stat !=0) printf(“Child finished normally\n”); else printf(“Child finished abnormally\n”); } 52
53
Computer Engineering Research Laboratory Zombie Processes 53 Terminated running ready new admitted waiting interrupt scheduler dispatch I/O or event waitI/O or event completion exit TTerminated Exit_Zombie Wait( )
54
Computer Engineering Research Laboratory 54
55
Computer Engineering Research Laboratory 55
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.