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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:32:36+00:00 2026-06-10T21:32:36+00:00

I’ve created a program that shows shared memory segment info by shmid (shared memory

  • 0

I’ve created a program that shows shared memory segment info by shmid (shared memory id) passed as an argument.

Comparing the data with those returned by the command ipcs, it is clear that my program shows some wrong information about shared memory segment.

Can you help me understand why?

Thank you.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <time.h>
#include <errno.h>

struct shmem_info {
    int id;
    struct shmid_ds data;
};

int *allocate_int(int n) {
    int *mem;

    if((mem=malloc(n*sizeof(int)))==NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    return mem;
}

struct shmem_info *allocate_shminfo(int n) {
    struct shmem_info *mem;

    if((mem=malloc(n*sizeof(struct shmem_info)))==NULL) {
        perror("malloc");
        exit(EXIT_FAILURE);
    }

    return mem;
}

int parse_int(char *str) {
    int n;

    n=atoi(str);

    return n;
}

int open_fileRW(char *path) {
    int fd;

    if((fd=open(path,O_RDWR|O_CREAT,0666))==-1) {
        perror("open");
        exit(EXIT_FAILURE);
    }

    return fd;
}

void close_file(int fd) {

    if(close(fd)==-1) {
        perror("close");
        exit(EXIT_FAILURE);
    }
}

struct shmid_ds get_shminfo(int id) {
    struct shmid_ds data;

    errno=0;

    if(shmctl(id,IPC_STAT,&data)==-1) {
        switch(errno) {
            case EACCES:
                fprintf(stderr,"Shmid %d: read acces not allowed\n", id);
                exit(EXIT_FAILURE);

            case EINVAL:
                fprintf(stderr,"Shmid %d: invalid shmid\n", id);
                exit(EXIT_FAILURE);

            default:
                perror("shmctl");
                exit(EXIT_FAILURE);
                break;
        }
    }

    return data;
}

void write_shminfo(int fd, struct shmem_info shmi) {
    ssize_t w, tot=0, size=sizeof(struct shmem_info);

    for(;;) {
        errno=0;

        w=write(fd,&shmi,size-tot);

        if(w==-1 && errno!=EINTR) {
            perror("write");
            exit(EXIT_FAILURE);
        }

        if(w==0) return;

        tot+=w;
    }
}

struct shmem_info read_shminfo(int fd) {
    ssize_t r, tot=0, size=sizeof(struct shmem_info);
    struct shmem_info shmi;

    for(;;) {
        errno=0;

        r=read(fd,&shmi,size-tot);

        if(r==-1 && errno!=EINTR) {
            perror("write");
            exit(EXIT_FAILURE);
        }

        if(r==0) return shmi;

        tot+=r;
    }
}

void print_shminfo(FILE *out, struct shmem_info shmi) {
    fprintf(out,"Shmid: %d\n", shmi.id);
    fprintf(out,"Size of segment (bytes): %d\n", (int)shmi.data.shm_segsz);
    fprintf(out,"PID of creator: %d\n", (int)shmi.data.shm_cpid);
    fprintf(out,"No. of current attaches: %d\n", (int)shmi.data.shm_nattch);
    fprintf(out,"Last attach time: %ld\n", shmi.data.shm_atime);
    fprintf(out,"Last detach time: %ld\n", shmi.data.shm_dtime);
    fprintf(out,"Last change time: %ld\n", shmi.data.shm_ctime);
    fprintf(out,"PID of last shmat/shmdt: %d\n", (int)shmi.data.shm_lpid);
    fprintf(out,"Key supplied to shmget(2): %d\n", (int)shmi.data.shm_perm.__key);
    fprintf(out,"Effective UID of owner: %d\n", (int)shmi.data.shm_perm.uid);
    fprintf(out,"Effective GID of owner: %d\n", (int)shmi.data.shm_perm.gid);
    fprintf(out,"Effective UID of creator: %d\n", (int)shmi.data.shm_perm.cuid);
    fprintf(out,"Effective GID of creator: %d\n", (int)shmi.data.shm_perm.cgid);
    fprintf(out,"Permissions + SHM_DEST and ESHM_LOCKED flag: %hd\n", shmi.data.shm_perm.mode);
    fprintf(out,"Sequence number: %hd\n", shmi.data.shm_perm.__seq);
    fprintf(out,"\n");
}

int main(int argc, char *argv[]) {
    int fd, n, i;
    struct shmem_info *shmi;

    if(argc<2) {
        fprintf(stderr,"Usage: %s <shmid> <shmid> ... <shmid>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    n=argc-1;

    argv=argv+1;

    shmi=allocate_shminfo(n);

    for(i=0;i<n;i++) {
        shmi[i].id=parse_int(argv[i]);
    }

    for(i=0;i<n;i++) {
        shmi[i].data=get_shminfo(shmi[i].id);
    }

    fd=open_fileRW("shmid_info-log.txt");

    for(i=0;i<n;i++) {
        write_shminfo(fd,shmi[i]);
    }

    for(i=0;i<n;i++) {
        shmi[i]=read_shminfo(fd);
    }

    for(i=0;i<n;i++) {
        print_shminfo(stdout,shmi[i]);
    }

    close_file(fd);

    free(shmi);

    exit(EXIT_SUCCESS);
}
  • 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-10T21:32:38+00:00Added an answer on June 10, 2026 at 9:32 pm

    It is your read_shminfo function which has a problem. It works on a local shmi, instead of dealing with the shmi allocated with shmi=allocate_shminfo(n);`

    One way to fix:

    void read_shminfo(int fd, struct shmem_info *shmi) {
    //    struct shmem_info shmi;
        ssize_t r, tot=0, size=sizeof(struct shmem_info);
    
        for(;;) {
            errno=0;
    
            r=read(fd,(void *)&shmi,size-tot);
    
            if(r==-1 && errno!=EINTR) {
                perror("write");
                exit(EXIT_FAILURE);
            }
    
            if(r==0)
                    return ;
    
            tot+=r;
        }
    }
    

    and in main:

    for(i=0;i<n;i++) {
        read_shminfo(fd,&shmi[i]);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I know there's a lot of other questions out there that deal with this
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I

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.