[多核并行计算]进程间通信(二)共享内存

本文探讨了多核并行计算中进程间通信的重要方式——共享内存。通过实例代码,详细解析了如何实现进程间的共享内存通信,帮助读者深入理解这一关键概念。
摘要由CSDN通过智能技术生成

先把代码贴上,后面在做讲解。。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h> 
#include <math.h> 
#include <sys/time.h> 
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <math.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/select.h>
#include <sys/epoll.h>

void set_thread_affinity(int cpuid);
int set_sc(int type,int prority);
void rtclock_gettime(unsigned long long *tt);
int allocate_rtcore();
int release_rtcore();
int smp_comm1();

#define PRINT_NS		1
#define SEND_ONLY		0
#define RT_MODE_ENABLE		1
#define MAX_PROCESS_NUM		64
#define MSG_SIZE		24
struct Process
{
	int     id;
	pid_t	pid;
	
}process;

struct Process *processes;
int nr_process = 4;
int shm_fd;
int epoll_fd;
int myid = 0;
static pthread_barrier_t *barrier;
static char *shm_buffer;
 
#define SHARE_RFIFO_PATH "/home/tmp/_fifo"
#define SHARE_WFIFO_PATH "/home/tmp/_fifo"
#define F 2.2E3
#define COMM_NUM 24
 
int    on_cpu;
double dval = 1.23;
double sendbuf[4096];
double recvbuf[4096];
static int testloop = 8000;
static int calcloop = 20;
static int cpu_freq = 0;

/*
   1. all processes use a same share memory
   2. The memory area is divided into several sub-blocks
   3. each process use two sub-blocks, one for read and another for write.

                         (Four process IPC)
                   --------              --------
                  |        | ---------> |        |
                  |process0| <--------- |process1|
                  |        |            |        |
                   --------              --------
                   /| |     |\\      //|   | |\
                    | |       \\    //     | |
                    | |        \\  //      | |
                    | |         \\//       | |
                    | |          *         | |
                    | |         //\\       | |
                    | |        //  \\      | |
                    | |       //    \\     | |
                    | |/    |//      \\|  \| |
                   --------              --------
                  |        |            |        |
                  |process2| ---------> |process3|
                  |        | <--------- |        |
                   --------             --------

                                 shared memory range(4 process)
 -----------------------------------------------------------------------------------------------
|       |       |       |       |       |       |       |       |       |       |       |       |
|  0-1  |  0-2  |  0-3  |  1-0  |  1-2  |  1-3  |  2-0  |  2-1  |  2-3  |  3-0  |  3-1  |  3-2  |
|       |       |       |       |       |       |       |       |       |       |       |       |
`-----------------------------------------------------------------------------------------------



             --------                    --------
            |        |-----+     +------|        |
            |process0|     |     |      |process1|
            |        |    \|     |/     |        |
             --
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值