Pthreadattrinitattr pthreadcreatetid attr consumer

This preview shows page 7 - 12 out of 12 pages.

pthread_attr_init(&attr); pthread_create(&tid, &attr, consumer, NULL); } //Sleep for user specified time sleep(sleepTime); return 0;
Image of page 7

Subscribe to view the full document.

} void *producer(void *param) { buffer_item random; int r; while(TRUE) { r = rand() % 5; sleep(r); random = rand(); if(insert_item(random)) fprintf(stderr, "Error"); printf("Producer produced %d \n", random); } } void *consumer(void *param) { buffer_item random; int r; while(TRUE) { r = rand() % 5; sleep(r); if(remove_item(&random)) fprintf(stderr, "Error Consuming"); else printf("Consumer consumed %d \n", random); } }
Image of page 8
windowsConsumerProducer.c //This program creates a producer and consumer thread. #include <stdio.h> #include <stdlib.h> #include <windows.h> //#include "buffer.h" #define BUFFER_SIZE 5 typedef int buffer_item; buffer_item buffer[BUFFER_SIZE]; HANDLE Mutex; HANDLE SemEmpty, SemFull; int insertPosition = 0, removePosition = 0, count =0; // define the insert and remove methods int insert_item(buffer_item); int remove_item(buffer_item *); //DWORD WINAPI Producer(LPVOID Param); //DWORD WINAPI Consumer(LPVOID Param); // the code the producer thread runs DWORD WINAPI Producer(LPVOID Param) { buffer_item randomproduced; while (TRUE) { Sleep(2000); randomproduced = rand(); if (insert_item(randomproduced)) { printf("could not insert item"); } } return 0; } // the code the consumer thread runs DWORD WINAPI Consumer(LPVOID Param)
Image of page 9

Subscribe to view the full document.

{ buffer_item random; while (TRUE) { Sleep(2000); if (remove_item(&random)) { printf("could not remove item"); } else { } } return 0; } int insert_item(buffer_item item) { WaitForSingleObject(SemFull, INFINITE); WaitForSingleObject(Mutex, INFINITE); //insert the item ++count; printf("producer produced %d count = %d\n", item, count); buffer[insertPosition] = item; ++insertPosition; if (insertPosition >= 5) { insertPosition = 0; } else if (insertPosition < 0 || count > 5) { // print out error and return -1 printf("error removing item!!!"); return -1; } //release the mutex and semempty to allow remove to run ReleaseMutex(Mutex); ReleaseSemaphore(SemEmpty, 1, NULL); return 0; } int remove_item(buffer_item *item) { WaitForSingleObject(SemEmpty, INFINITE);
Image of page 10
WaitForSingleObject(Mutex, INFINITE); // remove the item --count; item[0] = buffer[removePosition]; printf("                           consumer consumed %d count = %d\n", item[0], count); ++removePosition; if (removePosition >= 5) {
Image of page 11

Subscribe to view the full document.

Image of page 12
You've reached the end of this preview.
  • Spring '12
  • GwangS.Jung
  • producer, monitor, Reentrant mutex

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern