Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <unistd.h>
- #include <sys/types.h>
- #include <sys/msg.h>
- #include <stdlib.h>
- #include <sys/sem.h>
- #include <signal.h>
- #include <sys/shm.h>
- #include <err.h>
- int main()
- { //Zmienne na klucze
- key_t key;
- if ((key = ftok(".", 'B')) == -1)
- errx(1, "Blad tworzenia klucza!");
- //Deklaracja pamieci wspoldzielonej
- int shmid = shmget(key, 4, IPC_CREAT|0666);
- //Pobieram wskaznik na adres pamieci wspoldzielonej
- if(shmid == -1){
- perror("Utworzenie segmentu pamieci wspoldzielonej");
- exit(1);
- }
- if(fork() == 0) {/**PROCES K1*/
- int i;
- int *buf;
- buf = shmat(shmid, NULL, 0);
- for(i = 0; i <= 100; i++) {
- printf("Producent zapisuje liczbe %d\n", i);
- buf[0] = i;
- buf[1] = 0;
- sleep(1);
- }
- }
- if(fork() == 0) {/**PROCES K2*/
- int *buf;
- int n;
- buf = shmat(shmid, NULL, 0);
- while(1){
- if(buf[1] == 0)
- {
- n = *buf;
- printf("K1 od producenta otrzymalem : %i\n",n);
- buf[1] = 1; }
- }
- }
- if(fork() == 0) {/**PROCES K3*/
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement