Example of Fork Programming.
Example of Fork Programming.
h> void main() { printf("one pid=%d\n",getpid()); printf("two pid=%d\n",getpid()); printf("three pid=%d\n",getpid()); } Output: one pid=3515 two pid=3515 three pid=3515
fork1.c #include <stdio.h> #include <sys/types.h> #include <unistd.h> void main() { int pid1; printf("one pid=%d pid1=%d\n",getpid(),pid1); pid1=fork(); printf("two pid=%d pid1=%d\n",getpid(),pid1); printf("three pid=%d pid1=%d\n",getpid(),pid1); } Output: one pid=3545 pid1=4939764 two pid=3545 pid1=3546 three pid=3545 pid1=3546 two pid=3546 pid1=0 three pid=3546 pid1=0 Question: 1. How many process is created? -2 - pid 3545, 3546 2. Explain.
fork2.c #include <stdio.h> #include <sys/types.h> #include <unistd.h> void main() { int pid1=10,pid2=10; printf("one pid=%d pid1=%d pid2=%d\n",getpid(),pid1,pid2); pid1=fork(); printf("two pid=%d pid1=%d pid2=%d\n",getpid(),pid1,pid2); pid2=fork(); printf("three pid=%d pid1=%d pid2=%d\n",getpid(),pid1,pid2); }
Output: one pid=3480 pid1=10 pid2=10 two pid=3480 pid1=3481 pid2=10 three pid=3480 pid1=3481 pid2=3482 three pid=3482 pid1=3481 pid2=0 two pid=3481 pid1=0 pid2=10 three pid=3481 pid1=0 pid2=3483 three pid=3483 pid1=0 pid2=0 Question: 1. How many process is created? -4 - pid 3480, 3481, 3482, 3483 2. Explain.
fork3.c #include <stdio.h> #include <sys/types.h> #include <unistd.h> void main() { int pid1,pid2; pid1=fork(); pid2=fork(); printf("pid1=%d pid2=%d\n",pid1,pid2); }
Question: 1. How many process is created? -4 - pid 3595, 3596, 3597, 3598 2. Explain.
fork4.c #include <stdio.h> #include <unistd.h> #include <sys/types.h> void main() { int i,pid; for (i=0;i<3;i++) { pid=fork(); if (pid<0) { printf("sorry, cannot fork \n"); } else if (pid==0) { printf("pid %d child %d \n",pid,i); } else { printf("pid %d parent %d \n",pid,i); }
Output: pid main() 2848 pid 2849 parent 0 pid 2850 parent 1 pid 2851 parent 2 pid 0 child 2 pid 0 child 1 pid 2852 parent 2 pid 0 child 0 pid 2853 parent 1 pid 2854 parent 2 pid 0 child 2 pid 0 child 2 pid 0 child 1 pid 2855 parent 2 pid 0 child 2 Question: 1. How many process is created? - 8 process - pid 2848, 2849, 2850, 2851, 2852, 2853, 2854, 2855 2. Explain.
fork5.c #include <stdio.h> #include <unistd.h> void main() { int pid; pid=fork(); if (pid>0) { wait((int *)0); printf("ls completed\n"); exit(0); } if (pid==0) { execl("/bin/ls","ls","-l",(char *)0); printf("No return from exec()\n"); } perror("fork failed"); exit(1); } Output: foad@ubuntu:~/test_process/examplefork$ ./a.out total 32 -rwxrwxr-x 1 foad foad 7342 2012-03-05 21:28 a.out -rw-rw-r-- 1 foad foad 250 2012-03-05 19:50 fork1.c -rw-rw-r-- 1 foad foad 314 2012-03-05 20:10 fork2.c -rw-rw-r-- 1 foad foad 166 2012-03-05 20:26 fork3.c -rw-rw-r-- 1 foad foad 331 2012-03-05 21:19 fork4.c -rw-rw-r-- 1 foad foad 294 2012-03-05 21:27 fork5.c -rw-rw-r-- 1 foad foad 186 2012-03-05 19:48 getpid.c ls completed Question: 1. How many process is created? -2 2. Explain.
Operating Systems
Chapter 2
Operating Systems
Chapter 2
22