diff --git a/Task-8/task_8_1.c b/Task-8/task_8_1.c index 0600035..2598631 100644 --- a/Task-8/task_8_1.c +++ b/Task-8/task_8_1.c @@ -9,10 +9,13 @@ #include #include #include +#include #include #include int main(int argc, char *argv[]) { + srandom(time(NULL)); + const char *memory_name = "/memory"; char message[] = "Hello there"; diff --git a/Task-8/task_8_2.c b/Task-8/task_8_2.c new file mode 100644 index 0000000..77b24fc --- /dev/null +++ b/Task-8/task_8_2.c @@ -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 +#include +#include +#include +#include +#include +#include +#include + +#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; +} \ No newline at end of file diff --git a/Topic-7/task_7.c b/Topic-7/task_7.c index 76b7657..58dc97c 100644 --- a/Topic-7/task_7.c +++ b/Topic-7/task_7.c @@ -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 #include #include