/* * ios/user functions API : open,close,read */ int devOpen(myDev * dev, char * name, int mode) { fifo * i = 0; //Create a new fifo for this app and initialize it : fifo * newFifo = malloc(sizeof(fifo)); if(newFifo==NULL) { errnoSet(MEM_OVERFLOW); return -1; } initFifo(newFifo); newFifo->taskId = taskIdSelf(); //From now on we'll acces the device data, let's require the sem : if(semTake(dev->semMData,WAIT_FOREVER)!=OK) { free(newFifo); errnoSet(SEM_ERR); return -1; } dev->openned++; //keep track of the number of tasks that have openned this device dev->mode = mode; //this seems useless, todo : check //Add fifo at end of listeFifo : if (dev->firstFifo==NULL) { dev->firstFifo = newFifo; } else { for (i=dev->firstFifo;i->nextFifo!=NULL;i=i->nextFifo); i->nextFifo=newFifo; } semGive(dev->semMData); return (int)dev; //pointer returned is used as arg in devClose! }
int main(void) { pthread_t idProductores[N_PRO], idConsumidores[N_CON]; char str[30]; int i; FIFO fifo; initFifo(&fifo); for (i = 0; i < N_PRO; i++) pthread_create(&idProductores[i], NULL, producersFunction, (void*) &fifo); for (i = 0; i < N_CON; i++) pthread_create(&idConsumidores[i], NULL, consumersFunction, (void*) &fifo); for (i = 0; i < N_PRO; i++) pthread_join(idProductores[i], NULL); for (i = 0; i < N_CON; i++) pthread_join(idConsumidores[i], NULL); destroyFifo(&fifo); printf("type \"Return\" to finish\n"); fgets(str, sizeof(str), stdin); interrupt(&fifo); return 0; }