1 / 36

The Process

The Process . CIS 370, Fall 2013 Umass Dartmouth. The notion of a process. In UNIX a process is an instance of a program in execution A job or a task Each process has program code, data segment, program stack etc.

Download Presentation

The 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. The Process CIS 370, Fall 2013 Umass Dartmouth

  2. The notion of a process • In UNIX a process is an instance of a program in execution • A job or a task • Each process has program code, data segment, program stack etc. • The shell (or any command interpreter) starts a process for each command submitted.

  3. Process examples • $ cat file1 file2 • results in the shell creating a process specifically for running this command • $ ls | wc • results in the shell creating two processes where the output of ls is sent (via the pipe) as in input to wc • Processes are different from programs • a single program may create several processes.

  4. How to create a process • UNIX provides several mechanisms for creating processes. • fork - creates a new process by duplicating the calling process. • exec - perform the transformation of a process by overlaying its memory space with a new program. • wait - provides the ability for a process to be halted while a related process finishes. • exit - used to terminate a process.

  5. The fork system call • Usage : #include <sys/types.h> #include <unistd.h> pid_t fork(void); • A successful call will result in the creation of a new process that is more or less identical to the calling process.

  6. The Process • The newly created process is called the child process and the process that made the call is called the parent process. • After the system call is made successfully both processes begin execution at the statement after the call to fork. • Both processes will have identical data and code segments. All the variables will have the same values as before the fork call.

  7. PC PC PC PC OUTPUT printf(“one\n”); pid = fork(); printf(“two\n”); $ one $ two $ two Before fork()‏ After printf(“one\n”); pid = fork(); printf(“two\n”); printf(“one\n”); pid = fork(); printf(“two\n”);

  8. Process IDs • fork() returns two values, unlike most system calls. • fork() returns the process id of the newly created process to the parent. • fork() returns a 0 to the newly created process. • Since all the variables are common, this can be used to synchronize and distinguish the child process from the parent process.

  9. The exec family of calls • Unlike fork, exec will replace the calling process. • fork replicates the calling process, exec will create a new process and destroys the called process. The new process will inherit the process id of the called process. • What is the point? What is interesting about simply the same process over and over again?

  10. PC PC PC printf (…)‏ execl(“bin/ls”,…)‏ Before exec After 1st line of ls

  11. Data and File Sharing in fork • In fork(), an identical copy of the parent process is made. • All the variables are identical and all files open in the parent will remain open in the child process. • Since the child process occupies a different memory location, modifications are local. • Open files are however different. The read-write pointer is maintained by the OS.

  12. Data and File Sharing in exec • Open file descriptors are also passed across exec calls. • The read-write pointer remains in the same position as in the process that called exec. • What about variables and their values? • The system call fcntl can be used to set the close-on-exec flag. When this flag is set the file is closed when the exec calls is made.

  13. #include<fcntl.h> int fd; fd = open(“file”, O_RDONLY); . /* set close-on-exec flag on */ fcntl(fd, F_SETFD, 1); /*set close-on-exec flag off */ fcntl (fd, F_SETFD, 0); /* the value can be obtained as follows */ res = fcntl(fd, F_GETFD, 0);

  14. Terminating a process - exit • Usage • #include < stdlib.h> • void exit(int status);

  15. Synchronizing Processes - the wait system call • Usage • #include<sys/types.h> • #include<sys/wait.h> • pid_t wait (int *status); • wait() temporarily suspends the process until its child process finishes execution.

  16. More on wait • The call to wait returns the process id of the child process that exits • The argument to wait is an integer pointer that contains the exit code of the exiting child process. • If there are n child processes, n wait statements would be needed if the parent is to continue after all the child processes have exited.

  17. Waiting for a specific child • Usage • #include<sys/types.h> • #include<sys/wait.h> • pid_t waitpid(pid_t pid, int *status, int options); • The first argument specifies the id of the process the parent is waiting for (if this is set to -1 and the options to 0, this call behaves exactly like wait() ).

  18. waitpid system call • The second argument contains the exit status of the child. • The final argument options can take a variety of values defined in <sys/wait.h> • The most useful is the option WNOHANG • This allows waitpid to sit in a loop, without blocking. • waitpid returns the process id of the child process once terminated.

  19. Zombies and Orphans • A child process becomes a zombie when it exits before the parent process issues a wait. This results in the process occupying space in the system’s process table but nothing else. • If a parent process exits before a child process is done, that process is “orphaned” and is “adopted” by the OS (and killed!).

More Related