Introduced Task 8.2

This commit is contained in:
Sergiusz Warga 2021-04-29 19:36:08 +02:00
parent 653f699dab
commit 827533b42a
3 changed files with 103 additions and 4 deletions

View File

@ -9,10 +9,13 @@
#include <sys/mman.h> #include <sys/mman.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
#include <time.h>
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
srandom(time(NULL));
const char *memory_name = "/memory"; const char *memory_name = "/memory";
char message[] = "Hello there"; char message[] = "Hello there";

100
Task-8/task_8_2.c Normal file
View File

@ -0,0 +1,100 @@
// 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;
}

View File

@ -1,7 +1,3 @@
// master generates tasks and place them in a queue
// slave executes tasks (and sends the result back to the consumer)
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <mqueue.h> #include <mqueue.h>