LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-03-2015, 12:04 AM   #1
gauravpathak129
LQ Newbie
 
Registered: Jan 2014
Location: Pune, Maharashtra
Distribution: Fedora 17, Ubuntu 12.04
Posts: 18

Rep: Reputation: Disabled
Return value of mq_open()


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.
 
Old 03-03-2015, 03:10 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
mq_open doesn't return a file handle, so there is no point in comparing it with file handles such as 0,1,2, etc.
 
Old 03-03-2015, 03:49 AM   #3
gauravpathak129
LQ Newbie
 
Registered: Jan 2014
Location: Pune, Maharashtra
Distribution: Fedora 17, Ubuntu 12.04
Posts: 18

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-03-2015, 03:59 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,871
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
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.

Last edited by NevemTeve; 03-03-2015 at 04:05 AM.
 
Old 03-04-2015, 01:21 AM   #5
gauravpathak129
LQ Newbie
 
Registered: Jan 2014
Location: Pune, Maharashtra
Distribution: Fedora 17, Ubuntu 12.04
Posts: 18

Original Poster
Rep: Reputation: Disabled
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.

Thanks a lot.
 
Old 03-04-2015, 01:21 AM   #6
gauravpathak129
LQ Newbie
 
Registered: Jan 2014
Location: Pune, Maharashtra
Distribution: Fedora 17, Ubuntu 12.04
Posts: 18

Original Poster
Rep: Reputation: Disabled
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.

Thanks a lot.
 
  


Reply

Tags
file descriptor, ipc, thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mq_open-> Permission denied Niraj Kulkarni Linux - Software 1 08-05-2013 09:11 AM
ENOMEM on mq_open() call roxasmn Linux - Software 6 08-10-2011 11:16 AM
mq_open() returns koumodaki Programming 1 02-19-2009 06:46 AM
mq_open error mageshvan Programming 1 10-08-2007 12:05 PM
mq_open fails on Suse 9.2 / 9.3 supaflav Programming 1 11-04-2005 01:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration