Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- int main(int argc, char *argv[]) {
- pid_t pid;
- int status, input, i;
- input = atoi(argv[1]);
- for (i=0; i < input; i++) {
- //while(1) {
- pid = fork();
- switch(pid){
- printf("\t child pid = %d\n", getpid());
- case -1:
- printf("case -1");
- fatal("fork failed");
- break;
- case 0:
- /*Child calls exec*/
- execl("sleep.exe", "sleep.exe", (char *)0);
- printf("\tchild pid = %d\n", getpid());
- printf("\tchild ppid = %d\n", getppid());
- break;
- default:
- /*Parent uses wait to suspend execution until child finishes */
- printf("\tparent pid = %d\n", getpid());
- printf("\tparent pid = %d\n", getppid());
- printf("\tchild exited with %d\n", status);
- exit(0);
- }
- waitpid(pid, &status, 0);
- }
- }
- int fatal(char *s) {
- perror(s);
- exit(1);
- }
Add Comment
Please, Sign In to add comment