-
Notifications
You must be signed in to change notification settings - Fork 308
Expand file tree
/
Copy pathidle.c
More file actions
78 lines (68 loc) · 1.6 KB
/
idle.c
File metadata and controls
78 lines (68 loc) · 1.6 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// https://github.com/jlfwong/speedscope/issues/393
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <sys/time.h>
#include <sys/types.h>
void fselect() {
fd_set rfds;
struct timeval tv;
int retval;
FD_ZERO(&rfds);
FD_SET(0, &rfds);
tv.tv_sec = 2;
tv.tv_usec = 0;
retval = select(1, &rfds, NULL, NULL, &tv);
if (retval == -1)
perror("select()");
else if (retval)
printf("Data is available now.\n");
/* FD_ISSET(0, &rfds) will be true. */
else
printf("No data within two seconds.\n");
}
void timed(int microseconds) {
int sec, usec, diff;
struct timeval tv;
gettimeofday(&tv, NULL);
// printf("begin seconds : %ld micro seconds : %ld\n", tv.tv_sec, tv.tv_usec);
sec = tv.tv_sec;
usec = tv.tv_usec;
while (1) {
gettimeofday(&tv, NULL);
diff = (tv.tv_sec - sec) * 1000000 + tv.tv_usec - usec;
if (diff > microseconds) {
// printf("end seconds : %ld micro seconds : %ld\n", tv.tv_sec, tv.tv_usec);
break;
}
}
}
void a() { timed(5000); }
void b() { timed(5000); }
void c() { timed(5000); }
void d() { timed(5000); }
void e() { timed(5000); }
void func() {
for (int i = 0; i < 5; i++) {
a();
b();
fselect();
c();
d();
e();
printf("sleep begin\n");
sleep(0.1);
printf("sleep end\n");
}
}
void *fthread(void *args) {
func();
return NULL;
}
int main() {
pthread_t thread;
pthread_create(&thread, NULL, fthread, NULL);
func();
return 0;
}