日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > linux >内容正文

linux

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

發(fā)布時(shí)間:2025/3/15 linux 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux 管道交互,Linux C:具有独立读写命名管道的“交互式会话”? 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

我正在嘗試使用“使用命名管道的進(jìn)程間通信簡(jiǎn)介 - 使用命名管道的全雙工通信”,link;特別是fd_server.c(包括如下供參考)Linux C:具有獨(dú)立讀寫(xiě)命名管道的“交互式會(huì)話(huà)”?

這是我的信息,并編譯行:

:~$ 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創(chuàng)建兩個(gè)命名管道,一個(gè)用于讀取,一個(gè)用于寫(xiě)入。什么人可以做的,就是:在一個(gè)終端,運(yùn)行在服務(wù)器和讀取(通過(guò)cat)其寫(xiě)入管道:

:~$ ./fd_server & 2>/dev/null

[1] 11354

:~$ cat /tmp/np2

,并在另一個(gè),寫(xiě)(使用echo)到服務(wù)器的讀取管道:

:~$ echo "heeellloooo" > /tmp/np1

回到第一終端,人們可以看到:

:~$ cat /tmp/np2

HEEELLLOOOO

0[1]+ Exit 13 ./fd_server 2> /dev/null

我想什么做的,是讓不大不小的“互動(dòng)”(或“殼”類(lèi)似的)會(huì)議;也就是說(shuō),服務(wù)器像往常一樣運(yùn)行,但不是運(yùn)行cat和echo,我想使用類(lèi)似的屏幕。我的意思是,屏幕可以稱(chēng)為screen /dev/ttyS0 38400,然后它進(jìn)行一種交互式會(huì)話(huà),在終端中鍵入的內(nèi)容將傳遞到/dev/ttyS0,并將其響應(yīng)寫(xiě)入終端。現(xiàn)在,當(dāng)然,我不能使用screen,因?yàn)樵谖业那闆r下,程序有兩個(gè)單獨(dú)的節(jié)點(diǎn),并且據(jù)我所知,screen只能指一個(gè)。

如何在這種情況下實(shí)現(xiàn)這種“交互式”會(huì)話(huà)(使用兩個(gè)獨(dú)立的讀/寫(xiě)管道)?下面

代碼:

#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));

}

編輯:

權(quán),只是為了澄清 - 看來(lái)我發(fā)現(xiàn)了一個(gè)document討論非常類(lèi)似的東西,它是 - 腳本的修改有(” 對(duì)于例如,下面的腳本配置設(shè)備并啟動(dòng)一個(gè)后臺(tái)進(jìn)程,將從串行設(shè)備接收到的所有數(shù)據(jù)復(fù)制到標(biāo)準(zhǔn)輸出...“)為以下程序:

# 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

所以,保存腳本說(shuō)starter.sh和調(diào)用它,與下一屆會(huì)議的結(jié)果:

$ ./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

這是我會(huì)叫“交互會(huì)話(huà)”(忽略調(diào)試語(yǔ)句) - 服務(wù)器等待我輸入命令;它在接收到一個(gè)命令后給出它的輸出(在這種情況下,它在第一個(gè)命令之后退出,起始腳本也一樣)。除此之外,我想沒(méi)有緩沖輸入,但逐字發(fā)送(這意味著上面的會(huì)話(huà)應(yīng)該在第一次按鍵后退出,并且只打印出單個(gè)字母 - 這是我期望raw有幫助,但它不是:它只是殺死反應(yīng)既輸入和按Ctrl - ? :))

我只是游蕩,如果已經(jīng)有一個(gè)現(xiàn)有的命令(對(duì)于串行設(shè)備類(lèi)似于screen,我猜)會(huì)接受兩個(gè)這樣的命名管道作為參數(shù),并通過(guò)它們建立像會(huì)話(huà)那樣的“終端”或“shell”;或者我將不得不使用上面的腳本和/或編程自己的“客戶(hù)端”,它將起到終端的作用。

2010-05-06

sdaau

+0

使用Ctrl + K將代碼縮進(jìn)四個(gè)空格,這將創(chuàng)建一個(gè)語(yǔ)法高亮的代碼塊。我為你做了,停止編輯。 ;-) –

2010-05-06 13:12:45

+0

謝謝,約翰Kugelman,建議和編輯:) –

2010-05-06 13:18:13

總結(jié)

以上是生活随笔為你收集整理的linux 管道交互,Linux C:具有独立读写命名管道的“交互式会话”?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。