94 lines
2.1 KiB
C
94 lines
2.1 KiB
C
|
|
/*
|
||
|
|
* CS 361: Template project driver
|
||
|
|
*
|
||
|
|
* Name: Nicholas Tamassia
|
||
|
|
*/
|
||
|
|
|
||
|
|
#include <assert.h>
|
||
|
|
#include <getopt.h>
|
||
|
|
#include <inttypes.h>
|
||
|
|
#include <pthread.h>
|
||
|
|
#include <stdbool.h>
|
||
|
|
#include <stdio.h>
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include <string.h>
|
||
|
|
|
||
|
|
#include "mutex.h"
|
||
|
|
#include "pingpong.h"
|
||
|
|
|
||
|
|
int cmdline (int, char**, bool*, bool*, size_t *);
|
||
|
|
|
||
|
|
void
|
||
|
|
usage (void)
|
||
|
|
{
|
||
|
|
printf ("Usage: synch option [option...]\n");
|
||
|
|
printf (" At least one argument must be passed\n");
|
||
|
|
printf (" Options are:\n");
|
||
|
|
printf (" -m Execute the runner() threads to demonstrate locks\n");
|
||
|
|
printf (" -p N Run pingong() with N iterations to demonstrate "
|
||
|
|
"semaphore-based timing\n");
|
||
|
|
}
|
||
|
|
|
||
|
|
int
|
||
|
|
main (int argc, char **argv)
|
||
|
|
{
|
||
|
|
bool run_mutex = false;
|
||
|
|
bool run_pingpong = false;
|
||
|
|
size_t count = 0;
|
||
|
|
if (cmdline (argc, argv, &run_mutex, &run_pingpong, &count) < 0)
|
||
|
|
{
|
||
|
|
usage ();
|
||
|
|
return EXIT_FAILURE;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Use mutexes for mutual exclusion
|
||
|
|
if (run_mutex)
|
||
|
|
{
|
||
|
|
printf ("Running mutual exclusion test\n");
|
||
|
|
int64_t shared = run ();
|
||
|
|
printf ("Value of shared: %" PRId64 "\n", shared);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Use semaphores to demonstrate signaling
|
||
|
|
if (run_pingpong)
|
||
|
|
{
|
||
|
|
printf ("Running %zd iteration(s) of ping-pong\n", count);
|
||
|
|
ping (&count);
|
||
|
|
printf ("There should be 0 iterations left: %zd\n", count);
|
||
|
|
}
|
||
|
|
|
||
|
|
pthread_exit (NULL);
|
||
|
|
}
|
||
|
|
|
||
|
|
/*****************************************************************************
|
||
|
|
****************** DO NOT MODIFY FUNCTIONS IN THIS SECTION ******************
|
||
|
|
*****************************************************************************/
|
||
|
|
|
||
|
|
int
|
||
|
|
cmdline (int argc, char **argv, bool *mutex, bool *pingpong, size_t *count)
|
||
|
|
{
|
||
|
|
int option;
|
||
|
|
|
||
|
|
while ((option = getopt (argc, argv, "mp:h")) != -1)
|
||
|
|
{
|
||
|
|
switch (option)
|
||
|
|
{
|
||
|
|
case 'm': *mutex = true;
|
||
|
|
break;
|
||
|
|
case 'p': *pingpong = true;
|
||
|
|
*count = strtol (optarg, NULL, 10);
|
||
|
|
break;
|
||
|
|
case 'h': return -1;
|
||
|
|
break;
|
||
|
|
default: return -1;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!*mutex && !*pingpong)
|
||
|
|
{
|
||
|
|
printf ("You must pass at least one argument\n");
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|