Skip to content

Commit

Permalink
add shared memory demo code
Browse files Browse the repository at this point in the history
  • Loading branch information
jrtx0 committed Oct 19, 2023
1 parent 1529af0 commit 29db502
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# c-showcase
c code showcase

## shared memory
shared memory write and read demo.
Binary file added shared_memory/read
Binary file not shown.
88 changes: 88 additions & 0 deletions shared_memory/read.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define BUFSZ 512

int main(int argc, char *argv[])
{
int shmid;
int ret;
key_t key;
char *shmadd;

// create key value
key = ftok("../", 2015);
if (key == -1)
{
perror("ftok");
}

system("ipcs -m"); // check shared memory

/*
#include <sys/ipc.h>
#include <sys/shm.h>
int shmget(key_t key, size_t size, int shmflg); // create or open shared memory
*/
// open shared memory
shmid = shmget(key, BUFSZ, IPC_CREAT|0666);
if (shmid < 0)
{
perror("shmget");
exit(-1);
}

/*
#include <sys/types.h>
#include <sys/shm.h>
void *shmat(int shmid, const void *shmaddr, int shmflg); // map shared memory with process
*/
// mapping
shmadd = shmat(shmid, NULL, 0);
if (shmadd < 0)
{
perror("shmat");
exit(-1);
}

// read shard memory data
printf("data = [%s]\n", shmadd);

/*
#include <sys/types.h>
#include <sys/shm.h>
int shmdt(const void *shamaddr); detach shared memory with process
*/
// detach shared memory and current process
ret = shmdt(shmadd);
if (ret < 0)
{
perror("shmdt");
exit(1);
}
else
{
printf("deleted shared-memory.\n");
}

/*
#include <sys/ipc.h>
#include <sys/shm.h>
int shmctl(int shmid, int cmd, struct shmid_ds *buf); // control shared meory
*/
// delete shared memory
shmctl(shmid, IPC_RMID, NULL);

return 0;
}

/* compile command: gcc read.c -o read */
Binary file added shared_memory/write
Binary file not shown.
61 changes: 61 additions & 0 deletions shared_memory/write.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>

#define BUFSZ 512

int main(int argc, char *argv[])
{
int shmid;
int ret;
key_t key;
char *shmadd;

// create key value
key = ftok("../", 2015);
if (key == -1)
{
perror("ftok");
}

/*
#include <sys/ipc.h>
#include <sys/shm.h>
int shmget(key_t key, size_t size, int shmflg); // create or open shared memory
*/
// create shared memory
shmid = shmget(key, BUFSZ, IPC_CREAT|0666);
if (shmid < 0)
{
perror("shmget");
exit(-1);
}

/*
#include <sys/types.h>
#include <sys/shm.h>
void *shmat(int shmid, const void *shmaddr, int shmflg); // map shared memory with process
*/
// mapping
shmadd = shmat(shmid, NULL, 0);
if (shmadd < 0)
{
perror("shmat");
_exit(-1);
}

// copy data to shared memory
printf("copy data to shared memory.\n");
bzero(shmadd, BUFSZ); // clear shared memory
strcpy(shmadd, "how are you?");

return 0;
}

/* compile command: gcc write.c -o write */

0 comments on commit 29db502

Please sign in to comment.