For this assignment, you may work in pairs (2 people). All students (whether working in a pair or not) must individually turn in the assignment. Therefore, 2 copies of the assignment will be turned in for those working as a pair. The grader will choose to grade only one copy of a pair's work, and assign the same score to both students.
If one member of a pair does something unexpected with respect to turning in the assignment (for example: bad copy of the source code, late, names and sections are missing, or perhaps not turning in portions of the assignment), then both of the pair get the same penalty and the same score.
If working in a pair, the names and sections of both students must appear as comments at the top of all files of the turned in assignment.
The purpose of this program is to gain insight into the asynchronous nature of interrupts, and handling interrupts and traps, while expanding C programming skills. By their very definition, interrupts occur at times not connected to the running program; this makes them asynchronous.
This assignment consists of 2 programs, and each will be turned in.
These programs use specific software interrupts, requiring you to set up the capture of the interrupts to be handled, and also provide the handling code.
Write a C program called intdate.c
that is composed of two parts.
One part is the main function, which does absolutely nothing
in an infinite loop.
Before entering the infinite loop,
the main function will set up what is to happen when an alarm goes off
3 seconds later.
When the alarm goes off,
this causes a SIGALRM
interrupt to signal the program.
This interrupt is to be caught by the program,
and handled by the second part of the program,
an interrupt handler.
This handler function is to print out the current time, in the
same format as the
Unix date command,
re-arm the alarm to go off three seconds later,
and then return back to the main function (which continues
its infinite loop doing nothing).
Since the main function is to keep running forever,
the main()
function will contain an infinite loop such as
while (1) { }
Before executing the infinite loop, the main() function needs to set up the alarm. Once it starts executing the infinite loop it does nothing useful. For this partially complete program, output will look something like
% intdate Date will be printed every 3 seconds. Enter ^C to end the program: current time is Tue Mar 4 13:15:21 2014 current time is Tue Mar 4 13:15:24 2014 current time is Tue Mar 4 13:15:27 2014 ^C
Notice that to stop the program from running, you type in a Control-c. Remember for the next part of this assignment: typing Control-c sends a software interrupt (called SIGINT) to the running program.
Like the set of C programs in Assignment 1, you will use library functions to help you. Just like Assignment 1, it is important to check the return values of these functions, such that your program detects error conditions and acts in response to those errors. Reference the man pages of these functions that you will use:
time()
and ctime()
are library calls to help your handler obtain and print the
time in the correct format.
alarm()
activates the SIGALRM
interrupt to occur in a
specified number of seconds.
sigaction()
( do not use signal() )
sets up what happens when the specific type of interrupt
(specified as the first parameter) causes a software interrupt.
You are particularly interested in setting the sa_handler
field of the structure that sigaction()
needs;
it specifies the handler function to run upon an interrupt.
Once the periodic printing of the time is working,
add to the program to do something
other than exit the program the first time a Control-c is typed.
This extension to program only exits after the user types
Control-c 5 times.
Set up a SIGINT
handler (using sigaction()
to set
up the call back function).
The SIGINT
handler either prints how many more
times Control-c must be typed before exiting,
or it prints that it caught the 5th one and it calls exit()
.
Output of the program looked like this on Feb 26:
Date will be printed every 3 seconds. Enter ^C 5 times to end the program: ^C Control-c caught. 4 more before program is ended. current time is Thu Feb 26 15:15:51 2015 ^C Control-c caught. 3 more before program is ended. ^C Control-c caught. 2 more before program is ended. ^C Control-c caught. 1 more before program is ended. current time is Thu Feb 26 15:15:54 2015 ^C Final Control-c caught. Exiting.
The alarm interrupt handler will need to re-arm the
alarm each time it is called.
Since both main()
and the alarm handler both need
to know/use the number of seconds in order to arm the alarm,
make this value a global variable.
Interrupt handlers are not invoked by another function
within the program, so they cannot receive parameters
from another function in the program.
You will also need a global variable to keep track of the
number of times a Control-c has been typed.
It is only used by the SIGINT
handler,
but it needs to exist (single instantiation) the entire
time that the program runs.
1. Once you are through with this program, copy /p/course/cs354-common/public/bin/demo.c to your current working folder. Compile the program using the following option -
gcc demo.c -o demo -Wall -m32
2. Run the program using -
./demo
3. Observe the result. It should be something like this -
Enter ^C to end the program:
4. Now Press "Ctrl-C" to quit the program. Open the file demo.c and go through the code. Notice the printf statement after the infinite loop.
printf("This should not be printed\n");
Because the program is stuck in an infinite loop, the above printf statement is not executed. That is, the program does not go forward.
6. Now copy the file /p/course/cs354-common/public/bin/a4questions.txt to your folder and answer the question in the file.
7. Once you are done answering the question, copy this file along with intdate.c in your handin folder for submission of PART 1 of this assignment.
VERY IMPORTANT: Compile the program with a different set of options than used in assignment 1. When grading your programs, we will also use this command. You are leaving off the -O option which invokes the optimizer on your code. If you were to allow the compiler to optimize your code, it might eliminate necessary code.
% gcc -o intdate intdate.c -Wall -m32
One more note: Even if you have a personal computer with a C compiler, you will not be able to work on your own computer, as the set up and handling of the variety of interrupts is different on different platforms. Work on the csl machines to do this assignment.
Write a simple program that loops (forever) to
int1 / int2
,
printing these results, and keeping track of how many division
operations were successfully completed.
Place this endless loop into main()
.
Yes, this is poor style, but do it anyway.
A Control-c will cause this program to stop running.
Use fgets()
to read each line of input.
Then, use atoi()
to translate that C string to an integer.
Users tend to type in bad inputs occasionally.
For ease of programming, mostly ignore error checking on the input.
If the user enters in a bad integer value,
don't check for a bad integer value, and
don't worry about it.
Just use whatever value atoi()
returns.
If you still don't know what this value is, look it up in the
atoi()
man page!
The count of the number of completed divisions needs to be a global variable, as it will be needed by the second program enhancement (described below).
Call the source code for this program division.c
.
A sample run of the program might appear as
Enter first integer: 12 Enter second integer: 2 12 / 2 is 6 with a remainder of 0 Enter first integer: 100 Enter second integer: -7 100 / -7 is -14 with a remainder of 2 Enter first integer: 10 Enter second integer: 20 10 / 20 is 0 with a remainder of 10 Enter first integer: ab17 Enter second integer: 3 0 / 3 is 0 with a remainder of 0 Enter first integer: ^C
Once this program is working, enhance it in two ways.
The hardware traps when this unrecoverable
arithmetic error occurs, and the program crashes,
because it did not (catch and) handle the
SIGFPE
signal.
To make this situation a little bit better,
set up a handler that will be called if the program
receives the SIGFPE
signal.
In the signal handler you write, print a message stating that
a divide by 0 operation was attempted, print the number
of successfully completed division operations,
and then exit the program (gracefully, instead of crashing).
intdate
program did,
but on the first control-c interrupt,
the handler prints the number
of successfully completed division operations,
and then exits the program (gracefully).
Set up and add a handler for the SIGINT
signal.
The handler is to print a little message stating
the number of completed operations,
and then exit the program (gracefully).
Useful functions that you will need to use:
fgets()
reads up to a defined maximum number of
characters into a buffer,
stopping before that maximum is reached if a newline or
EOF is encountered.
atoi()
converts a C string into the integer
represented by that string.
sigaction()
( do not use signal() )
sets up what happens when the specific type of interrupt
specified as the first parameter causes a software interrupt.
You will be calling sigaction()
twice,
once to set up the handling of SIGFPE
, and another time to
set up the handling of SIGINT
.
Implement 2 independent handlers; do not combine the handlers.
Do not place the calls to sigaction()
within a loop.
These calls will be completed before
entering the loop that requests and does division on the two integers.
To turn in your source code for grading,
copy both source files (intdate.c
and division.c
) and a4questions.txt
into your handin directory.
Your handin directory for this first project is
/p/course/cs354-common/public/fall15.handin/login/p4
where login
is your CS login.
If you are working as part of a pair, you must turn in an extra file. This file will contain the names and sections of both students in the pair. As an example, if Karen worked with Urmish on this assignment, the contents of the extra file for both Karen and Urmish would be
Karen Miller section 1 Urmish Thakker section 2
The name of this file is specialized to help the 354 automated
grading tools identify who worked together.
This file name is composed of the CS logins of the partners
separated by a period.
The file name is of the form <login1>.<login2>.
Karen's login is smoler
, and Urmish's login is
uthakker
.
The file name that both use will be smoler.uthakker
;
please have both partners use the same file name.
It does not matter which partner's name is first within this
file name.
Your Handin Folder should have the following files
See Guidelines for Programs to see an indication of point allocation for program grading.
For almost any C program that does something useful, someone has already written this program and further, has posted it for others to use. This program does not do much that is useful, and is not likely posted anywhere. Still, it is academic misconduct for you to copy or use some or all of a program that has been written by someone else.
It is academic misconduct to post your solution, especially to a publicly accessible web site such as GitHub.
The penalty for academic misconduct on this assignment (and all CS/ECE 354 assignments) will be a failing grade in the course. This penalty is significantly more harsh than if you simply do not do the assignment. You will gain much more by doing the assignment than by copying, possibly modifying, and turning in someone else's effort.