linux高级编程(9)进程间通信

news2024/10/5 19:19:34

2的信号量集就是semaphore 

这个图很重要!!! 

无名管道: 

练习一:读操作

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
    
    int pipefd[2]={0};
    int ret = pipe(pipefd);
    if(-1 == ret)
    {
        perror("pipe");
        exit(1);
    }


    pid_t pid = fork();
    if(pid>0)
    {
        char buf[]="hello,world";
        close(pipefd[0]);
        sleep(3);
        write(pipefd[1],buf,strlen(buf));
    }
    else if(0 == pid)
    {
        close(pipefd[1]);
        char buf[100]={0};
        read(pipefd[0],buf,sizeof(buf));
        printf("pipe %s\n",buf);
    }
    else 
    {
        perror("fork");
        exit(1);
    }
    return 0;
}

结果为:

 

练习:写操作

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
    
    int pipefd[2]={0};
    int ret = pipe(pipefd);
    if(-1 == ret)
    {
        perror("pipe");
        exit(1);
    }


    pid_t pid = fork();
    if(pid>0)
    {
        char buf[1024]={0};
        close(pipefd[0]);
        memset(buf,'a',1024);
        int i = 0 ;
        for(i=0;i<65;i++)
        {
            write(pipefd[1],buf,sizeof(buf));
            printf("i is %d\n",i);
        }
    }
    else if(0 == pid)
    {
        close(pipefd[1]);
        char buf[100]={0};
//        read(pipefd[0],buf,sizeof(buf));
  //      printf("pipe %s\n",buf);
        while(1)sleep(1);
    }
    else 
    {
        perror("fork");
        exit(1);
    }
    return 0;
}

结果为:

 

练习3:broken操作(管道破裂)

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
    
    int pipefd[2]={0};
    int ret = pipe(pipefd);
    if(-1 == ret)
    {
        perror("pipe");
        exit(1);
    }


    pid_t pid = fork();
    if(pid>0)
    {
        char buf[]="hello,world";
        close(pipefd[0]);
        sleep(3);
        // 管道破裂 gdb  跟踪
        write(pipefd[1],buf,strlen(buf));
        printf("aaaa\n");
    }
    else if(0 == pid)
    {
        close(pipefd[1]);
        close(pipefd[0]);
    }
    else 
    {
        perror("fork");
        exit(1);
    }
    return 0;
}

结果如下:

 

练习4:end操作

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main(int argc, char *argv[])
{
    
    int pipefd[2]={0};
    int ret = pipe(pipefd);
    if(-1 == ret)
    {
        perror("pipe");
        exit(1);
    }


    pid_t pid = fork();
    if(pid>0)
    {
        char buf[]="hello,world";
        close(pipefd[0]);
        write(pipefd[1],buf,strlen(buf));
        write(pipefd[1],buf,strlen(buf));
        close(pipefd[1]);
    }
    else if(0 == pid)
    {
        close(pipefd[1]);
            char buf[5]={0};
        while(1)
        {
            //memset(buf,0,5);
            bzero(buf,sizeof(buf));
            int rd_ret = read(pipefd[0],buf,sizeof(buf)-1);
            if(rd_ret<=0)
            {
                break;
            }
            printf("pipe %s\n",buf);
        }
    }
    else 
    {
        perror("fork");
        exit(1);
    }
    return 0;
}

结果为:

 

练习5:cp操作,父进程与子进程,把父进程的内容cp到子进程里面

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
    
    int pipefd[2]={0};
    int ret = pipe(pipefd);
    if(-1 == ret)
    {
        perror("pipe");
        exit(1);
    }


    pid_t pid = fork();
    if(pid>0)
    {
        close(pipefd[0]);
       int fd =  open("/home/linux/1.png",O_RDONLY);
       if(-1 == fd)
       {
            perror("open");
            exit(1);
       }
       while(1)
       {
            char buf[4096]={0};
            int rd_ret = read(fd,buf,sizeof(buf));
            if(rd_ret<=0)
            {
                break;
            }
            write(pipefd[1],buf,rd_ret);
       
       }
       close(fd);
       close(pipefd[1]);
    }
    else if(0 == pid)
    {
        close(pipefd[1]);
        int fd = open("2.png",O_WRONLY|O_CREAT|O_TRUNC,0666);
        if(-1==fd)
        {
            perror("open");
            exit(1);
        }
        while(1)
        {
            char buf[1024]={0};
            int rd_ret = read(pipefd[0],buf,sizeof(buf));
            if(rd_ret<=0)
            {
                break;
            }
            write(fd,buf,rd_ret);
        }
        close(fd);
        close(pipefd[0]);
    }
    else 
    {
        perror("fork");
        exit(1);
    }
    return 0;
}

