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...

80.2K

Verified Solution

Question

Programming

Includes you will need:

#include

#include

#include

#include

#include

Create a .c file that does the following:

1. Use fork() to create a 2nd process, using the if checks we’veseen to determine if you’re the child or the parent. Print amessage from each process saying who is who. (APUE chapter 1/7)

• Note: The parent should save the pid_t of the child (returnedfrom fork()) for later.

2. Have the child register a signal handler for SIGTERM usingthe method we saw. Have your SIGTERM handler print a message andthen 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 youhave two options: •Implement the function above main().

• Put a function signature above main, such as static voidmySignalHandler(int);, and then implement the function belowmain.

3. Have the child go to sleep using this code (which will keepit asleep indefinitely): for ( ; ; ) { pause(); }

4. Have the parent sleep for 5 seconds sleep(5); (an imperfectway to ensure that the child has time to register its signalhandler), then use the kill() command to send SIGTERM to the child.Note that SIGTERM is a defined constant; you don’t need to look upthe number. (APUE chapter 10)

5. Have the parent do a waitpid(…) on the child’s pid (whichshould come back quickly since we just asked him to exit). Print amessage saying that the child has exited. (APUE chapter 8)

6. Have the parent register an atexit() function that prints amessage as you exit. (APUE chapter 7)

7. Let the parent exit (fall out of main, return from main, orcall exit()).

What should happen when you run theprogram:

• 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’ssignal handler (#2 above).

• Right after that you should see the message saying that thechild has exited (#5 above).

• Right after that you should see the message from atexit() (#6above).

Answer & Explanation Solved by verified expert
3.7 Ratings (422 Votes)
Please find the following c programPrograminclude include include include include include signal handlerstatic void mySignalHandlerintcustomized exit handlervoid customExitvoidvoid mainpidt chpid get child process if chpid fork    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students