惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

aimingoo的专栏
aimingoo的专栏
G
Google Developers Blog
B
Blog RSS Feed
A
About on SuperTechFans
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
Stack Overflow Blog
Stack Overflow Blog
C
Check Point Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Engineering at Meta
Engineering at Meta
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园 - 司徒正美
D
Docker
F
Fortinet All Blogs
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
H
Help Net Security
WordPress大学
WordPress大学
MyScale Blog
MyScale Blog
博客园 - Franky
人人都是产品经理
人人都是产品经理
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Blog — PlanetScale
Blog — PlanetScale
L
LangChain Blog

hwchiu learning note Blog

Kubernetes 怎麼計算 imageFS | hwchiu learning note Nginx Proxy_Pass 不會重新查詢 DNS | hwchiu learning note Multus 下如何透過 network policy 設定 | hwchiu learning note Linux Bridge MTU | hwchiu learning note Kubevirt 初體驗 | hwchiu learning note [MacOS ]隨手筆記 Sed 與 Rename 的使用 | hwchiu learning note Docusaurus 使用 blog mode 後連結一直反白的問題 | hwchiu learning note gcloud 切換帳號 | hwchiu learning note k8s 內安裝 redis-cluster | hwchiu learning note Helm Chart 中如何根據條件來動態安裝 Template 內的物件 | hwchiu learning note GCS 操作上注意事項 | hwchiu learning note istio 操作記錄 | hwchiu learning note terraform | hwchiu learning note Kubernetes GKE 維運上小筆記 | hwchiu learning note Git 修改 author/committer | hwchiu learning note GCP NAT 相關筆記 | hwchiu learning note gcloud ssh 到 GCP VM | hwchiu learning note CloudSQL 收費注意事項 | hwchiu learning note Loki 安裝上的參數調整以及 Ring 的問題除錯 | hwchiu learning note kustomize + helm | hwchiu learning note 觀測 K8s 內 OOM 事件 | hwchiu learning note GKE 上的 RBAC 筆記 | hwchiu learning note 本地產生 jwt token | hwchiu learning note ArgoCD 安裝筆記 | hwchiu learning note CircleCI Context 的使用 | hwchiu learning note 透過 GCP IAP Gateway 來保護 GKE 內的網站 | hwchiu learning note 閱讀筆記: 「SRE 的工作介绍」 | hwchiu learning note 閱讀筆記: 「DevOps is a failure」 | hwchiu learning note 閱讀筆記: 「面試人生 - 設計一個簡易的分散式 Job Scheduler」 | hwchiu learning note 閱讀筆記: 「Cloudflare 06/21 災後報告」 | hwchiu learning note
Linux下 pipe介紹 | hwchiu learning note
HungWei ChiuBlogger · 2013-03-29 · via hwchiu learning note Blog

#[User]

不論是bash,tcsh,又或者是windows的cmd,都有一種叫做PIPE的功能

能夠將兩個獨立的程式給串接起來,把前面程式的輸出當作下一個程式的輸入

擁有這個指令,就能將本來當一功能的程式給組合起來變成複雜的工具了

舉例來說,我想要知道我當前目錄下有多少個檔案

就可以使用ls跟wc兩個指令合作完成,

使用 ls | wc 就會將ls的結果(檔案列表)當作輸入傳給wc這隻程式,然後就可以輕鬆地算出當前目錄的檔案數量

或者是有時候想要搜尋某些特定的字串,都會使用grep這個指令,譬如想要搜尋某個特定使用者正在執行的所有程序

ps auxww | grep username

所以pipe對於系統管理來說,是個非常重要的概念,能夠將每個獨立細小的程式給串接起來完成複雜的工作。

#[程式設計]

在FreeBSD(linux)上,shell能夠辦得到這樣的功能,實際上是利用了kernel中pipe的功能,這邊就已linux kernel 3.5.4為架構。

在程式中,pipe的概念就是一個水管,這個水管有兩個端口,一端負責寫資料到pipe,一端負責將資料從pipe中讀出來,所以我們可以做個簡單的測試。

int main(){
int rand1,rand2;
int fd[2];// declare a two-d array, store file_descriptor of the pipe (two side)
// fd[0] mease read side, fd[1] means write side
pid_t pid;//child process的pid
pipe(fd); //call system call (pipe) to create a pipe
//use fork to create a child process
//child process will wrtie data to pipe, and parent will read data from pipe
//child process
if((pid=fork())==0){
srand(getpid());
close(fd[READ_END]);//child won't use read size, so close it
rand1=rand()%RANGE; //create random number
write(fd[WRITE_END],&rand1,sizeof(rand1)); //write to pipe
close(fd[WRITE_END]);//close write side when write over
printf("%d has been created In Child Process \n",rand1);
exit(1);
}
else if(pid>0){
srand(getpid());
close(fd[WRITE_END]);//parent won't use write size, so close it。
rand2=rand()%RANGE;//create random number
read(fd[READ_END],&rand1,sizeof(rand1));//read the data from pipe
printf("%d has been created In Parent Process \n",rand2);
wait();
printf("Parent Process calulate sum is :%d \n",rand1+rand2);
close(fd[READ_END]);//close read side
exit(1);
}
return 0;
}

執行結果: 8 has been created In Child Process

5 has been created In Parent Process

Parent Process calulate sum is :13


3 has been created In Child Process

3 has been created In Parent Process

Parent Process calulate sum is :6

實際上,如果想要對同個端口去進行寫跟讀的動作,是行不通的,乍看之下會覺得PIPE只是一個

buffer,放置資料而已,實際上在kernel中,pipe被視為是一個file,當我們呼叫pipe時,真正最後會

呼叫到do_pipe這個function,在這個function中,會針對pipe的兩個端口分別去設定

O_RDONLY;O_WRONLY的標籤,這樣的設定使得pipe的端口就真的一邊只能讀,一邊只能寫。

有空在來講述一下file_descriptor file file_operation三者的關係,以及到底 file,socket,pipe...等這些device到底在kernel中如何運作。