结果就是把1.png复制出了一个新的2.png(图片里面包含的是一些二进制的数字,所以要用文件IO,而且为了避免遇到0,不要使用strlen,因为strlen遇到0就不继续往后读了,所以要用sizeof)

 

 

有名管道:

练习1:写操作

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main(int argc, char *argv[])
{
    int ret = mkfifo("myfifo",0666);    
    if(-1 == ret)
    {
        //如果是管道文件已存在错误,让程序继续运行
        if(EEXIST== errno)
        {

        }else 
        {
            perror("mkfifo");
            exit(1);
        }
    }
    //open 会阻塞,等到另一端读段打开,解除阻塞
    int fd = open("myfifo",O_WRONLY);
    if(-1 == fd)
    {
        perror("open");
        exit(1);
    }

    char buf[256]="hello,fifo,test";
    write(fd,buf,strlen(buf));
    close(fd);

    return 0;
}

练习2:读操作

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    int ret = mkfifo("myfifo",0666);    
    if(-1 == ret)
    {
        //如果是管道文件已存在错误,让程序继续运行
        if(EEXIST== errno)
        {
        
        }else 
        {
            perror("mkfifo");
            exit(1);
        }
    }

    int fd = open("myfifo",O_RDONLY);
    if(-1 == fd)
    {
        perror("open");
        exit(1);
    }

    char buf[256]={0};
    read(fd,buf,sizeof(buf));
    printf("fifo %s\n",buf);
    close(fd);
    //remove("myfifo");

    return 0;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1887945.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

开源模型应用落地-FastAPI-助力模型交互-WebSocket篇(六)

一、前言 使用 FastAPI 可以帮助我们更简单高效地部署 AI 交互业务。FastAPI 提供了快速构建 API 的能力,开发者可以轻松地定义模型需要的输入和输出格式,并编写好相应的业务逻辑。 FastAPI 的异步高性能架构,可以有效支持大量并发的预测请求,为用户提供流畅的交互体验。此外,F…

【C语言】inline 关键字

在C语言中&#xff0c;inline关键字用于建议编译器对函数进行内联展开&#xff0c;而不是像普通函数一样调用。内联函数的目的是减少函数调用的开销&#xff0c;特别是对于简单的、频繁调用的函数。 内联函数的定义和使用 定义内联函数 要定义一个内联函数&#xff0c;需要在…

小红书运营教程02

小红书大致会分享10篇左右。微博、抖音、以及视频剪辑等自媒体运营相关技能以及运营教程相关会陆续的进行分享。 上次分享涉及到的对比,母婴系列,或者可以说是服装类型,不需要自己过多的投入,对比知识类博主来说,自己将知识讲述出来,然后要以此账号进行变现就比较麻烦,…

认识一下HttpMessageHandler处理管道

[S1208]HttpClient的默认管道结构 接下来我们通过如下的演示程序使用IHttpClientFactory工厂创建了 一个HttpClient对象&#xff0c;并查看其管道依次由哪些类型的HttpMessageHandler对象组成。如代码片段所示&#xff0c;我们定义了一个辅助方法PrintPipeline方法以递归的形式…

我等你,就在微软头马Open House开放日

当众讲话一直是我职业生涯中的重要部分&#xff0c;MSTMC 头马俱乐部更是我成长路上的重要伙伴。今天&#xff0c;我诚挚地邀请你参加即将在北京微软大厦举行的 微软头马Open House开放日活动&#xff01; 活动详情&#xff1a; &#x1f4c5; 日期&#xff1a;2024年7月3日&am…

Python 作业题1 (猜数字)

题目 你要根据线索猜出一个三位数。游戏会根据你的猜测给出以下提示之一&#xff1a;如果你猜对一位数字但数字位置不对&#xff0c;则会提示“Pico”&#xff1b;如果你同时猜对了一位数字及其位置&#xff0c;则会提示“Fermi”&#xff1b;如果你猜测的数字及其位置都不对&…

AI姓氏头像生成微信小程序系统源码

&#x1f525;【科技新潮流】AI姓氏头像生成系统&#xff0c;你的专属个性新名片&#xff01;&#x1f389; &#x1f31f; 开篇惊艳&#xff1a;一键解锁你的姓氏魅力 ✨ Hey小伙伴们&#xff0c;今天我要安利一个超酷炫的科技小玩意——AI姓氏头像生成系统&#xff01;是不…

API 授权最佳实践

API&#xff08;应用程序编程接口&#xff09;就像秘密之门&#xff0c;允许不同的软件程序进行通信。但并不是每个人都应该拥有每扇门的钥匙&#xff0c;就像不是每个软件都应该不受限制地访问每个 API 一样。 这些 API 将从银行的移动应用程序到您最喜欢的社交媒体平台的所有…

