![](/img/trans.png)
[英]Controlling a program on one Raspberry Pi from a separate Raspberry Pi c++
[英]Simplest IPC from one Linux app to another in C++ on raspberry pi
我需要從 RPi 上運行的一個 C++ 應用程序到另一個應用程序的最簡單、最可靠的 IPC 方法。
我要做的就是從一個應用程序向另一個應用程序發送 40 個字符的字符串消息
第一個應用程序在啟動時作為服務運行,另一個應用程序稍后啟動,經常退出並重新啟動以進行調試
第二個應用程序的頻繁調試是導致我迄今為止嘗試過的 IPC 出現問題的原因
我嘗試了大約 3 種不同的方法,這里是它們失敗的地方:
File FIFO,問題是一個程序掛起,而另一個程序正在寫入文件
共享內存:無法在一個線程上初始化並從另一個線程讀取。 在調試時頻繁退出導致 GDB 崩潰, the following GDB command is taking too long to complete -stack-list-frames --thread 1
帶有 localhost 的 UDP 套接字 - 與上述相同的問題,加上不正確的退出阻塞了套接字,迫使我重新啟動設備
非阻塞管道 - 在接收過程中沒有收到任何消息
我還能嘗試什么? 我不想獲得 DBus 庫,對於這個應用程序來說似乎太復雜了。
任何簡單的服務器和客戶端代碼或指向它的鏈接都會有所幫助
這是我的非阻塞管道代碼,它對我不起作用,我假設它是因為我沒有引用從一個應用程序到另一個應用程序的管道
代碼來自這里: 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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.