123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128 |
- #include "bench.h"
- /*
- * lat_pipe.c - pipe transaction test
- *
- * Copyright (c) 1994 Larry McVoy. Distributed under the FSF GPL with
- * additional restriction that results may published only if
- * (1) the benchmark is unmodified, and
- * (2) the version in the sccsid below is included in the report.
- * Support for this development by Sun Microsystems is gratefully acknowledged.
- */
- char *id = "$Id: lat_pipe.c,v 1.8 1997/06/16 05:38:58 lm Exp $\n";
- #include "bench.h"
- struct flock lock, unlock;
- struct flock s1, s2;
- int fd1, fd2;
- /*
- * Create two files, use them as a ping pong test.
- * Process A:
- * lock(1)
- * unlock(2)
- * Process B:
- * unlock(1)
- * lock(2)
- * Initial state:
- * lock is locked
- * lock2 is locked
- */
- #define waiton(fd) fcntl(fd, F_SETLKW, &lock)
- #define release(fd) fcntl(fd, F_SETLK, &unlock)
- void
- procA()
- {
- if (waiton(fd1) == -1) {
- perror("lock of fd1 failed\n");
- exit(1);
- }
- if (release(fd2) == -1) {
- perror("unlock of fd2 failed\n");
- exit(1);
- }
- if (waiton(fd2) == -1) {
- perror("lock of fd2 failed\n");
- exit(1);
- }
- if (release(fd1) == -1) {
- perror("unlock of fd1 failed\n");
- exit(1);
- }
- }
- void
- procB()
- {
- if (release(fd1) == -1) {
- perror("unlock of fd1 failed\n");
- exit(1);
- }
- if (waiton(fd2) == -1) {
- perror("lock of fd2 failed\n");
- exit(1);
- }
- if (release(fd2) == -1) {
- perror("unlock of fd2 failed\n");
- exit(1);
- }
- if (waiton(fd1) == -1) {
- perror("lock of fd1 failed\n");
- exit(1);
- }
- }
- int
- main()
- {
- char buf[10000];
- int pid;
- unlink("/tmp/lmbench-fcntl.1");
- unlink("/tmp/lmbench-fcntl.2");
- if ((fd1 = open("/tmp/lmbench-fcntl.1", O_CREAT|O_RDWR, 0666)) == -1) {
- perror("create");
- exit(1);
- }
- if ((fd2 = open("/tmp/lmbench-fcntl.2", O_CREAT|O_RDWR, 0666)) == -1) {
- perror("create");
- exit(1);
- }
- unlink("/tmp/lmbench-fcntl.1");
- unlink("/tmp/lmbench-fcntl.2");
- write(fd1, buf, sizeof(buf));
- write(fd2, buf, sizeof(buf));
- lock.l_type = F_WRLCK;
- lock.l_whence = 0;
- lock.l_start = 0;
- lock.l_len = 1;
- unlock = lock;
- unlock.l_type = F_UNLCK;
- if (waiton(fd1) == -1) {
- perror("lock1");
- exit(1);
- }
- if (waiton(fd2) == -1) {
- perror("lock2");
- exit(1);
- }
- switch (pid = fork()) {
- case -1:
- perror("fork");
- exit(1);
- case 0:
- for ( ;; ) {
- procB();
- }
- exit(0);
- default:
- break;
- }
- BENCH(procA(), SHORT);
- micro("Fcntl lock latency", 2 * get_n());
- kill(pid, 15);
- return (0);
- }
|