管道socket什么意思_pipe是什么意思

管道socket什么意思_pipe是什么意思在看Android输入系统的时候,第一次看到socketpair,发现和管道非常相似。唯他们的区别就是socketpair,默认支持全双工,而pipe是半双工的。他们一样只能用在父子进程或者线程之间通信。下面分别以socketpair和管道实现全双工通信。管道实现线程间全双工通信#include#include#include#

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

在看Android 输入系统的时候,第一次看到socketpair,发现和管道非常相似。唯他们的区别就是socketpair,默认支持全双工,而pipe是半双工的。他们一样只能用在父子进程或者线程之间通信。

下面分别以socketpair和管道实现全双工通信。

管道实现线程间全双工通信

#include<stdio.h>
#include<pthread.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>

#define SIZE 1024

int fd1[2],fd2[2]; //fd1[0]:read,  fd1[1]:write

void *func_thread1(void *arg)
{
    char buf[SIZE] = {0};
    int cnt = 0;
    while(1)
    {
        sprintf(buf,"hello main %d\n",cnt++);
        write(fd1[1],buf,strlen(buf));
        int len = read(fd2[0],buf,SIZE);
        buf[len] = '\0';
        printf("%s",buf);
        bzero(buf,SIZE);
        sleep(3);
    }
    return NULL;
}

int main(int agrc,char**argv)
{
    pthread_t thread1_t;

    /*1. create pipe*/
    pipe(fd1);
    pipe(fd2);
    /*2. create thread1*/
    pthread_create(&thread1_t, NULL,
                          func_thread1, NULL);
    char buf[SIZE] = {0};
    int cnt = 0;
    char * p = buf;
    printf("buf[SIZE] sizeof:%d, strlen:%d\n",sizeof(buf),strlen(buf));
    printf(" char * p sizeof:%d, strlen:%d\n",sizeof(p),strlen(p));
    while(1){
        int len = read(fd1[0],buf,SIZE);
        buf[len] = '\0';
        printf("%s",buf);
        bzero(buf,SIZE);
        sprintf(buf,"hello thread %d\n",cnt++);
        write(fd2[1],buf,strlen(buf));  
        sleep(3);
    }
    return 0;
}

Socketpair实现线程间全双工通信

#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>

#define SIZE 1024

void *func_thread1(void *arg)
{
    char buf[SIZE] = {
  
  0};
    int cnt = 0;
    int fd = (int)arg;
    while(1)
    {
        sprintf(buf,"hello main %d\n",cnt++);
        write(fd,buf,strlen(buf));
        int len = read(fd,buf,SIZE);
        buf[len] = '\0';
        printf("%s",buf);
        bzero(buf,SIZE);
        sleep(3);
    }
    return NULL;
}

int main(int agrc,char**argv)
{
    int fd[2];
    pthread_t thread1_t;

    /*1. create socketpair*/
    int ret = socketpair(AF_UNIX,SOCK_STREAM,0,fd);
    if(ret < 0){
        perror("socketpair");
        exit(-1);
    }
    /*2. create thread1*/
    pthread_create(&thread1_t, NULL,
                          func_thread1, fd[1]);
    char buf[SIZE] = {
  
  0};
    int cnt = 0;
    char * p = buf;

    while(1){
        int len = read(fd[0],buf,SIZE);
        buf[len] = '\0';
        printf("%s",buf);
        bzero(buf,SIZE);
        sprintf(buf,"hello thread %d\n",cnt++);
        write(fd[0],buf,strlen(buf));   
        sleep(3);
    }
    return 0;
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/181914.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Tomcat之——宕机自动重启和每日定时启动tomcat

    Tomcat之——宕机自动重启和每日定时启动tomcat在项目后期维护中会遇到这样的情况,tomcat在内存溢出的时候就出现死机的情况和遇到长时间不响应,需要人工手动关闭和重启服务,针对这样的突发情况,希望程序能自动处理问题而不需要人工关于,所以才有了目前的需求。一、设置tomcat定时启动1,首先将tomcat注册为服务,先打开tomcat的bin目录下service.bat文件,修改下面的值,这是sevvice的注册名称和显示名称,一般

    2022年7月23日
  • js 判断数组中是否包含某个元素_前端数组包含某个元素

    js 判断数组中是否包含某个元素_前端数组包含某个元素Array.prototype.S=String.fromCharCode(2); Array.prototype.in_array=function(e){ varr=newRegExp(this.S+e+this.S); return(r.test(this.S+this.join(this.S)+this.S)); }; vararr=

    2022年10月19日
  • 数据库系统原理——概述「建议收藏」

    数据库系统原理——概述「建议收藏」穷则独善其身,达则兼济天下一.什么是数据库狭义:存储数据的仓库广义:可以对数据进行存储和管理的软件以及数据本身统称为数据库数据库是由表、关系、操作组成二.为什么需要数据库几乎所有的应用软件的后台都需要数据库数据存储数据占用空间小,容易持久保存数据库的内容是存储在硬盘上,掉电之后任然存在存储比较安全软件是加密的,只能通过DBMS打开容易维护和升级使用SQL语句方便操作数据数据库移植比较容易简化对数据的操作为将来学习Oracle做准备.

    2022年11月3日
  • sqlserver分页查询语句_学mysql还是sql server

    sqlserver分页查询语句_学mysql还是sql serversqlserver的四种分页方式 第一种:ROW_NUMBER()OVER()方式select*from(    select*,ROW_NUMBER()OVER(OrderbyArtistId)ASRowIdfromArtistModels  )asb   whereRowIdbetween10and20  —where…

    2022年10月21日
  • MySQL数据库高并发优化配置

    MySQL数据库高并发优化配置

    2021年10月15日
  • FilterDispatcher已被标注为过时解决办法 >>> FilterDispatcher <<< is deprecated!

    FilterDispatcher已被标注为过时解决办法 >>> FilterDispatcher <<< is deprecated!一些struts2的教程都是比较早的,当我们基于较新版本的struts2来实现代码的时候,往往会出现一些问题.比如这个警告:FilterDispatcherisdeprecated!在web.xml中的配置如下:struts2org.apache.struts2.dispatcher.FilterDispatcher但

    2022年8月16日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号