You are on page 1of 4

PROGRAM:

//Creating and killing process


#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 =10146


2 child process created with its id no =10147
3 child process created with its id no =10148
4 child process created with its id no =10149
5 child process created with its id no =10150

to kill the child process

.Kill in ascending order


2.Kill in decesending 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

You might also like