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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:18:20+00:00 2026-05-18T07:18:20+00:00

For a uni assignment I need to create a circular list of up to

  • 0

For a uni assignment I need to create a circular list of up to 10 file names, and then store these in a shared memory area, so that 2 child processes can read/write to the list (using a semaphore to control access). Trouble is, that I am a total C novice and I feel loss and despair because its totally out of my depth. I need some help in “filling in the holes” of my knowledge.

Right now, I am simply focussing on it one problem at a time, and presently, I am only trying to get my circular list into the shared memory area.

So far I have:

typedef struct FILE
{
   struct FILE *f_link;  /* forward link for linked list */
   char name[255];       /* name of the file */

} FILE_entry;

As my struct which will hold the reference to the file that comes next (f_link). this way I can just call ->f_link to get the next item in the list, and where the 10th element will simply have its f_link directed back to the 1st. My reason for doing this is so that I can simply traverse the list without an iterator (and never have to check for the end of the list as I would with an array).

I also know that I need to use shmget to get the memory area, and I understand it, I pass shmget a key, a size and a flag (which i dont get) and it returns an identifier in the type of an int.

So my question is 2 fold. How do I store my linked list INTO the shared memory area – and how do I access it FROM the shared memory area?

  • 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-18T07:18:20+00:00Added an answer on May 18, 2026 at 7:18 am

    shmget just reserves some amount of shared memory – like creating a fixed-size file on disk. The flags are a permission mask (like the mode parameter for open) in the low 9 bits plus some extra flags, IPC_CREAT and IPC_EXCL, corresponding to O_CREAT and O_EXCL for open. To actually access that memory, you need to map it into the address space of your process (“attach” it – analogous to mmap for files). This is done using shmat (which returns a pointer). You then need to allocate your FILE structures from that pointer. The whole process looks something like this:

    int id;
    FILE_entry *entries;
    
    id = shmget(key, N * sizeof(FILE_entry), IPC_CREAT | 0644);
    entries = (FILE_entry *) shmat(id, NULL, 0);
    
    // you can now access entries as if it was a N-element array.
    // to turn it into a circular list, link the entries appropriately.
    

    After mapping, you can work with it like regular memory – since it is regular memory. That’s the whole point!

    EDIT: One important caveat I forgot to mention. Putting a linked list into a shared memory segment like this will only work if all involved processes map it to the same address! So you either need to do that (using the second argument of shmat) or switch from pointers to offsets relative to the base address of the shared memory range. That means turning your next field from a pointer to a ptrdiff_t and adding the base address of the mapped memory range whenever you load it (and subtracting it whenever you store it).

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

Sidebar

Related Questions

I'd like to create a function that automatically generates uni and multivariate regression analyses,
I have a wierd problem that i need to work out how to resolve,
I'm about to create WPF application. So far at uni the only way we
Hey guys, I'm currently trying to implement a function using C that takes in
I am in the process of choosing project for uni. And I am really
I took my first 'fundamentals of programming' lab session at uni today. One thing
Have to say I wish I could implement server-side persistence (i.e., session-based and/or database-based
How can I clone a git repo from my laptop (at home) to a
Good Afternoon Gurus, I am pretty familiar with basic socket programming, and the IO::Socket
I've researched online and found several interesting Perl modules/frameworks, such as HTML:Mason, HTML::Embperl, or

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.