commit d4c4993c7f544f33f0af2cc8b09b7fe02f7bb843 Author: leonnicolas Date: Thu Jun 4 14:40:20 2020 +0200 initial commit diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6a2c2ea --- /dev/null +++ b/Makefile @@ -0,0 +1,2 @@ +main: main.c + gcc -pedantic -Wall -o main main.c -lrt -pthread diff --git a/README.md b/README.md new file mode 100644 index 0000000..87e8f08 --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# shared memory +Simple example of inter process communication with shm_open(). + +The main process forks itself and then waits for the child to finish. The child writes into a shared memory and exits. After that the main process prints what the child wrote into the shared memory. + +## Note +The file noerror.c does the same as main.c, but no error handling. +## compile +```bash +make +``` +## run +```bash +./main +``` diff --git a/main.c b/main.c new file mode 100644 index 0000000..38b4eac --- /dev/null +++ b/main.c @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SHARED_MEM "/SHARED_MEM" +#define NUM_CALLS 5 + +struct shared_str { + sem_t sem; + size_t len; + char buf[256]; +}; + +void errExit(char* msg){ + perror(msg); + exit(EXIT_FAILURE); +} + +int main(){ + int fd = shm_open(SHARED_MEM, O_CREAT | O_RDWR, S_IRUSR); + if (fd < 0){ + errExit("failed to open shared memory object"); + } + if (ftruncate(fd, sizeof(struct shared_str))){ + errExit("failed to truncate"); + } + struct shared_str *sstr = mmap(NULL, sizeof(struct shared_str), + PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); + if (sstr == MAP_FAILED) + errExit("mmap"); + if (sem_init(&sstr->sem, 1, 0) == -1) + errExit("sem_init-sem1"); + pid_t pid; + if ((pid = fork()) < 0) { + errExit("failed to fork"); + } + if ( pid == 0 ){ + printf("child is running...\n"); + for (int i =0 ; i len = snprintf((char*)&sstr->buf,256,"hallo %d",i); + if (sem_post(&sstr->sem) == -1) + errExit("sem_post"); + } + close(fd); + exit(EXIT_SUCCESS); + } + for (int i =0 ; isem) == -1) + errExit("sem_wait"); + pread(fd,sstr, sizeof(struct shared_str),0); + printf("child: %s\n",sstr -> buf); + } + int status; + pid = wait(&status); + printf("child process %d terminated with status %d\n", pid, WEXITSTATUS(status)); + shm_unlink(SHARED_MEM); + exit(EXIT_SUCCESS); +} diff --git a/noerror.c b/noerror.c new file mode 100644 index 0000000..ca67cd3 --- /dev/null +++ b/noerror.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SHARED_MEM "/SHARED_MEM" +#define NUM_CALLS 5 + +struct shared_str { + sem_t sem; + size_t len; + char buf[256]; +}; + +void errExit(char* msg){ + perror(msg); + exit(EXIT_FAILURE); +} + +int main(){ + int fd = shm_open(SHARED_MEM, O_CREAT | O_RDWR, S_IRUSR); + ftruncate(fd, sizeof(struct shared_str)); + struct shared_str *sstr = mmap(NULL, sizeof(struct shared_str), + PROT_READ | PROT_WRITE, + MAP_SHARED, fd, 0); + sem_init(&sstr->sem, 1, 0); + pid_t pid = fork(); + if ( pid == 0 ){ + printf("child is running...\n"); + for (int i =0 ; i len = snprintf((char*)&sstr->buf,256,"hallo %d",i); + sem_post(&sstr->sem); + } + close(fd); + exit(EXIT_SUCCESS); + } + for (int i =0 ; isem); + pread(fd,sstr, sizeof(struct shared_str),0); + printf("child: %s\n",sstr -> buf); + } + int status; + pid = wait(&status); + printf("child process %d terminated with status %d\n", pid, WEXITSTATUS(status)); + shm_unlink(SHARED_MEM); + exit(EXIT_SUCCESS); +}