Example of fork programming. getpid.c #include <stdio.h> #include <sys/types.h> #include <unistd.

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

Question: 1. How many process is created? -1 - pid 3545 2. Explain.

getpid(). Explain. printf("two pid=%d pid1=%d\n".pid1).pid1). printf("three pid=%d pid1=%d\n". } 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.h> void main() { int pid1. 3546 2.h> #include <unistd.c #include <stdio.getpid(). printf("one pid=%d pid1=%d\n".getpid(). .pid1).fork1. pid1=fork().pid 3545. How many process is created? -2 .h> #include <sys/types.

fork2. pid1=fork(). 3482.pid2).getpid(). printf("two pid=%d pid1=%d pid2=%d\n". 3481.getpid().c #include <stdio.pid2).pid1.getpid(). 3483 2.pid1. pid2=fork().pid1.h> #include <sys/types.pid 3480. } 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 .h> #include <unistd.pid2). . printf("three pid=%d pid1=%d pid2=%d\n".h> void main() { int pid1=10. printf("one pid=%d pid1=%d pid2=%d\n".pid2=10. Explain.

} Output: pid1=3596 pid2=3597 pid1=3596 pid2=0 pid1=0 pid2=3598 pid1=0 pid2=0 Question: 1. 3596. .fork3.h> #include <unistd.h> #include <sys/types.pid1.c #include <stdio. How many process is created? -4 . 3597.pid2). printf("pid1=%d pid2=%d\n". Explain. 3598 2.pid 3595.h> void main() { int pid1. pid1=fork(). pid2=fork().pid2.

c #include <stdio.pid. } else if (pid==0) { printf("pid %d child %d \n".h> #include <unistd. for (i=0.fork4.h> #include <sys/types.i++) { pid=fork().pid. } else { printf("pid %d parent %d \n".i).h> void main() { int i.i<3. if (pid<0) { printf("sorry. } } } .i).pid. cannot fork \n").

2855 2. . 2852. 2853.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.pid 2848.8 process . 2849. Explain. How many process is created? . 2851. 2850. 2854.

c -rw-rw-r-. pid=fork(). } Output: foad@ubuntu:~/test_process/examplefork$ ."-l".out total 32 -rwxrwxr-x 1 foad foad 7342 2012-03-05 21:28 a.1 foad foad 331 2012-03-05 21:19 fork4.h> #include <unistd.1 foad foad 186 2012-03-05 19:48 getpid. printf("No return from exec()\n").c -rw-rw-r-. Explain.(char *)0). exit(1). exit(0).h> void main() { int pid. How many process is created? -2 2.c -rw-rw-r-. .c ls completed Question: 1./a.1 foad foad 314 2012-03-05 20:10 fork2.1 foad foad 294 2012-03-05 21:27 fork5.1 foad foad 166 2012-03-05 20:26 fork3.c -rw-rw-r-. } perror("fork failed").fork5."ls". } if (pid==0) { execl("/bin/ls".out -rw-rw-r-.1 foad foad 250 2012-03-05 19:50 fork1. printf("ls completed\n").c -rw-rw-r-.c #include <stdio. if (pid>0) { wait((int *)0).

. 21 Chapter 2 .changes made by one thread to shared system resources (such as closing a file) will be seen by all other threads.g.does its own independent flow control.Thread in UNIX Process Operating Systems THREADS WITHIN A UNIX PROCESS (e. .is "lightweight" because most of the overhead has already been taken by its process .exists within a process and uses the process resources.) Thread .

Chapter 2 22 . shared memory • Threads also have their own private data • Programmers are responsible for synchronizing access (protecting) globally shared data.Operating Systems Threat Shared Memory Model: • All threads have access to the same global.

Sign up to vote on this title
UsefulNot useful