Revert "getdents_c: read from two threads"

This made the code more complicated without reproducing
the problem.

This reverts commit 77632b7554.
This commit is contained in:
Jakob Unterwurzacher 2020-07-29 20:29:24 +02:00
parent 13f1980dc4
commit 5073459296
2 changed files with 17 additions and 50 deletions

View File

@ -1,2 +1,2 @@
getdents_c: *.c Makefile getdents_c: *.c Makefile
gcc getdents.c -lpthread -o getdents_c gcc getdents.c -o getdents_c

View File

@ -1,7 +1,6 @@
// See ../getdents/getdents.go for some info on why // See ../getdents/getdents.go for some info on why
// this exists. // this exists.
#define _GNU_SOURCE
#include <fcntl.h> #include <fcntl.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -10,38 +9,6 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/syscall.h> #include <sys/syscall.h>
#include <errno.h> #include <errno.h>
#include <pthread.h>
static pthread_mutex_t mutex;
static int sum;
struct up {
pthread_mutex_t *my_mutex;
pthread_mutex_t *other_mutex;
int fd;
};
void *reader(void *arg) {
struct up* up = (struct up*)arg;
int fd = up->fd;
char tmp[10000];
while(1) {
pthread_mutex_lock(up->my_mutex);
int n = syscall(SYS_getdents64, fd, tmp, sizeof(tmp));
if (n > 0) {
printf("t%ld: n=%d\n", gettid(), n);
} else {
printf("t%ld: n=0 errno=%d total %d bytes\n", gettid(), errno, sum);
if (n < 0) {
exit(1);
}
pthread_mutex_unlock(up->other_mutex);
break;
}
sum += n;
pthread_mutex_unlock(up->other_mutex);
}
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
@ -54,28 +21,28 @@ int main(int argc, char *argv[])
const char *path = argv[1]; const char *path = argv[1];
for (int i = 1 ; ; i ++ ) { for (int i = 1 ; ; i ++ ) {
sum = 0;
int fd = open(path, O_RDONLY); int fd = open(path, O_RDONLY);
if (fd == -1) { if (fd == -1) {
perror("open"); perror("open");
exit(1); exit(1);
} }
pthread_t reader1_thread, reader2_thread;
pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER, m2 = PTHREAD_MUTEX_INITIALIZER; char tmp[10000];
pthread_mutex_lock(&m2); int sum = 0;
struct up up1, up2; printf("%3d: getdents64: ", i);
up1.fd = fd; for ( ; ; ) {
up1.my_mutex = &m1; int n = syscall(SYS_getdents64, fd, tmp, sizeof(tmp));
up1.other_mutex = &m2; printf("n=%d; ", n);
up2.fd = fd; if (n <= 0) {
up2.my_mutex = &m2; printf("errno=%d total %d bytes\n", errno, sum);
up2.other_mutex = &m1; if (n < 0) {
pthread_create(&reader1_thread, NULL, reader, &up1); exit(1);
pthread_create(&reader2_thread, NULL, reader, &up2); }
pthread_join(reader1_thread, NULL); break;
pthread_join(reader2_thread, NULL); }
sum += n;
}
close(fd); close(fd);
printf("---\n");
usleep(100000); usleep(100000);
} }
} }