先把代码贴上,后面在做讲解。。
#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|
| | \| |/ | |
--