-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathch1.c
More file actions
63 lines (53 loc) · 1.57 KB
/
ch1.c
File metadata and controls
63 lines (53 loc) · 1.57 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
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <assert.h>
#include <unistd.h>
#include <pthread.h>
// Spin for 'howlong' seconds using busy-waiting
void Spin(int howlong) {
struct timeval start, end;
gettimeofday(&start, NULL);
double elapsed = 0.0;
while (elapsed < (double)howlong) {
gettimeofday(&end, NULL);
elapsed = (double)(end.tv_sec - start.tv_sec) +
(double)(end.tv_usec - start.tv_usec) / 1000000.0;
}
}
// Thread argument structure
typedef struct {
int thread_id;
char *message;
} thread_arg_t;
// Thread function
void *worker(void *arg) {
thread_arg_t *targ = (thread_arg_t *)arg;
while (1) {
Spin(1);
printf("[PID: %d | Thread-%d] %s\n", getpid(), targ->thread_id, targ->message);
fflush(stdout);
}
return NULL;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "usage: cpu <string>\n");
exit(1);
}
printf("=== Multi-threaded Demo ===\n");
printf("PID: %d\n", getpid());
printf("Creating 2 threads...\n\n");
fflush(stdout);
pthread_t t1, t2;
// Setup arguments for each thread
thread_arg_t arg1 = { .thread_id = 1, .message = argv[1] };
thread_arg_t arg2 = { .thread_id = 2, .message = argv[1] };
// Create threads
pthread_create(&t1, NULL, worker, &arg1);
pthread_create(&t2, NULL, worker, &arg2);
// Wait for threads (will never return since threads loop forever)
pthread_join(t1, NULL);
pthread_join(t2, NULL);
return 0;
}