进程02--无名管道

进程间的通信(IPC)方式:
1.管道(有名管道FIFO,无名管道PIPE)
2.信号signal
3.system V-IPC之共享内存
4.system V-IPC之消息队列
5.system V-IPC之信号量
6.套接字


无名管道pipe


#include <sys/wait.h>
       #include <stdio.h>
       #include <stdlib.h>
       #include <unistd.h>
       #include <string.h>

       int
       main(int argc, char *argv[])
       {
           int pipefd[2];
           pid_t cpid;
           char buf;

           if (argc != 2) {
            fprintf(stderr, "Usage: %s \n", argv[0]);
            exit(EXIT_FAILURE);
           }

           if (pipe(pipefd) == -1) {
               perror("pipe");
               exit(EXIT_FAILURE);
           }
           cpid = fork();
           if (cpid == -1) {
               perror("fork");
               exit(EXIT_FAILURE);
           }

           if (cpid == 0) {    /* Child reads from pipe */
               close(pipefd[1]);          /* Close unused write end */

               while (read(pipefd[0], &buf, 1) > 0)
                   write(STDOUT_FILENO, &buf, 1);

               write(STDOUT_FILENO, "\n", 1);
               close(pipefd[0]);
               _exit(EXIT_SUCCESS);

           } else {            /* Parent writes argv[1] to pipe */
               close(pipefd[0]);          /* Close unused read end */
               write(pipefd[1], argv[1], strlen(argv[1]));
               close(pipefd[1]);          /* Reader will see EOF */
               wait(NULL);                /* Wait for child */
               exit(EXIT_SUCCESS);
           }
       }


父子进程之间通信使用管道
需要为:父进程-写,子进程-读;
子进程-写,父进程-读;
管道通信中,如果只有写端,没有读端,当管道写满时,写会阻塞
管道文件中,没有数据时,读会阻塞
无名管道–适用于父子进程间,没有亲缘关系的进程之间资源没有继承关系,使用的管道也是独立的不是同一个

例子1:
#include<sys/types.h>
#include<unistd.h>
#include<stdio.h>

int main(int agrc,char *argv[])
{
    
    int pipefd[2]={0};//r , w
//    int a=0,b=0,c=0,d=0,e=0,f=0;
    //创建无名管道
    int ret = pipe(pipefd);//这里决定pipefd为1X2数组,pipefd[0]--读;pipefd[1]--写
    printf("ret = %d\n",ret);
    if(ret != 0)
    {
        perror("pipe creat fail");
        return -1;
    }
    pid_t pid = fork();

    if(pid==0)
    {
        int b = 0;
        while(1)
        {
            //close(pipefd[1]);
            write(pipefd[1],"wold!",5);
            
           // printf("pid = %d\n",getpid());
            printf("b = %d\n",b++);
            
            char buff[1024];
            int size = read(pipefd[0],buff,1024);
            printf("子进程pipe size = %d , buff = %s\n",size,buff);
            sleep(1);
        }
    }

    if(pid >0)
    {
        int a =0;
        while(1)
        {
            close(pipefd[0]);
            write(pipefd[1],"hello",5);
            printf("a = %d\n",a++);
            
            //char buff[1024];
           // int size = read(pipefd[0],buff,1024);
            //printf("父进程pipe size = %d , buff = %s\n",size,buff);
            sleep(1);
        }
    }


    return 0;
}
例子2:
#include <stdio.h>  
#include <unistd.h>  
#include <string.h>  
#include <errno.h>  
int main()  
{  
    int fd[2];  
    int ret = pipe(fd); //创建无名管道
    if (ret == -1)  
    {  
        perror("pipe error\n");  
        return 1;  
    }  
    pid_t id = fork();  
    if (id == 0)  
    {//child  
        int i = 0;  
        close(fd[0]);  //关闭读
        char *child = "I am  child!";  
        while (i<5)  
        {  
            write(fd[1], child, strlen(child) + 1);  //写入管道
            sleep(2);  
            i++;  
        }  
    }  
    else if (id>0)  
    {//father  
        close(fd[1]);  //关闭写
        char msg[100];  
        int j = 0;  
        while (j<5)  
        {  
            memset(msg,'\0',sizeof(msg));  
            ssize_t s = read(fd[0], msg, sizeof(msg));  //从管道读入
            if (s>0)  
            {  
                msg[s - 1] = '\0';  
            }  
            printf("%s\n", msg);  
            j++;  
        }  
    }  
    else  
    {//error  
        perror("fork error\n");  
        return 2;  
    }  
    return  0;  
}

你可能感兴趣的:(c语言,开发语言)