int main(int argc, char *argv[]) { srand(time(NULL)); int i, fd, result; int *file_memory; pid_t child; child = fork(); char* program = "./MemMapConsum";; char* arglist[] = {"./MemMapConsum", "MemMapConsum", NULL} ; fd = open(FILEPATH, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600); lseek(fd, FILE_LENGTH+1, SEEK_SET); write(fd, "", 1); file_memory = (int*)mmap(0, FILE_LENGTH, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if(child == 0){ for (i = 1; i <=N; ++i) { file_memory[i] = produce_item(); } } else { wait(); execvp(program, arglist); consumer(); } close(fd); munmap(file_memory, FILE_LENGTH); return 0; }
int svc (void) { for (int i = 0; i <= MAX_PROD; i++) produce_item (i); hang_up (); return 0; }
/******************************************************************************** * Description: * Input Args: * Output Args: * Return Value: ********************************************************************************/ int main (int argc, char **argv) { int i, len; struct circ_buf tx_ring; char data[LEN]; char buf[LEN]; memset(&tx_ring, 0, sizeof(struct circ_buf)); tx_ring.buf = malloc(CIRC_BUF_SIZE); if( NULL == tx_ring.buf ) { printf("Allocate Ring buffer failure.\n"); return -1; } memset(data, 0, sizeof(data)); /* Prepare for the data */ for(i=0; i<sizeof(data); i++) { data[i] = 30+i; } printf("CIRC_SPACE: %d\n", CIRC_SPACE(tx_ring.head, tx_ring.tail, CIRC_BUF_SIZE)); printf("CIRC_SPACE_TO_END: %d\n", CIRC_SPACE_TO_END(tx_ring.head, tx_ring.tail, CIRC_BUF_SIZE)); printf("CIRC_CNT: %d\n", CIRC_CNT(tx_ring.head, tx_ring.tail, CIRC_BUF_SIZE)); printf("CIRC_CNT_TO_END: %d\n", CIRC_CNT_TO_END(tx_ring.head, tx_ring.tail, CIRC_BUF_SIZE)); while(1) { produce_item(&tx_ring, data, sizeof(data)); len = consume_item(&tx_ring, buf, sizeof(buf) ); sleep(1); } return 0; } /* ----- End of main() ----- */
void producer() { int item; while(true) { if(count == N) { break; } item = produce_item(); buffer[count++] = item; } }
void producer(void) { int item; while(TRUE) { item = produce_item(); if (count == N) return; insert_item(item); count = count + 1; } }
void producer(void) { int item; while (TRUE){ /* TRUE is the constant 1 */ item = produce_item(); /* generate something to put in buffer */ down(&empty); /* decrement empty count */ down(&mutex); /* enter critical region */ insert_item(item); /* put new item in buffer */ up(&mutex); /* leave critical region */ up(&full); /* increment count of full slots */ } }
void producer(void){ int item; while(TRUE){ item = produce_item(); if(count == N){ sleep(); } insert_item(item); count++; if(count == 1){ wakeup(consumer); } } }
void* producer(void* ptr) { int item; while (TRUE) { item = produce_item(); // generate next item if (count == N) { // call consumer if buffer is full consumer(&ptr); } insert_item(item); // put item in buffer count = count + 1; // increment count of items in buffer if (count == 1) { // call consumer if buffer size == 1 consumer(&ptr); } } return NULL; }
static PT_THREAD(producer(struct pt *pt)) { static int produced; PT_BEGIN(pt); for(produced = 0; produced < NUM_ITEMS; ++produced) { PT_SEM_WAIT(pt, &full); add_to_buffer(produce_item()); PT_SEM_SIGNAL(pt, &empty); } PT_END(pt); }
void* IN(void *thread_number) {/*producer function to read bytes into buffer*/ int x; x = *((int *) thread_number); while (TRUE) { get_sleep(ts); sem_wait(&empty); //empty slots to fill wait sem_wait(&mutex); pthread_mutex_lock(&result); int tell = 0; if (!feof(file)) { /* if there is more data in the file*/ tell = ftell(file); buffer[in].offset = tell; //position of the bytes read soo far int data = fgetc(file); // gets the data produce_item(buffer, data); // puts data in the buffer if (fseek(logfile, ftell(logfile), SEEK_SET) == -1) { //writes thread information to a file fprintf(stderr, "error setting output file position to \n"); exit(-1); } fprintf(logfile, "read_byte\tPT%d\tO%ld\tB%d\t%d\n", x, buffer[in].offset, buffer[in].data, in); printf("read_byte\tPT%d\tO%ld\tB%d\t%d\n", x, buffer[in].offset, buffer[in].data, in); fprintf(logfile, "producer\tPT%d\tO%ld\tB%d\t%d\n", x, buffer[in].offset, buffer[in].data, in); printf("producer\tPT%d\tO%ld\tB%d\t%d\n", x, buffer[in].offset, buffer[in].data, in); } pthread_mutex_unlock(&result); sem_post(&mutex); sem_post(&full); if (feof(file)) { pthread_mutex_unlock(&result); sem_post(&mutex); sem_post(&full); //sem_getvalue(&full, &exit_condtion); //printf("FEOF My exit in producer is:%d\n", exit_condtion); break; } } fflush(stdout); pthread_exit(NULL); }
int main() { int msgqid = create_msgq(); msgp.mtype = 1; int *shared_buffer = msgp.mtext; int item; while(1) { item = produce_item(); sleep(1); insert_item(item, shared_buffer); print_buffer(shared_buffer); if (msgsnd(msgqid, &msgp, BUFFER_SIZE, IPC_NOWAIT) < 0) { perror("msgsnd: failed to send"); exit(1); } } return 0; }
int main(int argc, char *argv[]){ printf("Producer entered\n"); /* A variable of item used in processing */ /* Setup system environment */ signal(SIGINT, ctrl_c); /* specify routine to handle CTRL-C */ setbuf(stdout, NULL); /* turn off buffering of stdout */ setbuf(stderr, NULL); /* turn off buffering of stderr */ int fd = shm_open("nctvyc", O_RDWR, S_IRUSR | S_IWUSR); if (fd == -1){ printf("Producer Error creating shared memory\n"); perror("shm_open"); shm_unlink("nctvyc"); return -1; } sp = mmap(NULL, sizeof(shr_mem_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (sp == MAP_FAILED){ printf("Producer Map Failed\n"); perror("mmap"); shm_unlink("nctvyc"); return -1; } /* Get pointers to the semaphores created by the parent */ MUTEXptr = sem_open(sp->MUTEXname, O_RDWR); EMPTYptr = sem_open(sp->EMPTYname, O_RDWR); FULLptr = sem_open(sp->FULLname, O_RDWR); /* Read data and produce items until EOF */ char line[100]; int id; while (feof(stdin) != 1){ /* read lines of data fron stdin */ /* ... read new data item (id and string) from stdin ... */ /* validate item id values for correctness */ fgets(line, 100, stdin);//store line into temp char array sscanf(line,"%d ",&id);//extract line from the temp char array above //check line numbers here <0 & > 110 are invalid if(id>=0 && id <=110){ //now store the line number and string in the item struct variable struct item *newItem = malloc(sizeof(struct item)); newItem->id = id; strcpy(newItem->str, line); //call the produce_item function with the above item struct variable produce_item(newItem); } else{ printf("line number %d out of range\n",id); } } /* At EOF ... */ /* Add a sentinal item in Buffer to signal end of data input */ struct item *newItem = malloc(sizeof(struct item)); newItem->id = SENTINEL; strcpy(newItem->str, ""); /* produce a sentinel value after EOF */ produce_item(newItem); /* Write a termination message */ sem_wait(MUTEXptr); /* protect printf as a critical section */ printf("Producer has exited cleanly\n"); sem_post(MUTEXptr); shm_unlink("nctvyc"); exit(0); }