Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unistd.h>
- #include <stdio.h>
- #define ODCZYT 0
- #define ZAPIS 1
- #define WORKERS 10
- using namespace std;
- int semaphore[2];
- void LOCK()
- {
- int x;
- read(semaphore[ODCZYT],&x,sizeof(x));
- }
- void UNLOCK()
- {
- int x=0;
- write(semaphore[ZAPIS],&x,sizeof(x));
- }
- void UNLOCK_INIT()
- {
- int x=3;
- write(semaphore[ZAPIS],&x,3*sizeof(x));
- }
- void worker(int id, int potok_k)
- {
- int x = 0,i,j;
- cout << "Worker " << id << " startuje" << endl;
- LOCK();
- for(int i=0;i<7;i++)
- {
- cout << "[" << id << "]";
- fflush(stdout);
- sleep(1);
- }
- UNLOCK();
- cout << "Worker " << id << " konczy" << endl;
- write(potok_k,&x,sizeof(x));
- }
- int main()
- {
- int potok_konczacy[2],i,x;
- pipe(potok_konczacy);
- pipe(semaphore);
- UNLOCK_INIT();
- for(int i=0;i<WORKERS;i++)
- {
- if(!fork())
- {
- worker(i,potok_konczacy[ZAPIS]);
- return 0;
- }
- }
- for(int i=0;i<WORKERS;i++)
- {
- read(potok_konczacy[ODCZYT], &x, sizeof(x));
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement