linux 管道交互,Linux C:具有独立读写命名管道的“交互式会话”?

news/2024/7/4 11:42:51

我正在尝试使用“使用命名管道的进程间通信简介 - 使用命名管道的全双工通信”,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


http://www.niftyadmin.cn/n/4390365.html

相关文章

linux掌握物理页面的分配和回收,系统软件课程设计指导书-2010-12

2、本次课程设计要求阅读的Linux源代码版本为2.4.18,其他版本无效&#xff1b;3、结合操作系统基本原理进行代码分析&#xff0c;并进行详细分析和完整注释&#xff0c;注释越详细&#xff0c;成绩越好&#xff1b;5、设计型题目要按照要求完成全部算法6、一定要充分地考虑个人…

找不到linux32位的,找不到crtn。o,連接64位系統上的32位代碼

Im attempting to assemble some 32-bit code using NASM and GCC on a 64-bit system. I use the following two commands我正在嘗試在64位系統上使用NASM和GCC來組裝一些32位的代碼。我使用以下兩個命令nasm -f elf32 -g -F stabs coc.asmgcc -m32 -o coc coc.oNASM appears …

python yield的解释

链接地址&#xff1a; http://pyzh.readthedocs.io/en/latest/the-python-yield-keyword-explained.html https://www.jianshu.com/p/d09778f4e055

springsecurity中session失效后怎样处理_结合Spring Security进行web应用会话安全管理

在本文中&#xff0c;将为大家说明如何结合Spring Security 和Spring Session管理web应用的会话。一、Spring Security创建使用session的方法Spring Security提供4种方式精确的控制会话的创建&#xff1a;always&#xff1a;如果当前请求没有session存在&#xff0c;Spring Sec…

linux cat最新,linux cat

Linux命令总结—cat命令(1)命令功能cat命令用于将多个文件连接&#xff0c;并将结果通过标准输出显示出来。(2)命令语法cat(选项) (参数)(3)选项说明-n或-number&#xff1a;由1开始对所有输出的行数编号&#xff1b;-b或—number-nonblank&#xff1a;和-n相似&#xff0c;只不…

(四)mybatis缓存、事务、插件的基本知识

mybatis缓存、事务、插件的基础 一、缓存 &#xff08;一&#xff09;一级缓存与二级缓存 一级缓存 为了获得更好的性能&#xff0c;最重要的就是一级缓存。每个session对象维持一个一级缓存&#xff0c;session对象创建时缓存创建&#xff0c;session对象释放时缓存销毁。 缓存…

c语言typedef作用,C语言typedef的使用

C语言typedef的使用typedef 关键字能帮助你简化复杂的定义并让你的代码简洁可靠&#xff0c;当然&#xff0c;可靠这一点我还是持保留态度&#xff0c;因人而异吧。具体是如何使用呢&#xff1f;以下仅供参考&#xff01;具体方法如下&#xff1a;C 语言提供 typedef 关键字&am…

mysql时间戳14小时_SpringBoot时间戳与MySql数据库记录相差14小时排错

项目中遇到存储的时间戳与真实时间相差14小时的现象,以下为解决步骤.问题CREATE TABLE incident (id int(11) NOT NULL AUTO_INCREMENT,created_time timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,recovery_time timestamp NULL DEFAULT NULL,PRIMARY KEY (id)) ENGINEInnoDB…