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

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

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

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

View Full Document Right 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));
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; }
Image of page 3

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

View Full Document Right 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
Image of page 4
5 Signals Haviland – Ch. 6
Image of page 5

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

View Full Document Right 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.
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 synchronization, often handled by signals.
Image of page 7

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

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern