1 / 12

Process in UNIX

Process in UNIX. Process Identification. ‘ getpid ’ and ‘ getppid ’ – functions to return process ID (PID) and parent process ID (PPID) #include &lt;stdio.h&gt; #include &lt;unistd.h&gt; int main (void) { printf(“I am process %ld<br>”, (long)getpid()); printf(“My parent id %ld<br>”, (long)getppid());

Download Presentation

Process in UNIX

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 in UNIX

  2. Process Identification ‘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; }

  3. Creating a Process - Fork 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!

  4. 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?

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

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

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

  8. 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);

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

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

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

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

More Related