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

  • SEARCH
  • Home
  • 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 5996647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:07:58+00:00 2026-05-23T00:07:58+00:00

I want to have a message send & receive through 2 uni-direction FIFO Flow

  • 0

I want to have a message send & receive through 2 uni-direction FIFO

Flow of data

FIFO1 
stdin--->parent(client) writefd--->FIFO1-->child(server) readfd
FIFO2
child(server) writefd2---->FIFO2--->parent(client) readfd2--->stdout

I need to have boundary structed message mesg_len+mesg_type+mesg_data

The function is that if user input “Knock Knock” on stdin, which directs to client, client send this message to server through FIFO1, server compares the string, if matches with “Knock Knock”, then server reply message “Who’s there?” to client through FIFO2, and client write this message to the stdout.

The interactive part is like:

client:Knock Knock
server:who's there
client:eric
server:eric,welcome
client:exit
all signal terminate

Below is my code:

I need help on kill() signals when client types “exit”.It seems the client process still live after call kill(). So i need to type ctrl+c to end

Please help me. Thanks a lot!

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>

#define MAX_BUF 100
#define MAXMESGDATA (MAX_BUF - 2* sizeof(long))
#define MESGHDRSIZE (sizeof(struct mymesg)-MAXMESGDATA)
#define FIFO1 "/tmp/fifo.1"
#define FIFO2 "/tmp/fifo.2"
#define FILE_MODE (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)

struct mymesg{
    long mesg_len; //byte in mesg_data
    long mesg_type; //message type
    char mesg_data[MAXMESGDATA];
};

ssize_t mesg_send (int fd, struct mymesg *mptr){
    return (write (fd,mptr,MESGHDRSIZE + mptr->mesg_len));
}

ssize_t mesg_recv(int fd,struct mymesg *mptr){
    size_t len;
    ssize_t n;
    if ((n=read(fd,mptr,MESGHDRSIZE))==0) {//read hear first, get len of data
        return 0; //end of file
    } else if (n!=MESGHDRSIZE){
        printf("message header: expected %d, got %d\n", MESGHDRSIZE,n);
        exit(1);
    }
    if ((len=mptr->mesg_len)>0)
    {
        if ((n=read(fd,mptr->mesg_data,len))!=len)
        {
            printf("message data: expected %d, got %d\n", len,n);
            exit(1);
        }
    }
    return len;
}

void client(int readfd,int writefd){
    size_t len;
    ssize_t n;
    struct mymesg mesg;
    for (;;)
    {
        printf("\nClient:");
        fgets(mesg.mesg_data,MAXMESGDATA,stdin);//read mesg
        len=strlen(mesg.mesg_data);

        if (mesg.mesg_data[len-1]=='\n') //ignore newline
            len--;
        mesg.mesg_len=len;
        mesg.mesg_type=1;

        mesg_send(writefd,&mesg);//write to IPC channel
        //read from IPC,write to std output
        if((n=mesg_recv(readfd,&mesg))>0)
            write(STDOUT_FILENO,mesg.mesg_data,n);
    }
}

void server(int readfd,int writefd){
    ssize_t n;
    struct mymesg mesg;
    for(;;)
    {
        mesg.mesg_type=1;
        //read from IPC channel
        if ((n=mesg_recv(readfd,&mesg))==0){
            printf("Message missing");
            exit(1);
        }

        mesg.mesg_data[n]='\0';
        mesg.mesg_len=strlen(mesg.mesg_data);
        char* str=NULL;

        if (strcasecmp ("Knock Knock", mesg.mesg_data)==0){
             str="Server:Who's there?";
             strcpy(mesg.mesg_data,str);
             mesg.mesg_len=strlen(str)-1;
            }
        else if(strcasecmp ("Eric", mesg.mesg_data)==0){
            str="Server:Eric,Welcome!";
            strcpy(mesg.mesg_data,str);
            mesg.mesg_len=strlen(str)-1;
            }
        else if(strcasecmp ("Exit", mesg.mesg_data)==0){
            kill(getpid(),SIGTERM);
            kill(getppid(),SIGTERM);
            exit(0);
            }
        mesg_send(writefd,&mesg);
    }
}

int main(int argc, char ** argv){
    /*MAXMESGDATA== 92 bytes; sizeof(struct mymesg)== 100 bytes
     2* sizeof(long)== 8 bytes; MESGHDRSIZE ==8 bytes*/

    int readfd,writefd;
    pid_t childpid;
    //create 2 FIFOs
    if ((mkfifo(FIFO1,FILE_MODE)<0) && (errno!=EEXIST)){
        printf("can't create %s",FIFO1);
        exit(1);
    }
    if ((mkfifo(FIFO2,FILE_MODE)<0) && (errno!=EEXIST)){
        printf("can't create %s",FIFO1);
        unlink(FIFO1);
        exit(1);
    }
    if ((childpid=fork()==0)){//child
        readfd=open(FIFO1,O_RDONLY,0);
        writefd=open(FIFO2,O_WRONLY,0);
        server(readfd,writefd);
        exit(0);
    }
    //parent
    writefd=open(FIFO1,O_WRONLY,0);
    readfd=open(FIFO2,O_RDONLY,0);
    client(readfd,writefd);

    waitpid(childpid,NULL,0);
    close(readfd);
    close(writefd);
    unlink(FIFO1);
    unlink(FIFO2);
    return EXIT_SUCCESS;
}
  • 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-23T00:07:59+00:00Added an answer on May 23, 2026 at 12:07 am

    You open FIFO2 as WRONLY and FIFO1 as RDONLY in both the processes. Opening pipe for read will block until the other side opens for write, and you open for read on both sides – hence deadlocked.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to send and receive dynamic data using a SysV message queue for
I have a variable last_login and want to output a message if the last
I have a simple GtkStatusBar created with glade,and want to add a simple message
I have a custom NSActionCell subclass, and I want to use setTarget: message to
I want to have a PHP script send a XML formatted string to another
I want to have a select-only ComboBox that provides a list of items for
I want to have a text box that the user can type in that
I want to have a class which implements an interface, which specifies the specific
I want to have a web based admin to upload, delete files and folders
I want to have two items on the same line using float: left for

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.