posix01: add "verbose" flag
authorJeff Layton <jlayton@redhat.com>
Sat, 31 Aug 2013 23:45:57 +0000 (19:45 -0400)
committerJeff Layton <jlayton@redhat.com>
Sat, 31 Aug 2013 23:53:52 +0000 (19:53 -0400)
When enabled, print pid that acquires the lock. This allows us to get
a feel for how "fair" the locking is.

Signed-off-by: Jeff Layton <jlayton@redhat.com>
posix01.c

index 4041bad91dd5904322147b26e8122c30f065c654..d9226849af2e1eeaec6dba6de4b205e3c679808c 100644 (file)
--- a/posix01.c
+++ b/posix01.c
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 #include <time.h>
 #include <sys/mman.h>
+#include <stdbool.h>
 
 #include "timespec.h"
 
 static struct timespec *diff;
 
 static int
-lockunlock(const char *name, int nrlock, struct timespec *slot)
+lockunlock(const char *name, int nrlock, struct timespec *slot, bool verbose)
 {
        int fd, i, ret = 0;
        struct flock lck = { .l_whence = SEEK_SET };
        struct timespec start, end;
+       pid_t pid = getpid();
 
        fd = open(name, O_CREAT|O_RDWR, 0644);
        if (fd < 0) {
@@ -57,6 +59,9 @@ lockunlock(const char *name, int nrlock, struct timespec *slot)
                        break;
                }
 
+               if (verbose)
+                       printf("pid:%u\n", pid);
+
                lck.l_type = F_UNLCK;
                ret = fcntl(fd, F_SETLKW, &lck);
                if (ret < 0) {
@@ -80,12 +85,13 @@ lockunlock(const char *name, int nrlock, struct timespec *slot)
 void
 usage(char *prog)
 {
-       printf("usage: %s [-n nr_procs] [-l nr_locks] filename\n", prog);
+       printf("usage: %s [-n nr_procs] [-l nr_locks] [-v] filename\n", prog);
 }
 
 int
 main(int argc, char **argv)
 {
+       bool verbose = false;
        int i, opt, valid = 0;
        int nproc = NRPROC;
        int nlock = NRLOCK;
@@ -93,7 +99,7 @@ main(int argc, char **argv)
        struct timespec total = { .tv_sec = 0,
                                  .tv_nsec = 0 };
 
-       while ((opt = getopt(argc, argv, "l:n:")) != -1) {
+       while ((opt = getopt(argc, argv, "l:n:v")) != -1) {
                switch (opt) {
                case 'l':
                        nlock = atoi(optarg);
@@ -101,6 +107,9 @@ main(int argc, char **argv)
                case 'n':
                        nproc = atoi(optarg);
                        break;
+               case 'v':
+                       verbose = true;
+                       break;
                default:
                        usage(argv[0]);
                        return 1;
@@ -128,7 +137,8 @@ main(int argc, char **argv)
        for (i = 0; i < nproc; ++i) {
                pids[i] = fork();
                if (!pids[i])
-                       return lockunlock(argv[optind], nlock, &diff[i]);
+                       return lockunlock(argv[optind], nlock,
+                                               &diff[i], verbose);
        }
 
        for (i = 0; i < nproc; ++i) {