In: Computer Science
Includes you will need:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
Create a .c file that does the following:
1. Use fork() to create a 2nd process, using the if checks we’ve seen to determine if you’re the child or the parent. Print a message from each process saying who is who. (APUE chapter 1/7)
• Note: The parent should save the pid_t of the child (returned from fork()) for later.
2. Have the child register a signal handler for SIGTERM using the method we saw. Have your SIGTERM handler print a message and then call exit(). (APUE chapter 10)
• Hint: functions in C need to be declared before they are used. Normally this happens in a header file, but within your .c file you have two options: •Implement the function above main().
• Put a function signature above main, such as static void mySignalHandler(int);, and then implement the function below main.
3. Have the child go to sleep using this code (which will keep it asleep indefinitely): for ( ; ; ) { pause(); }
4. Have the parent sleep for 5 seconds sleep(5); (an imperfect way to ensure that the child has time to register its signal handler), then use the kill() command to send SIGTERM to the child. Note that SIGTERM is a defined constant; you don’t need to look up the number. (APUE chapter 10)
5. Have the parent do a waitpid(…) on the child’s pid (which should come back quickly since we just asked him to exit). Print a message saying that the child has exited. (APUE chapter 8)
6. Have the parent register an atexit() function that prints a message as you exit. (APUE chapter 7)
7. Let the parent exit (fall out of main, return from main, or call exit()).
What should happen when you run the program:
• The parent and child will print who is who (in either order, or even with characters overlapping—this is okay).
• After 5 seconds, you should see the message from the child’s signal handler (#2 above).
• Right after that you should see the message saying that the child has exited (#5 above).
• Right after that you should see the message from atexit() (#6 above).
Please find the following c program.
Program:
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
//signal handler
static void mySignalHandler(int);
//customized exit handler
void customExit(void);
void main()
{
pid_t ch_pid;
/* get child process */
if ((ch_pid = fork()) < 0)
{
perror("fork");
exit(1);
}
if (ch_pid == 0)
{ /* child process block*/
printf("I am child process with process id as: %d.\n",
getpid());
//register the signal handler
signal(SIGTERM, mySignalHandler);
for(;;)
{
pause();
}
}
else /* parent process block*/
{
printf("I am parent process with process id as: %d.\n",
getpid());
sleep(5);
printf("PARENT: sending SIGTERM\n\n");
//parent kills the child process
kill(ch_pid, SIGTERM);
//parent waits for child process to get completed
if(waitpid(ch_pid, NULL, 0))
{
printf("PARENT: Child with process id %d is exited.\n",
ch_pid);
}
//register the exit function pointer
if( atexit(customExit) != 0 )
{
printf("Unable to set customized exit function\n");
return;
}
return;
}
}
static void mySignalHandler(int signal)
{
printf("CHILD: I have received a signal SIGTERM(%d)\n",
signal);
exit(0);
}
void customExit(void)
{
printf("PARENT has exited..\n");
}
Output:
USER>./a.out
I am parent process with process id as: 106885.
I am child process with process id as: 106886.
PARENT: sending SIGTERM
CHILD: I have received a signal SIGTERM(15)
PARENT: Child with process id 106886 is exited.
PARENT has exited..
USER>
Screen Shot: