-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpmt1-wasi.c
131 lines (119 loc) · 3.15 KB
/
mpmt1-wasi.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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/*
* mpmt1.c: A stupid simple example of C threading and multiprocessing.
* C Language pthread version of below:
* https://github.com/thatsdone/junkbox/blob/master/python/mpmt1.py
*
* STATUS:
* Under development
* License:
* Apache License, Version 2.0
* History:
* 2021/12/21 v0.1 Initial version based on:
* Author:
* Masanori Itoh <[email protected]>
* BUILD:
* * `$ gcc -o mpmt1 mpmt1.c -lpthread`
* TODO:
* * Use pthread_attr (affinity etc.)
* * Write multi process version.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
//#include <wait.h>
int use_thread = 1;
void *worker(void *arg)
{
struct timeval tv, tv_save;
long ts, ts_save;
long duration = *(long *)arg;
pthread_t tid = 0;
pid_t pid = 0;
if (use_thread) {
tid = pthread_self();
printf("%s: TID: %p running: %ld (us)\n",
__func__, tid, *(long *)arg);
} else {
printf("Process model not supported.");
}
gettimeofday(&tv_save, NULL);
ts_save = tv_save.tv_sec * 1000 * 1000 + tv_save.tv_usec;
while (1) {
gettimeofday(&tv, NULL);
ts = tv.tv_sec * 1000 * 1000 + tv.tv_usec;
if ((ts - ts_save) > duration) {
printf("%s: TID: %p Expired!\n", __func__, tid);
break;
}
}
if (use_thread) {
//pthread_exit(0);
return NULL;
} else {
printf("Process model not supported.");
}
return NULL;
}
#define MAX_CONTEXT 16
int main(int argc, char **argv)
{
int i, ret, opt;
pthread_t th[MAX_CONTEXT];
pthread_t tid;
pid_t pr[MAX_CONTEXT];
pid_t pid;
int num_context = 4;
long duration = 5 * 1000 * 1000;
while ((opt = getopt(argc, argv, "n:d:m:")) != -1) {
switch (opt) {
case 'n':
num_context = atoi(optarg);
if (num_context > MAX_CONTEXT) {
printf("%s: -n too big. %d (<= %d)\n", argv[0], num_context, MAX_CONTEXT);
exit(-1);
}
break;
case 'd':
duration = atol(optarg) * 1000 * 1000;
break;
case 'm':
if (*optarg == 't' || *optarg == 'T') {
printf("Multi thread mode,\n");
use_thread = 1; //thread
} else if (*optarg == 'p' || *optarg == 'P') {
printf("Multi process mode,\n");
use_thread = 0; //process
} else {
printf("Unknown -m value : %s\n", optarg);
exit(-1);
}
break;
default: /* '?' */
fprintf(stderr, "Usage: %s [-n NUM_CONTEXT] [-d DURATION] [-m MODE]\n",
argv[0]);
exit(-1);
}
}
// printf("%s: PID: %d. Creating workers.\n", __func__, getpid());
for (i = 0; i < num_context; i++) {
if (use_thread) {
ret = pthread_create(&th[i], NULL, worker, (void *)&duration);
printf("%s: pthread_create retruned %d\n", __func__, ret);
} else {
printf("Process model not supported.");
}
}
// printf("%s: PID: %d. Waiting for completion of workers.\n", __func__, getpid());
for (i = 0; i < num_context; i++) {
if (use_thread) {
ret = pthread_join(th[i], (void **)NULL);
printf("%s: pthread_join for %d retruned %d / %lu\n",
__func__, i, ret, (unsigned long)0);
} else {
printf("Process model not supported.");
}
}
}