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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:08:55+00:00 2026-05-23T04:08:55+00:00

in a single main() function,so need signal handling. Use Posix Message Queue IPC mechanism

  • 0

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:Pilcrow,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;
}
  • 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-23T04:08:55+00:00Added an answer on May 23, 2026 at 4:08 am

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to build a simple, single user database application for Windows. Main requirements
I need a QDialog to send a signal to redraw the main window. But
A single Biztalk Server can have multiple Host processes. Is it possible to create
Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using
I'm writing a module with mixin templates to supply a main function for unit-testing
I am trying to create a single query for getting information from the main
I have multiple views defined in my main view. I want to add single
What single aspect of agile development should we implement first to improve our development
What is the single most effective practice to prevent arithmetic overflow and underflow ?
I'm a single developer looking to get off of Visual Source Safe and move

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.