who|sort实现
生活随笔
收集整理的這篇文章主要介紹了
who|sort实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
who|sort實現
宗旨:技術的學習是有限的,分享的精神是無限的。
? ? ? ? 創建無名管道,執行who命令的進程將輸出重定向到管道的寫端;執行sort命令的進程將輸入重定向到管道的讀端。即who的輸出連接到sort的輸入。
#include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <fcntl.h> #include <sys/wait.h> #include <stdlib.h>#define PIPE_SIZE 128int main(void) {int fds[2];if(pipe(fds) == -1){perror("pipe");exit(EXIT_FAILURE);}if(fork() == 0){char buf[PIPE_SIZE];dup2(fds[0], 0);close(fds[1]);execlp("sort", "sort", (char *)0);}else{if(fork() == 0){dup2(fds[1], 1);close(fds[0]);execlp("who", "who", (char *)0);}else{close(fds[0]);close(fds[1]);wait(NULL);wait(NULL);}}return 0; }總結
以上是生活随笔為你收集整理的who|sort实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 域名服务器的配置文档,dns域名服务器的
- 下一篇: android默认壁纸的历史,Andro