Commits

Wang Yuanxuan  committed 67f8523

Fix sembench bug

  • Participants
  • Parent commits 000db46

Comments (0)

Files changed (3)

File sembench/config.h

 #ifndef _CONFIG_H_
 #define _CONFIG_H_
 
-#define NR_PINGPONGS 10
+#define NR_PINGPONGS 100000
 
 #endif /* _CONFIG_H_ */

File sembench/pingpong.c

 #include <stdio.h>
 #include <string.h>
 #include <sys/ipc.h>
-#include <sys/shm.h>
+#include <sys/wait.h>
 #include <sys/sem.h>
 
+#include "config.h"
+
 static int
 ping(int semid)
 {
         semid = semget(semkey, 2, 0666);
     } else {
         semkey = 1001;
-        if ((semid = shmget(semkey, 2, 0666 | IPC_CREAT)) < 0)
+        if ((semid = semget(semkey, 2, 0666 | IPC_CREAT)) < 0)
             exit(-1);
         /* Init semephore array with {1, 0}, so the ping process will run first */
         semctl(semid, 0, SETVAL, 1);
     printf("pingpong begin with semkey=%d, semid=%d\n", semkey, semid);
     pid = fork();
     if (pid == 0) {
-        for (i = 0; i < 10; i++) {
-            printf("ping\n");
+        for (i = 0; i < NR_PINGPONGS; i++) {
             ping(semid);
         }
     } else {
-        for (i = 0; i < 10; i++) {
-            printf("pong\n");
+        for (i = 0; i < NR_PINGPONGS; i++) {
             pong(semid);
         }
+        waitpid(pid, NULL, 0);
     }
 
     return 0;

File sembench/sembench.c

 #include <pthread.h>
 #include <assert.h>
 #include <netdb.h>
-#include <sys/shm.h>
 #include <sys/sem.h>
 #include <sys/ipc.h>
 #include <sys/wait.h>
     key = 1000;
     for (i = 0; i < nchilds; i++) {
         printf("Getting semaphore #%d...", i);
-        while ((semid[i] = shmget(key, 2, 0666 | IPC_CREAT)) < 0)
+        do {
             key ++;
+        } while ((semid[i] = semget(key, 2, 0666 | IPC_CREAT)) < 0);
         printf("<%d>\n", key);
         semkey[i] = key;
         /* Init semephore array with {1, 0}, so the ping process will run first */
     start = read_tsc();
     for (i = 0; i < nchilds; i++) {
         pid[i] = fork();
+        affinity_set(i);
         if (pid[i] == 0) {
             sprintf(buf, "%d", semkey[i]);
             execlp("./pingpong", "./pingpong", buf, (char *) NULL);