Advertisement
Guest User

Untitled

a guest
May 20th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.62 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/ipc.h>
  4. #include <sys/sem.h>
  5. #include <sys/shm.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <time.h>
  9.  
  10. #include "header.h"
  11.  
  12. int msggen(char *buf) {
  13.   int length = rand() % (MSGSIZE - 2) + 1;
  14.   int i;
  15.   for(i = 0; i < length; ++i) {
  16.     buf[i] = (char)(rand() % (END_CHAR - START_CHAR) + START_CHAR);
  17.   }
  18.   buf[length] = '\0';
  19.   ++length;
  20.  
  21.   return length;
  22. }
  23.  
  24. int detachSharedMem(void *attachedMem) {
  25.   if(shmdt(attachedMem) == ERRCODE) {
  26.     perror("shmdt");
  27.     return EXIT_FAILURE;
  28.   }
  29.  
  30.   return EXIT_SUCCESS;
  31. }
  32.  
  33. int delSharedMem(int sharedMem) {
  34.   if(shmctl(sharedMem, IPC_RMID, NULL) == ERRCODE) {
  35.     perror("shmctl");
  36.     return EXIT_FAILURE;
  37.   }
  38.  
  39.   return EXIT_SUCCESS;
  40. }
  41.  
  42. int delSem(int sem) {
  43.   if(semctl(sem, DUMMY, IPC_RMID, DUMMY) == ERRCODE) {
  44.     perror("semctl");
  45.     return EXIT_FAILURE;
  46.   }
  47.  
  48.   return EXIT_SUCCESS;
  49. }
  50.  
  51. int produce(int sem, char *attachedMem) {
  52.   struct sembuf waitReading = {SEM_PRODUCER, LOCK, SEM_UNDO};
  53.   struct sembuf produced = {SEM_CONSUMER, UNLOCK, SEM_UNDO};
  54.  
  55.   int i;
  56.   for(i = 0; i < MSGCOUNT; ++i) {
  57.     msggen(attachedMem);
  58.  
  59.     printf("Send: %s\n", attachedMem);
  60.  
  61.     if(semop(sem, &produced, 1) == ERRCODE) {
  62.       perror("semop");
  63.       return EXIT_FAILURE;
  64.     }
  65.  
  66.     if(semop(sem, &waitReading, 1) == ERRCODE) {
  67.       perror("semop");
  68.       return EXIT_FAILURE;
  69.     }
  70.  
  71. //  sleep(1);
  72.   }
  73.  
  74.   return EXIT_SUCCESS;
  75. }
  76.  
  77. int main() {
  78.   srand(time(NULL));
  79.  
  80.   key_t idKey = ftok("header.h", PROJECT_PREFIX);
  81.   if(idKey == ERRCODE) {
  82.     perror("ftok");
  83.     return EXIT_FAILURE;
  84.   }
  85.  
  86.   int sem = semget(idKey, SEMCOUNT, IPC_CREAT | ACCESS_RIGHTS);
  87.   if(sem == ERRCODE) {
  88.     perror("semget");
  89.     return EXIT_FAILURE;
  90.   }
  91.  
  92.   int sharedMem = shmget(idKey, MSGSIZE, IPC_CREAT | ACCESS_RIGHTS);
  93.   if(sharedMem == ERRCODE) {
  94.     perror("shmget");
  95.     delSem(sem);
  96.     return EXIT_FAILURE;
  97.   }
  98.  
  99.   char *attachedMem = shmat(sharedMem, ANY_PLACE, NO_FLAGS);
  100.   if(attachedMem == NULL) {
  101.     delSem(sem);
  102.     delSharedMem(sharedMem);
  103.     perror("shmat");
  104.     return EXIT_FAILURE;
  105.   }
  106.  
  107.   if(produce(sem, attachedMem) != EXIT_SUCCESS) {
  108.     delSem(sem);
  109.     detachSharedMem(attachedMem);
  110.     delSharedMem(sharedMem);
  111.     return EXIT_FAILURE;
  112.   }
  113.  
  114.   if(detachSharedMem(attachedMem) != EXIT_SUCCESS) {
  115.     delSharedMem(sharedMem);
  116.     delSem(sem);
  117.     return EXIT_FAILURE;
  118.   }
  119.  
  120.   if(delSharedMem(sharedMem) != EXIT_SUCCESS) {
  121.     delSem(sem);
  122.     return EXIT_FAILURE;
  123.   }
  124.  
  125.   if(delSem(sem) != EXIT_SUCCESS) {
  126.     return EXIT_FAILURE;
  127.   }
  128.  
  129.   return EXIT_SUCCESS;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement