Skip to content

Commit

Permalink
Added dining table structre and added new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Herrera committed Oct 8, 2021
1 parent e36ee5a commit 70efd1b
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 70 deletions.
23 changes: 21 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,30 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Unittest Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/code/bin/unittest",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
},
{
"name": "(gdb) Attach",
"type": "cppdbg",
"request": "attach",
"program": "${workspaceFolder}/code/bin/unittest",
"program": "${workspaceFolder}/code/bin/dining-philosophers",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"setupCommands": [
Expand All @@ -23,7 +42,7 @@
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/code/bin/unittest",
"program": "${workspaceFolder}/code/bin/dining-philosophers",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
Expand Down
97 changes: 92 additions & 5 deletions code/src/main.cpp
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);
}
2 changes: 1 addition & 1 deletion code/src/waiting-state/chopstick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Chopstick::Chopstick(int index)

void Chopstick::putAway()
{
int rc = semctl( m_semid, 1, IPC_RMID );
int rc = semctl( m_semid, 0, IPC_RMID );
if (rc==-1)
{
std::string error = "ID: " + std::to_string(m_index) + " semctl() remove id failed\n";
Expand Down
63 changes: 63 additions & 0 deletions code/src/waiting-state/diningTable.cpp
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");
}
20 changes: 20 additions & 0 deletions code/src/waiting-state/diningTable.h
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
6 changes: 3 additions & 3 deletions code/src/waiting-state/philosopher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,15 @@ void Philosopher::waitChopstick(int index)

void Philosopher::pickupChopstick(int index)
{
printf("ID: %i - pickup chopstick: %i\n",m_index,index);
printf("ID: %i - Pickup chopstick: %i\n",m_index,index);
// std::cout<<"Me: "<<m_index<<"Waiting for chopstick: "<<index<<std::endl;
semaphoreOperation(index, 1);

}

void Philosopher::putdownChopstick(int index)
{
printf("ID: %i - putdown chopstick: %i\n",m_index,index);
printf("ID: %i - Putdown chopstick: %i\n",m_index,index);
// std::cout<<"Me: "<<m_index<<"Waiting for chopstick: "<<index<<std::endl;
semaphoreOperation(index, -1);

Expand All @@ -89,7 +89,7 @@ void Philosopher::semaphoreOperation(int index, short action)

// Generate an IPC key for the semaphore set
semKey = ftok(SEMKEYPATH,index);
if ( m_semkey == (key_t)-1 )
if ( semKey == (key_t)-1 )
{
std::string error = "ID: " + std::to_string(m_index) + " ftok() for sem failed\n";
perror(error.c_str());
Expand Down
Loading

0 comments on commit 70efd1b

Please sign in to comment.