GamerGirl
GamerGirl

Reputation: 25

Checking if a shared memory exists and if it does access it

I have another program1 where i create a shared memory and i have a program2 that i want to check if the shared memory has been created, if not then print an error and dont do nothing and if it has been created already then acess it. If the shared memory has been created before then it works fine and i can acess it but the problem im having is that if the shared memory has not been created then instead of doing nothing and printing an error the program2 creates the shared memory and i didn't want that to happen. Any help would be appreciated and please ask any information that might be usefull, thanks!

void main(){

int sm_id;
sm_id = shmget(9000, MAX_USERS * sizeof(User), IPC_CREAT | IPC_EXCL | 0666);
if(sm_id>0){
    perror("Shared memory has not been created yet");
    exit(1);
}else{
    sm_id = shmget(9000, MAX_USERS * sizeof(User), IPC_CREAT | 0666);
    list = (User *)shmat(sm_id,0,0);
    exit_on_null(list, "Exit on null(error attach)");
}

}

Upvotes: 1

Views: 8986

Answers (1)

µtex
µtex

Reputation: 908

Do not use IPC_CREAT flag. It will return error ENOENT if that shared memory does not exist.

Check the man page for further information..

SHMGET

Upvotes: 5

Related Questions