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 3348366
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T01:32:02+00:00 2026-05-18T01:32:02+00:00

I’m trying to pass messages between 2 threads using a queue, but I haven’t

  • 0

I’m trying to pass messages between 2 threads using a queue, but I haven’t gotten any results so far. When I print the contents of the message after it’s received and before it’s sent, it only seems to keep its value within the tread. I need to implement it with 1 server thread and multiple client threads, but for now I’m only using 1 of each. Here’s my code

struct msg                                //struct for client requests to server
{
        long mtype;
        int numResources;           //number of resources to be requested
        int ID;                     //ID associated with client thread
};                                         

int c1PID;                              //process ID variable for client thread 1
int serverPID;

key_t key1;
key_t keyS;

int msqid1;
int msqidS;

int main(int arc, char *argv[])
{
        key1 = ftok(".", '1');      //queue for client thread 1 to receive msgs from server
        msqid1 = msgget(key1, 666 | IPC_CREAT);

        keyS = ftok(".", 's');                          //general queue for server
        msqidS = msgget(keyS, 666 | IPC_CREAT);

        pthread_t threads[2];              //create an array of pthreads

        if ((serverPID = pthread_create(&threads[0], NULL, server, NULL)) != 0)
        {
                perror("server thread");
                exit(1);
        }

        if ((c1PID = pthread_create(&threads[1], NULL, client, NULL)) != 0)
        {
                perror("client thread");
                exit(1);
        }

        pthread_exit(NULL);
}

void *server()
{                                  
        struct msg request;
        size_t size = sizeof(struct msg) - offsetof(struct msg, numResources);

        while (1)
        {

                msgrcv(msqidS, &request, size, 2, 0);

                printf("received: numResources requested = %d\n", request.numResources);

                request.numResources = 9001;

                printf("sending: numResources requested = %d\n", request.numResources);

                msgsnd(msqid1, &request, size, 0);

                sleep(1);
        }
}

void *client()
{
        struct msg request;
        size_t size;
        request.numResources = 0;
        size = sizeof(struct msg) - offsetof(struct msg, numResources);

        msgsnd(msqidS, &request, size, 0);

        while(1)
        {
                msgrcv(msqid1, &request, size, 2, 0);

                printf("received: numResources requested = %d\n", request.numResources);

                request.numResources += 1;//(int)(ceil((double)(rand()%2)) + 1);

                printf("sending: numResources requested = %d\n", request.numResources);

                msgsnd(msqidS, &request, size, 0);

                sleep(1);
}

I took out a lot of my print statements, but it looks something like this:

Server thread: 
received: numResources = 9001;
sending: numResources = 9001;

client thread:
received: numResources = 1;
sending: numResources = 2;

Server thread: 
received: numResources = 9001;
sending: numResources = 9001;

client thread:
received: numResources = 2;
sending: numResources = 3;
  • 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-05-18T01:32:03+00:00Added an answer on May 18, 2026 at 1:32 am

    Edit:
    The sizeof(struct msg) – offsetof(struct msg, numResources); should be ok.

    But, your mtype needs to be, as per the docs, a positive integer. Initialize it to 2, as your calls to msgrecv says to receive only message type 2.

    Add error checking to all the msgsnd/msgrecv calls , so you’re sure you’re not silently getting errors.

    Add error checking to your ftok and msgget calls.

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

Sidebar

Related Questions

No related questions found

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.