Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <unistd.h>
- #include <pthread.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/ipc.h>
- #include <sys/msg.h>
- struct msgbuf{
- long mtype;
- char mtext[128];
- };
- int n = 8;
- int size = 4;
- int ukupnoPosjetitelja = 0;
- int brojDretve = 0;
- int zelim=0;
- int sjeo=0;
- key_t keys;
- int vqid = -1;
- int qids[8] = { 0 };
- void *posjetitelj(void *vargp) {
- brojDretve++;
- struct msgbuf pwrite;
- key_t pkey = keys+brojDretve;
- int msgqid = msgget(pkey, 0600 | IPC_CREAT);
- int sleeptime;
- int ponovi=0;
- while(ponovi<3){
- sleeptime = rand() % (2 + 1 - 1) + 1;
- sleep(sleeptime); //SPAVAJ X MILISEKUNDI
- //ZELIM SE VOZITI
- if(zelim<8){
- qids[zelim]=msgqid;
- zelim++;
- pwrite.mtype = 1;
- strcpy(pwrite.mtext, "Zelim");
- size_t len = strlen(pwrite.mtext)+1;
- //printf("POSJETITELJ zeli reda %d > %d .\n", qids[zelim-1], zelim-1);
- msgsnd(vqid, &pwrite, len, 0);
- int k=-1;
- while(k==-1) {
- int k=msgrcv(msgqid, &pwrite, 128, 1, 0);
- if(k!=-1){
- int checkSjeo = strcmp(pwrite.mtext, "Sjedi");
- if(checkSjeo==0){
- printf("Sjeo POSJETITELJ %d \n", msgqid);
- sjeo++;
- k=-1;
- }
- int checkUstao = strcmp(pwrite.mtext, "Ustani");
- if(checkUstao==0){
- printf("Sisao POSJETITELJ %d \n", msgqid);
- sjeo--;
- zelim--;
- break;
- }
- }
- }
- ponovi++;
- //printf("ponovi %d POSJETITELJ %d \n",ponovi, msgqid);
- }
- }
- brojDretve--;
- msgctl(msgqid, IPC_RMID, NULL);
- printf("POSJETITELJ %d ZAVRSIO\n", msgqid);
- return NULL;
- }
- void *vrtuljak(void *vargp) {
- struct msgbuf vread;
- key_t vkey = keys;
- vqid = msgget(vkey, 0600 | IPC_CREAT);
- //printf("VRTULJAK red %d kljuc %d.\n", vqid, vkey); //
- while(1) {
- while(ukupnoPosjetitelja < size){
- int status = msgrcv(vqid, &vread, 128, 1, 0);
- if(status != -1){
- int rckey=qids[ukupnoPosjetitelja];
- // printf("VRTULJAK > poruka -> %s od %d \n", vread.mtext, rckey);
- ukupnoPosjetitelja++;
- if(ukupnoPosjetitelja <= size){
- vread.mtype = 1;
- strcpy(vread.mtext, "Sjedi");
- size_t len = strlen(vread.mtext)+1;
- // printf("VRTULJAK > poslana poruka -> %s prema %d.\n", vread.mtext, rckey);
- msgsnd(rckey, &vread, len, 0);
- }
- }
- }
- if(ukupnoPosjetitelja == size & sjeo==size){
- printf("VRTULJAK pokrenut.\n");
- int sleeptime = rand() % (3 + 1 - 1) + 1;
- sleep(sleeptime); //SPAVAJ X MILISEKUNDI
- printf("VRTULJAK zaustavljen.\n");
- for(int j=0; j<size; j++){
- //printf("SIDI POSJETITELJU %d.\n", qids[j]);
- vread.mtype = 1;
- strcpy(vread.mtext, "Ustani");
- size_t l = strlen(vread.mtext)+1;
- msgsnd(qids[j], &vread, l, 0);
- //pomakni red cekanja
- qids[j]=qids[j+size];
- //printf("sljecedi red %d.\n", qids[j]);
- }
- while(sjeo!=0){}
- ukupnoPosjetitelja = 0;
- if(brojDretve<size){
- msgctl(vqid, IPC_RMID, NULL);
- printf("VRTULJAK GOTOV \n");
- break;
- }
- }
- }
- return NULL;
- }
- int main(){
- srand((unsigned)time(NULL));
- pthread_t threads[n+1];
- int i=0;
- keys=getuid();
- //pokreni vrtuljak();
- pthread_create(&threads[i], NULL, vrtuljak, NULL);
- while(vqid==-1){}
- for (i = 1; i <=n; i++) {
- //pokreni posjetitelja(i);
- pthread_create(&threads[i], NULL, posjetitelj, NULL);
- }
- pthread_exit(NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement