Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _GNU_SOURCE
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdint.h>
- #include <sys/wait.h>
- #include <unistd.h>
- #include <errno.h>
- #include <string.h>
- #include <time.h>
- #include <mqueue.h>
- #define ERR(source) (fprintf(stderr,"%s:%d\n",__FILE__,__LINE__),\
- perror(source),kill(0,SIGKILL),exit(EXIT_FAILURE))
- void usage(){
- fprintf(stderr,"USAGE: queue div n \n");
- fprintf(stderr,"100 > n > 0 - number of queues\n");
- exit(EXIT_FAILURE);
- }
- int main(int argc, char** argv){
- int n;
- if (argc != 2)
- usage();
- n = atoi(argv[1]);
- if (n <= 0 || n >= 100)
- usage();
- struct mq_attr attr;
- attr.mq_maxmsg = 100;
- attr.mq_msgsize = sizeof(int);
- printf("Allocating %d\n",(int)(n*sizeof(mqd_t)));
- mqd_t* queues;
- queues = (mqd_t*) malloc(n*sizeof(mqd_t));
- printf("Done\n");
- int i;
- i = 0;
- printf("%d", i);
- while(i++<n){
- printf("here");
- char* name = strcat((const char * restrict)"/q", (const char * restrict)i);
- printf("Opening %s\n", name);
- queues[i] = mq_open(name, O_RDWR | O_NONBLOCK | O_CREAT, 0600, &attr);
- if (queues[i] == (mqd_t)-1)
- ERR("mq open");
- }
- sleep(2);
- for (int i=0; i < n; i++){
- printf("Closing %d\n", i);
- mq_close(queues[i]);
- }
- for (int i=0; i < n; i++){
- char* name = strcat("/q", (char)i);
- if (mq_unlink(name))
- ERR("mq unlink");
- }
- free(queues);
- printf("END\n");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement