Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/shm.h>
- #include <sys/ipc.h>
- #include <stdio.h>
- #include <errno.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/sem.h>
- #include <string.h>
- #include <wait.h>
- //-----------------------------------------------
- //-----------------------------------------------
- struct plist
- {
- char name[50];
- int wtime;
- int dtime;
- };
- struct process
- {
- char name[50];
- int count;
- };
- int find(struct process a, struct plist *b, int c)
- {
- for(int i = 0; i < c; i++)
- {
- if(strcmp(a.name, b[i].name) == 0)
- {
- return i;
- }
- }
- }
- //-----------------------------------------------
- //-----------------------------------------------
- int main()
- {
- //------------------------------------------------------------
- struct plist list[15];
- struct process dirty;
- char rubish[50];
- //-------------------------------------------------------
- FILE* fi;
- fi = fopen("washer.txt", "r");
- int w = 0;
- while(fscanf(fi, "%s %d\n", list[w].name, &list[w].wtime) == 2)
- {
- w++;
- }
- fclose(fi);
- //-------------------------------------------------------
- fi = fopen("scratcher.txt", "r");
- int s = 0;
- while(fscanf(fi, "%s %d\n", rubish, &list[s].dtime) == 2)
- {
- s++;
- }
- fclose(fi);
- //------------------------------------------------------------
- int semid;
- int N;
- N = atoi(getenv("TABLE_LIMIT"));
- struct sembuf buf[] = { {0, N, 0}, {0, -1, 0}, {0, 1, 0}};
- key_t key;
- key = ftok("test.c", 0);
- //printf("%d\n", key);
- semid = semget(key, 1, 0666 | IPC_CREAT);
- semop(semid, &buf[0], 1);
- //-------------------------------------------------------------
- int fd[2];
- int pid;
- pipe(fd);
- //-------------------------------------------------------------
- pid = fork();
- int a;
- int b;
- //-------------------------------------------------------------
- if (pid == 0)
- {
- fi = fopen("dirty.txt", "r");
- close(fd[0]);
- while(fscanf(fi, "%s %d\n", dirty.name, &dirty.count) == 2)
- {
- a = find(dirty, list, w);
- //printf("a = %d\n", a);
- for(int i = 0; i < dirty.count; i++)
- {
- printf("Начал мыть %s\n", list[a].name);
- sleep(list[a].wtime);
- write(fd[1], &a, 4);
- semop(semid, &buf[1], 1);
- printf("Помыл и поставил на стол %s %d\n", list[a].name, semctl(semid, 0, GETVAL, 0));
- }
- }
- exit(0);
- }
- else
- {
- close(fd[1]);
- while(read(fd[0], &b, 4) != 0)
- {
- semop(semid, &buf[2], 1);
- printf("Взял со стола %s %d\n", list[b].name, semctl(semid, 0, GETVAL, 0));
- sleep(list[b].dtime);
- printf("Вытер %s\n", list[b].name);
- }
- semctl(semid, 0, IPC_RMID, 0);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement