Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/mman.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include "message.h"
- const char* shar_object = "posix-shar-object";
- void sys_err (char *msg) {
- puts (msg);
- exit (1);
- }
- int keepRunning = 1;
- void intHandler(int dummy) {
- keepRunning = 0;
- }
- int main () {
- int shmid;
- message_t *msg_p;
- if ( (shmid = shm_open(shar_object, O_CREAT|O_RDWR, 0666)) == -1 ) {
- perror("shm_open");
- sys_err ("server: object is already open");
- } else {
- printf("Object is open: name = %s, id = 0x%x\n", shar_object, shmid);
- }
- if (ftruncate(shmid, sizeof (message_t)) == -1) {
- perror("ftruncate");
- sys_err ("server: memory sizing error");
- return 1;
- } else {
- printf("Memory size set and = %lu\n", sizeof (message_t));
- }
- msg_p = mmap(0, sizeof (message_t), PROT_WRITE|PROT_READ, MAP_SHARED, shmid, 0);
- if (msg_p == (message_t*)-1 ) {
- }
- signal(SIGINT, intHandler);
- msg_p->type = MSG_TYPE_EMPTY;
- while (1) {
- if (keepRunning) {
- if (msg_p->type != MSG_TYPE_EMPTY) {
- if (msg_p->type == MSG_TYPE_STRING) {
- printf ("%d\n", msg_p->n);
- } else if (msg_p->type == MSG_TYPE_FINISH) {
- break;
- }
- msg_p->type = MSG_TYPE_EMPTY;
- }
- } else {
- msg_p->type = MSG_TYPE_FINISH;
- }
- }
- if(shm_unlink(shar_object) == -1) {
- perror("shm_unlink");
- sys_err ("server: error getting pointer to shared memory");
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement