YCP Logo Lab 30: Signals

Due: Tuesday, December 9th by 11:59 PM

Getting Started

Like Lab 29, this lab will use gcc rather than Visual Studio.

Download CS200_Lab30.zip, and save it in the root folder of your H: drive. Extract its contents (right click on the zip file, and choose Extract all.) This step will create a folder called H:\CS200_Lab30. The folder will contain a single file called signals.c. Open this file using Notepad++ (Start->All Programs->Programming->Notepad++).

Start the Cygwin bash shell (Start->All Programs->Programming->Cygwin->Cygwin bash shell). Type the following commands:

cd h:
cd CS200_Lab30

These commands navigate into the directory containing the file signals.c.

Now, compile the program. From the bash shell, type the commands

gcc -c -Wall signals.c signals.o
gcc -o signals.exe signals.o

Run the program:


When prompted for the number of elements to sort, type 50000. The program output should look something like this:

How many elements to sort? 50000
Sorting took about 13 seconds

Your Task

The program in signals.c performs an Insertion sort on an array of integer values. Insertion Sort is a slow algorithm, so for large arrays, it can take a significant amount of time to run.

Your task is to modify the program so that whenever the user types Control-C (hold down the Control key, and press the C key), the program will print the message "Interrupt!". Also, before the program exits, it will print out the total number of times it was interrupted.

Here is what the output of the modified program should look like:

How many elements to sort? 50000
Sorting took about 14 seconds
Sorting was interrupted 5 times

In this case, the user pressed Control-C 5 times.


The SIGINT signal is raised when the user presses Control-C.

Use a global int variable to keep track of the number of times the program received the SIGINT signal.

Define a handler function for the SIGINT signal:

void handle_interrupt(int sig_num)
        // ...

Within this function, put the code to print the "Interrupt!" message and increment the counter.

In the main function, register the handler function as the handler for SIGINT:

signal(SIGINT, &handle_interrupt);

Note that when printing output from the signal handler, it may be necessary to call the fflush function to ensure that the output appears immediately: e.g.



Create a zip file of your solution by navigating to the CS200_Lab30 folder in your H: drive, right-clicking, and choosing Send to->Compressed Folder.

Upload the zip file to the submission server as lab30. The server URL is