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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:46:23+00:00 2026-06-12T22:46:23+00:00

I was trying to figure out shared memory and tried to write a simple

  • 0

I was trying to figure out shared memory and tried to write a simple program involving a consumer and a producer. I didnt make it to the consumer part and found this weird little problem: The parent will return at the *spool=3; with no rhyme or reason on why. Nothing on dmesg.

#include <unistd.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define PRODUCER_ERROR(msg)         \
    do {perror("Producer Error: " msg "\n"); exit(1); }while(0)

#define SHM_NAME "/my_sharedmem"

void producer();

int main(int argc, char *argv[])
{
    pid_t pID = fork();
    if (pID == 0) {
    // Code only executed by child process
    printf ("Son here\n");
    return 0;
    } else if (pID < 0) {
    perror("Unable to fork\n");
    exit(1);
    } else {
    // Code only executed by parent process
    printf ("Parent here\n");
        producer();
    return 0;
    }

    return 0;
}


void producer()
{
    int fd, d;
    unsigned* spool;

    printf("<<Producer>> started\n");
    fd = shm_open(SHM_NAME, O_CREAT | O_RDWR,  S_IRWXU | S_IRWXG | S_IRWXO ); // FIXED

    printf ("<<Producer>> memory file opened\n");

    spool = mmap(NULL, sizeof(unsigned), PROT_READ | PROT_WRITE | PROT_EXEC, MAP_SHARED, fd, 0);
    printf ("<<Producer>> mmaped to %p\n\tGonna write.\n", spool);
    perror(NULL);

    *spool = 3;
    // msync(spool, sizeof(unsigned), MS_SYNC | MS_INVALIDATE);

    printf("<<Producer>> ended\n");
}

EDIT: fixed shm_open mode argument

  • 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-12T22:46:26+00:00Added an answer on June 12, 2026 at 10:46 pm

    You have the mode argument to shm_open wrong. This is supposed to be a mode specification as for open. Probably your version here by conincidence forbids writing to the address, so the process then crashes when you try to write to it.

    BTW: you should always check the return of library calls such as shm_open and mmap.

    Edit: As I also observed in a comment below, you are also missing to scale the segment to an appropriate size with ftruncate.

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

Sidebar

Related Questions

I am trying to figure out the relationship between shared memory based concurrency algorithms
I am trying to figure out a way to find shared credits by two
Trying to figure out how I can do this properly. The print_r looks like
trying to figure out why this is happening - I have an input text
Trying to figure out why my silverlight app suddenly just displays nothing (right click
Trying to figure out how to type (via events not set the value) on
Trying to figure out a regex for validating a network path ie: \\comp\xyz or
Trying to figure out why my recipient multiselect isn't validating on form submission. Should
Trying to figure out why File Attachment isn't showing on a custom content type.
Trying to figure out the best way to set up collection lists for users

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.