daily pastebin goal
72%
SHARE
TWEET

Untitled

a guest Dec 7th, 2017 52 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <sys/types.h>
  2. #include <sys/ipc.h>
  3. #include <sys/sem.h>
  4. #include <semaphore.h>
  5. #include <sys/shm.h>
  6. #include <fcntl.h>
  7. #include <string.h>
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <errno.h>
  12. #include "semadefs.h"
  13.  
  14. int main(int argc, char* argv[]){
  15.     int fd_read_file;
  16.     if (argc < 2){
  17.         fprintf(stderr, "No address of file.\n");
  18.         return 1;
  19.     }
  20.     int file_index = 1;
  21.     if ((strcmp(argv[1], "-h") == 0)){
  22.         printf("Usage: [key] <filename>.\nKeys:\n-h: show this help.\n-i: ignore another running client.\n");
  23.         return 0;
  24.     }
  25.     else if (strcmp(argv[1], "-i") == 0){
  26.         if (argc < 3){
  27.             fprintf(stderr, "No address of file.\n");
  28.             return 1;
  29.         }
  30.         file_index = 2;
  31.         if (remove("/dev/shm/sem.clientlock") < 0){                          //delete semaphore semaphore
  32.             if (errno == ENOENT){}
  33.             else {
  34.                 perror("");
  35.                 return 1;
  36.             }
  37.         }
  38.     }
  39.     Message *message_ptr;
  40.     key_t key;
  41.     sem_t* sem_blocker;
  42.     int shmem_id, sem_proc_id, semval;
  43.     int n_reader = -1;
  44.     fd_read_file = open(argv[file_index], O_RDONLY);
  45.     //open given file
  46.     if (fd_read_file < 0) {
  47.         perror("An error occured while trying to open file");
  48.         return 1;
  49.     }
  50.     //generate key
  51.     if ((key = ftok(shfname, 'A')) < 0){
  52.         fprintf (stderr, "Server wasn't runned\n");
  53.         return 1;
  54.     }
  55.     //getting memory id
  56.     if ((shmem_id = shmget(key, sizeof(Message), IPC_CREAT | 0666)) < 0){
  57.         perror("Cannot create shared memory space");
  58.         return 1;
  59.     }
  60.     //attaching memory
  61.     if ((message_ptr = (Message *)shmat(shmem_id, 0, 0)) < 0){
  62.         perror("Cannot attach shared memory");
  63.         return 1;
  64.     }
  65.     //getting semaphores
  66.     if ((sem_proc_id = semget(key, 1, IPC_CREAT | 0666)) < 0){
  67.         perror("An error occured while accessing semaphore");
  68.         return 1;
  69.     }
  70.     //get blocker semaphore
  71.     if ((sem_blocker = sem_open("/clientlock", O_CREAT, IPC_CREAT | 0666, 0)) == SEM_FAILED){
  72.         perror ("An error occured while creating locker semaphore");
  73.         return 1;
  74.     }
  75.     //check locks from another clients
  76.     if (sem_getvalue(sem_blocker, &semval) < 0){
  77.         perror ("An error occured while accessing semaphore");
  78.     }
  79.     if (semval > 0){
  80.         fprintf(stderr, "Another client is running.\n");
  81.         return 1;
  82.     }
  83.     //set lock from another clients
  84.     if (sem_post(sem_blocker) < 0){
  85.         perror("An error occured while doing semaphore operation");
  86.         return 1;
  87.     }
  88.     while (n_reader!=0){
  89.         //wait for unlocking, then lock memory
  90.         if (semop(sem_proc_id, &client_start, 1) < 0){
  91.             perror("An error occured while doing semaphore operation");
  92.             return 1;
  93.         }
  94.         if ((n_reader = read(fd_read_file, message_ptr->buff, BUF_SIZE)) < 0){
  95.             perror("An error occured while reading to shared memory");
  96.             return 1;
  97.         }
  98.         message_ptr->size = n_reader;
  99.         //unlock memory
  100.         if (semop(sem_proc_id, &client_end, 1) < 0){
  101.             perror("An error occured while doing semaphore operation");
  102.             return 1;
  103.         }
  104.     }
  105.     //detatch from memory
  106.     if (shmdt(message_ptr) < 0){
  107.         perror("Message error");
  108.         return 1;
  109.     }
  110.     if (sem_close(sem_blocker) < 0){
  111.         perror ("Semaphore destroying error");
  112.         return 1;
  113.     }
  114.     if (remove("/dev/shm/sem.clientlock") < 0){
  115.         perror("Semaphore destroying error");
  116.         return 1;
  117.     }
  118.     //and successfully return
  119.     return 0;
  120. }
RAW Paste Data
Top