100 lines
2.1 KiB
C
100 lines
2.1 KiB
C
|
// Producer:
|
||
|
// Retransfers sinusoidal signal sampled at a frequency of 2kHz
|
||
|
// Writes to the circular buffer:
|
||
|
// Write sample into the free buffer position
|
||
|
// Incremets the free position by one
|
||
|
// After idx = size go to the beginning
|
||
|
|
||
|
// Consumers:
|
||
|
//
|
||
|
|
||
|
#include <fcntl.h>
|
||
|
#include <math.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <sys/mman.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <unistd.h>
|
||
|
#include <time.h>
|
||
|
|
||
|
#define PI 3.14
|
||
|
|
||
|
// void generate_signal(double *signal, int n_samples) {
|
||
|
// static double signal[n_samples];
|
||
|
// double step = 2.*PI/(double)n_samples;
|
||
|
// for (int i = 0; i < n_samples; ++i) {
|
||
|
// signal[i] = sin(i*step);
|
||
|
// }
|
||
|
// }
|
||
|
|
||
|
struct CircBuffer {
|
||
|
union {
|
||
|
struct {
|
||
|
int size;
|
||
|
int rd_idx;
|
||
|
int wr_idx;
|
||
|
} info;
|
||
|
char padding[1024];
|
||
|
};
|
||
|
int data[1024];
|
||
|
};
|
||
|
|
||
|
void producer(const char * memory_name, int fd) {
|
||
|
printf("The producer has been called\n");
|
||
|
double signal[] = {0, 0.7, 1, 0.7, 0};
|
||
|
|
||
|
struct CircBuffer *buffer;
|
||
|
|
||
|
|
||
|
buffer = mmap(NULL, 64, PROT_WRITE, MAP_SHARED, fd, 0);
|
||
|
if (buffer == MAP_FAILED) {
|
||
|
fprintf(stderr, "mmap: ");
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
}
|
||
|
|
||
|
int main(int argc, char *argv[]) {
|
||
|
int buffer_size = 64;
|
||
|
const char *memory_name = "/dsp";
|
||
|
int fd = shm_open(memory_name, O_CREAT | O_RDWR, 0777);
|
||
|
if (fd < 0) {
|
||
|
fprintf(stderr, "shm_open: ");
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
printf("Shred memory file descriptor: %d\n", fd);
|
||
|
|
||
|
|
||
|
// double *signal;
|
||
|
// generate_signal(signal, 100);
|
||
|
|
||
|
struct CircBuffer *buffer;
|
||
|
|
||
|
if (ftruncate(fd, sizeof(struct CircBuffer)) < 0) {
|
||
|
fprintf(stderr, "ftruncate: \n");
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
|
||
|
pid_t pid;
|
||
|
pid = fork();
|
||
|
if (pid == -1) {
|
||
|
perror("fork: ");
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
if (pid == 0) {
|
||
|
buffer = (struct CircBuffer *)mmap(NULL, sizeof(buffer), PROT_READ, MAP_SHARED, fd, 0);
|
||
|
if (buffer == MAP_FAILED) {
|
||
|
fprintf(stderr, "mmap: ");
|
||
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
exit(EXIT_SUCCESS);
|
||
|
} else {
|
||
|
producer(memory_name, fd);
|
||
|
}
|
||
|
|
||
|
|
||
|
return 0;
|
||
|
}
|