1 / 66

Understanding Process Management in Operating Systems

Explore the creation of processes and threads, tasks, and major process management topics in operating systems such as memory management, process states, and Process Control Blocks (PCBs).

dfisk
Download Presentation

Understanding Process Management in Operating Systems

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. September 22, 2015 Kyu Ho Park Lecture 5Process, Thread and Task

  2. Major Topics of Lect. 5 • How to create a process. • How to create a thread. • What is the task?

  3. creating a process

  4. creating a process

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

  6. Process in Memory Stack SP Heap Data Text PC

  7. Memory Map of a process

  8. Memory Map of a process 4GB 3GB Stack heap data text

  9. Virtualization • Processes are provided with 2 virtualizations: • Virtualized Processor • Virtualized Memory

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

  11. Diagram of Process State terminated new admitted exit interrupt ready running scheduler dispatch I/O or event completion I/O or event wait waiting

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

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

  14. Process Descriptor Process Descriptor Process Switching Creating Processes Destroying Processes • 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

  15. Task List • Representation of a process: A process descriptor of the type struct task_struct //<linux/sched.h> 1.7KBytes on a 32-bit machine • Task list: A circular doubly linked list of task_struct

  16. CPU Switch From Process to Process P0 OS P1 Save state into PCB0 reload from PCB1 idle Save to PCB1 Reload from PCB0

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

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

  19. fork() Initial Process fork() Returns a new pid(child process) Returns zero Original Process Continues New Process:Child

  20. fork() Registers Registers Registers SP PC . . Stack SP PC . . SP PC . . Stack Stack Resources Resources Resources PC open files . . open files . . open files . . Heap Heap Heap BSS BSS BSS ID ID ID Data Data Data pid=1000 pid=1000 pid=1001 Text . fork( ) . Text . fork( ) . Text . fork( ) . Child Parent PC PC

  21. fork()

  22. forkOut

  23. fork() pid=999; fork() pid=1000; running parent child pid=1234; pid=1345; pid=1452; ready

  24. 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); } } }

  25. fork( ) output

  26. Creation of a process • 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;

  27. fork() - exec()-wait() wait resumes parent fork() exec() exit() child

  28. forkWait

  29. fork()-wait() output

  30. forkWaitExeclp

  31. output

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

  33. forkWait.c

  34. Duplicating a process image #include <sys/types.h> #include <unistd.h> pid_t fork(void); -return vaule of fork() : pid of the child if successful to the parent, 0 to the child. If failed, -1 to the parent. • unistd.h : fork() prototype is defined. • types.h : pid_t is defined.

  35. fork() Original process • #include <unistd.h> • #include <sys/types.h> • #include <stdio.h> • #include <stdlib.h> • main() • { pid_t pid; • printf("Start!\n"); • pid = fork(); • if( pid == 0) printf("I am the Child !\n"); • else if (pid > 0) printf("Parent pid=%d, Child pid=%d\n", (int)getpid(),pid); • else printf("fork() failed!\n");} PC pid ==0 pid > 0 Parent process Child process • #include <unistd.h> • #include <sys/types.h> • #include <stdio.h> • #include <stdlib.h> • main() • { pid_t pid; • printf("Start!\n"); • pid = fork(); • if( pid == 0) printf("I am the Child !\n"); • else if (pid > 0) printf("Parent pid=%d, Child pid=%d\n", (int)getpid(),pid); • else printf("fork() failed!\n");} • #include <unistd.h> • #include <sys/types.h> • #include <stdio.h> • #include <stdlib.h> • main() • { pid_t pid; • printf("Start!\n"); • pid = fork(); • if( pid == 0) printf("I am the Child !\n"); • else if (pid > 0) printf("Parent pid=%d, Child pid=%d\n", (int)getpid(),pid); • else printf("fork() failed!\n");} PC+1 PC+1

  36. Replacing a process image #include <unistd.h> 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[]);

  37. Examples of exe*() #include <unistd.h> 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);

  38. Waiting for a process #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *status); The parent process executing wait(), pauses until its child process stops. The call returns the pid of the child process

  39. status • status: it is the value transferred to the parent by exit(int status). Parent process: wait(&status) Child process: exit(1); exit(1) root@ubuntu:~/Test/FORK# ./forkwait Start! Parent pid=25820, Child pid=25821 I am the Child ! status=256

  40. If(pid !=0){ int stat; pid_t pid_child; pid_child = wait(&status); printf(“Child has finished,pid_child=%d\n”,pid_child); if(status !=0) printf(“Child finished normally\n”); else printf(“Child finished abnormally\n”); }

  41. forkwait.c #include <unistd.h> #include <sys/types.h> #include <stdio.h> #include <stdlib.h> main() { pid_t pid; int status; printf("Start!\n"); pid = fork(); if( pid == 0) { printf("I am the Child !\n"); exit(100); } else if (pid > 0){ printf("Parent pid=%d, Child pid=%d\n", (int)getpid(),pid); wait(&status); printf("status=%d\n", status); } else printf("fork() failed!\n"); } ./forkwait root@ubuntu:~/Test/FORK# ./forkwait Start! Parent pid=25789, Child pid=25790 I am the Child ! status=25600 root@ubuntu:~/Test/FORK# vi forkwait.c

  42. exit() #include <stdlib.h> void exit(int status); /* Terminating current process and transfer the status to the parent. The value of status ranges from 0 to 255 integer value. */

  43. waitpid( ) #include <sys/types.h> #include <sys/wait.h> pid_t waitpid(pid_t pid, int *status, int options); pid : pid of the child, status : transferred from the child executing exit(status), options 0 : usual wait state, that is , the parent waits until the child process finishes, WNOHANG :the parent process does not stay at ‘wait’ state.

  44. Zombie Processes Terminated new admitted exit interrupt ready running Exit_Zombie scheduler dispatch Wait( ) I/O or event completion I/O or event wait waiting TTerminated

  45. forkZombie.c

  46. forkZombie.c

  47. Threads • 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. 47

  48. Computer Computer Program counter Process Thread (b) (a) Process and Threads • Three processes each with one thread. • One process with three threads.

  49. Thread Usage[Tanenbaum] A word processor with three threads

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

More Related