-
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.
- Loading branch information
Diego Herrera
committed
Oct 8, 2021
1 parent
63d981c
commit e36ee5a
Showing
5 changed files
with
148 additions
and
91 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include "chopstick.h" | ||
#include "constants.h" | ||
|
||
#include <sys/ipc.h> | ||
#include <sys/sem.h> | ||
#include <sys/shm.h> | ||
|
||
#include <iostream> | ||
|
||
#include <unistd.h> | ||
|
||
using namespace WaitingPhilosopher; | ||
|
||
Chopstick::Chopstick(int index) | ||
{ | ||
int rc= 0; | ||
m_index = index; | ||
|
||
/* Generate an IPC key for the semaphore set and the shared */ | ||
/* memory segment. Typically, an application specific path and */ | ||
/* id would be used to generate the IPC key. */ | ||
m_semkey = ftok(SEMKEYPATH,m_index); | ||
if ( m_semkey == (key_t)-1 ) | ||
{ | ||
std::string error = "ID: " + std::to_string(m_index) + " ftok() for sem failed\n"; | ||
perror(error.c_str()); | ||
abort(); | ||
} | ||
|
||
|
||
/* Create a semaphore set using the IPC key. The number of */ | ||
/* semaphores in the set is two. If a semaphore set already */ | ||
/* exists for the key, return an error. The specified permissions*/ | ||
/* give everyone read/write access to the semaphore set. */ | ||
|
||
m_semid = semget( m_semkey, NUMSEMS, 0666 | IPC_CREAT | IPC_EXCL ); | ||
if ( m_semid == -1 ) | ||
{ | ||
std::string error = "ID: " + std::to_string(m_index) + " semget() failed\n"; | ||
perror(error.c_str()); | ||
abort(); | ||
} | ||
|
||
// Initialize the semaphore in the set to 0 | ||
rc = semctl( m_semid, 0, SETVAL, 0); | ||
if(rc == -1) | ||
{ | ||
std::string error = "ID: " + std::to_string(m_index) + " semctl() initialization failed\n"; | ||
perror(error.c_str()); | ||
|
||
} | ||
} | ||
|
||
void Chopstick::putAway() | ||
{ | ||
int rc = semctl( m_semid, 1, IPC_RMID ); | ||
if (rc==-1) | ||
{ | ||
std::string error = "ID: " + std::to_string(m_index) + " semctl() remove id failed\n"; | ||
perror(error.c_str()); | ||
} | ||
} |
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,25 @@ | ||
#ifndef CHOPSTICK_H_ | ||
#define CHOPSTICK_H_ | ||
|
||
// #ifdef __cplusplus | ||
// extern "C" { | ||
// #endif | ||
#include <string> | ||
|
||
namespace WaitingPhilosopher | ||
{ | ||
class Chopstick | ||
{ | ||
public: | ||
Chopstick(int index); | ||
void putAway(); | ||
protected: | ||
int m_index; | ||
int m_semid; | ||
key_t m_semkey; | ||
}; | ||
} | ||
|
||
#endif /* CHOPSTICK_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
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