This program shows how to create process and display Process ID (pid) of Both Parent and Child processes. The process is created using the fork system call in UNIX (Linux) operating systems.
#include<stdio.h>
#include<dirent.h>
main(int argc,char **argv)
{
int pid,i;
for(i=0;i<atoi(argv[1]);i++)
{
pid=fork();
if(pid==0)
{
printf("child process id %d Parent process
id %d\n",getpid(),getppid());
}}}
Search This Blog
Showing posts with label fork. Show all posts
Showing posts with label fork. Show all posts
How to use fork and exec System Call
Using fork and exec System calls in Linux (UNIX) operating systems. Fork system call is used to create a child process from a parent process.
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("%d\n",pid);
if(pid==0)
{
exec("/bin/date\n",NULL,NULL);
exit(0); }
else
{
printf("Parent process %d\n",pid); }}
if(sp)
while(dd=readdir(sp))
printf("--> %s\n",dd->d_name);
}
}
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("%d\n",pid);
if(pid==0)
{
exec("/bin/date\n",NULL,NULL);
exit(0); }
else
{
printf("Parent process %d\n",pid); }}
if(sp)
while(dd=readdir(sp))
printf("--> %s\n",dd->d_name);
}
}
How to make parent process wait till Completion of Child Process (Joining parent with child process)
How to make the parent process wait till the completion of execution of child process. The following C program makes the parent process to wait till the completion of its child process.
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("%d\n",pid);
if(pid==0)
{
printf("From child process \n");
}
else
{
wait(0);
printf("From parent process\n");
}
}
#include<stdio.h>
main()
{
int pid;
pid=fork();
printf("%d\n",pid);
if(pid==0)
{
printf("From child process \n");
}
else
{
wait(0);
printf("From parent process\n");
}
}
How to make Child process an Orphan Process
Creating A Process in Linux (UNIX) - fork() Example Program
The system call fork() is used to create a new process in UNIX based operating systems and Linux systems. The fork() system call creates a child process when called from a parent process. Unix will make an exact copy of the parent's address space and give it to the child. Therefore, the parent and child processes have separate address spaces. Here is a C program which uses the fork() system call to create a process during execution. The program is commented well for better understanding.
Program
/*The program (parent process) reads an array from the user.
It sorts it in descending order.
The child process created by parent process sorts it ascending order
*/
#include<stdlib.h>
#include<stdio.h>
int main()
{
int cpid;
int temp,a[100],n,i,j;
printf("\nEnter number of elements : ");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\n");
cpid=fork(); /*Creating child process using fork() system call */
if(cpid<0)
{
printf("Cannot create a new process\n");
exit(0);
}
if(cpid != 0)
{
/*fork returns processid of newly created process.
Therefore, the value of integer variable cpid in parent process is the process id of child process.
This block (if block) is executed only by the parent because the value of cpid is zero in child process. We use the value of variable cpid to decide which process (parent or child) should execute which part of code*/
printf("\nThis is parent with id: %d\n",getpid());
/* The UNIX system call getpid() return the process id of process being executed.*/
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]<a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Parent sorted the array in descending order\n");
}else
{
/*This block (else block) is executed only by the child process because value of cpid is zero only for child process.*/
printf("\nThis is child with id: %d \n",getpid());
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Child sorted the array in ascending order\n");
}
/*The following part of code is executed by both the processes because it is not restricted using the value of cpid.*/
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
return 0;
}
Output:
Enter number of elements : 5
Enter elements
8
6
10
2
12
This is parent with id: 4261
Parent sorted the array in descending order
12
10
8
6
2
This is child with id: 4262
Child sorted the array in ascending order
2
6
8
10
12
Program
/*The program (parent process) reads an array from the user.
It sorts it in descending order.
The child process created by parent process sorts it ascending order
*/
#include<stdlib.h>
#include<stdio.h>
int main()
{
int cpid;
int temp,a[100],n,i,j;
printf("\nEnter number of elements : ");
scanf("%d",&n);
printf("Enter elements\n");
for(i=0;i<n;i++){
scanf("%d",&a[i]);
}
printf("\n");
cpid=fork(); /*Creating child process using fork() system call */
if(cpid<0)
{
printf("Cannot create a new process\n");
exit(0);
}
if(cpid != 0)
{
/*fork returns processid of newly created process.
Therefore, the value of integer variable cpid in parent process is the process id of child process.
This block (if block) is executed only by the parent because the value of cpid is zero in child process. We use the value of variable cpid to decide which process (parent or child) should execute which part of code*/
printf("\nThis is parent with id: %d\n",getpid());
/* The UNIX system call getpid() return the process id of process being executed.*/
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]<a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Parent sorted the array in descending order\n");
}else
{
/*This block (else block) is executed only by the child process because value of cpid is zero only for child process.*/
printf("\nThis is child with id: %d \n",getpid());
for(i=0;i<n;i++){
for(j=0;j<n-1;j++){
if(a[j]>a[j+1]){
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf("Child sorted the array in ascending order\n");
}
/*The following part of code is executed by both the processes because it is not restricted using the value of cpid.*/
for(i=0;i<n;i++){
printf("%d\n",a[i]);
}
return 0;
}
Output:
Enter number of elements : 5
Enter elements
8
6
10
2
12
This is parent with id: 4261
Parent sorted the array in descending order
12
10
8
6
2
This is child with id: 4262
Child sorted the array in ascending order
2
6
8
10
12
![]() |
| Output of the program |
Subscribe to:
Posts (Atom)
