Write and test a signal handler for sigint that prints out


Assignment: Exec, Signals and Time Representation

Part I: Using the exec function.

Write a main program that launches 3 child programs. Each child does the same thing but in a slightly different way.

a. The first child executes a subroutine with 2 arguments - a delay in microseconds and an exit code. The subroutine announces its pid and reports on its arguments, goes to sleep for the specified number of microseconds and then exits using the exit code. (1)

b. The second child uses one of the exec functions to pass the same two arguments to a 2nd program using argv. To do this you should convert the numbers to strings using either sprintf or asprintf. The 2nd program recovers and reports these values from argv along with its pid, then goes to sleep for the specified number of microseconds and then exits using the exit code. (3)

c. The 3rd child uses another of the exec function to pass the same two arguments to a 3rd program using envp. To do this you should convert the numbers to environment variable strings, ie: "DELAY=50000" and "EXITCODE=21". The 3rd program recovers these values from envp, reports these values along with its pid, then goes to sleep for the specified number of microseconds and then exits using the error code.

d. The main program should generate random delays in microseconds for each child process. These values should range from 1 to 3 million. This means that the 3 children could finish in any order. After launching the 3rd child have the main program loop and wait for each of the children, and print out the pid and exit status of each child. (2)

Part 2: Signals -

In a single program create and test the following signal handlers.

1) Write your program that when it begins it stores the current time using gettimeofday in microseconds since the EPOCH. Write and test a signal handler for SIGINT that prints out the elapsed time since the program began.

2) When you send the program the signal SIGURG use popen to run a shell command that counts the number of processes running on the system - a combination of ps and wc. Read in the result - keeping in mind that you will need to adjust the result by subtracting 1 for the column headers.

3) In sigDemo5 we demonstrated that the system function sent a SIGCHILD interrupt to the parent program. Demonstrate that popen does the same. Insert sleep commands and sleep function calls in your code to determine when the signal is sent. When is this signal sent? Justify your answer.

a) when popen starts to run the command
b) when popen finishes running the command
c) when when pclose is executed.

4) A common use of SIGHUP is to reinitialize a program. For example, to modify Apache's parameter's while it is still running, a systems manager might modify the file apache.conf and then send it a SIGHUP interrupt signal

a) When your program is sent a SIGHUP close all open files (not including stdin, stdout and stderr) using the first and last recorded file descriptor #. (This instruction will make more sense after you read b) and c))

b) Create a file files.list consisting of several file names. When your program is sent a SIGHUP interrupt, read in the list of files and open them, then display the list of open files in /proc/pid/fd. As you are opening them record the 1st and the last file descriptor numbers. (Remember, they open up in order)

c) Modify the list of files. Resend SIGHUP. Verify that the old files are closed and the new files are now opened.

Part 3: Dealing with Different Time Representations.

1) In a single program demonstrate the use of time functions dealing with the time of day.

a) Use the data structure struct tm (see the man pages for strftime and ctime) to create a variable and initialize the fields so that it represents the day you were born. Play close attention to the year field or you might not be born for another 1800 years!!! If you don't know the time of day you were born, call your mom if she's available (tell her you need to know it for an assignment) or make up your own time. (1)

b) Use mktime to convert this time to seconds since the epoch and print out the result. Use ctime, and asctime to print out the result as string. (1)

c) Use strftime to create a string and print out your birthday using at least 6 time related format codes. The output should preserve the day, month, year and hour that you were born but can be presented in any order. The format codes for this are identical to the Unix date command.

Repeat using a different format with the information in a different order.

d) Use gmtime to convert the time to Grenwich Mean Time and then print it out. How many hours difference is there? - write the answer in pen next to your code. (1)

e) Write function addTime(struct tm * anyTime, int months)

i) Now use the 1st format to write out when your birthday will be for the first 10 years of your life.

ii) Reopen or rewind the file. Read in the times one line at a time and use strptime with your first format to decode the string to a struct tm, then print out the result using your 2nd format string.

Request for Solution File

Ask an Expert for Answer!!
C/C++ Programming: Write and test a signal handler for sigint that prints out
Reference No:- TGS02741341

Expected delivery within 24 Hours