Tuesday, 31 January 2017

Threaded test case for radix tree __alloc_fd

The purpose of this test cases is to stress the locking inside the kernel.

All threads share a common file descriptor table. As each thread opens and closes its file descriptors, it must ensure that other threads do not interfere. So if we have many threads running, simultaneously opening and closing file descriptors, we’re trying to provoke a crash, or a corruption, or some other bad behavior by the kernel.


 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
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>

#define NUM_THREADS 12
#define BUF_SIZE 1024

void *fd_task(void *arg)
{
 int fd, err = 0;
 int ret;
 char buf[BUF_SIZE];

 fd = open(/etc/passwd,O_RDONLY);
 if(fd == -1) {
  printf(open failed\n);
 }

 ret = read(fd, buf, 1024);
 if(ret < 0) {
  printf(read failed\n);
 }
 close(fd);
}

int main(int argc, char *argv[]) {
 int i, err;
 pthread_t thread[NUM_THREADS];

/* Create threads*/
 for (i = 0; i < NUM_THREADS; ++i) {
 if ((err = pthread_create(&thread[i], NULL, fd_task, NULL)))
 {
  printf(Error : pthread_create()\n);
  goto out;
 }
 }

/* block until all threads complete */
 for (i = 0; i < NUM_THREADS; ++i)
 {
  pthread_join(thread[i], NULL);
 }
out:
 return err;
}

No comments:

Post a Comment