进程间通信系列3,进程间通信系列2

作者: 编程技术  发布:2019-09-23

linux 进程间通信种类2,使用UNIX_SOCKET

linux 进程间通讯种类3,使用socketpair,pipe

1,使用stream,落成进度间通信

1,使用socketpair,完结进程间通讯,是双向的。

2,使用DGRAM,实现进度间通讯

2,使用pipe,完毕进度间通讯

使用pipe关键点:fd[0]只能用来吸收接纳,fd[1]不得不用来发送,是单向的。

关键点:使用贰个一时的文件,举办新闻的互传。

  s_un.sun_family = AF_UNIX;  strcpy(s_un.sun_path, "/tmp/afunix_text");

3,使用pipe,用典型输入往里写。

使用stream,server端:

闷葫芦:在代码2里不写wait函数的话,父进程不能了事,可是在代码3里也绝非写wait函数,父进度却得以了结???

#include <stdio.h>#include <string.h>#include <unistd.h>#include <sys/socket.h>#include <sys/un.h>#define FILEPATH "/tmp/afunix_text"int main(){  int s0, sock;  sockaddr_un s_un;  sockaddr_un s_un_accept;  socklen_t addrlen;  s0 = socket(AF_UNIX, SOCK_STREAM, 0);  if(s0 < 0){    perror;    return 1;  }  s_un.sun_family = AF_UNIX;  strcpy(s_un.sun_path, FILEPATH);  if(bind(s0, (sockaddr*)&s_un, sizeof != 0){    perror;    return 1;  }  if(listen != 0){    perror;    return 1;  }  addrlen = sizeof(s_un_accept);  sock = accept(s0, (sockaddr*)&s_un_accept, &addrlen);  if(sock < 0){    perror;    return 1;  }  printf("after acceptn");  write(sock, "the msg is send from server", 27);  close;  close;  unlink;  return 0;}

github源代码

1,使用socketpair:

使用stream,client端:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <sys/socket.h>#include <wait.h>int main(){  int sv[2];  pid_t pid;  char buf[128];  memset(buf, 0, sizeof;  if(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) != 0){    perror("socketpair");    return 1;  }  pid = fork();  if(pid < 0){    perror;    return 1;  }  if{    close;    read(sv[1], buf, sizeof;    printf("child process : data from parant process [%s]n", buf);    exit;  }  else {    int status;    close;    write(sv[0], "HELLO", 5);    printf("parent process : child process id %dn", pid);    wait(&status);  }  return 0;}

github源代码

#include <stdio.h>#include <string.h>#include <unistd.h>#include <sys/socket.h>#include <sys/un.h>int main(){  int sock;  sockaddr_un s_un;  int n;  char buf[128];  sock = socket(AF_UNIX, SOCK_STREAM, 0);  if(sock < 0){    perror;    return 1;  }  s_un.sun_family = AF_UNIX;  strcpy(s_un.sun_path, "/tmp/afunix_text");  if(connect(sock, (sockaddr*)&s_un, sizeof != 0){    perror("connect");    return 1;  }  printf("after connectn");  memset(buf, 0, sizeof;  n = read(sock, buf, sizeof;  if{    perror;    return 1;  }  printf("%sn", buf);  close;  return 0;}

github源代码

2,使用pipe:

使用dgram,发送端:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <wait.h>int main(){  int p[2];  pid_t pid;  char buf[128];  memset(buf, 0, sizeof;  if != 0){    perror;    return 1;  }  pid = fork();  if(pid < 0){    perror;    return 1;  }  if{    close;    read(p[0], buf, sizeof;    printf("child process : data form parent process [%s]n", buf);    exit;  }  else{    close;    write(p[1], "aaaa", 4);    printf("parent process : child process is %dn", pid);    int status;    wait(&status);  }  return 0;}

github源代码

#include <stdio.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/un.h>int main(){  int sock;  sockaddr_un addr;  socklen_t addrlen;  sock = socket(AF_UNIX, SOCK_DGRAM, 0);  addr.sun_family = AF_UNIX;  strcpy(addr.sun_path, "/tmp/afu_dgram");  int n = sendto(sock, "HELLOn", 6, 0, (sockaddr*)&addr, sizeof;  printf("send datan");  close;  return 0;}

github源代码

3,使用pipe,用标准输入往里写。

使用dgram,接收端:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <wait.h>int main(){  int p[2];  pid_t pid;  char buf[1024];  memset(buf, 0, sizeof;  if != 0){    perror;    return 1;  }  pid = fork();  if(pid < 0){    perror;    return 1;  }  if{    printf("child process : my_id=%dn",getpid;    close;    //把标准输出给管道1了    dup2(p[1], fileno;    char *argv[ ]={"ls", "/home/ys/cpp/network"};    //利用ls命令,往标准输出里,输入文件夹里文件的的名字,标准输出又连接到了上面开的管道1里。    if(execve("/bin/ls", argv, NULL) < 0){      perror;      return 1;    }    exit;  }else{    int n;    FILE* filep;    close;    printf("parent process : child process id=%dn", pid);    //先打开管道1    filep = fdopen(p[0], "r");    if(filep == NULL){      perror;      return 1;    }    //再从管道1里读取数据    while(fgets(buf, sizeof, filep) != NULL){      printf("get:%sn", buf);    }    int status;    wait(&status);  }  return 0;}

github源代码

#include <stdio.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <sys/un.h>int main(){  int sock;  sockaddr_un addr;  socklen_t addrlen;  char buf[1024];  int n;  sock = socket(AF_UNIX, SOCK_DGRAM, 0);  addr.sun_family = AF_UNIX;  strcpy(addr.sun_path, "/tmp/afu_dgram");  bind(sock, (sockaddr*)&addr, sizeof;  while{    memset(buf, 0, sizeof;    n = recv(sock, buf, sizeof - 1, 0);    printf("recv:%sn", buf);  }  close;  return 0;}

github源代码

c/c++ 学习互助QQ群:877684253

图片 1

c/c++ 学习互助QQ群:877684253

图片 2

自己微信:xiaoshitou5854

作者微信:xiaoshitou5854

本文由贝博体育app发布于编程技术,转载请注明出处:进程间通信系列3,进程间通信系列2

关键词: