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 7005133
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:18:47+00:00 2026-05-27T21:18:47+00:00

I have question regarding shared memory segmentation in c using POSIX system calls. Is

  • 0

I have question regarding shared memory segmentation in c using POSIX system calls. Is it right that I am detaching and removing segment from client and server or i just need to remove from server?

Consider I have 2 programmes

One for server and one for client

the steps for the server

1)create memory segment
2)attach
3)detach
4)remove

steps for the client

1)create
2)attach
3)detach
4)remove

this is my code:

//server

#include<stdlib.h>
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/shm.h>

#define SHMSZ 100
int main()
{

key_t key;
char c;
int shmid;
char *shm;

key=1025;

//locate
if((shmid=shmget(key,SHMSZ,0666 | IPC_CREAT))<0)
{
perror("shmget");
exit(-1);
}



//attach
if((shm=shmat(shmid,NULL,0))==(char*)-1)
{
perror("shmat");
exit(-1);
}


sprintf(shm,"Hi there");

//shm="Hi There";

while(*shm!='*');
sleep(1);

//detach
shmctl(shmid,IPC_RMID,NULL);
return 0;
}

this is client side

//client

#include<stdlib.h>
#include<stdio.h>
#include<sys/ipc.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<sys/shm.h>

#define SHMSZ 100

int main()
{


key_t key;
int shmid;
char c;
char *shm, *s;

key=1025;
//locate
if((shmid=shmget(key,SHMSZ,0666 | IPC_CREAT))<0)
{
perror("shmget");
exit(-1);
}

//attach
if((shm=shmat(shmid,NULL,0))==(char*)-1)
{
perror("shmat");
exit(-1);
}

printf("%s\n",shm);

*shm='*';

shmdt(&shmid);
shmctl(shmid, IPC_RMID,NULL);

return 0;
}
  • 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-27T21:18:48+00:00Added an answer on May 27, 2026 at 9:18 pm

    Since you’re using System V IPC rather than POSIX IPC, check the value of shm_nattch in the data-structure associated with the ID of the shared memory segment. You can get this value by calling shmctl with a flag of IPC_STAT. Calling shmdt will reduce this value by one, and the last process to call this function will set the value of shm_nattach to 0. Once the value is zeroed-out, you can safely make a call to shmctl to remove the memory segment.

    So in both your client and server code if the server is not guaranteed to outlive a client, you should check the value of shm_nattch with a separate call to shmctl after calling shmdt to see if the number of processes accessing the shared memory segment has been reduced to zero. You should also make sure to error-check the results of this IPC_STAT call in order to avoid a race-condition where two separate processes call shmdt, reducing the value of shm_nattch to zero, but the process that was actually the last to call shmdt is suspended by the OS, and the other process sees the value of shm_nattch is zero and removes the memory segment. Since both checking and removing the shared memory segment requires calls to shm_ctl, and that call will fail if the ID of the shared-memory segment is invalid, you theoretically shouldn’t run into any race-conditions if you only make calls to shm_ctl or shmdt after a single process has removed the shared memory segment. The thing you want to avoid though would be accessing a pointer to the shared memory segment after it’s been removed. Checking for failed calls to shm_ctl will help you avoid those types of situations. In other words if the call fails, then you can’t safely access the pointer anymore.

    If on the other-hand your server is guaranteed to outlive any of the clients, then the server can safely make the call to remove the shared memory segment, since it will be the last process using it … all of the other clients will not need to remove the memory segment, but simply detach from it.

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

Sidebar

Related Questions

I have a class which opens a posix shared memory using shm_open . Now
I have a question regarding static function in php. let's assume that I have
I have a question regarding the integration of business objects developed using Linq To
I have a scope question regarding Linq expressions that are defined in a loop.
I have a question regarding Shared Objects (or DLLs). I am working on a
I have question regarding the SQLAlchemy. How can I add into my mapped class
I have question regarding the use of function parameters. In the past I have
I have a question regarding the two additional columns (timeCreated, timeLastUpdated) for each record
I have a question regarding an update function I created... CREATE OR REPLACE FUNCTION
I have a question regarding handling errors in a J2EE application. Our current application

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.