Advertisement
_takumi

client.c

Mar 4th, 2023
771
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/mman.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7. #include <sys/types.h>
  8.  
  9. #include "message.h"
  10.  
  11. const char* shar_object = "posix-shar-object";
  12.  
  13. void sys_err (char *msg) {
  14.   puts (msg);
  15.   exit (1);
  16. }
  17.  
  18. int keepRunning = 1;
  19. void intHandler(int dummy) {
  20.     keepRunning = 0;
  21. }
  22.  
  23. int main () {
  24.   int shmid;
  25.   message_t *msg_p;
  26.  
  27.   if ( (shmid = shm_open(shar_object, O_CREAT|O_RDWR, 0666)) == -1 ) {
  28.     perror("shm_open");
  29.     sys_err ("client: object is already open");
  30.   } else {
  31.     printf("Object is open: name = %s, id = 0x%x\n", shar_object, shmid);
  32.   }
  33.  
  34.   msg_p = mmap(0, sizeof (message_t), PROT_WRITE|PROT_READ, MAP_SHARED, shmid, 0);
  35.   if (msg_p == (message_t*)-1 ) {
  36.   }
  37.  
  38.   signal(SIGINT, intHandler);
  39.   while (1) {
  40.     if(keepRunning && msg_p->type != MSG_TYPE_FINISH) {
  41.       msg_p->type = MSG_TYPE_STRING;
  42.       msg_p->n = rand() % 1000;
  43.       sleep(1);
  44.     } else {
  45.       msg_p->type = MSG_TYPE_FINISH;
  46.     }
  47.     if(msg_p->type == MSG_TYPE_FINISH) {
  48.       break;
  49.     }
  50.   }
  51.   close(shmid);
  52.   return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement