Create Five Child Process Kill Acending Dscending Both Order

To create 5 child process and kill them in ascending and descending order. In the integer variable I declare three variables I, id1,id2, and pi doc child array 5. After creating of 5 process it will kill the process in ascending and descending order depends upon the user choice .
Source code step by step Programming
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<signal.h>
int main()
{
system("clear");
int i,id1,id2,choice;
pid_t child[5];
printf("program to create 5 child process and kill them\n");
for(i=0;i<5;i++)
{
child[i]=fork();
if (child[i]==0)
{
while(1)
{
}
}
printf("\n%d child process created with its id no =%d",i+1,child[i]);
}
printf("\n\n to kill the child process\n");
printf("\n1.Kill in ascending order");
printf("\n2.Kill in decesending order");
printf("\n Enter your choice");
scanf("%d",&choice);
if(choice==1)
{
for(i=0;i<5;i++)
{
id1=kill(child[i],SIGKILL);
if(id1==0)
printf("\nchild process %d is killed\n",i+1);
}
}
else
if(choice==2)
{
for(i=4;i>=0;i--)
{
id2=kill(child[i],SIGKILL);
if(id2==0)
printf("\nchild process %d is killed\n",i+1);
}
}
}
OUTPUT:: program to create 5 child process and kill them
1 child process created with its id no =3476
2 child process created with its id no =3477
3 child process created with its id no =3478
4 child process created with its id no =3479
5 child process created with its id no =3480

to kill the child process
1.Kill in ascending order
2.Kill in descending order
Enter your choice1

child process 1 is killed
child process 2 is killed
child process 3 is killed
child process 4 is killed
child process 5 is killed

Enter your choice2
child process 5 is killed
child process 4 is killed
child process 3 is killed
child process 2 is killed
child process 1 is killed
CONCLUSION: The following c program creates the processes and kill them in ascending and descending order.

Related post