美文网首页
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/套接字

    FIFIO管道 通过命名管道通信,命名管道之间的通信读和写必须同时执行,否则就会阻塞,但是命名管道可以用于非亲缘进...

  • 8.ipc

    进程间通信 Linux中的进程间通信主要有:管道、FIFO、消息队列、信号量、共享存储以及网络IPC中的套接字。 ...

  • Linux 进程之间的通信方式

    linux使用的进程间通信方式 管道(pipe)、流管道(s_pipe)、无名管道(FIFO)、 套接字 sock...

  • Go:Unix域套接字

    关于同一个Linux主机上的进程之间的进程间通信(IPC)方式,有多个选择:例如FIFO、管道、共享内存、套接字等...

  • unix套接字

    unix套接字常识点 性能 很多时候,我们用本机进程间通信时,会使用本地套接字unix socket来通信。本地套...

  • linux下netlink的使用简介

    一、什么是netlink Netlink套接字是用以实现用户进程与内核进程通信的一种特殊的进程间通信(IPC) ,...

  • socket进程通信

    socket是通过网络来进行进程间通信的。socket也称为“套接字”,是网络通信中的概念,分为流式套接字和用户数...

  • 20170926 http服务和apache(一)

    Socket 套接字http服务介绍httpd介绍 一、Socket 套接字 在建立通信连接的每一端,进程间的传输...

  • socket简介

    什么是socket socket(简称 套接字) 是进程间通信的一种方式,它与其他进程间通信的一个主要不同是:它能...

  • 进程相关(四)--进程间通信(套接字)

    套接字 什么是套接字 TCP用主机的IP地址加上主机上的端口号作为TCP连接的端点,这种端点就叫做套接字(sock...

网友评论

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

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