简体   繁体   中英

Simplest IPC from one Linux app to another in C++ on raspberry pi

I need the simplest most reliable IPC method from one C++ app running on the RPi to another app.

All I'm trying to do is send a string message of 40 characters from one app to another

The first app is running as a service on boot, the other app is started at a later time and is frequently exited and restarted for debugging

The frequent debugging for the second app is whats causing problems with the IPCs I've tried so far

I've tried about 3 different methods and here is where they failed:

  1. File FIFO, the problem is one program hangs while the other program is writing to the file

  2. Shared memory: cannot initialize on one thread and read from another thread. Also frequent exiting while debugging causing GDB crashes with the following GDB command is taking too long to complete -stack-list-frames --thread 1

  3. UDP socket with localhost - same issue as above, plus improper exits block the socket, forcing me to reboot device

  4. Non blocking pipe - not getting any messages on the receiving process

What else can I try? I dont want to get the DBus library, seems too complex for this application.

Any simple server and client code or a link to it would be helpful

Here is my non-blockign pipe code, that doesnt work for me, I assume its because I dont have a reference to the pipe from one app to the other

Code sourced from here: https://www.geeksforgeeks.org/non-blocking-io-with-pipes-in-c/

char* msg1 = "hello"; 
char* msg2 = "bye !!"; 
int p[2], i;

bool InitClient()
{   
    // error checking for pipe 
    if(pipe(p) < 0) 
        exit(1); 

    // error checking for fcntl 
    if(fcntl(p[0], F_SETFL, O_NONBLOCK) < 0) 
        exit(2); 

    //Read
    int nread; 
    char buf[MSGSIZE]; 

    // write link 
    close(p[1]); 

    while (1) { 

        // read call if return -1 then pipe is 
        // empty because of fcntl 
        nread = read(p[0], buf, MSGSIZE); 
        switch (nread) { 
        case -1: 

            // case -1 means pipe is empty and errono 
            // set EAGAIN 
            if(errno == EAGAIN) { 
                printf("(pipe empty)\n"); 
                sleep(1); 
                break; 
            } 

        default: 

            // text read 
            // by default return no. of bytes 
            // which read call read at that time 
            printf("MSG = % s\n", buf); 
        } 
    } 

    return true;
}   

bool InitServer()
{      
    // error checking for pipe 
    if(pipe(p) < 0) 
        exit(1); 

    // error checking for fcntl 
    if(fcntl(p[0], F_SETFL, O_NONBLOCK) < 0) 
        exit(2); 


    //Write
    // read link 
    close(p[0]); 

    // write 3 times "hello" in 3 second interval 
    for(i = 0 ; i < 3000000000 ; i++) { 
        write(p[0], msg1, MSGSIZE); 
        sleep(3); 
    } 

    // write "bye" one times 
    write(p[0], msg2, MSGSIZE); 

    return true;
}       

Please consider ZeroMQ

https://zeromq.org/

It is lightweight and has wrapper for all major programming languages.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM