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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T18:27:03+00:00 2026-05-22T18:27:03+00:00

Tried to create shared memory block, and what I got is strange behaviour. #include

  • 0

Tried to create shared memory block, and what I got is strange behaviour.

#include <sys/shm.h>
#include "stdio.h"
#include <sys/ipc.h>

int main() {
printf("starting\n");

int mid = -1;
mid = shmget((key_t)1234, 4096, IPC_CREAT|0666);
if(mid == -1) {
    printf("cant get mid\n");
    return 1;
} else {
    printf("got mid");
}

int* maddr = 0;
maddr = shmat(mid, NULL ,0);
if(maddr == (int*)-1) {
    printf("cant attach memory\n");
    return 1;
} else {
    printf("got maddr");
}

while(1) {
    int cval = __sync_add_and_fetch(maddr, 1);
    if(cval % 2) { // odd values
            printf("messager 1");
            sleep(1000);
    }
}
}

If I try to execute that code, it prints starting and hangs, nothing more happens, but some why it accepts input from stdin, so I can print just like if scanf is running

  • 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-22T18:27:03+00:00Added an answer on May 22, 2026 at 6:27 pm

    stdout is line-buffered by default, which means that it is not flushed until a newline is printed. This means that you need to put \n at the end of your "got mid", "got maddr" and "messager 1" strings, or fflush(); after those printf()s.

    By the way, SYSV shared memory is outdated. The POSIX mechanisms are significantly better designed – see shm_open() and related calls.

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

Sidebar

Related Questions

So I have tried to create a generator like: #include <iostream> #include <iomanip> #include
UPDATE 3: I created a Visual Studio 2008 test project and tried to create
I am very, very new to MYSQL.I tried to create a table named option.
I have tried to find how to create DLL-s on linux using google, but
how to create a shortcut for a exe from a batch file. i tried
I am trying to create a shared library on Windows. I am able to
I tried to create button (child window) inside WM_NCCREATE message, and its position seemed
Tried to map it from Preferences -> Settings -> Keyboard, but the key combo
Tried something like this: HttpApplication app = s as HttpApplication; //s is sender of
I tried to install Delphi 7 on Vista several times and Vista prevented me

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.