-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnoswitch.c
48 lines (40 loc) · 1.08 KB
/
noswitch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/* NOSWITCH.C
* Measure overhead of using the sched_yield() call.
There is a structure that contains the handler for the alarm.
*/
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <signal.h>
#include <sched.h>
int nyield = 0;
main()
{
struct sigaction sa;
extern void alarm_handler();
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sa.sa_handler = alarm_handler;
if (sigaction(SIGALRM, &sa, NULL) < 0) {
perror("sigaction SIGALARM");
exit(1);
}
switcher(); /* Switch with self - i.e., no context switch */
printf("Unexpected exit from test program!\n");
exit(4);
}
switcher()
{
// sets a one shot alarm to go off in 60 seconds and then continues to do sched_yield
alarm(60);
while(1) {
sched_yield();
nyield++;
}
}
void alarm_handler()
{
printf("%d yield calls in 60 seconds = %d yield/sec\n",
nyield, nyield/60);
exit(0);
}