Skip to content

Commit

Permalink
Fix ProcCommunicator construction of semaphores
Browse files Browse the repository at this point in the history
  • Loading branch information
ben committed Aug 18, 2024
1 parent cc0ad58 commit 52153c0
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 37 deletions.
2 changes: 1 addition & 1 deletion main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ struct ConnectionsInfo

int main(int argc, char *argv[])
{
const std::string shared_memory_name{"/shared_mem_"};
const std::string shared_memory_name{"/_shmem4"};
bool isStopRequested{false}, connectionConfirmed{false};
std::unique_ptr<ProcCommunicator> slave = std::make_unique<ProcCommunicator>(false, true, shared_memory_name);
am::configuration::Configuration default_conf{75, 10, 1, 50, 5, 10.0};
Expand Down
27 changes: 16 additions & 11 deletions sh_mem/ProcCommunicator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,32 +25,36 @@ ProcCommunicator::ProcCommunicator(const bool isMasterMode,
m_sender = std::make_unique<SharedMemorySender>(master_mem_name.c_str());
m_receiver = std::make_unique<SharedMemoryReceiver>(slave_mem_name.c_str());

m_master_received = sem_open((shMemName + "_m_rsem").c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_slave_received = sem_open((shMemName + "_s_rsem").c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_master_sent = sem_open((shMemName + "_m_sent").c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_slave_sent = sem_open((shMemName + "_s_sent").c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_master_received = sem_open((shMemName + "_m_rsem").c_str(), O_RDWR, 0666, SEMAPHORE_DISABLED);
m_slave_received = sem_open((shMemName + "_s_rsem").c_str(), O_RDWR, 0666, SEMAPHORE_DISABLED);
m_master_sent = sem_open((shMemName + "_m_sent").c_str(), O_RDWR, 0666, SEMAPHORE_DISABLED);
m_slave_sent = sem_open((shMemName + "_s_sent").c_str(), O_RDWR, 0666, SEMAPHORE_DISABLED);

if (isMultipleMasters)
m_slave_ready = sem_open((shMemName + "_s_ready").c_str(), O_CREAT, 0666, SEMAPHORE_ENABLED);
m_slave_ready = sem_open((shMemName + "_s_ready").c_str(), O_RDWR, 0666, SEMAPHORE_ENABLED);
}
else
{
m_sender = std::make_unique<SharedMemorySender>(slave_mem_name.c_str());
m_receiver = std::make_unique<SharedMemoryReceiver>(master_mem_name.c_str());

m_master_received = sem_open((shMemName + "_m_rsem").c_str(), O_RDWR, 0666, SEMAPHORE_DISABLED);
m_slave_received = sem_open((shMemName + "_s_rsem").c_str(), O_RDWR, 0666, SEMAPHORE_DISABLED);
m_master_sent = sem_open((shMemName + "_m_sent").c_str(), O_RDWR, 0666, SEMAPHORE_DISABLED);
m_slave_sent = sem_open((shMemName + "_s_sent").c_str(), O_RDWR, 0666, SEMAPHORE_DISABLED);
m_master_received = sem_open((shMemName + "_m_rsem").c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_slave_received = sem_open((shMemName + "_s_rsem").c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_master_sent = sem_open((shMemName + "_m_sent").c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);
m_slave_sent = sem_open((shMemName + "_s_sent").c_str(), O_CREAT, 0666, SEMAPHORE_DISABLED);

if (isMultipleMasters)
m_slave_ready = sem_open((shMemName + "_s_ready").c_str(), O_RDWR, 0666, SEMAPHORE_ENABLED);

m_slave_ready = sem_open((shMemName + "_s_ready").c_str(), O_CREAT, 0666, SEMAPHORE_ENABLED);
}
if (m_master_received == SEM_FAILED || m_slave_received == SEM_FAILED ||
m_master_sent == SEM_FAILED || m_slave_sent == SEM_FAILED || m_slave_ready == SEM_FAILED)
{
perror("ProcCommunicator sem_open failure.");
exit(1);
}
if(isMultipleMasters && m_slave_ready==SEM_FAILED){
perror("ProcCommunicator MultiMaster mode sem_open failure.");
exit(1);
}
#else
m_sender = std::make_unique<SharedMemorySender>(slave_mem_name.c_str());
Expand Down Expand Up @@ -80,6 +84,7 @@ ProcCommunicator::ProcCommunicator(const bool isMasterMode,
m_master_sent == NULL || m_slave_sent == NULL || m_slave_ready == NULL )
{
perror("ProcCommunicator sem_open failure.");
exit(1);
}

#endif
Expand Down
45 changes: 20 additions & 25 deletions sh_mem/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,19 @@ static const std::string shared_mem_name{"/shmsh1"};
// Function to be executed in a separate thread
void backgroundTask()
{
std::cout << "Background task started...\n";
ProcCommunicator *slave = new ProcCommunicator(false, true, shared_mem_name);
Message *res = nullptr;

std::cout << "Background task starts.\n";
Message msg_hand{2, MessageType::HANDSHAKE};
ProcCommunicator master(true, true, shared_mem_name);
int counter = 0;

while (counter < 200)
while (counter < 1)
{
{
Message msg{777, MessageType::HANDSHAKE_OK};
Message *res = slave->receive();
msg.id = res->id;
slave->send(&msg);
counter++;
}
master.send(&msg_hand);
auto msg_resp = master.receive();
std::cout << "m 2 " << msg_resp->id << std::endl;
counter++;
}

delete slave;
std::cout << "Background task completed.\n";
}

Expand All @@ -38,7 +33,7 @@ void backgroundTaskMasterMaster()

int counter = 0;

while (counter < 100)
while (counter < 1)
{

master.send(&msg_hand);
Expand All @@ -53,25 +48,25 @@ void backgroundTaskMasterMaster()
int main()
{
std::cout << "Main thread starts...\n";
std::vector<int> vec1{1, 2, 3, 4, 5};

Message msg_hand{2, MessageType::HANDSHAKE};
ProcCommunicator master(true, true, shared_mem_name);
std::cout << "Server starts...\n";
ProcCommunicator *slave = new ProcCommunicator(false, true, shared_mem_name);
Message *res = nullptr;

int counter = 0;
std::thread worker(backgroundTask);
std::thread worker_master(backgroundTaskMasterMaster);

// std::this_thread::sleep_for(std::chrono::seconds(5));
int counter = 0;

while (counter < 100)
while (counter < 2)
{
master.send(&msg_hand);
auto msg_resp = master.receive();
// std::cout << "m 2 " << msg_resp->id << std::endl;
Message msg{777, MessageType::HANDSHAKE_OK};
Message *res = slave->receive();
msg.id = res->id;
slave->send(&msg);
counter++;
}

delete slave;

if (worker.joinable())
worker.join();

Expand Down

0 comments on commit 52153c0

Please sign in to comment.