Assignment 4

CS/ECE 354, Fall 2015



Due Wednesday November 25 before 9am.

Please post all questions to the piazza page: http://www.piazza.com/wisc/fall2015/cs354/


Collaboration Policy

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.


Overview

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.

Program Description

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.

First Program: A Periodic Alarm

Periodic Alarm: Step 1

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:

Periodic Alarm: Step 2

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.

Periodic Alarm: Step 3


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.

Second Program: Do Some Division

Write a simple program that loops (forever) to

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.

  1. Try your program on a divide by 0 operation. What happens?

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

  2. You needed to interrupt this program to cause it to stop running. The second enhancement to this program captures the control-c just like the 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:

Handing In the Programs

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

  1. intdate.c
  2. a4questions.txt
  3. division.c

Requirements

  1. Include a comment at the top of the source code of all programs with your name and section (and your partner's name and section, if working in a pair).
  2. Your program must follow style guidelines as given in Style Guidelines.

    See Guidelines for Programs to see an indication of point allocation for program grading.

  3. Use the galapogos Linux machines for this assignment! Your programs must compile on a galapogos Linux machine as indicated in this specification without warnings or errors.

About Copying Code and Academic Misconduct

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.