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: Eric
Server:Eric,Welcome.
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
Below is my code,
What I got is :
client:knock knock
Server:Who's there?
client:eric
eric
client:Exit
Exit
1st round succeed get the right result.From 2nd round, the client put the same typing on console.
Please help. Remember to use gcc -lrt to link mq_function.
#include <mqueue.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <string.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)
static void sig_usr1(int signo) {
mqflag = 1;
sigprocmask(SIG_BLOCK, &newmask, &oldmask);
return;
}