Last active
December 18, 2018 09:11
-
-
Save tensaix2j/47ce2de6b00e35fc3b85cb016fc52f63 to your computer and use it in GitHub Desktop.
How to read/write from/to shared memory
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/ipc.h> | |
#include <sys/shm.h> | |
#define SHKEY 0x11213007 | |
int main() { | |
int shm_id = shmget( SHKEY , 32, 0666 ); | |
if ( shm_id != -1 ) { | |
char *str = (char *) shmat( shm_id, NULL, 0 ); | |
printf("shared mem = |%s|\n", str ); | |
} else { | |
printf("unable to get shmid , key is 0x%x\n", SHKEY); | |
} | |
return 0; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <sys/ipc.h> | |
#include <sys/shm.h> | |
#include <string.h> | |
#define SHKEY 0x11213007 | |
int main( int argc , char *argv[] ) { | |
char *str; | |
int shm_id = shmget( SHKEY , 32, IPC_CREAT | 0666 ); | |
if ( shm_id != -1 ) { | |
printf( " shm_id ok %d\n ", shm_id ); | |
str = (char *) shmat( shm_id, NULL, 0 ); | |
if ( str == (char *)-1 ) { | |
printf("shmat error!\n"); | |
} else { | |
if ( argc > 1 ) { | |
strcpy( str, argv[1] ); | |
printf( "%s has been put in shared mem\n", argv[1]); | |
} else { | |
printf("argv not supplied\n"); | |
} | |
} | |
} else { | |
printf("unable to get shmid , key is 0x%x\n", SHKEY); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment