0% found this document useful (0 votes)
117 views

Example of Fork Programming.

The document discusses examples of fork programming in C. In the first example, one process is created with PID 3515 as the same PID is printed for each call to getpid(). The second example creates two processes, with PIDs 3545 and 3546 printed. The third example creates four processes with PIDs 3480, 3481, 3482, and 3483. The fourth example uses a for loop in fork to create eight processes with PIDs from 2848 to 2855. The last example uses fork and exec to run the ls command, creating two processes.

Uploaded by

amey_mcr
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
117 views

Example of Fork Programming.

The document discusses examples of fork programming in C. In the first example, one process is created with PID 3515 as the same PID is printed for each call to getpid(). The second example creates two processes, with PIDs 3545 and 3546 printed. The third example creates four processes with PIDs 3480, 3481, 3482, and 3483. The fourth example uses a for loop in fork to create eight processes with PIDs from 2848 to 2855. The last example uses fork and exec to run the ls command, creating two processes.

Uploaded by

amey_mcr
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

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.

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

Output: pid1=3596 pid2=3597 pid1=3596 pid2=0 pid1=0 pid2=3598 pid1=0 pid2=0

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.

Thread in UNIX Process

Operating Systems

THREADS WITHIN A UNIX PROCESS (e.g.)


Thread - does its own independent flow control. - exists within a process and uses the process resources. - is "lightweight" because most of the overhead has already been taken by its process - changes made by one thread to shared system resources (such as closing a file) will be seen by all other threads.
21

Chapter 2

Operating Systems

Threat Shared Memory Model:


All threads have access to the same global, shared memory Threads also have their own private data Programmers are responsible for synchronizing access (protecting) globally shared data.

Chapter 2

22

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy