Writing to a shared memory segment causes segmentation fault.

Solution Verified - Updated -

Issue

  • Writing to a shared memory segment causes segmentation fault.
  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <sys/ipc.h>
  4 #include <sys/msg.h>
  5 #include <sys/mman.h>
  6 #include <sys/stat.h>
  7 #include <fcntl.h>
  8 #include <string.h>
  9 #include <stddef.h>
 10 #include <errno.h> 
 11 
 12 #define PROBEFTOK1 "/var/www/html/specialemptyftokfile.php"  
 13 #define PROBEFTOK2 53  
 14 
 15 int main() {
 16         key_t key;
 17         int msqid;
 18         void *shmptr;
 19         int i;  
 20         
 21         key = ftok(PROBEFTOK1,PROBEFTOK2);
 22         msqid = shmget(key,200,(IPC_CREAT | 438));
 23         printf("msqid %d\n",msqid);  
 24         shmptr = shmat(msqid,NULL,0);
 25         printf("shmptr: %p\n",shmptr);
 26         strcpy(shmptr,"1234");  
 27 }       

Environment

  • Red Hat Enterrpise Linux

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.