Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7663871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:09:33+00:00 2026-05-31T14:09:33+00:00

For homework I need to use IPC. I write some code for shared memory

  • 0

For homework I need to use IPC. I write some code for shared memory but it does not work as I want. I want server process to run before client. What I am doing wrong? How to fix it?

//main.cpp
#include "stockexchangeserver.h"
#include "stockexchangeclient.h"
#include <semaphore.h>

int main(int argc,char *argv[])
{
   StockExchangeServer server;
   StockExchangeClient client;
   pid_t   pid;
   sem_t sem;
   int pshared = 1;
   unsigned int value = 0;
   sem_init(&sem,pshared,value);

   if ((pid = fork()) < 0) {
       std::cout<<"fork error\n";
    } else if (pid > 0) {
       sem_wait(&sem);
       client.start2();
       sem_post(&sem);
    } else {
       server.start2();
       sem_post(&sem);
    }
   return 0;
}

   //stockexchangeclient.cpp
   void StockExchangeClient::start2() {
    int sharedMemoryId;
    key_t key;
    int *shm;
    key = 6000;

    if((sharedMemoryId = shmget(key,sizeof(int),0666)) < 0) {
        std::cout<<"Shared memory create error\n";
        exit(1);
    }
    else{}

    if((shm = (int *)shmat(sharedMemoryId,NULL,0)) == (int *)-1) {
        std::cout<<"Shared memory attach error\n";
        exit(1);
    }
    else{}
    *shm = 1;
    exit(0);
}

//stockexchangeserver.cpp
void StockExchangeServer::start2()
{
    int sharedMemoryId;
    key_t key;
    int *shm;
    key = 6000;

    if((sharedMemoryId = shmget(key,sizeof(int),IPC_CREAT | 0666)) < 0) {
        std::cout<<"Shared memory create error\n";
        exit(1);
    }
    else{}
    if((shm = (int *)shmat(sharedMemoryId,NULL,0)) == (int *)-1) {
        std::cout<<"Shared memory attach error\n";
        exit(1);
    }
    else{}
   *shm = 0;
    while(*shm == 0) {
        sleep(1);
    }
    std::cout<<"Shared memory succeded\n";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T14:09:34+00:00Added an answer on May 31, 2026 at 2:09 pm

    I tested this and in fact server process is running first ( StockExchangeServer::start2 ) But the problem is in

    while(*shm == 0) {
        sleep(1);
    }
    

    This is resulting in an infinite loop and you are not giving an opportunity to StockExchangeClient::start2() to change *shm as parent is waiting indefinitely at sem_wait as the child never executed sem_post

    Instead can do a sem_post before entering the loop in StockExchangeServer::start2 so that you release the parent from its sem_wait. For this you need to send &sem to StockExchangeServer::start2. May be by changing its prototype into something like StockExchangeServer::start2( sem_t *sem ).

    But as sem is an unnamed semaphore, and 2 copies exist, one in parent and one in child, and if you want both parent and child to use it, you need to create it in a shared memory region using shmget and then access it across processes. If you don’t want all this pain, you can switch to named semaphores which can be even accessed by unrelated processes. In short, unnamed semaphores are generally used for threads ( as threads share data )and named semaphores for processes.

    Also as observed by David Schwartz in comments make sure to prevent unintended optimization of the while loop. Say for example, print out the value of shm before the loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use latex to write up homework assignments. I need to include both charts
No problems here, just need explanation how does that work. I was doing homework
I understand that I need to figure out my own homework, but seeing that
This is not homework, I need this for my program :) I ask this
I have this homework problem where I need to use regex to remove every
First of all this is not homework, I'm in a desperate need for a
I need some smalltalk with GUI for my homework, and I'm running Mac OS
This is homework, but I need a nudge. I can't find how to sort
Welcome mon amie , In some homework of mine, I feel the need to
I have homework which I have to use scriptlets in , I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.