线程之间传递消息

线程之间传递消息

本文介绍了C-使用队列在线程之间传递消息的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用队列在2个线程之间传递消息,但是到目前为止我还没有得到任何结果.当我在收到消息之后和发送消息之前打印消息的内容时,似乎只将其值保持在范围内.我需要使用1个服务器线程和多个客户端线程来实现它,但是现在我只使用每个线程中的1个.这是我的代码

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;

推荐答案

sizeof(struct msg)-offsetof(struct msg,numResources);应该没事.

The sizeof(struct msg) - offsetof(struct msg, numResources); should be ok.

但是,根据文档,一个正整数.将其初始化为2,因为您对msgrecv的调用说它只接收消息类型2.

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.

向所有msgsnd/msgrecv调用中添加错误检查,这样您就可以确保不会静默地得到错误.

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

为您的ftok和msgget调用添加错误检查.

Add error checking to your ftok and msgget calls.

这篇关于C-使用队列在线程之间传递消息的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!