Thread: Inter process communication with named pipe

  1. #1
    Registered User
    Join Date
    Dec 2022
    Posts
    2

    Inter process communication with named pipe

    Hello everyone,
    I'm currently working on inter-process communication.I want that a program call process1.c will take a user input (one character at a time) and write it in a named pipe,then a second program call process2.c will dynamically read the pipe and print the character each time a user press a new character.
    In summary,the user start process1.c in one terminal window and process2.c in another terminal window. In process1.c terminal window,she is ask to enter an input,each time she enter a character,the character will immediately appear in the process2.c terminal window.This must be done without her pressing enter in process1.c terminal window.
    I'm on linux,I try to do this,but when the user enter a character in process1.c terminal window it doesn't show up on process2.c terminal window unless I hit enter.Here is the code:
    Code:
    // PROGRAM process1.c //
    ///////////////////////
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<unistd.h>
    #include<fcntl.h>
    #include<sys/stat.h>
    
    int main() {
    
        mkfifo("my_pipe_p1_p2", 0600);
        int desc = open("my_pipe_p1_p2", O_WRONLY);
        printf("your input: ");
        char c[1];
        while(1) {
            c[0] = getchar();
            write(desc, c, 1);
        }
    
        return 0;
    }
    
    // PROGRAM process2.c //
    ///////////////////////
    #include<stdio.h>
    #include<stdlib.h>
    #include<sys/types.h>
    #include<unistd.h>
    #include<fcntl.h>
    #include<sys/stat.h>
     
    int main() {
    
        mkfifo("my_pipe_p1_p2", 0600);
        int desc = open("my_pipe_p1_p2", O_RDONLY);
        char c[1];
        while(1) {
            read(desc, c, sizeof(c));
            printf("%s", c);
        }
            
        return 0;
    }
    What am I doing wrong? Thanks in advance for your help.
    Last edited by Salem; 12-03-2022 at 09:50 PM. Reason: Removed font from code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    A couple of things.

    1. The input needs to be non-blocking
    c - How to read terminal&#39;s input buffer immediately after keypress - Stack Overflow

    2. printf("%s", c);
    Use %c, not %s
    What you have is not a string.

    3. All your mkfifo, write and read calls should be checked for error returns.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2022
    Posts
    2
    Hello,
    I follow the link you posted, it helps me solve my issue.Thank you very much for your help.
    Have a nice day.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need advice regarding inter-process communication.
    By Absurd in forum C Programming
    Replies: 7
    Last Post: 04-27-2015, 05:12 PM
  2. Inter process communication
    By Amitesh93 in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-02-2014, 07:07 AM
  3. Advice on inter-process communication
    By erupter in forum C Programming
    Replies: 8
    Last Post: 08-10-2012, 01:31 PM
  4. C program for Inter Process communication ( Tx and RX)
    By hiharsh in forum C Programming
    Replies: 3
    Last Post: 05-03-2007, 10:23 AM
  5. Inter-Process Communication in Windows
    By JBravo in forum C++ Programming
    Replies: 3
    Last Post: 03-11-2002, 10:55 AM

Tags for this Thread