7 #define MAX_THREADS 1024
8 #define RSIZE (16*1024)
9 static int num_threads;
10 static size_t read_size;
11 static pthread_t tid[MAX_THREADS];
12 static pid_t pid[MAX_THREADS];
14 static void thread_main(void)
22 fd1 = open("/dev/zero", O_RDONLY);
23 fd2 = open("/dev/null", O_WRONLY);
25 for (i=0;i<read_size;i++) {
26 if (read(fd1, buf, RSIZE) != RSIZE) {
27 printf("read failed\n");
30 if (write(fd2, buf, RSIZE) != RSIZE) {
31 printf("write failed\n");
40 static void fork_test(void)
43 time_t start = time(NULL);
45 for (i=0;i < num_threads; i++) {
46 if ((pid[i] = fork()) == 0) {
50 if (pid[i] == (pid_t)-1) {
51 printf("fork %d failed\n", i);
56 for (i=0;i < num_threads; i++) {
57 waitpid(pid[i], NULL, 0);
60 printf("using fork took %d seconds\n", time(NULL) - start);
64 static void pthread_test(void)
67 time_t start = time(NULL);
69 for (i=0;i < num_threads; i++) {
70 if (pthread_create(&tid[i], NULL, thread_main, NULL) != 0) {
71 printf("pthread_create failed\n");
76 for (i=0;i < num_threads; i++) {
77 pthread_join(tid[i], NULL);
80 printf("using pthreads took %d seconds\n", time(NULL) - start);
84 int main(int argc, char *argv[])
87 printf("usage: %s <num_threads> <num_loops>\n",argv[0]);
91 num_threads = atoi(argv[1]);
92 read_size = atoi(argv[2]);
94 if (num_threads > MAX_THREADS) {
95 printf("max_threads is %d\n", MAX_THREADS);
104 printf("all done\n");