Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a C program that creates a child process and the child process prints whe

ID: 3694727 • Letter: C

Question

Create a C program that creates a child process and the child process prints when it is created and then goes into a infinite loop to wait for a signal. In the main function for the parent process, list several signals the the user can choose from to send to the child process, get the number for the signal from the user, and send it to the child process. When the child process receives a signal, print out that it has and which signal it received. Have the parent process wait on the child to process to finish and then print out "All done!"

Example runs:

Child process created!

Enter the number for the signal you want to send.

SIGHUP 1

SIGINT 2

SIGFPE 8

SIGSEGV 11

SIGTERM 15

Number: 1

Child process received signal: SIGHUP

All done!

Child process created!

Enter the number for the signal you want to send.

SIGHUP 1

SIGINT 2

SIGFPE 8

SIGSEGV 11

SIGTERM 15

Number: 8

Child process received signal:

SIGFPE All done!

Explanation / Answer

#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
#include<stdlib.h>
#include<signal.h>
void main()
{
   pid_t child;
   int n;
   clrscr();
   printf("The main program process ID %d ",(int)getpid());
   child=fork();
   if(child>0)
   {
       printf("the Child process %d ",(int)child);
   }
   else if (child==0)
   {
       printf("this is child %d ",(int) getpid());
   }
   else
       printf("fork failed ");

   printf("SIGHUP 1 SIGINT 2 SIGFPE 8 SIGSEGV 11 SIGTERM 15 ");
   printf("Enter your number ");
   scanf("%d",&n);
   switch(n)
   {
       case 1: signal(SIGHUP,sighandler);
           break;
       case 2: signal(SIGINT,sighandler);
           break;
       case 8: signal(SIGFPE,signhandler);
           break;
       case 11: signal(SIGSEGV,sighandler);
           break;
       case 15: signal(SIGTERM,sighandler);
           break;
   }
}
void sighandler(int signum)
{
   printf("Signal performed %d",signum);
   exit(1);
}
}