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

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

View Full Document Right Arrow Icon
Function Pointers King – Chapter 17.7
Background image of page 1

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

View Full DocumentRight Arrow Icon
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));
Background image of page 2
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; }
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
5 Signals Haviland – Ch. 6
Background image of page 5

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

View Full DocumentRight Arrow Icon
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.
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
Image of page 8
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 Right Arrow Icon
Ask a homework question - tutors are online