From ed10bd81d0f348a3bce39b77fb41be31019fc4b6 Mon Sep 17 00:00:00 2001 From: Raghavendra Bhat Date: Tue, 24 Jan 2012 23:36:45 +0530 Subject: ping_pong: spawn multiple threads, which will do locking on the same fd This patch provides an option for the user to give the number of threds as an argument. Before, ping_pong functionlity (i.e. locking the byte range) was done in a separate single thread. With this patch depending upon the input argument, multiple threads will be spawned all of which will try to do the locking using the same fd. Change-Id: Ib68bef407d75351664a76e81474814bd703d285c Signed-off-by: Raghavendra Bhat --- c_pgms/ping_pong.c | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) (limited to 'c_pgms') diff --git a/c_pgms/ping_pong.c b/c_pgms/ping_pong.c index 90141cf..e6dd160 100644 --- a/c_pgms/ping_pong.c +++ b/c_pgms/ping_pong.c @@ -191,13 +191,14 @@ out: int main(int argc, char *argv[]) { char *fname; - int fd, num_locks; + int fd, num_locks, num_threads; int c; file_info_t *info_file = NULL; - pthread_t thread; - int tid = -1; int zzzz = 600; int ret = 0; + int i = 0; + + num_threads = 1; while ((c = getopt(argc, argv, "rwm")) != -1) { switch (c){ @@ -220,7 +221,7 @@ int main(int argc, char *argv[]) argc -= optind; if (argc < 2) { - printf("ping_pong [options] [run_time]\n"); + printf("ping_pong [options] [num_threads] [run_time]\n"); printf(" -r do reads\n"); printf(" -w do writes\n"); printf(" -m use mmap\n"); @@ -231,19 +232,39 @@ int main(int argc, char *argv[]) ret = check_options_valid (argv[1], &num_locks); if (ret < 0) - goto out; + return ret; if (argv[2]) { - ret = check_options_valid (argv[2], &zzzz); + ret = check_options_valid (argv[2], &num_threads); + if (ret < 0) + return ret; + } + + if (argv[3]) { + ret = check_options_valid (argv[3], &zzzz); if (ret < 0) - goto out; + return ret; + } + + if (num_threads <= 0) { + fprintf (stderr, "number of threads cannot be -ve. Defaulting to 1\n"); + num_threads = 1; + } + + if (num_threads >= num_locks) { + fprintf (stderr, "number of threads (%d) should be lesser than" + " the number of locks (%d)\n", num_threads, num_locks); + return -1; } if (zzzz < 0) { - fprintf (stderr, "Cannot sleep for -ve seconds. Defaulting to 600"); + fprintf (stderr, "Cannot sleep for -ve seconds. Defaulting to 600\n"); zzzz = 600; } + pthread_t thread[num_threads]; + int tid[num_threads]; + fd = open(fname, O_CREAT|O_RDWR, 0600); if (fd == -1) exit(1); @@ -256,11 +277,18 @@ int main(int argc, char *argv[]) info_file->fd = fd; info_file->num_locks = num_locks; - tid = pthread_create (&thread, NULL, (void *)ping_pong, (void *)info_file); + printf ("num_locks: %d, num_threads: %d, time: %d\n", + num_locks, num_threads, zzzz); + sleep (1); + + for (i = 0; i < num_threads ; i++) { + tid[i] = pthread_create (&thread[i], NULL, (void *)ping_pong, (void *)info_file); + } if (zzzz == 0) { printf ("running indefinitely\n"); - pthread_join (thread, NULL); + for (i = 0; i < num_threads; i++) + pthread_join (thread[i], NULL); goto out; } -- cgit