-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added dining table structre and added new tests
- Loading branch information
Diego Herrera
committed
Oct 8, 2021
1 parent
e36ee5a
commit 70efd1b
Showing
7 changed files
with
284 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,99 @@ | ||
#include "waiting-state/philosopher.h" | ||
#include <iostream> | ||
#include <unistd.h> | ||
#include "waiting-state/diningTable.h" | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <getopt.h> | ||
#include <iostream> | ||
#include <unistd.h> /* read, write, pipe, _exit */ | ||
#include <string.h> | ||
|
||
|
||
using namespace std; | ||
|
||
int main(int argc, char *argv[]) | ||
/* Flag set by ‘--verbose’. */ | ||
static int verbose_flag; | ||
|
||
|
||
|
||
void displayUsage() | ||
{ | ||
cout << | ||
"-n/--number <n>: Number 'n' of philosophers. <Default: 5>\n" | ||
"-h/--help: Show help\n"; | ||
exit(EXIT_SUCCESS); | ||
} | ||
|
||
int main (int argc, char **argv) | ||
{ | ||
return 0; | ||
int c; | ||
int n = 5; | ||
|
||
while (true) | ||
{ | ||
static struct option long_options[] = | ||
{ | ||
/* These options set a flag. */ | ||
{"verbose", no_argument, &verbose_flag, 1}, | ||
{"brief", no_argument, &verbose_flag, 0}, | ||
/* These options don’t set a flag. | ||
We distinguish them by their indices. */ | ||
|
||
{"number", required_argument, 0, 'n'}, | ||
{"help", no_argument, 0, 'h'}, | ||
{0, 0, 0, 0} | ||
}; | ||
|
||
/* getopt_long stores the option index here. */ | ||
int option_index = 0; | ||
|
||
c = getopt_long (argc, argv, "hn:", | ||
long_options, &option_index); | ||
|
||
|
||
|
||
/* Detect the end of the options. */ | ||
if (c == -1) | ||
break; | ||
|
||
switch (c) | ||
{ | ||
case 0: | ||
/* If this option set a flag, do nothing else now. */ | ||
if (long_options[option_index].flag != 0) | ||
break; | ||
printf ("option %s", long_options[option_index].name); | ||
if (optarg) | ||
printf (" with arg %s", optarg); | ||
printf ("\n"); | ||
break; | ||
|
||
case 'n': | ||
n = atoi(optarg); | ||
break; | ||
case 'h': | ||
case '?': | ||
default: | ||
displayUsage(); | ||
abort (); | ||
} | ||
} | ||
|
||
/* Instead of reporting ‘--verbose’ | ||
and ‘--brief’ as they are encountered, | ||
we report the final status resulting from them. */ | ||
if (verbose_flag) | ||
puts ("verbose flag is set"); | ||
|
||
/* Print any remaining command line arguments (not options). */ | ||
if (optind < argc) | ||
{ | ||
printf ("non-option ARGV-elements: "); | ||
while (optind < argc) | ||
printf ("%s ", argv[optind++]); | ||
putchar ('\n'); | ||
} | ||
|
||
WaitingPhilosopher::DiningTable::startDinner(n); | ||
|
||
exit (EXIT_SUCCESS); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include <string> | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
|
||
#include <sys/ipc.h> | ||
#include <sys/sem.h> | ||
#include <sys/shm.h> | ||
#include <sys/wait.h> | ||
|
||
#include "diningTable.h" | ||
#include "philosopher.h" | ||
#include "chopstick.h" | ||
#include "constants.h" | ||
|
||
using namespace WaitingPhilosopher; | ||
using namespace std; | ||
|
||
void DiningTable::startDinner(int numberPhilosophers) | ||
{ | ||
Philosopher* philosophers = (Philosopher*)malloc(sizeof(Philosopher) * numberPhilosophers); | ||
Chopstick* chopsticks = (Chopstick*)malloc(sizeof(Chopstick) * numberPhilosophers);; | ||
pid_t pids[numberPhilosophers]; | ||
|
||
printf("\n------ Dining Philosophers ------\n\n\n"); | ||
printf("%i philosophers dining\n\n", numberPhilosophers); | ||
for (int i = 0; i < numberPhilosophers; i++) | ||
{ | ||
philosophers[i] = Philosopher(i, numberPhilosophers); | ||
chopsticks[i] = Chopstick(i); | ||
} | ||
|
||
|
||
for (int i = 0; i < numberPhilosophers; i++) | ||
{ | ||
if ((pids[i] = fork()) < 0) | ||
{ | ||
perror("fork"); | ||
abort(); | ||
} | ||
else if (pids[i] == 0) | ||
{ | ||
philosophers[i].dine(); | ||
exit(0); | ||
} | ||
} | ||
|
||
int status; | ||
pid_t pid; | ||
int n = numberPhilosophers; | ||
while (n > 0) | ||
{ | ||
pid = wait(&status); | ||
//printf("Child with PID %ld exited with status 0x%x.\n", (long)pid, status); | ||
--n; // TODO(pts): Remove pid from the pids array. | ||
} | ||
|
||
for (int i = 0; i < numberPhilosophers; i++) | ||
{ | ||
chopsticks[i].putAway(); | ||
} | ||
|
||
printf("\nFinished Diner\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#ifndef DININGTABLE_H_ | ||
#define DININGTABLE_H_ | ||
|
||
// #ifdef __cplusplus | ||
// extern "C" { | ||
// #endif | ||
#include <string> | ||
|
||
namespace WaitingPhilosopher | ||
{ | ||
class DiningTable | ||
{ | ||
public: | ||
static void startDinner(int numberPhilosophers); | ||
}; | ||
} | ||
|
||
#endif /* DININGTABLE_H_ */ | ||
|
||
// EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.