1 / 30

Process

Learn about different types of processes and their states in operating systems, including creation, termination, and hierarchy. Explore process identification, creating and deleting processes, and process synchronization.

judynorton
Download Presentation

Process

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

  2. Processes • Aprocessis an abstraction for sequence of operations that implement a computation/program. • A process may be manipulated, suspended, scheduled and terminated • Process Types: • OS processes executing system code program • User processes executing user code program • Processes are executed concurrently with CPU multiplexing among them

  3. Process States • Possible process states • Running (occupy CPU) • Blocked • Ready (does not occupy CPU) • Other states: suspended, terminated • Why not the following transitions? • Ready to blocked • Blocked to running

  4. program counter of CPU2 13000 Multiple CPUs can run Multiple Processes 1 CPU can run Multiple Processes

  5. Events are: I/O Synchronization Linux 5 State Process Model • Add states for creating and deleting process • Add transitions Timeout, Dispatch, Event Occurs

  6. Process Identification • UNIX identifies processes via unique value • Process ID • Each process has also parent process ID since each process is created from a parent process. • Root process is the ‘init’ process • ‘getpid’ and ‘getppid’ – functions to return process ID (PID) and parent process ID (PPID) • #include <stdio.h> • #include <unistd.h> • int main (void) { • printf(“I am process %ld\n”, (long)getpid()); • printf(“My parent id %ld\n”, (long)getppid()); • return 0; • }

  7. Creating a Process - Fork • Creating a process and executing a program are two different things in UNIX • Fork duplicates a process so that instead on one process you get two--- But the code being executed doesn’t change!!! • Fork returns • 0 if child • -1 if fork fails • Child’s PID if parent process • Child gets new program counter, stack, file descriptors, heap, globals, pid!

  8. Creating a Process in Unix #include <stdio.h> #include <unistd.h> int main(void) { int x; x = 0; fork(); x = 1; printf("I am process %ld and my x is %d\n", (long)getpid(), x); return 0; } What does this print?

  9. UNIX Example #include <stdio.h> #include <unistd.h> #include <sys/types.h> int main(void) { pid_t parentpid; pid_t childpid; if ((childpid = fork()) == -1) { perror(can’t create a new process); exit(1); } else if (childpid == 0) {/* child process executes */ printf(“child: childpid = %d, parentpid = %d \n”, getpid(), getppid()); exit(0); } else { /*parent process executes */ printf(“parent: childpid = %d, parentpid = %d \n”, childpid, getpid()); exit(0); }

  10. UNIX Example childpid = fork() if (childpid == 0) { printf(“child: childpid = %d, parentpid = %d \n”, getpid(), getppid()); exit(0); } else { printf(“parent: childpid = %d, parentpid = %d \n”, childpid, getpid()); exit(0); }

  11. Chain and Fan Chain Fan pid_t childpid = 0; for (i=1;i<n;i++) if ((childpid = fork()) <=0) break; pid_t childpid = 0; for (i=1;i<n;i++) if (childpid = fork()) break; Parent Parent Child Child Child Child … …

  12. Process Operations (Creation) • When creating a process, we need resources such as CPU, memory files, I/O devices • Process can get resources from the OS or from the parent process • Child process is restricted to a subset of parent resources • Prevents many processes from overloading system • Execution possibilities are • Parent continues concurrently with child • Parent waits until child has terminated • Address space possibilities are: • Child process is duplicate of parent process • Child process has a new program loaded into it

  13. Process Termination • Normal exit (voluntary) • End of main() • Error exit (voluntary) • exit(2) • Fatal error (involuntary) • Divide by 0, core dump / seg fault • Killed by another process (involuntary) • Kill procID, end task

  14. Process Operations (Termination) • When a process finishes last statement, it automatically asks OS to delete it • Child process may return output to parent process, and all child’s resources are de-allocated. • Other termination possibilities • Abort by parent process invoked • Child has exceeded its usage of some resources • Task assigned to child is no longer required • Parent is exiting and OS does not allow child to continue without parent

  15. Process Hierarchies • Parent creates a child process, a child process can create its own processes • Forms a hierarchy • UNIX calls this a "process group" • Windows has no concept of process hierarchy • all processes are created equal

  16. wait() Function • wait function allows parent process to wait (block) until child finishes • wait function causes the caller to suspend execution until child’s status is available • waitpid function allows a parent to wait for a particular child

  17. Waiting for a child to finish – C Manual #include <errno.h> #include <sys/wait.h> pid_t childpid; childpid = wait(NULL); if (childpid != -1) printf(“waited for child with pid %ld\n”, childpid);

  18. Waiting for a child to finish: RR:72 #include <errno.h> #include <sys/wait.h> pid_t r_wait(int *stat_loc) { int retval; while (((retval = wait(stat_loc)) == -1) && (errno == EINTR)) ; return retval; } • Restarts wait if interrupted by a signal • Part of Restart library in RR, Appendix B

  19. Scheduling vs Dispatcher? • Scheduler decides which process to run next • Dispatcher gives control to the next process selected by the scheduler • Switching process context • Switching to user mode • Jumping to the proper location in the user program to restart that program

  20. Wait for any child of current process Waitpid returns 0 to report there are possible unwaited-for children but their status is not available Restart waitpid if function interrupted by signal or successfully waited for child Wait for all children that have finished pid_t childpid; while (childpid = waitpid(-1, NULL, WNOHANG)) { if ((childpid == -1) && (errno != EINTR)) break; } // keep waiting

  21. Dead children may become zombies • When a process terminates but its parent does not wait for it? • Zombie (pid entry remains in system process table) • Zombies unlike orphans do not consume many resources. • Professional code installs signal handler for signal SIGCHLD … which issues a wait() call

  22. What happens • When the parent process terminates first? • “Child is orphaned” • Child is “re-parented” to init process • Child continues to consume resources • init process (id=1) waits for children

  23. Status Values for wait(int *stat_loc) The following macros are available for checking the return status of a child: #include <sys/wait.h> WIFEXITED(int stat_val) WEXITSTATUS(int stat_val) WIFSIGNALED(int stat_val) WTERMSIG(int stat_val) WIFSTOPPED(int stat_val) WSTOPSIG(int stat_val) They are used in pairs.If WIFEXITED returns true, the child executed normally and the return status (at most 8 bits) can be gotten with WEXITSTATUS.

  24. Simple example (without checking EINTR) int status; childpid = wait(&status); if (childpid == -1) perror("Failed to wait for child"); else if (WIFEXITED(status) && 0==WEXITSTATUS(status)) printf(“Child terminated normally”);

  25. Typical process creation code (RR:74) #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main (void) { pid_t childpid; /* set up signal handlers here ... */ childpid = fork(); if (childpid == -1) { perror("Failed to fork");return 1;} if (childpid == 0) fprintf(stderr, "I am child %ld\n", (long)getpid()); else if (wait(NULL) != childpid) fprintf(stderr, "A signal must have interrupted the wait!\n"); else fprintf(stderr, "I am parent %ld with child %ld\n", (long)getpid(), (long)childpid); return 0; }

  26. Results from running program • 6 Possible forms of output!!! • Fork fails (1) • Parent catches signal after child executes fprintf but before child returns (2,3) • Parent catches signal after child terminates and wait returns successfully(2,4) • Parent catches signal between time child terminates and wait returns (2,3) or (2,4) • Parent catches signal before child executes fprintf and parent fprintf happens first (3,2) • Parent catches signal before child executes fprintf and child executes fprintf first (2,3)

  27. exec() Function • Exec function allows child process to execute code that is different from that of parent • Exec family of functions provides a facility for overlaying the process image of the calling process with a new image. • Exec functions return -1 and sets errno if unsuccessful

  28. Argv[0] is by convention program name so need ls in parameters The array of pointers must be terminated by a NULL pointer. Exec Example #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> int main (void) { pid_t childpid; childpid = fork(); if (childpid == -1) { perror("Failed to fork"); return 1; } if (childpid == 0) { /*child code*/ execl(“/bin/ls”, “ls”, “-l”, NULL); perror(“Child failed to exec ls”); return 1; } if (wait(NULL) != childpid) { /* parent code */ perror(“Parent failed to wait due to signal or error”); return 1; } return 0; }

  29. Background Processes and Daemons • Shell – command interpreter creates background processes if line ends with & • When shell creates a background process, it does not wait for the process to complete before issuing a prompt and accepting another command • Ctrl-C does not terminate background process • Daemon is a background process that normally runs indefinitely

  30. Daemons • Not connected to a terminal (tty) • Create logs in /var/log/ • Parent process “forks, disconnects tty and dies” so they are orphans • Event driven • Examples: ftpd, sshd, pageout daemon, httpd,mysqld • Special privileges & access rights to hardware • Execute as root or with a specific daemon userid • A target for buffer overflow/Denial of Service attacks

More Related