-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
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
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 not shown.
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
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 not shown.
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
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 */ |