- Compile and run the code.
- Analyze the code and output.
- You should compile with -lpthread whenever necessary.
- Signal; http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code28.c code28.c
- Signals are mechanisms for communicating with and manipulating processes.
- A signal is a special message sent to a process. Signals are asynchronous; when a process receives a signal, it processes the signal immediately, without finishing the current function or even the current line of code.
- Each signal type is specified by its signal number, but in programs, you usually refer to a signal by its name.
- How to terminate the program? Break with Ctrl+Z, you will get
[1]+ Stopped code28
then kill the stopped process with
kill %1
- Signal Handling; - http://siber.cankaya.edu.tr/OperatingSystems/cfiles/code29.c code29.c
- Even assigning a value to a global variable can be dangerous because the assignment may actually be carried out in two or more machine instructions, and a second signal may occur between them, leaving the variable in a corrupted state.
- If you use a global variable to flag a signal from a signal-handler function, it should be of the special type sig_atomic_t.
- Assignments to variables of this type are performed in a single instruction and therefore cannot be interrupted midway.
- This program uses a signal-handler function to count the number of times that the program receives SIGUSR1, one of the signals reserved for application use.
- Exercise: Write a C program which controls the POSIX interrupt signal (SIGINT).
- Your program should continuously increase an integer value.
- With each interrupt request (CTRL-C), your program should;
- Display the current value of counter.
- Toggle whether the counter should continue increasing, or stop.
Cem Ozdogan
2010-04-10