CSCI 4061 Lab08: Signal Handlers
- Due: 11:59pm Mon 3/22/2021 on Gradescope
- Approximately 1.00% of total grade
CODE DISTRIBUTION: lab08-code.zip
- Download the code distribution
- See further setup instructions below
CHANGELOG: Empty
Table of Contents
1 Rationale
This Lab gives some practical experience setting up a signal handler in a standard utility. It demonstrates how the usual logic of a program can incorporate signals to allow "graceful shutdown": terminating the program as quickly as possible while still producing some output that has been calculated so far. This emulates the common situation of needing to finalize some file operations before shutting a sensitive program down due to a signal
Grading Policy
Credit for this Lab is earned by completing the exercises here and
submitting a Zip of the work to Gradescope. Students are responsible
to check that the results produced locally via make test
are
reflected on Gradescope after submitting their completed
Zip. Successful completion earns 1 Engagement Point.
Lab Exercises are open resource/open collaboration and students are encouraged to coopearte on labs. Students may submit work as groups of up to 5 to Gradescope: one person submits then adds the names of their group members to the submission.
See the full policies in the course syllabus.
2 Codepack
The codepack for this lab is linked at the top of this document. Always download it and unzip/unpack it. It should contain the following files which are briefly described.
File | Use | Description |
---|---|---|
QUESTIONS.txt |
EDIT | Questions to answer: fill in the multiple choice selections in this file. |
wc_signal.c |
EDIT | C file to study to complete for the CODE portion |
QUESTIONS.txt.bk |
Backup | Backup copy of the original file to help revert if needed |
Makefile |
Build | Enables make test and make zip |
testy |
Testing | Test running scripts |
test_lab08.org |
Testing | Tests for this lab |
3 Signal Handlers
There are several signals that standard programs react to by
terminating. One such utility is the wc
which does 'word counts' as
well as character and line counts. Its use is show below.
> seq 10 # demonstrate 'seq' output: sequence of numbers 1 2 3 4 5 6 7 8 9 10 > seq 10 | wc # pipe 'seq' to the 'wc' wordcount utility 10 10 21 # 10 lines, 10 words, 21 characters > yes | wc # pipe infinite 'yes' output to wc C-c # send interrupt via Ctrl-c # no output from wc which was terminated
Note in the last case that the keyboard interrupt, triggered by
pressing Ctrl-C
, sends the SIGINT
signal which terminates the
running programs with no output.
In this lab, a similar program called wc_signal
will be constructed
which will "catch" a signal and handle it by exiting normally and
outputting its current results. The basic behavior of wc_signal
is
below.
> make # build wc_signal program gcc -Wall -Werror -g -c wc_signal.c gcc -Wall -Werror -g -o wc_signal wc_signal.o > seq 10 | ./wc_signal # pipe 'seq' output to wc_signal 10 lines 10 words 21 chars # input ends, reports amount read > yes | ./wc_signal # pipe inifinite 'yes' output to wc_signal C-c # send interrupt via Ctrl-c, output still reported 28725026 lines 28725027 words 57450053 chars
4 QUESTIONS.txt File Contents
Below are the contents of the QUESTIONS.txt
file for the lab.
Follow the instructions in it to complete the QUIZ and CODE questions
for the lab.
__________________ LAB 08 QUESTIONS __________________ Lab Instructions ================ Follow the instructions below to experiment with topics related to this lab. - For sections marked QUIZ, fill in an (X) for the appropriate response in this file. Use the command `make test-quiz' to see if all of your answers are correct. - For sections marked CODE, complete the code indicated. Use the command `make test-code' to check if your code is complete. - DO NOT CHANGE any parts of this file except the QUIZ sections as it may interfere with the tests otherwise. - If your `QUESTIONS.txt' file seems corrupted, restore it by copying over the `QUESTIONS.txt.bk' backup file. - When you complete the exercises, check your answers with `make test' and if all is well, create a zip file with `make zip' and upload it to Gradescope. Ensure that the Autograder there reflects your local results. - IF YOU WORK IN A GROUP only one member needs to submit and then add the names of their group. QUIZ Questions sigaction() function =================================== Which of the following options must be set in the `struct sigaction' to indicate that on receiving a signal, system calls should be restarted automatically? (You may wish to consult the manual pages for `sigaction()'). - ( ) SA_AGAIN - ( ) SA_SYSCALL - ( ) SA_RESTART - ( ) SA_RESETHAND How does one indicate that a custom handler function should be run when a signal is received using the `sigaction()' function? - ( ) The `.sa_handler' field of a `struct sigaction' is set to the function and the struct is passed to `sigaction()' - ( ) The a pointer is to the function is passed as an argument `sigaction()' - ( ) Global functions which start with the name `handler_...()' are automatically assigned to be signal handlers - ( ) Trick question: the `sigaction()' function cannot be used to set up a signal handler. An easy way to convey that a signal has been received from the signal handling function to the main body of a code is.. - ( ) To send a second signal the process within the signal handler - ( ) To call a recursive function defined by the code - ( ) To call `printf()' to print out that the signal has been caught - ( ) To change the value of a global variable CODE Complete wc_signal.c ========================= Complete the code in `wc_signal.c' so that the program catches `SIGINT' and prints its current counts prior to shutting down as shown in the demo below. Sections to do so are marked as TODO. ,---- | > make # build wc_signal program | gcc -Wall -Werror -g -c wc_signal.c | gcc -Wall -Werror -g -o wc_signal wc_signal.o | | > seq 10 | ./wc_signal # pipe 'seq' output to wc_signal | | 10 lines 10 words 21 chars # input ends, reports amount read | | > yes | ./wc_signal # pipe inifinite 'yes' output to wc_signal | C-c # send interrupt via Ctrl-c, output still reported | 28725026 lines 28725027 words 57450053 chars `----
5 Submission
Follow the instructions at the end of Lab01 if you need a refresher on how to upload your completed lab zip to Gradescope.