美文网首页
Linux-C-day-3-进程间通信--FIFO/套接字

Linux-C-day-3-进程间通信--FIFO/套接字

作者: 秋风弄影 | 来源:发表于2017-06-05 15:55 被阅读0次

    FIFIO管道

    通过命名管道通信,命名管道之间的通信读和写必须同时执行,否则就会阻塞,但是命名管道可以用于非亲缘进程间通信;
     创建命名管道:int mkfifo(pathname,mode),pathname表示文件路径,该文件必须不存在;mode表示文件模式;返回值0表示成功,否则表示失败;
     打开FIFO文件:int open(const char *path,int mode),pathname:文件路径,也就是已经创建的文件的路径;O_RDONLY:表示阻塞只读模式;O_RDONLY| O_NONBLOCK:表示非阻塞只读模式;O_WRONLY:表示阻塞只写模式;O_WRONLY | O_NONBLOCK:表示的是非阻塞只写模式;函数的返回值是-1时表示失败,否则返回正常的文件描述符;和文件的操作是相同的;

    FIFO管道的创建

    fifocreate.c:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    
    int main(){
        if(-1 == mkfifo("/tmp/test",0644)){
            perror("mkfifo error");
            return 1;
        }
    }
    
    FIFO管道的读操作

    fiforead.c:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    int main(){
        int fd = open("/tmp/test",O_RDONLY);
        if(-1 == fd){
            perror("open error");
            return 1;
        }
        char buf[BUFSIZ];
        bzero(buf,BUFSIZ);
        read(fd,buf,BUFSIZ);
        printf("read:%s\n",buf);
    }
    
    FIFO管道的写操作

    fifowrite.c:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    
    int main(){
        int fd = open("/tmp/test",O_WRONLY|O_NONBLOCK);
        if(-1 == fd){
            perror("open error");
            return 1;
        }
        char str[] = "Hello fifo";
        write(fd,str,sizeof(str));
        printf("write:%s\n",str);
    }
    
    FIFO管道的删除操作

    fiform.c:

    #include <unistd.h>
    
    int main(){
        unlink("/tmp/test");
    }
    
    使用管道实现CS模型

    C-S服务模型的实现

    使用单个管道实现C-S模型的程序,getoptin.c缺点是server没有启动时,client自己写,自己读取;
    client.c:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <fcntl.h>
    
    int main(int argc,char* argv[]){
        mkfifo("/tmp/cs",0644);
        int fifofd = open("/tmp/cs",O_RDWR);
        char buf[BUFSIZ];
        write(fifofd,argv[1],strlen(argv[1])+1);    
        printf("%d write file:%s",getpid(),argv[1]);
        //sleep(1);
        read(fifofd,buf,BUFSIZ);
        printf("%d client read:\n%s",getpid(),buf);
    }
    

    server.c:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <fcntl.h>
    
    int main(int argc,char* argv[]){
        mkfifo("/tmp/cs",0644);
        int fifofd = open("/tmp/cs",O_RDWR);
        char buf[BUFSIZ];
        read(fifofd,buf,BUFSIZ);
        printf("%d file path:%s\n",getpid(),buf);
        int fd = open(buf,O_RDONLY);
        bzero(buf,BUFSIZ);
        read(fd,buf,BUFSIZ);
        write(fifofd,buf,BUFSIZ);
        close(fifofd);
    }
    

    使用两个管道实现CS模型
    client02.c:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <fcntl.h>
    
    int main(int argc,char* argv[]){
        mkfifo("/tmp/cs",0644);
        mkfifo("/tmp/cs1",0644);
        int fifofd = open("/tmp/cs",O_WRONLY);
        char buf[BUFSIZ];
        write(fifofd,argv[1],strlen(argv[1])+1);    
        printf("%d write file:%s",getpid(),argv[1]);
        //sleep(1);
        int fifofd1 = open("/tmp/cs1",O_RDONLY);
        read(fifofd1,buf,BUFSIZ);
        printf("%d client read:\n%s",getpid(),buf);
    }
    

    server02.c:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <fcntl.h>
    
    int main(int argc,char* argv[]){
        mkfifo("/tmp/cs",0644);
        int fifofd = open("/tmp/cs",O_RDONLY);
        char buf[BUFSIZ];
        read(fifofd,buf,BUFSIZ);
        printf("%d file path:%s\n",getpid(),buf);
        int fd = open(buf,O_RDONLY);
        bzero(buf,BUFSIZ);
        read(fd,buf,BUFSIZ);
    
        mkfifo("/tmp/cs1",0644);
        int fifofd1= open("/tmp/cs1",O_WRONLY);
        write(fifofd1,buf,BUFSIZ);
        close(fifofd);
        close(fifofd1);
    }
    

    使用双匿名管道实现CS模型
    client-server01.c

    
    

    使用单个管道实现CS模型
    client-server03.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <fcntl.h>
    
    int main(int argc,char* argv[]){
        mkfifo("/tmp/cs",0644);
        int fifofd = open("/tmp/cs",O_RDWR);
        char buf[BUFSIZ];
        if(fork()){// parent
            write(fifofd,argv[1],strlen(argv[1])+1);    
            printf("write file:%s",argv[1]);
            //sleep(1);
            read(fifofd,buf,BUFSIZ);
            printf("client read:\n%s",buf);
        }else{// child
            read(fifofd,buf,BUFSIZ);
            printf("file path:%s\n",buf);
            int fd = open(buf,O_RDONLY);
            bzero(buf,BUFSIZ);
            read(fd,buf,BUFSIZ);
            write(fifofd,buf,BUFSIZ);
        //  read(pfd[1],buf,BUFSIZ);
        }
        close(fifofd);
        unlink("/tmp/cs");
    }
    

    进程间通信--套接字

    套接字是一种进程间全双工通信模型,套接字的创建使用int socketpair(int domain,int type,int protocol,int sv[2]);domain:表示的是套接口的域,AF_LOCAL表示本地域;AF_UNIX 表示的是跨网络域;type:表示的是套接口的乐行,SOCK_STREAM,SOCK_DGRAM;protocol:表示的是协议,必须是0;sv:表示的是文件描述符的指针,s[0],s[1],都可以用来读写;返回值0表示成功,-1表示出错;如果需要进行关闭则两个都需要进行关闭:close(sv[0]);close(sv[1]);
    创建套接字的一个实例:
    socketpair.c:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <fcntl.h>
    
    int main(){
        int sv[2];
        if(-1 == socketpair(AF_LOCAL,SOCK_STREAM,0,sv)){
            perror("socketpair error");
            return 1;
        }
        printf("%d %d",sv[0],sv[1]);
    
        char str[]="Hello sockerpair\n";
        write(sv[0],str,sizeof(str));
        char buf[BUFSIZ];
        
        //fcntl(sv[0],F_SETFL,O_NONBLOCK);
        read(sv[0],buf,BUFSIZ);
        printf("read %s\n",buf);
        
        bzero(buf,BUFSIZ);
        read(sv[0],buf,BUFSIZ);
        printf("read %s\n",buf);
    }
    

    无数据写入时,读取阻塞的一个程序
    socketpair02.c:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <fcntl.h>
    
    int main(){
        int sv[2];
        if(-1 == socketpair(AF_LOCAL,SOCK_STREAM,0,sv)){
            perror("socketpair error");
            return 1;
        }
        printf("%d %d\n",sv[0],sv[1]);
        if(fork()){
                char str[]="Hello sockerpair\n";
                write(sv[0],str,sizeof(str));
        }else{  
                char buf[BUFSIZ];
                read(sv[1],buf,BUFSIZ);
                printf("read %s\n",buf);
    
                bzero(buf,BUFSIZ);
                read(sv[1],buf,BUFSIZ);
                printf("read %s\n",buf);
        }   
    }
    

    无数据写入时,阻塞取消的一种写法
    socketpair03.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <fcntl.h>
    
    int main(){
        int sv[2];
        if(-1 == socketpair(AF_LOCAL,SOCK_STREAM,0,sv)){
            perror("socketpair error");
            return 1;
        }
        printf("%d %d\n",sv[0],sv[1]);
        if(fork()){
                char str[]="Hello sockerpair\n";
                write(sv[0],str,sizeof(str));
        }else{  
                char buf[BUFSIZ];
                read(sv[1],buf,BUFSIZ);
                printf("read %s\n",buf);
    
                fcntl(sv[1],F_SETFL,O_NONBLOCK);
                bzero(buf,BUFSIZ);
                read(sv[1],buf,BUFSIZ);
                printf("read %s\n",buf);
        }   
    }
    
    使用套接字实现CS模型

    client-server01.c

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <sys/socket.h>
    #include <fcntl.h>
    
    int main(int argc,char* argv[]){
        int sv[2];
        char buf[BUFSIZ];
        socketpair(AF_LOCAL,SOCK_STREAM,0,sv);
        if(fork()){// parent
            close(sv[0]);
            write(sv[1],argv[1],strlen(argv[1])+1); 
            read(sv[1],buf,BUFSIZ);
            printf("client read:\n%s",buf);
        }else{// child
            close(sv[1]);
            read(sv[0],buf,BUFSIZ);
            printf("file path:%s\n",buf);
            int fd = open(buf,O_RDONLY);
            bzero(buf,BUFSIZ);
            read(fd,buf,BUFSIZ);
            write(sv[0],buf,BUFSIZ);
        //  read(sv[1],buf,BUFSIZ);
        }
    }
    

    相关文章

      网友评论

          本文标题:Linux-C-day-3-进程间通信--FIFO/套接字

          本文链接:https://www.haomeiwen.com/subject/kombfxtx.html