Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* В родителе D(1), далее в цикле write() текст в pipe, A(2), Zero - блок семафора, read();
- В ребенке в цикле D(2) - запуск родителя, read(), write(). A(1) */
- #include <sys/types.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/ipc.h>
- #include <sys/sem.h>
- #include <sys/shm.h>
- int main() {
- int fd[2], result, N;
- size_t size;
- char resstring[14];
- char path[] = "task03.c";
- key_t key;
- struct sembuf mybuf;
- int semid;
- printf("Введите N: ");
- scanf("%d", &N);
- if (pipe(fd) < 0) {
- printf("Can\'t open pipe\n");
- exit(-1);
- }
- if ((key = ftok(path, 0)) < 0) {
- printf("Can\'t generate key");
- exit(-1);
- }
- if ((semid = semget(key, 1, 0666 | IPC_CREAT)) < 0) {
- printf("Can\'t create semaphore set\n");
- exit(-1);
- }
- //Add_1
- mybuf.sem_num = 0;
- mybuf.sem_op = 1;
- mybuf.sem_flg = 0;
- if (semop(semid, &mybuf, 1) < 0) {
- printf("Can\'t set value in semaphore\n");
- exit(-1);
- }
- result = fork();
- if (result < 0) {
- printf("Can\'t fork child\n");
- exit(-1);
- } else if (result > 0) {
- /* Parent process */
- //Decrease_1
- mybuf.sem_num = 0;
- mybuf.sem_op = -1;
- mybuf.sem_flg = 0;
- if (semop(semid, &mybuf, 1) < 0) {
- printf("Can\'t set value in semaphore\n");
- exit(-1);
- }
- for (int i = 0; i < N; ++i) {
- size = write(fd[1], "Hello, world!", 14);
- if (size != 14) {
- printf("Can\'t write all string to pipe\n");
- exit(-1);
- }
- //Add_2
- mybuf.sem_num = 0;
- mybuf.sem_op = 2;
- mybuf.sem_flg = 0;
- if (semop(semid, &mybuf, 1) < 0) {
- printf("Can\'t set value in semaphore\n");
- exit(-1);
- }
- //Zero
- mybuf.sem_num = 0;
- mybuf.sem_op = 0;
- mybuf.sem_flg = 0;
- if (semop(semid, &mybuf, 1) < 0) {
- printf("Can\'t set value in semaphore\n");
- exit(-1);
- }
- //Decrease_1
- mybuf.sem_num = 0;
- mybuf.sem_op = -1;
- mybuf.sem_flg = 0;
- if (semop(semid, &mybuf, 1) < 0) {
- printf("Can\'t set value in semaphore\n");
- exit(-1);
- }
- size = read(fd[0], resstring, 14);
- if (size < 0) {
- printf("Can\'t read all string to pipe\n");
- exit(-1);
- }
- printf("Parent read from the pipe: %s\n", resstring);
- }
- printf("Parent exit\n");
- } else {
- /* Child process */
- for (int i = 0; i < N; ++i) {
- //Decrease_2
- mybuf.sem_num = 0;
- mybuf.sem_op = -2;
- mybuf.sem_flg = 0;
- if (semop(semid, &mybuf, 1) < 0) {
- printf("Can\'t set value in semaphore\n");
- exit(-1);
- }
- size = read(fd[0], resstring, 14);
- if (size < 0) {
- printf("Can\'t read string from pipe\n");
- exit(-1);
- }
- printf("Child read from the pipe: %s\n", resstring);
- size = write(fd[1], "Hello, world!", 14);
- if (size != 14) {
- printf("Can\'t write all string to pipe\n");
- exit(-1);
- }
- //Add_1
- mybuf.sem_num = 0;
- mybuf.sem_op = 1;
- mybuf.sem_flg = 0;
- if (semop(semid, &mybuf, 1) < 0) {
- printf("Can\'t set value in semaphore\n");
- exit(-1);
- }
- }
- if (close(fd[0]) < 0) {
- printf("child: Can\'t close reading side of pipe\n");
- exit(-1);
- }
- if (close(fd[1]) < 0) {
- printf("child: Can\'t close writing side of pipe\n");
- exit(-1);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement