Thread: Posix mqueue bad file descriptor.

  1. #1
    Registered User
    Join Date
    May 2011

    Posix mqueue bad file descriptor.

    Hi, I trying to make a program with one thread sending a message to a message queue and the other receiving and printing it. This is what I've written so far:
    #include <stdlib.h>
    #include <stdio.h>
    #include <sched.h>
    #include <time.h>
    #include <mqueue.h>
    #include <pthread.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #define MQUEUE_NAME "/mqueue"
    pthread_t send_thread, receive_thread;
    pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
    struct timespec start_time, end_time, thread_sleep_time;
    mqd_t message_queue;
    struct mq_attr mqueue_attr;
    char msg = 'a';
    char msg_received;
    unsigned int TimespecToNanosec(struct timespec tspec) {
    	unsigned int nanosec = tspec.tv_sec * 1000000000 + tspec.tv_nsec;
    void* send_task() {
    	mqueue_attr.mq_flags = 0;
    	mqueue_attr.mq_maxmsg = 2;
    	mqueue_attr.mq_msgsize = sizeof(msg);
    	mqueue_attr.mq_curmsgs = 0;
    	int flags = O_RDWR | O_CREAT;
    	mode_t mode  = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
    	printf("Opening mqueue \n");	
    	if(mq_open(MQUEUE_NAME, flags, mode, &mqueue_attr) == -1) {
    		perror("In mq_open ");
    	printf("Sending message \n");
    	if(mq_send(message_queue, (char *) &msg, sizeof(msg), 0) == -1) {
    		perror("In mq_send()");
    void* receive_task() {
    	printf("Receiving message \n");
    	if(mq_receive(message_queue, (char* ) &msg_received, mqueue_attr.mq_msgsize, 0) == -1) {
    		perror("In mq_receive()");
    	else printf("Message is: %c \n", msg_received);
    int main() {
    	pthread_attr_t attr;
    	struct sched_param thread_param, main_param;
    	thread_param.sched_priority = 90;
    	main_param.sched_priority = 80;
    	if(sched_setscheduler(0, SCHED_FIFO, &main_param) == -1) {
    		perror("sched_setscheduler failed");
    	pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
    	pthread_attr_setschedparam(&attr, &thread_param);
    	pthread_create(&send_thread, &attr, send_task, NULL);
    	pthread_create(&receive_thread, &attr, receive_task, NULL);
    	pthread_join(send_thread, NULL);
    	pthread_join(receive_thread, NULL);
    When I run the program I get a bad file descriptor error in mq_send. Can anyone check the code and tell me what I'm doing wrong? Thanks.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    mq_open(MQUEUE_NAME, flags, mode, &mqueue_attr)
    You open a message queue, and then throw it away. How do you intend to send to that message queue later, when you no longer know where it is?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening a file Descriptor
    By vin_pll in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2007, 05:03 AM
  2. How to read the file descriptor
    By vin_pll in forum C++ Programming
    Replies: 2
    Last Post: 11-26-2007, 01:58 AM
  3. Finding a port associated with a file descriptor
    By drrngrvy in forum Networking/Device Communication
    Replies: 1
    Last Post: 08-20-2007, 11:19 AM
  4. file descriptor redirection
    By msenthil in forum C Programming
    Replies: 15
    Last Post: 05-24-2007, 08:45 AM
  5. why wrong file descriptor ??
    By alokdotnet in forum C Programming
    Replies: 2
    Last Post: 08-13-2006, 12:59 AM

Tags for this Thread