Midterm-II Prep

Class: CSCE-313


Notes:

1.1

1.2 Signals

Problem 1

Stacks as a Data Structure (in C). Implement a stack in C using a dynamic array (i.e., using malloc/realloc). Your implementation should support pushpoppeek, and is_empty. Make your implementation signal-safe?

Problem 2

Suppose a program installs a handler for SIGINT.

void handler(int sig) {
    printf("Signal received\n");
}

Problem 3

Write a program that installs a signal handler for SIGINT. The handler should increment a counter and print how many times the user has pressed Ctrl+C. After 3 presses, the program should exit cleanly. What constraints apply to code inside a signal handler?

Problem: Signals can be coalesced into one if received concurrently, it will not be counted, it will just tell you that you have pending signals. There are real time signals that actually count, but we are not doing that.

Problem 4

Which of the following are valid things to do safely inside a signal handler, and why?

Notes:

Example

long long x;
x++;

Anything that is atomic is signal-safe?