#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) {
break;
}
+ if (verbose)
+ printf("pid:%u\n", pid);
+
lck.l_type = F_UNLCK;
ret = fcntl(fd, F_SETLKW, &lck);
if (ret < 0) {
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;
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);
case 'n':
nproc = atoi(optarg);
break;
+ case 'v':
+ verbose = true;
+ break;
default:
usage(argv[0]);
return 1;
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) {