This C program creates 5 child processes using fork(), prints their process IDs, then allows the user to choose whether to kill the child processes in ascending or descending order using kill() with SIGKILL. If the user chooses ascending order, the child processes are killed from 1-5. If descending order is chosen, they are killed from 5-1. After each kill, it prints which child process was terminated.
This C program creates 5 child processes using fork(), prints their process IDs, then allows the user to choose whether to kill the child processes in ascending or descending order using kill() with SIGKILL. If the user chooses ascending order, the child processes are killed from 1-5. If descending order is chosen, they are killed from 5-1. After each kill, it prints which child process was terminated.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online from Scribd
This C program creates 5 child processes using fork(), prints their process IDs, then allows the user to choose whether to kill the child processes in ascending or descending order using kill() with SIGKILL. If the user chooses ascending order, the child processes are killed from 1-5. If descending order is chosen, they are killed from 5-1. After each kill, it prints which child process was terminated.
Copyright:
Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online from Scribd
#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