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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:10:05+00:00 2026-05-30T04:10:05+00:00

Please help check the following code: running it on linux encountered segmentation fault. #deinf

  • 0

Please help check the following code: running it on linux encountered segmentation fault.

#deinf QID 2012

typedef struct {
   pid_t       pid;
   pthread_t   tid;
   char        msg;
} MSG;

typedef struct {
   int    q_head;
   int    q_rear;
   MSG    msgbuf[16*1024];
} QUE;

int attach_que(int que_name, int *shmid, void *shm_ptr)
{

   *shmid = shmget((key_t)que_name, sizeof(QUE), 0666 | IPC_CREAT);
   if (*shmid == -1)
   {
      printf("%d:%d failed to get shared memory.\n", getpid(),que_name);
      return -1;
   }

   printf("%d:is attaching to share memory %d.\n", getpid(), *shmid);

   shm_ptr = shmat(*shmid, (void *)0, 0);
   if (shm_ptr == (void *)-1)
   {
      printf("%d:%d failed to attch to shmget.\n", getpid(),que_name);
      return -1;
   }

   printf("%d: attched to share memory %d.\n", getpid(),*shmid);
   return -1;

}

int main()
{

   void        *shm_ptr = NULL;
   TDM_QUE     *tdm_que_ptr;
   DMINT       shmid;
   pid_t       pid;

   pid = getpid();

   printf("L-SIMCO %d:is starting.\n", pid);

   attach_tdm_que(Q_LSIMCO, &shmid, shm_ptr);
   printf("LSMICO %d:shared memory 0x%x as Q_%d.\n", pid, shm_ptr, Q_LSIMCO);
   tdm_que_ptr = (TDM_QUE *)shm_ptr;
   tdm_que_ptr->q_head = 0;
   tdm_que_ptr->q_rear = 0;

   /* if this is not first time when creating shared memory, do not init data */
   if (tdm_que_ptr->is_creat != 777777)
   {
      printf("%d: Init shared memory 0x%x by LSIMCO %d.\n", pid, shm_ptr, Q_LSIMCO);
      /* init data for shared memory */

      tdm_que_ptr->is_creat = 777777;
   }

   printf("%d is reading shared memory 0x%x.\n",pid,shm_ptr);
   ...
}
=========================
Running result is following:
-bash-3.2$ ./lsimco
 6341:is starting.
 QUE is size of 4620
6341:is attaching to share memory 0.
6341: attched to share memory 0.
6341:shared memory 0x0 as Q_2012.
Segmentation fault

thanks.

  • 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-30T04:10:06+00:00Added an answer on May 30, 2026 at 4:10 am

    You must use **shm_ptr in attach_que invocation in order to return a valid pointer. So, your call must me:

    attach_tdm_que(Q_LSIMCO, &shmid, &shm_ptr);
    

    and, of course, inside attach_tdm_que, use pointer asignation properly:

    *shm_ptr = shmat(*shmid, (void *)0, 0);
    if (*shm_ptr == (void *)-1)
    {
        printf("%d:%d failed to attch to shmget.\n", getpid(),que_name);
        return -1;
    }
    

    More things: you are returning allways -1from attach_tdm_que.
    Besides, allways post a compilable code.

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

Sidebar

Related Questions

I am using the following code to check the application is first time running
please help i have the following php code for my login session but i
Please help me with a sanity check. Assuming a many-to-many relationship: Post, PostTagAssoc, Tag
How to check visited link using jquery without using any plugin please help to
Please help! I couldn't figure it out how to map the following situation: I
I have written the following code to check if a tree is a Binary
Please check the code below: objDDLTable = HttpContext.Current.Cache[TestSet] as Hashtable; if (objDDLTable == null)
I am using the following code to check if the value in a textbox
I have created customize UIView with following code .....it's running with no issue....but when
I use following code to check if a user input is lowercase or not.

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.