python机器人编程——用pytorch实现六轴机械臂的正向和逆向数值解算,及python算法解析

目录 一、前言二、实现原理2.1正向建模2.2张量化2.3绘制3D动画及操作UI 三、结论四、python源码PS.扩展阅读ps1.六自由度机器人相关文章资源ps2.四轴机器相关文章资源ps3.移动小车相关文章资源 一、前言 前面对六轴&#xff08;或多轴&#xff09;机械臂进行了一些研究&#x…

SCI一区级 | Matlab实现BO-Transformer-LSTM时间序列预测

SCI一区级 | Matlab实现BO-Transformer-LSTM时间序列预测 目录 SCI一区级 | Matlab实现BO-Transformer-LSTM时间序列预测效果一览基本介绍![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/caef5d232c4b436ebb01d717819d5ff1.png)程序设计参考资料 效果一览 基本介绍…

C语言的数据结构:图的基本概念

前言 之前学过了其它的数据结构&#xff0c;如&#xff1a; 集合 \color{#5ecffd}集合 集合 —— 数据元素属于一个集合。 线型结构 \color{#5ecffd}线型结构 线型结构 —— 一个对一个&#xff0c;如线性表、栈、队列&#xff0c;每一个节点和其它节点之间的关系 一个对一个…

【Linux】生物信息学常用命令

参考资料来自生信技能树 先输入echo export PS1"[\033]2;\h:\u \w\007\033[33;1m]\u \033[35;1m\t\033[0m [\033[36;1m]\w[\033[0m]\n[\e[32;1m]$ [\e[0m]" >> ~/.bashrc 再输入source ~/.bashrc就能够让命令字体带上颜色&#xff0c;同时命令将会在下一行开…

Spark on k8s 源码解析执行流程

Spark on k8s 源码解析执行流程 1.通过spark-submit脚本提交spark程序 在spark-submit脚本里面执行了SparkSubmit类的main方法 2.运行SparkSubmit类的main方法&#xff0c;解析spark参数&#xff0c;调用submit方法 3.在submit方法里调用doRunMain方法&#xff0c;最终调用r…

LiveNVR监控流媒体Onvif/RTSP用户手册-视频广场:状态记录、播放、回放入口、筛选在线离线、搜索

LiveNVR监控流媒体Onvif/RTSP用户手册-视频广场:状态记录、播放、回放入口、筛选在线离线、搜索 1、视频广场1.1、搜索筛选1.2、状态记录1.3、播放1.4、视频信息1.5、回放入口 2、RTSP/HLS/FLV/RTMP拉流Onvif流媒体服务 1、视频广场 1.1、搜索筛选 可以下拉筛选 在线、离线 &a…

小红书 达芬奇:生活问答 AI 机器人

小红书去年 9 月开始内测的生活问答 AI 机器人&#xff1a;达芬奇&#xff0c;现在可以在小红书 APP 上用了 得益于小红书平台的特性&#xff0c;该助手擅长吃、住、宠、喝、学等等各类生活知识&#xff0c;目前还在搞活动&#xff0c;写评测笔记最高得 666 元

《后端程序猿 · 基于 Lettuce 实现缓存容错策略》

&#x1f4e2; 大家好&#xff0c;我是 【战神刘玉栋】&#xff0c;有10多年的研发经验&#xff0c;致力于前后端技术栈的知识沉淀和传播。 &#x1f497; &#x1f33b; 近期刚转战 CSDN&#xff0c;会严格把控文章质量&#xff0c;绝不滥竽充数&#xff0c;如需交流&#xff…

LinkedList底层原理

LinkedList特有方法 源码分析

Redis 缓存预热、缓存雪崩、缓存击穿、缓存穿透业务实践

0、前言 本文所有代码可见 > 【gitee code demo】 本文会涉及 缓存预热、缓存雪崩、缓存击穿、缓存穿透介绍和解决方案业务实践 1、缓存预热 1.1、描述 提前将热点数据加载到缓存&#xff0c;提前响应&#xff0c;降低后端数据源访问压力 1.2、实践 Autowiredprivate R…

Python基础002

Python数据类型 1、字符串&#xff08;str&#xff09; str3 """I miss you so much""" print("str3 ", str3,type(str3)) str3 I miss you so much <class str>2、整数&#xff08;int&#xff09; str1 55 print(&quo…

什么是定时器?

前言&#x1f440;~ 上一章我们介绍了阻塞队列以及生产者消息模式&#xff0c;今天我们来讲讲定时器 定时器 标准库中的定时器 schedule()方法 扫描线程 手动实现定时器 任务类 存储任务的数据结构 定时器类 如果各位对文章的内容感兴趣的话&#xff0c;请点点小赞&am…