Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Signals and Timing here is provided a basic Hello World program, the signal_base.c which you will build upon to complete the task. 1. /* signal_base.c

Signals and Timing

here is provided a basic Hello World program, the signal_base.c which you will build upon to complete the task.

1. /* signal_base.c */

2. #include

3. #include

4. #include

5. #include

6.

7. void handler(int signum) //signal handler

8. { printf("Hello World! ");

9. exit(1); //exit after printing

10. }

11.

12. int main(int argc, char * argv[])

13. { signal(SIGALRM,handler); //register handler to handle SIGALRM

14. alarm(1); //Schedule a SIGALRM for 1 second

15. while(1) sleep(5); //waiting for signal to be delivered

16. return 0; //never reached

17. }

There are two key function calls in signal_base.c: signal() and alarm(). The alarm() system call instructs the operating system to deliver a SIGALRM signal after n seconds, n being 1 in this example. The signal() system call instructs the operating system to execute the function handler when the SIGALRM signal is delivered. Now, it is clear that the Hello World program sets up a signal handler for SIGALRM, line 13; a timing for the delivery of the SIGALRM, line 14; busy waits for the signal to be delivered, line 15; and once the signal is delivered, the signal handler is invoked, printing Hello World and exiting, lines 7-9. At this point you should compile and execute signal_base.c and observe the timing of the output, it is delayed by 1 second.

This style of programming with signals is based on the principles of preemptive execution. That is, the execution of the handler function preempts the main execution of the program. Once the signal is delivered (during the busy wait), program execution jumps to the handler function, and once the handler returns, execution jumps back to the point where the main execution was preempted (or would have, if there was not an exit() call). The command kill -l will display the list of all the signals available in Linux, and the command man 7 signal will show you a complete table with a brief summary of the meaning of each signal.

Signal Handling Programming Problems

Program the solutions to the following problems by extending signal_base.c:

1. Change signal_base.c such that after the handler is invoked, an additional printf("Turing was right! ") occurs in main() before exiting. You will probably need to use a global variable and change the condition on the while loop. Furthermore, change signal_base.c such that every second, first Hello World! prints from the signal handler followed by Turing was right! in main(), over and over again indefinitely. The output should look like:

Hello World!

Turing was right!

Hello World!

Turing was Right!

2. Write a new program based on your signal_base.c that after exiting (via CTRL-C), will print out the total time the program was executing in seconds. To accomplish this task, you will need to register a second signal handler for the SIGINT signal, the signal that is delivered when CTRL-C is pressed. Conceptually, your program will request a SIGALRM signal to occur every second, tracking the number of alarms delivered, and when the program exits via CTRL-C, it will print how many alarms occurred, or the number of seconds it was executed.

Name the solution programs as code-1.c and code-2.c

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Introduction To Databases And Data Warehouses

Authors: Nenad Jukic, Susan Vrbsky, Svetlozar Nestorov

1st Edition

1943153191, 978-1943153190

More Books

Students also viewed these Databases questions

Question

What were the issues and solutions proposed by each team?

Answered: 1 week ago