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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:50:46+00:00 2026-06-13T20:50:46+00:00

All headers are included as per requirement. This piece of code below is working

  • 0

All headers are included as per requirement. This piece of code below is working but the problem is with shmat(seg_id,NULL,0).NULL in the 2nd argument means the Operating system will take care of the location on Users’ behalf . But we can give our own memory location but how ? I don’t know , Please help . (OS - Ubuntu 11.04 ,Compiler gcc 4.5.2)
I tried man shmat but didn’t understand it completely

typedef struct {
         int id;
} emp;

int main() {
         emp *e;
         int seg_id;
         const int size=sizeof(emp);
         seg_id=shmget( IPC_PRIVATE, size, S_IRUSR | S_IWUSR); 
         pid_t pid=fork();
         if(pid<0) {
                 printf("Error");
                 return -1;
         }
         else if(pid == 0) {
                e=(emp *)shmat(seg_id,NULL,0);     
                 e->id=123;
                 shmdt(e);                       
         }
         else {
                 wait(NULL);
                 e=(emp *)shmat(seg_id,NULL,0);  
                 printf("\nEmp Id : %d\n\n",e->id);
                 shmdt(e);                          
                 shmctl(seg_id, IPC_RMID, NULL);  
         }
         return 0;
}

I tried this also to get our own address for shmget() for 4K page alignment

emp *e;
void **p;
posix_memalign(&p,4096,4096); // alignment as of pagesize 4K 
e=p; // get the first address of the aligned memory 
free(p); // free the allocated memory

then used this as shmat(seg_id,e,0); // thinking that e will be the address that I want.
But it’s giving segmentation fault

Or, Is there a problem with the 3rd argument also ?
Any help or suggestion will be greatly appreciated

  • 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-06-13T20:50:47+00:00Added an answer on June 13, 2026 at 8:50 pm

    I’m not sure what you’re trying to do here with posix_memalign(). You are calling that function to allocate a block of memory from the heap and then trying to use the same address that was returned by posix_memalign() as the location to map the shared memory segment. That address is obviously not available since it’s part of the heap!

    I see that you free the block returned by posix_memalign() before calling shmat(), but freeing a block of memory from the heap doesn’t (in general) cause the heap to shrink, so the address is in fact still part of the heap.

    If you must choose your own address for shmat(), you should choose an address that is far from anything else in your address space, to avoid conflicting with your heap or any other mapping, and to avoid having the heap run up against your mapping as it grows during the life of your process. Determining such an address is inherently unportable.

    The only reason I can think of for wanting to choose a fixed address for your mapping is to make sure it’s mapped as the same address in several different processes (so that internal pointers can work). If that’s what you want, you can let the OS choose the location in the first process and call shmat() with that same address in the second and all other processes. Even that may not work though, because other processes may happen to already have something mapped at the same address.

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

Sidebar

Related Questions

All i need help in downloading a file through headers its working fine but
Can anyone please tell why this error? I have included everything ( all header,
I want to display JQGrid with all column headers but without any data rows
I have all the headers and the top navigation included in the page with
I have a header file, lets say Common.h, that is included in all of
main.c (with all the headers like stdio, stdlib, etc): int main() { int input;
I would like to store all paths to headers in separate file. I'm going
I just want to intercept raw data (hex)--headers and all--sent to a port, and
I've been playing with PHP QR Code all day and I can’t seem to
I had a problem with a part of my code, which after some iterations

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.