Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7968125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:01:40+00:00 2026-06-04T07:01:40+00:00

I have assignment when I need to write simple time server and a client

  • 0

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;

Server

int m_queue;

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

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");
        exit(1);
    }
    printf("created message queue = %d\n", m_queue);
    fflush(stdout);
    //t = malloc(sizeof(TimeMessage));
    signal(SIGINT, cleanup);

    while (1) {
        if (msgrcv(m_queue, &pid_message, sizeof(pid_message.pid), 1, 0) == -1) {
            break;
        } 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);
        }
    }

    cleanup(0);
}

Client

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

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

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

    while (1) {
        if (msgsnd(m_queue, &client, sizeof(client.pid), 0) == -1) {
            perror("Error sending to queue");
            exit(1);
        } else {
            if (msgrcv(m_queue, t, sizeof(struct tm), client.pid, 0) == -1) {
                perror("Error reading from queue");
                exit(1);
            }   
            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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:01:42+00:00Added an answer on June 4, 2026 at 7:01 am

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have an assignment where we need to profile a 'simple instruction' (addition or
For an assignment I have to write a Tribool class in C# using a
I have to write two files for an assignment, a client file that will
I have an assignment where I need to implement church numerals in SML using
I have a homework assignment where I need to take input from a file
Hi I have a homework assignment where I need to implement an intersection of
Hello is have a question for a school assignment i need to : Read
I have an assignment to create a GUI using MATLAB GUIDE and am having
I have an assignment about fftw and I was trying to write a small
I have created a simple table and i need to position. does anyone have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.