سؤال

I recently found out after updating the OSX software on our server that I am unable to run one of my programs without getting Segmentation Fault 11. It is a simple program which tries to allocate shared memory and then free it. It works fine when i compile and run my file using sudo. Any help would be much appreciated!

Source Code:

#include <stdlib.h>
#include <stdio.h>
#include <sys/shm.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <stddef.h>
#include <sys/un.h>
#include <unistd.h>

int main(){
    double *shared;
    int arrsize, shmid;
    arrsize = 10;
    shmid = shmget(IPC_PRIVATE, arrsize * sizeof(double), IPC_CREAT | 666);
    shared = (double*) shmat(shmid, NULL, 0);
    shared[2] = 3.0; //source of Segmentation Fault
    shmdt( (void*) shared );
    shmctl(shmid, IPC_RMID, NULL);
    printf("Success!");
    return 0;
}

System:

ProductName:    Mac OS X
ProductVersion: 10.9.1
BuildVersion:   13B42
هل كانت مفيدة؟

المحلول 2

Your shmget call may be failing when running as a normal user because your literal value 666 is being parsed as a decimal literal. You need a leading 0 for it to be recognized as octal as intended.

نصائح أخرى

You are not setting your permissions correctly for the shared memory. You have to use the macros in sys/stat.h to pass the right permissions. Consult man 2 chmod:

shmid = shmget(IPC_PRIVATE, arrsize * sizeof(double), S_IRUSR | S_IWUSR );

Will do the trick.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top