I want to write in a file called "sharedmem" which is in the same directory that the code, but i get segmentation fault. I think I have found the line of the error, but i dont know how to solve it.
This is the code:
This is the code:
Code:
#include <stdio.h> #include <stdlib.h> #include <sys/shm.h> #include <errno.h> long int *seg_part; void abandon(char message[]){ perror(message); exit(EXIT_FAILURE); } int main(int argc, char **argv){ key_t cle; int id, reponse; long int *seg_part; if(cle = ftok("sharedmem", 'A') == -1) abandon("ftok"); if(id = shmget( cle, sizeof(long int), IPC_CREAT | IPC_EXCL | 0666) == -1) if(errno == EEXIST) abandon("Le segment exist deja"); else abandon("shmget"); if((seg_part = (long int *) shmat(id, NULL, SHM_R | SHM_W)) == NULL) abandon("shmat"); *seg_part = 0; if(shmdt((char*) seg_part) == -1) abandon("shmdt"); if(shmctl(id, IPC_RMID, NULL) == -1) abandon("shmctl (remove)"); return EXIT_SUCCESS; }
Comment