Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.56 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include <sys/wait.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <string.h>
  9. #include <time.h>
  10. #include <mqueue.h>
  11.  
  12. #define ERR(source) (fprintf(stderr,"%s:%d\n",__FILE__,__LINE__),\
  13.     perror(source),kill(0,SIGKILL),exit(EXIT_FAILURE))
  14.  
  15.  
  16. void usage(){
  17.     fprintf(stderr,"USAGE: queue div n \n");
  18.     fprintf(stderr,"100 > n > 0 - number of queues\n");
  19.     exit(EXIT_FAILURE);
  20. }
  21.  
  22. int main(int argc, char** argv){
  23.     int n;
  24.     if (argc != 2)
  25.         usage();
  26.     n = atoi(argv[1]);
  27.     if (n <= 0 || n >= 100)
  28.         usage();
  29.    
  30.     struct mq_attr attr;
  31.     attr.mq_maxmsg = 100;
  32.     attr.mq_msgsize = sizeof(int);
  33.     printf("Allocating %d\n",(int)(n*sizeof(mqd_t)));
  34.     mqd_t* queues;
  35.     queues = (mqd_t*) malloc(n*sizeof(mqd_t));
  36.     printf("Done\n");
  37.  
  38.     int i;
  39.     i = 0;
  40.     printf("%d", i);
  41.     while(i++<n){
  42.         printf("here");
  43.         char* name = strcat((const char * restrict)"/q", (const char * restrict)i);
  44.         printf("Opening %s\n", name);
  45.         queues[i] = mq_open(name, O_RDWR | O_NONBLOCK | O_CREAT, 0600, &attr);
  46.         if (queues[i] == (mqd_t)-1)
  47.             ERR("mq open");
  48.     }
  49.  
  50.     sleep(2);
  51.    
  52.     for (int i=0; i < n; i++){
  53.         printf("Closing %d\n", i);
  54.         mq_close(queues[i]);
  55.     }
  56.  
  57.     for (int i=0; i < n; i++){
  58.         char* name = strcat("/q", (char)i);
  59.         if (mq_unlink(name))
  60.             ERR("mq unlink");
  61.     }
  62.  
  63.     free(queues);
  64.  
  65.     printf("END\n");
  66.     return EXIT_SUCCESS;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement