Created
November 18, 2019 08:22
-
-
Save tanakamura/25089f85049cb8c1c14a89579054c217 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <fcntl.h> | |
#include <pthread.h> | |
#include <stdio.h> | |
static double getsec() | |
{ | |
struct timespec ts; | |
clock_gettime(CLOCK_MONOTONIC, &ts); | |
return ts.tv_sec + ts.tv_nsec/1e9; | |
} | |
struct shared { | |
int n; | |
int p0[2], p1[2]; | |
}; | |
void *t0(void *p) | |
{ | |
struct shared *s = (struct shared*)p; | |
int i; | |
int n = s->n; | |
for (int i=0; i<n; i++) { | |
char b; | |
read(s->p0[0], &b, 1); | |
write(s->p1[1], &b, 1); | |
} | |
} | |
void test(struct shared *sd) | |
{ | |
pthread_t pt; | |
pthread_create(&pt, NULL, | |
t0, sd); | |
int n = sd->n; | |
double t0 = getsec(); | |
for (int i=0; i<n; i++) { | |
char b=4; | |
write(sd->p0[1], &b, 1); | |
read(sd->p1[0], &b, 1); | |
} | |
double t1 = getsec(); | |
printf("%f[ns]\n", ((t1-t0)/sd->n)*1e9); | |
pthread_join(pt, NULL); | |
} | |
int main() | |
{ | |
struct shared sd; | |
int n = 65536; | |
sd.n = n; | |
pipe(sd.p0); | |
pipe(sd.p1); | |
test(&sd); | |
test(&sd); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment