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

  • Home
  • SEARCH
  • 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 174779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:35:01+00:00 2026-05-11T13:35:01+00:00

I am getting a segmentation fault while running this code. I can’t work out

  • 0

I am getting a segmentation fault while running this code. I can’t work out why this is happening – can anyone see a possible reason? (I have already got and initialized the semaphore’s shared memory.)

My code:

   #include<stdlib.h>    #include<sys/types.h>    #include<sys/shm.h>    #include<sys/ipc.h>    #include<stdio.h>    #include<sys/sem.h>     union semun     {    int val;    struct semid_ds *buf;    unsigned short *array;    } arg;      int main()   {   key_t semkey;   int shmid,semid,nsem,sops;   struct sembuf buf[1];    char *ptrr,*shm,c,*s;    semkey=ftok("/home/mawia/abc.c",'a');    printf("entered into main of producer\n");   if(semkey<0)   {    perror("ftok");    exit(1);   }    shmid=shmget(semkey,30,0777);    if(shmid<0)   {    printf("error");    perror("shmget");    exit(1);   }    shm=shmat(shmid,0,0);   if(shm==(char *) -1)   {   perror("shm");   exit(1);   }     s=shm;   semid=semget(semkey,1,0777);  if(semid<0)  {   printf("error");   perror("semget");   exit(0);  }   ptrr=shm+1;  *s='w';  printf("going to check the value 0th semaphores\n");  buf[0].sem_num=0;  buf[0].sem_op=0;   buf[0].sem_flg=0;  buf[1].sem_num=0;  buf[1].sem_op=1;  buf[1].sem_flg=0;   printf("entered the critical region\n");  //printf("waiting to enter the buffer zone...");  semop(semid,buf,2);   printf("entered the critical region\v");  if(*s!='r')  {   printf("\nPRODUCER IS PRODUCING\n\n\n");    printf("ENTER DATA\n");    while((c=getchar())!='\n')   {     *ptrr++=c;   }   *ptrr='\0';   *s='r';  }     else    printf("RESOURCE IS FULL:CAN'T PRODUCE");   //printf("produced enough for  the consumer \nexiting from the buffer area now...");  buf[0].sem_num=0;  buf[0].sem_op=-1;  buf[0].sem_flg=0;   semop(semid,buf,1);   ptrr=shm+1;    if(!strcmp(ptrr,"exit"))   {   printf("exiting...");   exit(0);   }   sleep(1);   return 0;   } 
  • 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. 2026-05-11T13:35:01+00:00Added an answer on May 11, 2026 at 1:35 pm

    After a quick glance (very quick), i would say that it MAY be caused by

    struct sembuf buf[1];  /*some other code*/  buf[1].sem_num=0; buf[1].sem_op=1; buf[1].sem_flg=0; 

    You are accessing memory outside of the buffer. buf[1] reserves memory in the stack for only one struct sembuf, you are trying to use 2. In that case, you should use

     struct sembuf buf[2] 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 89k
  • Answers 89k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I'd use std::vector<unsigned char>. Most operations you need can be… May 11, 2026 at 5:58 pm
  • Editorial Team
    Editorial Team added an answer I would say go with $:.unshift File.dirname(__FILE__) over the other… May 11, 2026 at 5:58 pm
  • Editorial Team
    Editorial Team added an answer I'm not aware of a safe way to abort a… May 11, 2026 at 5:58 pm

Related Questions

I am getting a segmentation fault while running this code. I can't work out
I have trouble getting boost:thread to work. It runs fine when compiling without optimization:
I am getting a NoClassDefFoundError when I run my Java application. What is typically
I am getting a MouseEvent in Java. the getPoint() method is adding the height
I am getting a very non specific error when trying to connect to SQL

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.