阅读后如何从消息队列中清除消息?

时间:2019-10-21 18:04:31

标签: c message-queue msgrcv

我试图创建一个程序,其中主控器不断向用户进程发送消息。每次用户进程接收到一条消息时,它都应发送一条确认消息(将消息发送回主服务器),说它收到了该消息,并选择一个介于1-3之间的随机数。因此,我在用户进程中设置了while(1)。但是发生的是,用户进程在收到消息后不断循环直到消息终止,然后才将消息发送回主服务器。 我只是希望用户流程选择一个号码将消息发送回去,但是用户流程是:
选择一个号码发送消息
选择一个号码发送消息
选择一个号码发送消息

更新 我尝试在msgsend之后从用户到master添加sleep(1)来将控件交给master,但这仍然没有做任何事情。试图删除while循环,但这在第二次该过程收到消息时给我带来麻烦。

这是到目前为止的代码: user.c

struct mesg_buffer {
    long mesg_type;
    int mesg_text[2];
} message;
void main()
{
        int pid=getpid();
        int error1;
        printf("\nhello from %d\n",getpid());
        int times=0,timen=0,zero=0,randomnum,timelimit,n=0,sum,i;

        int msgid = msgget(key, 0666 | IPC_CREAT);
        if(msgid==-1)
            perror("User:error in message get");
        else
            printf("msgid=%d\n",msgid);

        while(1)
        {
                error1=msgrcv(msgid, &message, sizeof(message), pid, 0);
                if(error1==-1)
                    perror("User: Error in receieving message from OSS ");
                else
                {
                    printf("In User: Data received from oss\n");
                    int work=(rand() % (3)) + 1;
                    printf("random work %d\n",work);
                    if(work==1)
                    {
                        printf("the process is ending\n");
                        message.mesg_text[0]=pid;
                        message.mesg_text[1]=1;

                        if( msgsnd(msgid, &message, sizeof(message), 0)==-1)
                            perror("error in sending message back to OSS");
                        else
                                printf("message sent to oss now I can die in peace\n");

                        exit(0);

                    }
                    if(work==2)
                    {
                        message.mesg_text[0]=pid;
                        message.mesg_text[1]=2;

                        if( msgsnd(msgid, &message, sizeof(message), 0)==-1)
                            perror("error in sending message back to OSS");
                        else
                            printf("message sent to oss now I can die in peace\n");
                    }
                    if(work==3)
                    {
                        message.mesg_text[0]=pid;
                        message.mesg_text[1]=3;
                        if( msgsnd(msgid, &message, sizeof(message), 0)==-1)
                            perror("error in sending message back to OSS");
                        else
                            printf("message sent to oss now I can die in peace\n");
                    }
                }//endif
        }

master.c

int sendm=msgsnd(msgid, &message, sizeof(message), 0);

                if( sendm==-1)
                    perror("error in sending message to user process");
                else
                {
                    printf("message sent to process %d\n",currentprocessPID);
                    currentprocessstarted=true;
                                        //do some stuff
                }   
            //receive message from user
            if(msgrcv(msgid, &message, sizeof(message), currentprocessPID, 0)==-1)
                perror("error in recieving message from user process");
            else
            {

                printf("message received from %d\n",message.mesg_text[0]);

                if(message.mesg_text[1]==1)
                {
                    printf("process %d wants to terminate\n",currentprocessPID);
                    int corpse,status;
                    while ((corpse = waitpid(message.mesg_text[0], &status, 0)) != message.mesg_text[0] && corpse != -1)
                    {
                        char pmsg[64];
                        snprintf(pmsg, sizeof(pmsg), "logParse: PID %d exited with status 0x%.4X", corpse, status);
                        perror(pmsg);
                    }
                }
                else if(message.mesg_text[1]==2)
                {
                    //do some stuff
                }
                else if(message.mesg_text[1]==3)
                {
                    //do some stuff
                }
                    printf("%d process is done with its work\n",message.mesg_text[0]);

我的输出

hello from 15496
msgid=67272706
In User: Data received from oss
random work 2
message sent to oss now I can die in peace
In User: Data received from oss
random work 2
message sent to oss now I can die in peace
In User: Data received from oss
random work 1
the process is ending
message sent to oss now I can die in peace

message sent to process 15496
message received from 15496
process 15496 wants to terminate
15496 process is done with its work

0 个答案:

没有答案