Week8-Signals

# Week8-Signals - Function Pointers King Chapter 17.7...

This preview shows pages 1–8. Sign up to view the full content.

Function Pointers King – Chapter 17.7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Pointers to Functions • Since a pointer is just an address, we can have pointers to functions! int cube(int x) { return x*x*x; } int (*f)(int); /*Define a function pointer*/ f = cube; /* Call the function that f points to */ printf ("%d\n", (*f)(5));
Example void map(int *nums, int size, int (*f)(int)) { int i; for(i = 0; i < size; i++) nums[i] = (*f)(nums[i]); } int square(int x) { return x*x; } int div2(int x) { return x/2; } int main() { int a[8] = {2, 4, 6, 8, 10, 12, 14, 16}; int i; map(a, 8, square); map(a, 8, div2); for(i = 0; i < 8; i++) { printf("a[%d] = %d\n", i, a[i]); } return 0; }

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Example: qsort • Example - generic quick sort routine: void qsort(void * base, size_t nmemb, size_t size, int (* compar)(const void *, const void *) ); • Can use any comparison function • Just give a pointer to a function like strcmp • Another example: bsearch() for binary search King: 386-388
5 Signals Haviland – Ch. 6

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 Signals • Unexpected/unpredictable asynchronous events – floating point error – death of a child – interval timer expired (alarm clock) – control-C (termination request) – control-Z (suspend request) • Events are called interrupts • When the kernel recognizes an event, it sends a signal to the process. • Normal processes may send signals.
7 What are signals for? • When a program forks into 2 or more processes, rarely do they execute independently. • The processes usually require some form of

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This document was uploaded on 08/02/2011.

### Page1 / 19

Week8-Signals - Function Pointers King Chapter 17.7...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online