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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:58:14+00:00 2026-05-17T20:58:14+00:00

I’ve looked over this about 15 times by now to no avail. I cannot

  • 0

I’ve looked over this about 15 times by now to no avail. I cannot understand why this is seg faulting? It doesn’t even get to the “print” statement which makes no sense. the error codes actually do work tho (when I dont have a shared memory present) I have a load.c program but it works perfectly (im 100% sure of this)

#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <sys/sem.h>
#include "header.h"

//BEGIN MAIN FUNCTION
main()
{
    int id;             //ID to data shmem
    struct StudentInfo *infoptr;    //ptr to data
    int found = 0;          //found 'boolean'
    char input[15];         //user input buffer
    struct StudentInfo *beginptr;   //ptr to beginning of data
    int rcid;           //Read count ID to shmem
    int *rcptr;         //RC ptr
    int sema_set;           //ID to shared semaphores

    //Find the shmem at our ID
        id = shmget(KEY,SEGSIZE,0);
        if(id < 0)
        {
                perror("Query: shmget failed");
                exit(1);
        }

    //set the ptr to our shared mem and attach to program
        infoptr = (struct StudentInfo *)shmat(id,0,0);
        if(infoptr <= (struct StudentInfo *)(0))
        {
                perror("Query: shmat failed");
                exit(1);
        }

    //Get our RC in shared memory 
    rcid = shmget(RCKEY,READCOUNT,0);
    if(rcid < 0)
    {
        perror("Query: shmget failed");
        exit(1);
    }
    //Set ptr to shmem and attach to process
    rcptr = (int*)shmat(rcid,0,0);
    if(rcptr <= (int*)(0))
    {
        perror("Print: Shmat failed");
        exit(1);
    }

    //Get semaphores
    sema_set = semget(SEMA_KEY,NUM_SEMAPHS,0);
    if(sema_set < 0)
    {
        perror("Query: Semget failed");
        exit(1);
    }   

    //Set program to queue up to wait
    Wait(sema_set,1);

    //Increment the read counter
    *rcptr += 1;

    //If we are the first reader, stop writers
    if(*rcptr == 1)
    Wait(sema_set,0);

    //Signal readers
    Signal(sema_set,1);

    //Set our begin ptr
    beginptr = infoptr;

    //Begin user input loop
    while(1)
    {
    //Ask user for input IT DOESN"T EVEN GET TO HERE <--
    printf("Please input a student ID :");
    scanf("%s",input);

    //While the record is not found search  
    while(strcmp(infoptr->Name,"")!=0 && found != 1)
    {
        //If record found, print the record
        if((strncmp(input,infoptr->ID,9)) == 0)
        {
            //Set found
            found = 1;

            printf("\n%s\n",infoptr->Name);
                    printf("%s\n",infoptr->telNumber);
                    printf("%s\n",infoptr->Address);
                    printf("%s\n\n",infoptr->ID);
        }
        else
            infoptr++;
    }

    //If not found, print error message
    if(found == 0)
        printf("Record not found.\n");

    //Wait on readers
    Wait(sema_set,1);
    //Decrement
    *rcptr--;
    //If no readers left
    if(*rcptr == 0)
        Signal(sema_set,0); //Signal writers
    //Signal readers
    Signal(sema_set,1);
    exit(0);        
    }
}

HEADER

#define KEY  ((key_t)(11111)) /*change it to last five digits of your SSN*/
#define SEGSIZE  sizeof(struct StudentInfo)

#define NUM_SEMAPHS 2
#define SEMA_KEY   ((key_t)(1111)) /* change this to last four digits of SSN */

#define READCOUNT sizeof(int)   //Set the size of shmem for read count
#define RCKEY ((key_t)(4003))   //Set the key of the shmem for RCount

//Struct student info
struct StudentInfo{
  char Name[20];
  char ID[15];
  char Address[50];
  char telNumber[15];
};

//Checks the semaphore whether or not to wait
void Wait(int semaph, int n);
//Signals that it's ok to run
void Signal(int semaph, int n);
//Gets the semaphore information
int GetSemaphs(key_t k, int n);
  • 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-17T20:58:15+00:00Added an answer on May 17, 2026 at 8:58 pm

    Your problem might come from your use of shmat. In C, never cast the return type of such a function. That you felt the need for it probably means that you had a spurious error message that came from the fact that you are missing the “sys/shm.h” header.

    What happens in such cases is that gcc takes the return type for an int, usually a 32 bit quantity, and re-interprets it as a pointer. So the upper half of your address that shmat gives you is lost.

    As a general rule, don’t cast away problems. Cast are rarely needed in C if all your headers are properly written. Casting the return type of a system function is almost always wrong.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
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
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms

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.