-
Notifications
You must be signed in to change notification settings - Fork 0
/
tvsp.c
165 lines (142 loc) · 3.98 KB
/
tvsp.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <unistd.h>
#include <fcntl.h>
#include <sched.h>
#include <linux/sched.h>
#include <sys/mman.h>
#include <sys/wait.h>
#define STACK_SIZE (1024 * 16)
struct args
{
int threads;
int parallelism;
int iterations;
};
int parse_args(int argc, char *argv[], struct args *args)
{
args->threads = 0;
// Parse arguments using getopt
int opt;
while ((opt = getopt(argc, argv, "tp:i:")) != -1)
{
switch (opt)
{
case 't':
args->threads = 1;
break;
case 'p':
args->parallelism = atoi(optarg);
break;
case 'i':
args->iterations = atoi(optarg);
break;
default:
fprintf(stderr, "Usage: %s [-t] [-p parallelism]\n", argv[0]);
return 1;
}
}
// Check that the arguments are valid
if (args->parallelism < 1)
{
fprintf(stderr, "Parallelism must be at least 1\n");
return 1;
}
return 0;
};
struct thread_args
{
int id;
int read_pipe;
int write_pipe;
int iterations;
pid_t pid;
};
int thread(void *arg)
{
struct thread_args *args = (struct thread_args *)arg;
// printf("Thread %d pid=%d/%d\n", args->id, getpid(), args->pid);
int send_buf = 0, recv_buf = 0;
for (int i = 0; i < args->iterations; i++)
{
if (args->id == 0)
{
write(args->write_pipe, &send_buf, sizeof(int));
read(args->read_pipe, &recv_buf, sizeof(int));
send_buf = recv_buf + 1;
}
else
{
read(args->read_pipe, &recv_buf, sizeof(int));
send_buf = recv_buf + 1;
write(args->write_pipe, &send_buf, sizeof(int));
}
}
// fprintf(stderr, "Thread %d pid=%d/%d r=%d w=%d buf=%d\n", args->id, getpid(), args->pid, args->read_pipe, args->write_pipe, recv_buf);
return 0;
}
int main(int argc, char *argv[])
{
struct args args;
if (parse_args(argc, argv, &args) != 0)
{
return 1;
}
// Print arguments
printf("Threads: %d\n", args.threads);
printf("Parallelism: %d\n", args.parallelism);
// Create pipes for each thread
int *all_pipes = malloc(sizeof(int) * args.parallelism * 2);
for (int i = 0; i < args.parallelism; i++)
{
int *cur_pipe = all_pipes + (i * 2);
if (pipe(cur_pipe) == -1)
{
perror("pipe");
return 1;
}
}
// Create threads
for (int i = 0; i < args.parallelism; i++)
{
int ret;
int *prev_pipe = all_pipes + ((i + args.parallelism - 1) % args.parallelism) * 2;
int *next_pipe = all_pipes + i * 2;
struct thread_args *thread_args = malloc(sizeof(struct thread_args));
void *stack = NULL;
/* Allocate memory to be used for the stack of the child. */
stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
if (stack == MAP_FAILED)
perror("mmap");
void *stack_top = stack + STACK_SIZE;
thread_args->id = i;
thread_args->read_pipe = prev_pipe[0];
thread_args->write_pipe = next_pipe[1];
thread_args->iterations = args.iterations;
// Clone the process
int flags = CLONE_CHILD_CLEARTID | CLONE_CHILD_SETTID | SIGCHLD | CLONE_FILES | CLONE_FS;
if (args.threads)
{
flags |= CLONE_VM | CLONE_SIGHAND | SIGCHLD;
ret = clone(thread, stack_top, flags, thread_args, NULL, NULL, &thread_args->pid);
}
else
{
ret = clone(thread, stack_top, flags, thread_args, NULL, NULL, &thread_args->pid);
}
if (ret == -1)
{
perror("clone");
return 1;
}
}
// Wait for threads to finish
for (int i = 0; i < args.parallelism; i++)
{
int status;
wait(&status);
}
}