linux下多线程通信(一)「建议收藏」

linux下多线程通信(一)「建议收藏」在linux下进行多线程编程,肯定会涉及到线程通信问题,本文主要分析pipe,即管道在多线之间通信实现。#include<unistd.h>intpipe(intfiledes[2]);返回值:成功,返回0,否则返回-1。参数数组包含pipe使用的两个文件的描述符。fd[0]:读管道,fd[1]:写管道两个线程之间通信简单实现,单向pipe_1.c在这里插入代码片…

大家好,又见面了,我是你们的朋友全栈君。

在linux下进行多线程编程,肯定会涉及到线程通信问题,本文主要分析pipe,即管道在多线之间通信实现。
#include<unistd.h>
int pipe(int filedes[2]);
返回值:成功,返回0,否则返回-1。
参数数组包含pipe使用的两个文件的描述符。fd[0]:读管道,fd[1]:写管道

两个线程之间通信简单实现,单向pipe_1.c
源码地址:https://github.com/jeremy505/multi-thread-communication

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
Thread *m_Threads;
static int threadcount = 1;
void* work_thread(void* argc)
{
  Thread* param = (Thread*) argc;
  printf("childthread_tid=%lu\n", param->tid);
  int contant = 0;
  //sleep(2);
  printf("childthread--read return %d\n",read(param->notifyReceiveFd, &contant, sizeof(int)));
  printf("childthread--read from pipe %d\n", contant);
}

int main(int argc, char** argv)
{
     //在主线程和子线程之间建立管道
    m_Threads = malloc(sizeof(Thread) * threadcount);
    int fds[2];
    if( pipe(fds) )
    {
      perror("create pipe error");
    }
    m_Threads[0].notifyReceiveFd = fds[0];
    pthread_create(&m_Threads[0].tid, NULL,  work_thread, (void*)&m_Threads[0]);
    printf("mainthread_tid=%lu\n", m_Threads[0].tid);
    int contant = 1;
    // sleep(2);
    printf("mainthread--write %d to pipe\n", contant);
    printf("mainthread--write return %d\n",write(fds[1], &contant, sizeof(int)));
    pthread_join(m_Threads[0].tid, NULL);
    close(fds[0]);
    close(fds[1]);
    return 0;
}

以上只是单向通信,即主线程向子线程写int型值1,子线程读到int型值1.
输出见下:
在这里插入图片描述
在这里插入图片描述
可见,输出与设想的一致,只是此时通信只是单向,如果要实现双向,最简单的办法就是再创建一组pipe,实现pipe_1_d.c如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
typedef struct __Thread
{
  pthread_t tid;    //线程的ID
  int notifyReceiveFd;  //管道的接收端
  int notifySendFd;   //管道的发送端
}Thread;

Thread *m_Threads;
static int threadcount = 1;
void* work_thread(void* argc)
{
  Thread* param = (Thread*) argc;
  printf("childthread_tid=%lu\n", param->tid);
  int contant = 0;
  //sleep(2);
  printf("childthread--read return %d\n",read(param->notifyReceiveFd, &contant, sizeof(int)));
  printf("childthread--read from pipe %d\n", contant);
  contant = 2;
  printf("childthread--write %d to pipe\n", contant);
  printf("childthread--write return %d\n",write(param->notifySendFd, &contant, sizeof(int)));
}

