进程间通信

2020-04-21  本文已影响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

上一篇下一篇

猜你喜欢

热点阅读