开发者

help:wrong behavior code:client-server IPC signal handling on POSIX Message Queue UNIX C programming

in a single main() function,so need signal handling. Use Posix Message Queue IPC mechanism , can ignore the priority and other linked list message,to implement the scenario:

client:Knock Knock

server:who's there

client: pilcrow

Server:pilcrow,thanks a lot.

client:exit

all process terminated

stdin->POSIX MsgQ client send "knock knock" to server->Server compares string and send "who's there" back to client

What I got is :

client:knock knock

Server:Who's there?

client:pilcrow

pilcrow

client:Exit

Exit

1st round successfully give me the right result.From 2nd round, the client output the same typing on console.

Please help. Remember to use gcc -lrt to link mq_function.

Below is my code,

#include <mqueue.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#define MSG_SIZE 100  //max size of msg
#define MAX_MSG 1  //max # of msg
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)
volatile sig_atomic_t mqflag; /* set nonzero by signal handler */
static void sig_usr1(int);
sigset_t zeromask, newmask, oldmask;

int  main(int argc, char **argv) {
int c,flags;/* for getopt() */
pid_t child_pid;
mqd_t msgq_id;
struct mq_attr attr;
struct sigevent sigev;
char *buff_forward,*buff_backward;
flags=O_RDWR | O_CREAT;
attr.mq_msgsize=MSG_SIZE;
attr.mq_maxmsg=MAX_MSG;
buff_forward=malloc(attr.mq_msgsize);
buff_backward=malloc(attr.mq_msgsize);

while ((c= getopt(argc, argv, "e")) != -1) {
    switch (c) {
       case 'e':   /* create the queue exclusive */
           flags|= O_EXCL;
           break;
    }
}

if (optind!=argc-1){
    printf("usage: [-e] <name>");
    exit(1);
}

msgq_id = mq_open(argv[optind],flags,FILE_MODE,&attr);
/* producing the message */
mq_getattr(msgq_id, &attr) ;
printf("Queue \"%s\":\n\t- stores at most %ld messages\n\t- "
        "large at most %ld bytes each\n\t- currently holds %ld messages\n",
        argv[optind], attr.mq_maxmsg, attr.mq_msgsize, attr.mq_curmsgs);

sigemptyset(&zeromask); /* no signals blocked */
sigemptyset(&newmask);
sigemptyset(&oldmask);
sigaddset(&newmask, SIGUSR1);
/* establish signal handler, enable notification */
signal(SIGUSR1, sig_usr1);
sigev.sigev_notify = SIGEV_SIGNAL;
sigev.sigev_signo = SIGUSR1;
sigprocmask(SIG_BLOCK, &newmask, &oldmask);/* block SIGUSR1 */

if ((child_pid=fork())==0){
    for (; ; ) {
        while (mqflag == 0)
            sigsuspend(&zeromask);
        mqflag =0; /* reset flag */
        msgq_id=mq_open(argv[optind],O_RDONLY);
        mq_receive(msgq_id, buff_forward, attr.mq_msgsize, NULL);
        mq_close(msgq_id);

        if (strcasecmp ("Knock Knock",buff_forward)==0){
             strcpy(buff_backward,"Server:Who's there?");
        }
        else if(strcasecmp ("pilcrow", buff_forward)==0){
             strcpy(buff_backward,"Server:Pil开发者_运维技巧crow,thanks a lot!");
        }
        else if(strcasecmp ("Exit",buff_forward)==0){
             kill(getppid(),SIGTERM);
             exit(0);
        }

    msgq_id=mq_open(argv[optind],O_WRONLY);
    mq_send(msgq_id,buff_backward,MSG_SIZE,NULL);
    mq_close(msgq_id);
        mq_notify(msgq_id, &sigev); /* reregister */
    }
    sigprocmask(SIG_UNBLOCK, &newmask, NULL); /* unblock SIGUSR1 */
    exit(0);
}
else if(child_pid>0){
    for(;;){
        printf("client:");
        gets(buff_forward);
        msgq_id=mq_open(argv[optind],O_WRONLY);
        mq_send(msgq_id,buff_forward,MSG_SIZE,NULL);
        mq_close(msgq_id);
        mq_notify(msgq_id, &sigev);
        while(mqflag==0)
            sigsuspend(&zeromask);
        mqflag==0;
        msgq_id=mq_open(argv[optind],O_RDONLY);
        mq_receive(msgq_id, buff_backward, attr.mq_msgsize, NULL);
        printf("%s\n",buff_backward);
        mq_close(msgq_id);
        }
    sigprocmask(SIG_UNBLOCK, &newmask, NULL); /* unblock SIGUSR1 */
    exit(0);
    }
return (EXIT_SUCCESS);
}
static void sig_usr1(int signo) {
    mqflag = 1;
    sigprocmask(SIG_BLOCK, &newmask, &oldmask);
    return;
}


The child calls sigsuspend before calling mq_notify (because mq_flag is implicitly initialized to zero and the check for interruption is improperly performed before notification). It can never be woken up as intended.

Initialize mq_flag to 1 to see the difference. Then refactor.

UPDATED

The OP changed the code substantially, so I changed this answer, too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