migration
This commit is contained in:
54
semester-4/ОС/lb-5/src/calc/op-writer.c
Normal file
54
semester-4/ОС/lb-5/src/calc/op-writer.c
Normal file
@ -0,0 +1,54 @@
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/mman.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "shared.h"
|
||||
|
||||
int EXIT_CODE = 0;
|
||||
|
||||
int main() {
|
||||
int fd;
|
||||
calc_data *data;
|
||||
|
||||
fd = open(SF_NAME, O_CREAT | O_RDWR, 0666);
|
||||
if (fd == -1) {
|
||||
err(EXIT_FAILURE, "open", __LINE__ - 1);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (ftruncate(fd, SF_SIZE) == -1) {
|
||||
err(EXIT_FAILURE, "ftruncate", __LINE__ - 1);
|
||||
goto fd_close;
|
||||
}
|
||||
|
||||
data = (calc_data *)mmap(NULL, SF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
|
||||
fd, 0);
|
||||
if (data == MAP_FAILED) {
|
||||
err(EXIT_FAILURE, "mmap", __LINE__ - 1);
|
||||
goto fd_close;
|
||||
}
|
||||
|
||||
while (!(data->ready_flags & 1))
|
||||
spin("Waiting for numbers", 250);
|
||||
|
||||
printf("\rEnter operation (+, -, *, /): ");
|
||||
if (scanf(" %c", &data->operation) != 1) {
|
||||
err(EXIT_FAILURE, "Getting operation", __LINE__ - 1);
|
||||
goto munmap;
|
||||
}
|
||||
|
||||
__sync_or_and_fetch(&data->ready_flags, 2);
|
||||
|
||||
printf("Operation '%c' is written to the shared memory.\n", data->operation);
|
||||
|
||||
munmap:
|
||||
munmap(data, SF_SIZE);
|
||||
|
||||
fd_close:
|
||||
close(fd);
|
||||
|
||||
exit:
|
||||
return EXIT_CODE;
|
||||
}
|
Reference in New Issue
Block a user