/* quick test of semaphore speed */ /* Results with the following defines: #define NSEMS 20 #define NPROCS 60 #define NUMOPS 10000 OSF1 dominion.aquasoft.com.au V4.0 564 alpha (233 MHz) fcntl: 132.814 secs ipc: 13.5186 secs Linux dominion.aquasoft.com.au 2.0.30 (alpha 233 MHz) fcntl: 16.9473 secs ipc: 61.9336 secs Linux 2.1.57 on a P120 fcntl: 21.3006 secs ipc: 93.9982 secs Solaris 2.5 on a Ultra170 fcntl: 192.805 secs ipc: 18.6859 secs IRIX 6.4 on a Origin200 fcntl: 183.488 secs ipc: 10.4431 secs SunOS 4.1.3 on a 2 CPU sparc 10 fcntl: 198.239 secs ipc: 150.026 secs Solaris 2.5.1 on a 4 CPU SPARCsystem-600 fcntl: 312.025 secs ipc: 24.5416 secs Linux 2.1.131 on K6/200 fcntl: 8.27074 secs ipc: 49.7771 secs Linux 2.0.34 on P200 fcntl: 7.99596 secs ipc: 52.3388 secs AIX 4.2 on 2-proc RS6k with 233MHz processors fcntl: 12.4272 secs ipc: 5.41895 secs */ #include #include #include #include #include #include #include #include #include #ifdef HAVE_POSIX_SEM #include #endif #include #include #define NSEMS 20 #define NPROCS 60 #define NUMOPS 10000 #define SEMAPHORE_KEY 0x569890 #define SHM_KEY 0x568698 static int fcntl_fd; static int sem_id; static int shm_id; static int *shm_p; static int mypid; #ifdef HAVE_POSIX_SEM static sem_t *posix_sems; #endif static int state[NSEMS]; #define BUFSIZE (1024) static char buf1[BUFSIZE]; static char buf2[BUFSIZE]; #ifdef NO_SEMUN union semun { int val; struct semid_ds *buf; unsigned short *array; }; #endif static struct timeval tp1,tp2; static void start_timer() { gettimeofday(&tp1,NULL); } static double end_timer() { gettimeofday(&tp2,NULL); return((tp2.tv_sec - tp1.tv_sec) + (tp2.tv_usec - tp1.tv_usec)*1.0e-6); } /************************************************************************ USING FCNTL LOCKS ***********************************************************************/ void fcntl_setup(void) { fcntl_fd = open("fcntl.locks", O_RDWR | O_CREAT | O_TRUNC, 0600); write(fcntl_fd, state, sizeof(state)); } void fcntl_close(void) { unlink("fcntl.locks"); } void fcntl_sem_lock(int i) { struct flock lock; int ret; lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = i*sizeof(int); lock.l_len = sizeof(int); lock.l_pid = 0; errno = 0; if (fcntl(fcntl_fd, F_SETLKW, &lock) != 0) { perror("WRLCK"); exit(1); } } void fcntl_sem_unlock(int i) { struct flock lock; int ret; lock.l_type = F_UNLCK; lock.l_whence = SEEK_SET; lock.l_start = i*sizeof(int); lock.l_len = sizeof(int); lock.l_pid = 0; errno = 0; if (fcntl(fcntl_fd, F_SETLKW, &lock) != 0) { perror("WRLCK"); exit(1); } } /************************************************************************ USING SYSV IPC SEMAPHORES ***********************************************************************/ void ipc_setup(void) { int i; union semun su; su.val = 1; sem_id = semget(SEMAPHORE_KEY, NSEMS, IPC_CREAT|IPC_EXCL|0600); if (sem_id == -1) { sem_id = semget(SEMAPHORE_KEY, 0, 0); if (sem_id != -1) { semctl(sem_id, 0, IPC_RMID, su); sem_id = semget(SEMAPHORE_KEY, NSEMS, IPC_CREAT|IPC_EXCL|0600); } } if (sem_id == -1) { perror("semget"); exit(1); } for (i=0;i