int main(int argc, char** argv)
{
     //在主线程和子线程之间建立管道
    m_Threads = malloc(sizeof(Thread) * threadcount);
    int fds[2];
    if( pipe(fds) )
    {
      perror("create pipe fds error");
    }
    int fds_1[2];
    if( pipe(fds_1) )
    {
      perror("create pipe fds_1 error");
    }
    m_Threads[0].notifyReceiveFd = fds[0];
    m_Threads[0].notifySendFd = fds_1[1];
     pthread_create(&m_Threads[0].tid, NULL,
                   work_thread, (void*)&m_Threads[0]);
    printf("mainthread_tid=%lu\n", m_Threads[0].tid);
    int contant = 1;
    // sleep(2);
    printf("mainthread--write %d to pipe\n", contant);
    printf("mainthread--write return %d\n",write(fds[1], &contant, sizeof(int)));
    printf("mainthread--read return %d\n",read(fds_1[0], &contant, sizeof(int)));
    printf("mainthread--read from pipe %d\n", contant);
    pthread_join(m_Threads[0].tid, NULL);
    close(fds[0]);
    close(fds[1]);
    close(fds_1[0]);
    close(fds_1[1]);
    }

另外创建一组pipe,主线读,子线程写。主线程先写1,然后阻塞等待子线程网管道中写值,子线程通过管道读到1之后往管道写2,此时管道有数据,主线程读取值2,输出如下:
在这里插入图片描述

以上只是简单的通过pipe线程之间进行同行,注意到读写都是阻塞的。如果不希望线程使用阻塞方式,一般会设置管道文件描述符为非阻塞,然后借助epoll或者select监听管道文件描述符读写事件。
线程之间通过pipe通信也可以应用到进程之间,在使用fork之后,管道描述符被拷贝了一份,所以父子进程必须关闭其中之一,假设父进程关闭读[close(fd[0])],子进程就要关闭写[close(fd[1])],,实现单向通信,反过来也是一样.
进程之间的通信,推荐一种方式使用共享内存,共享内存区是最快的IPC形式,此种方式也可在两个完全独立的程序之间进行数据传递,后续再详细介绍。
当然,进程以及线程之间的通信不止以上方法,还有使用socket,eventfd等。

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

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

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


相关推荐

  • 蓝牙脂肪秤模块测量原理

    蓝牙脂肪秤模块原理跟普通电子体重秤的原理差不多,都是利用压力传感器及芯片设计完成功能的实现。蓝牙电子秤的外形和地面有四个接触点,四个接触点那里都放着一种压力传感器,压力传感器将人体的重量转换成电信号,后经过芯片设计完成处理器AD采样,再经过换算便可以得到人体的体重。蓝牙脂肪秤模块测量体脂是通过电阻抗法测量出来的,它的具体原理是由电极片发出微弱电流,与人体形成一个闭环,通过肌肉易导电,脂肪不导电的…

    2022年4月11日
    54
  • 线性回归最小二乘法公式推导「建议收藏」

    线性回归最小二乘法公式推导「建议收藏」#1.符号表示首先我们将训练样本的**特征矩阵X**进行表示,其中N为样本个数,p为特征个数,每一行表示为每个样本,每一列表示特征的每个维度:

    2022年5月13日
    67
  • PHP实现记录浏览历史页面

    PHP实现记录浏览历史页面

    2021年10月30日
    43
  • Latex角标(subscript/superscript)

    Latex角标(subscript/superscript)

    2021年9月12日
    274
  • DELPHI程序员招聘_招聘java程序员

    DELPHI程序员招聘_招聘java程序员北京地区招聘Delphi程序员,要求工作经验2年以上,熟悉Delphi7+SQL有PB开发经验优先可全职或外派工作地点:西城区六铺炕联系QQ:408390946

    2022年8月1日
    7
  • 怎么自定义服务器的404,如何自定义404页面

    怎么自定义服务器的404,如何自定义404页面404错误页面是WWW网站访问比较经常出现的错误。大家最熟悉的也是最常见的出错提示:404notfound。404页面就是当用户输入了错误的链接时,返回的页面。而默认的404错误页面呆板麻木,让访问者感觉很挫败,可能会直接离开您的网站。自定义404页面的目的是:告诉浏览者其所请求的页面不存在或链接错误,同时引导用户使用网站其他页面而不是关闭窗口离开。是增强用户体验的很好的做法。简而言之,有两点…

    2022年7月27日
    4

发表回复

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

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