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 synchronization, often handled by signals.

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.

{[ snackBarMessage ]}

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern