ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am working on an applicaiton where I have to deal with multiple threads and have to implement IPC mechanism to commnicate between threads, I am using message queue for this purpose.
However, at one instant mq_open() is returning 0 (zero) which is the integer file descriptor associated with the stream stdin, which is causing trouble for taking input from user as mq_close() is being called in the program.
Now my question is how should I ensure that mq_open() will never return any standard value which is implemeted in linux for varoius files and descriptors for itself.
But in my case mq_open() is returning 0 (zero) that is why I am unable to use scanf, getchar, fgetc etc. because these calls returns immediately and gives a garbage value in variable used in function, such as if i have used scanf("%d",&choice) the value in choice is something random and it does not wait for user input. Same thing goes for all other calls such as getchar etc.
When I debugged my code then I came to know that mq_open is returning zero and as soon as my main thread call scanf the thread in which I called mq_close is closing the file decsriptor. But after some efforts I have successfully executed my program but only once, and now it is behaving in same manner as it was previously.
You must have misunderstood something, the return value of mq_open shouldn't effect the behaviour of standard input. If you think otherwise, provide a complete, minimal example program that demonstrates problem. (Note: it has to be compilable with flags -W -Wall -Wextra -Werror)
Note: multi-threaded programs are not for beginners. Or anyone else, really.
I also agree with you Nevem, but it is behaving in that way. So, now I switched from POSIX message queues to System V Message queues and now I am not facing any issues now.
I also agree with you Nevem, but it is behaving in that way. So, now I switched from POSIX message queues to System V Message queues and now I am not facing any issues now.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.