Hi,
My code is segment faulting on line 50 (*ptr = 10)
I can't understand why, could someone explain ?
Code:(Headers here) int main() { // Close the shared memory we use just to be safe shm_unlink("/shared_mem"); // Set up some shared memory where we can record the total amount of bytes sent by all processes int shmfd; if( (shmfd = shm_open("/shared_mem", O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR)) == -1) { fprintf(stderr, "Error opening shared memory in recordTotalBytes() \n"); exit(EXIT_FAILURE); } // Set the size of the shared memory to 1 long ftruncate(shmfd, sizeof(long) *1 ); // Map the shared memory into our address space long *ptr; ptr = mmap(NULL, sizeof(long) *1, PROT_READ | PROT_WRITE, MAP_SHARED, shmfd, 0); // Create a mutex and initalise it pthread_mutex_t * muxlock; pthread_mutex_init(&muxlock, NULL); // Lock the mutex some work is going to happen with shared resources pthread_mutex_lock(&muxlock); // Set ptr to 10 ptr* = 10; printf("%ld", *ptr); // Unlock the mutex pthread_mutex_unlock(&muxlock); }
Thanks !



LinkBack URL
About LinkBacks


