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 ("client: object is already open");
- } else {
- printf("Object is open: name = %s, id = 0x%x\n", shar_object, shmid);
- }
- msg_p = mmap(0, sizeof (message_t), PROT_WRITE|PROT_READ, MAP_SHARED, shmid, 0);
- if (msg_p == (message_t*)-1 ) {
- }
- signal(SIGINT, intHandler);
- while (1) {
- if(keepRunning && msg_p->type != MSG_TYPE_FINISH) {
- msg_p->type = MSG_TYPE_STRING;
- msg_p->n = rand() % 1000;
- sleep(1);
- } else {
- msg_p->type = MSG_TYPE_FINISH;
- }
- if(msg_p->type == MSG_TYPE_FINISH) {
- break;
- }
- }
- close(shmid);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement