Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <sys/wait.h>
- #include <sys/ipc.h>
- #include <sys/shm.h>
- #define SHMSIZE 1
- #define ARRAY_ELEM 10
- int main(int argc, char *argv[])
- {
- pid_t pid;
- key_t key = ftok(".", 1);
- int shmid = shmget(key, SHMSIZE, IPC_CREAT | 0755);
- char symbol;
- pid = fork();
- if (pid == -1)
- {
- perror("fork");
- exit(EXIT_FAILURE);
- }
- else if (pid == 0) //child -> producer
- {
- char *buff = shmat(shmid, NULL, 0);
- for (symbol = 'a'; symbol <= 'z'; symbol++)
- {
- usleep(500);
- *buff = symbol;
- usleep(500);
- }
- shmdt(buff);
- }
- else if (pid > 0) //parent -> consumer
- {
- int i;
- char *buff = shmat(shmid, 0, 0);
- for (int i = 0; i <= 'z' - 'a'; i++)
- {
- usleep(500);
- symbol = *buff;
- printf("%d Received symbol %c\n", (i+1), symbol);
- usleep(500);
- }
- shmdt(buff);
- wait(NULL);
- shmctl(shmid, IPC_RMID, NULL);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement