美文网首页
进程间通信

进程间通信

作者: 如果听见下雨的声音 | 来源:发表于2020-04-21 13:51 被阅读0次

声明:图片资源摘自于网络

管道(PIPE)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>

static const int MSG_SIZE = 50;

void pipe_no_name()
{

    int ret;

    printf("present pid = %d \n", getpid());

    int pipesFD[2];

    ret = pipe(pipesFD);

    if (ret < 0)
    {
        printf("create pipe failure \n");

        return;
    }

    ret = fork();

    if (ret < 0)
    {
        printf("create process failure \n");

    } else if (ret > 0)
    {

        printf("present pid = %d \n", getpid());

        char msg[MSG_SIZE];

        std::fill(msg, msg + MSG_SIZE, 0);

        for (int i = 0;; i++)
        {
            sprintf(msg, "msg number=%d", i);

            write(pipesFD[1], msg, MSG_SIZE);

            std::fill(msg, msg + MSG_SIZE, 0);

            sleep(1);
        }

    } else
    {

        printf("child pid = %d \n", getpid());

        char msg[MSG_SIZE];

        for (;;)
        {
            read(pipesFD[0], msg, MSG_SIZE);

            printf("accept msg: %s \n", msg);

            std::fill(msg, msg + MSG_SIZE, 0);

            sleep(1);
        }


    }


    close(pipesFD[0]);
    close(pipesFD[1]);

}

有名管道(FIFO)

进程1

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>
#include <algorithm>

static const char FIFO_NAME[] = "/tmp/fifo666";
static const int MSG_SIZE = 50;

void fifo_name()
{

    int ret;

    remove(FIFO_NAME);

    ret = mkfifo(FIFO_NAME, 0666);

    if (ret < 0 && errno != EEXIST)
    {

        printf("create fifo failure\n");

        return;
    }

    ret = open(FIFO_NAME, O_WRONLY);

    if (ret < 0)
    {

        printf("open file failure\n");

        return;
    }

    int fifoRFD = ret;

    char msg[MSG_SIZE];

    for (int i = 0;; i++)
    {
        sprintf(msg, "msg number=%d", i);

        printf("send msg: %d", i);

        write(fifoRFD, msg, MSG_SIZE);

        sleep(1);
    }

    close(fifoRFD);


}

进程2

#include <iostream>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>

static const char FIFO_NAME[] = "/tmp/fifo666";
static const int MSG_SIZE = 50;

void fifo_name()
{

    int ret;

    ret = open(FIFO_NAME, O_RDONLY);

    if (ret < 0)
    {
        printf("open file failure\n");

        return;
    }

    int fifoRFD = ret;

    char msg[MSG_SIZE];


    for (int i = 0;; i++)
    {
        read(fifoRFD, msg, MSG_SIZE);

        printf("accept msg: %s \n", msg);

        sleep(1);
    }

    close(fifoRFD);

}


int main()
{
    fifo_name();

    return 0;
}

高级管道(popen)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>

void pipe_popen()
{

    char cmd[] = "ls";

    FILE *p = popen(cmd, "r");

    char buf[256];

    while (fgets(buf, 256, p))
    {

        printf("%s", buf);

    }

    pclose(p);

}

共享内存(shared memory)

server

#include <sys/ipc.h>
#include <sys/shm.h>
#include <csignal>
#include <iostream>

#define SHM_PATH "/tmp/test_shm"
#define SHM_SIZE 0x6400

inline void handle_sig(int sig)
{
    printf("handle signal, signal : %s\n", strsignal(sig));

    isLoop = false;
}


void s_shm()
{
    signal(SIGALRM, handle_sig);

    int shm_id;

    char *shmAddr;

    shm_id = shmget(ftok(SHM_PATH, 0x03), SHM_SIZE, IPC_CREAT | IPC_EXCL | 0600);

    if (shm_id == -1)
    {
        perror("shmget failure!");

        exit(errno);
    }

    shmAddr = reinterpret_cast<char *>(shmat(shm_id, NULL, 0));

    int count = 0;

    alarm(10);

    while (isLoop)
    {
        sprintf(shmAddr, "test shared memory! ---> %d", count++);
    }

    strcpy(shmAddr, "exit");

    sleep(1);

    shmdt(shmAddr);

    shmctl(shm_id, IPC_RMID, NULL);

}


client

#include <sys/ipc.h>
#include <sys/shm.h>
#include <iostream>

#define SHM_PATH "/tmp/test_shm"
#define SHM_SIZE 0x6400

void c_shm()
{
    int shm_id;

    char *shmAddr;

    shm_id = shmget(ftok(SHM_PATH, 0x03), SHM_SIZE, IPC_EXCL | 0600);

    if (shm_id == -1)
    {
        perror("shmget failure!");

        exit(errno);
    }

    shmAddr = reinterpret_cast<char *>(shmat(shm_id, NULL, 0));

    while (true)
    {
        if (strstr(shmAddr, "exit"))
        {
            break;
        }

        printf("%s\n", shmAddr);
    }

    shmdt(shmAddr);

    shmctl(shm_id, IPC_RMID, NULL);
}

消息队列(msg)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <memory.h>
#include <fcntl.h>
#include <sys/msg.h>

void msg_queue()
{
//    ftok();
//    msgget(123, 0666 | IPC_CREAT);
//    msgrcv()
//    msgsnd()
//    msgctl()
}

内存映射(mmap)

1572632609052.png
1572632942205.png
1572632499901.png
1572632392157.png

相关文章

  • linux进程间通信(1)

    一、进程通信概述 1、什么是进程间通信?什么是线程间通信? 进程间通信: 进程间通信就指的是用户空间中进程A与进程...

  • 第二十三章 进程间通信介绍(一)

    本章目标: 进程同步与进程互斥 进程间通信目的 进程间通信发展 进程间通信分类 进程间共享信息的三种方式 IPC对...

  • 进程间的通信

    进程间的通信主要分为本机器进程间的通信和不同机器间进程的通信。本文主要描述本机进程间的通信。 一、传统Linux的...

  • 进程间通信

    进程间通信 进程空间相对独立,资源无法相互获取,此时在不同进程间通信需要专门方法 进程间通信就是在不同的进程间进行...

  • 进程间通信,线程间通信

    进程间通信 进程间通信又称IPC(Inter-Process Communication),指多个进程之间相互通信...

  • Android IPC机制

    IPC 即Inter-Process-Communication,含义是进程间通信/跨进程通信。是指多个进程间通信...

  • 进程管理(五)进程间通信、死锁

    (一)进程间通信 除了同步和互斥外,进程间还有其他的通信手段。 进程间的通信 --> IPC (InterProc...

  • 6. 进程间通信

    参考链接:1. 进程间通信及使用场景2. 进程间通信机制IPC3. 看图理解进程间通信IPC==重点4. 进程间通...

  • 【python】进程间通信:Queue的详细用法

    关于python 进程间通信 Process之间有时需要通信,操作系统提供了很多机制来实现进程间的通信。 进程间通...

  • 进程间通信(IPC)与伯克利套接字

    进程间通信(IPC) 进程间通信(IPC,Inter-Process Communication),指至少两个进程...

网友评论

      本文标题:进程间通信

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