c - Why is errno set to 22: mq_open() POSIX -
im receiving errno 22 when trying create message_queue in posix c. far can tell, comparing example code available on web, i've set parameters correctly.
here snippet:
int open_flags; mqd_t mqfd; int bytes_per_msg; struct mq_attr attr; unsigned int* msgbuff; printf("from 1 400, n? : "); scanf("%d", &n); bytes_per_msg = (n + 1) * (sizeof(unsigned int)); msgbuff = (unsigned int*)malloc(bytes_per_msg); open_flags = o_creat|o_rdwr; attr.mq_maxmsg = n; attr.mq_msgsize = bytes_per_msg; attr.mq_flags = 0; mqfd = mq_open("/myqueue", open_flags, 0666, &attr); if(mqfd == -1){ printf("queue creation failed, errno: %d\n",errno); }
edit: apologies not being more clear. errno 22 invalid argument. --the meanings of error numbers can found on errno.h
i assume using mq_open(3) on linux, , errno
getting einval
. according documentation may happen when:
name doesn't follow format in mq_overview(7).
or
o_creat specified in oflag, , attr not null, attr->mq_maxmsg or attr->mq_msqsize invalid. both of these fields must greater zero. in process unprivileged (does not have cap_sys_resource capability), attr->mq_maxmsg must less or equal msg_max limit, , attr->mq_msgsize must less or equal msgsize_max limit. in addition, in privileged process, attr->mq_maxmsg cannot exceed hard_max limit. (see mq_overview(7) details of these limits.)
so should read mq_overview(7)
btw, rtfm faster asking on forum here. have lost both , our time.
next time, use perror(3) on error cases. notice posix errno.h specification not assign numerical values error numbers einval
(and on purpose, several posix compliant systems have different numbers).
btw, should check return value of scanf(3), in case:
printf("from 1 400, n? : \n"); n= 0; if (scanf("%d", &n)<1 || n<=0 || n>400) { fprintf(stderr, "bad number (n=%d)\n", n); exit(exit_failure); }
Comments
Post a Comment