我正在尝试使用“使用命名管道的进程间通信简介 - 使用命名管道的全双工通信”,link;特别是fd_server.c(包括如下供参考)Linux C:具有独立读写命名管道的“交互式会话”?
这是我的信息,并编译行:
:~$ cat /etc/issue
Ubuntu 10.04 LTS \n \l
:~$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
:~$ gcc fd_server.c -o fd_server
fd_server.c创建两个命名管道,一个用于读取,一个用于写入。什么人可以做的,就是:在一个终端,运行在服务器和读取(通过cat)其写入管道:
:~$ ./fd_server & 2>/dev/null
[1] 11354
:~$ cat /tmp/np2
,并在另一个,写(使用echo)到服务器的读取管道:
:~$ echo "heeellloooo" > /tmp/np1
回到第一终端,人们可以看到:
:~$ cat /tmp/np2
HEEELLLOOOO
0[1]+ Exit 13 ./fd_server 2> /dev/null
我想什么做的,是让不大不小的“互动”(或“壳”类似的)会议;也就是说,服务器像往常一样运行,但不是运行cat和echo,我想使用类似的屏幕。我的意思是,屏幕可以称为screen /dev/ttyS0 38400,然后它进行一种交互式会话,在终端中键入的内容将传递到/dev/ttyS0,并将其响应写入终端。现在,当然,我不能使用screen,因为在我的情况下,程序有两个单独的节点,并且据我所知,screen只能指一个。
如何在这种情况下实现这种“交互式”会话(使用两个独立的读/写管道)?下面
代码:
#include
#include
#include
#include
#include
#include
//#include /* For name of the named-pipe */
#define NP1 "/tmp/np1"
#define NP2 "/tmp/np2"
#define MAX_BUF_SIZE 255
#include //exit
#include //strlen
int main(int argc, char *argv[])
{
int rdfd, wrfd, ret_val, count, numread;
char buf[MAX_BUF_SIZE];
/* Create the first named - pipe */
ret_val = mkfifo(NP1, 0666);
if ((ret_val == -1) && (errno != EEXIST)) {
perror("Error creating the named pipe");
exit (1);
}
ret_val = mkfifo(NP2, 0666);
if ((ret_val == -1) && (errno != EEXIST)) {
perror("Error creating the named pipe");
exit (1);
}
/* Open the first named pipe for reading */
rdfd = open(NP1, O_RDONLY);
/* Open the second named pipe for writing */
wrfd = open(NP2, O_WRONLY);
/* Read from the first pipe */
numread = read(rdfd, buf, MAX_BUF_SIZE);
buf[numread] = '0';
fprintf(stderr, "Full Duplex Server : Read From the pipe : %sn", buf);
/* Convert to the string to upper case */
count = 0;
while (count < numread) {
buf[count] = toupper(buf[count]);
count++;
}
/*
* Write the converted string back to the second
* pipe
*/
write(wrfd, buf, strlen(buf));
}
编辑:
权,只是为了澄清 - 看来我发现了一个document讨论非常类似的东西,它是 - 脚本的修改有(” 对于例如,下面的脚本配置设备并启动一个后台进程,将从串行设备接收到的所有数据复制到标准输出...“)为以下程序:
# stty raw #
(./fd_server 2>/dev/null;)&
bgPidS=$!
(cat < /tmp/np2 ;)&
bgPid=$!
# Read commands from user, send them to device
echo $(kill -0 $bgPidS 2>/dev/null ; echo $?)
while [ "$(kill -0 $bgPidS 2>/dev/null ; echo $?)" -eq "0" ] && read cmd; do
# redirect debug msgs to stderr, as here we're redirected to /tmp/np1
echo "$? - $bgPidS - $bgPid" >&2
echo "$cmd"
echo -e "\nproc: $(kill -0 $bgPidS 2>/dev/null ; echo $?)" >&2
done >/tmp/np1
echo OUT
# Terminate background read process - if they still exist
if [ "$(kill -0 $bgPid 2>/dev/null ; echo $?)" -eq "0" ] ;
then
kill $bgPid
fi
if [ "$(kill -0 $bgPidS 2>/dev/null ; echo $?)" -eq "0" ] ;
then
kill $bgPidS
fi
# stty cooked
所以,保存脚本说starter.sh和调用它,与下一届会议的结果:
$ ./starter.sh
0
i'm typing here and pressing [enter] at end
0 - 13496 - 13497
I'M TYPING HERE AND PRESSING [ENTER] AT END
0~�.N=�(�~� �����}����@������~� [garble]
proc: 0
OUT
这是我会叫“交互会话”(忽略调试语句) - 服务器等待我输入命令;它在接收到一个命令后给出它的输出(在这种情况下,它在第一个命令之后退出,起始脚本也一样)。除此之外,我想没有缓冲输入,但逐字发送(这意味着上面的会话应该在第一次按键后退出,并且只打印出单个字母 - 这是我期望raw有帮助,但它不是:它只是杀死反应既输入和按Ctrl - ç :))
我只是游荡,如果已经有一个现有的命令(对于串行设备类似于screen,我猜)会接受两个这样的命名管道作为参数,并通过它们建立像会话那样的“终端”或“shell”;或者我将不得不使用上面的脚本和/或编程自己的“客户端”,它将起到终端的作用。
2010-05-06
sdaau
+0
使用Ctrl + K将代码缩进四个空格,这将创建一个语法高亮的代码块。我为你做了,停止编辑。 ;-) –
2010-05-06 13:12:45
+0
谢谢,约翰Kugelman,建议和编辑:) –
2010-05-06 13:18:13