Function Pointers and Signals Signals
Pointers to Functions Unexpected/unpredictable asynchronous events
o Since a pointer is just an address we can have pointers to functionso Floating point error, death of a child
o Ex.1 o Interval timer expired (alarm clock)
int cube(int x) { o Terminate, Suspend, Quit and Kill a process
return x*x*x;
} Events are called interrupts
int (*f)(int); // Declare a function pointer o When the kernel recognizes an event, it sends a signal to the process
f = cube; // assign it to cube o Normal processes may send signals
What are signals for? For of communications btwn processes
// Call the function that f points to o When a program forks into 2 or more processes, rarely do they
printf ("%d\n", (*f)(5)); execute independently
The processes usually requires some form of synchronization,
o Ex.2 develop a map function that takes an array of integers and a
function to apply to each element often handled by signals
1. #include To transfer data between processes, we will use pipes and
2. sockets
3. void map(int *nums, int size, int (*f) (int)) {
4. int i; o Signals are generated by
5. for (i = 0; i < size; i++ ) { Machine interrupts
6. nums[i] = (*f)(nums[i]); The program itself, other programs or the user
7. }
8. } Sofware interrupts
9. o lists the signal types on CDF
o "man 7 signal" gives some description of various signals
10. int square(int x) {
11. return x * x; SIGTERM, SIGABRT, SIGKILL, SIGSEGV, SIGBUS, SIGSTOP,
12. } SIGCONT, SIGCHLD, SIGPIPE, SIGUSR1, SIGUSR2
13. Signal handlers
14. int decrement(int x) {
15. return x-1; o When a C program receives a signal, control is immediately passed to
16. } a function called a signal handler
17. o The signal handler function can execute some C statements and exit
18. int main() { in 3 different ways:
19. int a[3] = {2, 4, 6};
20. int i; Return control to the place in the program which was executing
21. printf("original:\n") when the signal occurred
22. for (i = 0; i < 3; i++) { Return control to some other point in the program
23. printf("a[%d] = %d\n", i, a[i]);
24. } Terminate the program by calling exit
o Default actions
25. map(a, 3, square); Each signal has a default action
26. printf("\nsquared:\n") Terminate – kill process w/ an optional core dump
27. for (i = 0; i < 3; i++) {
28. printf("a[%d] = %d\n", i, a[i]); Stop – supended, process to be resumed later
29. } Ignore – thrown away the signal
30. map(a, 3, decrement); The default action can be changed for most signal types using
31. printf("\ndecremented:\n")
32. for (i = 0; i < 3; i++) { the sigaction() function
33. printf("a[%d] = %d\n", i, a[i]); The exceptions are SIGKILL and SIGSTOP
34. }
35. return 0; int sigaction(int sig, const struct sigaction *act,
36. } struct sigaction *oldact);
Install a signal handler act, for the signal sig
Output
$ gcc -Wall -g -o fpointer fpointer.c Struct defined in to fill in to pass in for act
$ ./fpointer struct sigaction {
original: // SIG_DFL, SIG_IGN, or pointer to function
a[0] = 2
void (*sa_handler)(int);
a[1] = 4 sigset_t sa_mask; // sigs to block during handler
a[2] = 6 int sa_flags; // flags and options
};
squared: You may come across various extensions, including another filed
a[0] = 4 in the signaction struct for a function to catch signals
a[1] = 16
a[2] = 36 o For each process, UNIX maintains a table of actions that should be
performed for each kind of signal
decremented: SIGNALS Default Shortcut Comment
a[0] = 3 Action
a[1] = 15
a[2] = 35 SIGKILL Terminate Terminate a process
SIGINT Terminate Ctrl-C Terminate a process by user
o Ex.3 qsort() used for generic quick sort routine input
SIGQUIT Terminate, Ctrl-\ Terminate a process by user
void qsort(void *base, size_t nmeb, size_t size, Dump Core input, then core dump
int (* compar)(const void *, const void *));
compar is a pointer to a comparison function for the SIGSTOP Stop Ctrl-Z Suspend a process to be
resumed later
elements to be sorted SIGSEGV Terminate, Segmentation fault,
Just give a pointer to a function like strcmp Dump Core Invalid memory ref
Similar to bserach() for binary search
More
Less