Removeposition 0 else if removeposition 0 count 0

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

removePosition = 0; } else if (removePosition < 0 || count < 0) { // print out error and return -1 printf("error removing item!!!"); return -1; } // release the mutex and semfull to allow insert to run ReleaseMutex(Mutex); ReleaseSemaphore(SemFull, 1, NULL); return 0; } int main(int argc, char *argv[]) { int SleepTime, ProducerThreads, ConsumerThreads, i; DWORD ThreadId; HANDLE ConsumerThreadHandles [100], ProducerThreadHandles [100], CreatedThread; Mutex = CreateMutex(NULL, FALSE, NULL); SemFull = CreateSemaphore(NULL, 5, 5, NULL); SemEmpty = CreateSemaphore(NULL, 0, 5, NULL); //check for correct number of parameters if (argc != 4) { printf("must pass three positive integer parameters"); return -1; } SleepTime = atoi(argv[1]); ProducerThreads = atoi(argv[2]); ConsumerThreads = atoi(argv[3]);
Image of page 11

Subscribe to view the full document.

// check for valid parameters if (SleepTime < 0 || ProducerThreads < 0 || ConsumerThreads < 0) { printf("must pass three positive integer parameters"); return -1; } printf("\n"); // create the producer threads and insert into array for (i = 0; i < ProducerThreads; i++) { CreatedThread = CreateThread(NULL, 0, Producer, NULL, 0, &ThreadId); ProducerThreadHandles[i] = CreatedThread; } // create the consumer threads and insert into array for (i = 0; i < ConsumerThreads; i++) { CreatedThread = CreateThread(NULL, 0, Consumer, NULL, 0, &ThreadId); ConsumerThreadHandles[i] = CreatedThread; } // sleep and allow the program to run Sleep(SleepTime); // join on threads WaitForMultipleObjects(ProducerThreads, ProducerThreadHandles, TRUE, 0); WaitForMultipleObjects(ConsumerThreads, ConsumerThreadHandles, TRUE, 0); CloseHandle(ProducerThreadHandles); CloseHandle(ConsumerThreadHandles); return 0; }
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