Last active
January 27, 2023 05:48
-
-
Save mtak/4943802 to your computer and use it in GitHub Desktop.
Shared memory in Linux using C
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 <sys/stat.h> | |
int main () | |
{ | |
int segment_id; | |
char bogus; | |
char* shared_memory; | |
struct shmid_ds shmbuffer; | |
int segment_size; | |
const int shared_segment_size = 0x6400; | |
/* Allocate a shared memory segment. */ | |
segment_id = shmget (IPC_PRIVATE, shared_segment_size, IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); | |
/* Attach the shared memory segment. */ | |
printf("Shared memory segment ID is %d\n", segment_id); | |
shared_memory = (char*) shmat (segment_id, 0, 0); | |
printf ("shared memory attached at address %p\n", shared_memory); | |
/* Determine the segment's size. */ | |
/* | |
shmctl (segment_id, IPC_STAT, &shmbuffer); | |
segment_size = shmbuffer.shm_segsz; | |
printf ("segment size: %d\n", segment_size); | |
*/ | |
/* Write a string to the shared memory segment. */ | |
sprintf (shared_memory, "Hello, world."); | |
/* Detach the shared memory segment. */ | |
shmdt (shared_memory); | |
printf("Wrote Hello World to the segment\n"); | |
} |
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 <sys/stat.h> | |
int main () | |
{ | |
int segment_id; | |
printf("Enter the shared memory id: "); | |
scanf("%d", &segment_id); | |
/* Deallocate the segment. */ | |
shmctl (segment_id, IPC_RMID, 0); | |
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 <sys/stat.h> | |
int main () | |
{ | |
int segment_id; | |
char bogus; | |
char* shared_memory; | |
struct shmid_ds shmbuffer; | |
int segment_size; | |
const int shared_segment_size = 0x6400; | |
printf("Enter the shared memory id: "); | |
scanf("%d", &segment_id); | |
/* Reattach the shared memory segment, at a different address. */ | |
shared_memory = (char*) shmat (segment_id, (void*) 0x5000000, 0); | |
printf ("shared memory reattached at address %p\n", shared_memory); | |
/* Print out the string from shared memory. */ | |
printf ("The contents of the shared memory is:\n%s\n", shared_memory); | |
/* Detach the shared memory segment. */ | |
shmdt (shared_memory); | |
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 <sys/stat.h> | |
#include <time.h> | |
int main () | |
{ | |
int segment_id; | |
char bogus; | |
char* shared_memory; | |
struct shmid_ds shmbuffer; | |
int segment_size; | |
const int shared_segment_size = 0x6400; | |
time_t t; | |
time(&t); | |
printf("Enter the shared memory id: "); | |
scanf("%d", &segment_id); | |
/* Reattach the shared memory segment, at a different address. */ | |
shared_memory = (char*) shmat (segment_id, (void*) 0x5000000, 0); | |
printf ("shared memory reattached at address %p\n", shared_memory); | |
/* Add to the segment */ | |
sprintf (shared_memory, "Modified the segment at %s", ctime(&t)); | |
/* Print out the string from shared memory. */ | |
printf ("The contents of the shared memory is:\n%s", shared_memory); | |
printf ("#################################################\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good work!