Linux管道編程執行個體,linux編程執行個體

來源:互聯網
上載者:User

Linux管道編程執行個體,linux編程執行個體

#include <unistd.h>#include <signal.h>#include <stdio.h>char parent[] = "a message from parrent";char child[] = "a message from child";main (){    int chan1[2], chan2[2];    int pid;    char buf[100];    pipe(chan1);    pipe(chan2);    pid=fork();    if(pid > 0)    {        close(chan1[0]);        close(chan2[1]);        write(chan1[1], parent, sizeof(parent));        close(chan1[1]);        read(chan2[0],buf, 100);        printf("%s\n", buf);        close(chan2[0]);    }    else if(pid == 0)    {        close(chan1[1]);        close(chan2[0]);        read(chan1[0], buf, 100);        printf("%s\n", buf);        write(chan2[1], child, sizeof(child));        close(chan2[1]);        close(chan1[0]);    }}





linux管道編程問題

就是C語言的
 
linux管道編程 100解

//具體不懂得地方你晚上再問我吧
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<fcntl.h>
#include<string.h>

int main(int argc, char *argv) {
int fd[2];
int len;
pid_t pid;
char filename[10];
char childbuf[10];
if (pipe(fd) < 0) {
perror("pipe error!");
exit(1);
}
if ((pid = fork()) < 0) {
perror("fork error!");
exit(1);
}
if (pid == 0) {

close(fd[1]);
len = read(fd[0], childbuf, 100);
childbuf[len] = '\0';
printf("%s\n", childbuf);
if (execlp("cat", "cat", childbuf, (char*) 0) < 0) {
perror("exec error!");
exit(1);
}
} else {
close(fd[0]);
printf("請輸入檔案名稱\n");
scanf("%s", filename);
write(fd[1], filename, strlen(filename));
waitpid(pid, NULL, 0);
return 0;
}
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.