Linux_ pipe 匿名管道demo

main1.c

#include 
#include 
#include 

int main(void) 
{
    int fd[2];
    int ret;
    char buff1[1024];
    char buff2[1024];

    ret = pipe(fd);
    if (ret !=0) {
        printf("create pipe failed!\n");
        exit(1);
    }

    strcpy(buff1, "Hello!");
    write(fd[1], buff1, strlen(buff1)); 
    printf("send information:%s\n", buff1);

    bzero(buff2, sizeof(buff2));
    read(fd[0], buff2, sizeof(buff2));
    printf("received information:%s\n", buff2);

    return 0;   
}

main2.c

#include 
#include 
#include 

int main(void) 
{
    int fd[2];
    int ret;
    char buff1[1024];
    char buff2[1024];
    pid_t pd;

    ret = pipe(fd);
    if (ret !=0) {
        printf("create pipe failed!\n");
        exit(1);
    }

    pd = fork();
    if (pd == -1) {
        printf("fork error!\n");
        exit(1);
    } else if (pd == 0) {
        bzero(buff2, sizeof(buff2));
        read(fd[0], buff2, sizeof(buff2));
        printf("process(%d) received information:%s\n", getpid(), buff2);
    } else {
        strcpy(buff1, "Hello!");
        write(fd[1], buff1, strlen(buff1)); 
        printf("process(%d) send information:%s\n", getpid(), buff1);
    }

    if (pd > 0) {
        wait();
    }

    return 0;   
}

main3.c

#include 
#include 
#include 
#include 

int main(void) 
{
    int fd[2];
    int ret;
    char buff1[1024];
    char buff2[1024];
    pid_t pd;

    ret = pipe(fd);
    if (ret !=0) {
        printf("create pipe failed!\n");
        exit(1);
    }

    pd = fork();
    if (pd == -1) {
        printf("fork error!\n");
        exit(1);
    } else if (pd == 0) {
        //bzero(buff2, sizeof(buff2));
        sprintf(buff2, "%d", fd[0]);
        execl("main3_2", "main3_2", buff2, 0);
        printf("execl error!\n");
        exit(1);
    } else {
        strcpy(buff1, "Hello!");
        write(fd[1], buff1, strlen(buff1)); 
        printf("process(%d) send information:%s\n", getpid(), buff1);
    }

    if (pd > 0) {
        wait();
    }

    return 0;   
}

main4.c

#include 
#include 
#include 

int main(void) 
{
    int fd[2];
    int ret;
    char buff1[1024];
    char buff2[1024];
    pid_t pd;

    ret = pipe(fd);
    if (ret !=0) {
        printf("create pipe failed!\n");
        exit(1);
    }

    pd = fork();
    if (pd == -1) {
        printf("fork error!\n");
        exit(1);
    } else if (pd == 0) {
        close(fd[1]);
        bzero(buff2, sizeof(buff2));
        read(fd[0], buff2, sizeof(buff2));
        printf("process(%d) received information:%s\n", getpid(), buff2);
    } else {
        strcpy(buff1, "Hello!");
        close (fd[0]);
        write(fd[1], buff1, strlen(buff1)); 
        printf("process(%d) send information:%s\n", getpid(), buff1);

        close (fd[1]);      
    }

    if (pd > 0) {
        wait();
    }

    return 0;   
}

main5.c

#include 
#include 
#include 

#define CMD_EXIT  "exit"

int main(void) 
{
    int fd[2];
    int ret;
    char buff[80];
    pid_t pd;

    ret = pipe(fd);
    if (ret !=0) {
        printf("create pipe failed!\n");
        exit(1);
    }

    pd = fork();
    if (pd == -1) {
        printf("fork error!\n");
        exit(1);
    } else if (pd == 0) {
        close(fd[1]);

        while (1) {
            bzero(buff, sizeof(buff));
            if (read(fd[0], buff, sizeof(buff)) == 0) {
                break;
            }
            printf("received information:%s\n", buff);
        }
        printf("receive end!\n");
        close(fd[0]);
    } else {
        close (fd[0]);

        while(1) {
            scanf("%s", buff);
            if (strcmp(buff, CMD_EXIT) == 0) {
                break;
            }

            write(fd[1], buff, strlen(buff));
        }       

        close (fd[1]);      
    }

    if (pd > 0) {
        wait();
    }

    return 0;   
}

main6.c

#include 
#include 
#include 
#include 

int main(void) 
{
    int fd[2];
    int ret;
    char buff1[1024];
    char buff2[1024];
    pid_t pd;

    ret = pipe(fd);
    if (ret !=0) {
        printf("create pipe failed!\n");
        exit(1);
    }

    pd = fork();
    if (pd == -1) {
        printf("fork error!\n");
        exit(1);
    } else if (pd == 0) {
        //bzero(buff2, sizeof(buff2));
        //sprintf(buff2, "%d", fd[0]);
        close(fd[1]);

        close(0);
        dup(fd[0]);
        close(fd[0]);

        execlp("od", "od", "-c", 0);
        printf("execl error!\n");
        exit(1);
    } else {
        close(fd[0]);

        strcpy(buff1, "Hello!");
        write(fd[1], buff1, strlen(buff1)); 

        close(fd[1]);
    }

    return 0;   
}

main7.c

#include 
#include 

#define BUFF_SIZE   1024

int main(void)
{
    FILE * file;
    char buff[BUFF_SIZE+1];
    int cnt;

    // system("ls -l > result.txt");
    file = popen("ls -l", "r");
    if (!file) {
        printf("fopen failed!\n");
        exit(1);
    }

    cnt = fread(buff, sizeof(char), BUFF_SIZE, file);
    if (cnt > 0) {
        buff[cnt] = '\0';
        printf("%s", buff);
    }   

    pclose(file);

    return 0;   
}

main8.c

#include 
#include 
#include 

#define BUFF_SIZE   1024

int main(void)
{
    FILE * file;
    char buff[BUFF_SIZE+1];
    int cnt;

    file = popen("./p2", "w");
    if (!file) {
        printf("fopen failed!\n");
        exit(1);
    }

    strcpy(buff, "hello world!");
    cnt = fwrite(buff, sizeof(char), strlen(buff), file);

    pclose(file);

    return 0;   
}

你可能感兴趣的:(Linux,linux,pipe,匿名管道)