Take the 2-minute tour ×

I have assignment when I need to write simple time server and a client using Linux message queue. The server opens a message queue and the client sends a request with his PID (message with type 1) and the server reads that message and sends a message with type of PID (taken from the message read). I put all the code below because I don't know where I made the mistake. I'm not Linux programming expert. Don't even know if I written server correct.

File that is included by server and client (I need to write it in this way).

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/msg.h>
#include <sys/ipc.h>
#include <sys/types.h>
#include <string.h>
#include <signal.h>

#define QUEUE 100
#define PERM_ALL 0777

typedef struct my_msgbuf {
    long mtype;
    int pid;
} ClientMessage;

typedef struct my_msgbuf2 {
    long mtype;
    struct tm time;
} TimeMessage;


int m_queue;

void cleanup(int signum) {
    if (msgctl(m_queue, IPC_RMID, NULL) == -1) {
        printf("Something happen on clean up\n");

int main() {
    ClientMessage pid_message;
    TimeMessage t;
    time_t clock;
    struct tm *cur_time;

    if ((m_queue = msgget(QUEUE, PERM_ALL | IPC_CREAT)) == -1) {
        printf("Can't create and open message queue\n");
    printf("created message queue = %d\n", m_queue);
    //t = malloc(sizeof(TimeMessage));
    signal(SIGINT, cleanup);

    while (1) {
        if (msgrcv(m_queue, &pid_message, sizeof(pid_message.pid), 1, 0) == -1) {
        } else {
            t.mtype = pid_message.pid;
            clock = time(NULL);
            cur_time = localtime(&clock);
            memcpy(&t.time, cur_time, sizeof(struct tm));
            msgsnd(m_queue, &t, sizeof(struct tm), 0);



int main() {
    int m_queue;
    TimeMessage *t;
    ClientMessage client;

    if ((m_queue = msgget(QUEUE, PERM_ALL)) == -1) {
        perror("Error in opening queue");

    client.mtype = 1;
    client.pid = getpid();

    while (1) {
        if (msgsnd(m_queue, &client, sizeof(client.pid), 0) == -1) {
            perror("Error sending to queue");
        } else {
            if (msgrcv(m_queue, t, sizeof(struct tm), client.pid, 0) == -1) {
                perror("Error reading from queue");
            printf("time: %d:%d:%d\n", t->time.tm_hour, t->time.tm_min, t->time.tm_sec);
    return 0;

Both program compile without errors but client return "Error reading from queue" msgrcv is returning -1.

share|improve this question
Suggestion: When working with APIs which set errno (like message queue APIs in your case) it might be a good idea to use perror or strerror –  another.anon.coward May 19 '12 at 16:10
thanks @another.anon.coward I was wondering why examples uses perror –  jcubic May 19 '12 at 16:16
Now msgrcv return "Bad address" –  jcubic May 19 '12 at 16:18
That means "The address pointed to by msgp (buffer) isn't accessible.", it appears you have not allocate memory for t, how about trying to change TimeMessage *t to TimeMessage t & pass &t instead of t? –  another.anon.coward May 19 '12 at 16:31
Also the size your are passing in msgrcv seems to be incorrect, change sizeof(struct tm) to sizeof t (assuming you have changed *t to t) –  another.anon.coward May 19 '12 at 16:34
show 1 more comment

1 Answer

up vote 1 down vote accepted

After adding the perror it appears that you have got the error stating "Bad Address" (EFAULT) which means that "The address pointed to by msgp (buffer) isn't accessible". From the code it appears that there has been no memory allocated to TimeMessage *t so you can either allocate memory or just change it to TimeMessage t and pass &t instead of t to msgrcv. Also size should be sizeof t (assuming the change from *t to t, or sizeof(TimeMessage) for *t) instead of sizeof(struct tm) (& obviously you would change printf statement accordingly)
Hope this helps!

share|improve this answer
Thanks, I add t = malloc(sizeof(TimeMessage)); and it work. –  jcubic May 23 '12 at 6:31
add comment

Your Answer


