Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <mutex>
- #include <stdio.h>
- #include <semaphore.h>
- #include <random>
- #include <unistd.h>
- using namespace std;
- sem_t f, e;
- std::mutex counterMutex, bufferMutex, indexMutex;
- int MAX_SLEEPING_TIME_COLLECTOR = 20000000;
- int MAX_SLEEPING_TIME_MONITOR = 1000;
- int MAX_SLEEPING_TIME_COUNTER = 10;
- int NUMBER_OF_THREADS = 1000;
- int NUMBER_OF_MONITORS = 5;
- int NUMBER_OF_COLLECTORS = 2;
- int MAXIMUM_SIZE = 100;
- int counter = 0;
- int monitorIndex = 0;
- int collectorIndex = 0;
- int buffer [MAXIMUM_SIZE] = {0};
- void *count(void *args);
- void *monitor(void *args);
- void *collect(void *args);
- void *helpCounter(void *args);
- void *helpMonitor(void *args);
- void *helpCollector(void *args);
- void *helpCounter(void *args)
- {
- pthread_t counter[NUMBER_OF_THREADS];
- for(int i = 0; i < NUMBER_OF_THREADS; i++)
- {
- pthread_create(&counter[i],NULL,count,(void*)&(i));
- pthread_join(counter[i],NULL);
- usleep(rand() % MAX_SLEEPING_TIME_COUNTER);
- }
- return NULL;
- }
- void *helpMonitor(void *args)
- {
- pthread_t mmonitorThread;
- for(int i = 0; i < NUMBER_OF_MONITORS; i++)
- {
- pthread_create(&mmonitorThread,NULL,monitor,NULL);
- pthread_join(mmonitorThread,NULL);
- usleep(rand() % 100 + MAX_SLEEPING_TIME_MONITOR);
- }
- return NULL;
- }
- void *helpCollector(void *args)
- {
- pthread_t collectorThread;
- pthread_create(&collectorThread,NULL,collect,NULL);
- pthread_join(collectorThread,NULL);
- return NULL;
- }
- void *collect(void* args)
- {
- if(buffer[collectorIndex] == -1){
- cout << "NOTHING TO COLLECT";
- return;
- }
- std::cout << "Monitor thread : reading a count value of = " << buffer[collectorIndex] << "from index = " << collectorIndex % MAXIMUM_SIZE << endl;
- buffer[collectorIndex] = -1;
- collectorIndex++;
- return NULL;
- }
- void *monitor(void *args)
- {
- counterMutex.lock();
- std::cout << "Monitor thread : waiting to read a counter" << endl;
- std::cout << "Monitor thread : reading a count value of = " << counter << endl;
- bufferMutex.lock();
- if(monitorIndex % MAXIMUM_SIZE == -1){
- buffer[monitorIndex % MAXIMUM_SIZE] = counter;
- monitorIndex++;
- }
- else{
- cout << "Buffer is FULL!" << endl;
- }
- bufferMutex.unlock();
- counter = 0;
- counterMutex.unlock();
- return NULL;
- }
- void *count(void *args)
- {
- /*
- indexMutex.lock();
- std::cout << "Counter thread " << *i << " : received a message" << endl;
- std::cout << "Counter thread " << *i << " : waiting to write" << endl;
- indexMutex.unlock();
- */
- counterMutex.lock();
- int* i = (int*) args;
- std::cout << "Counter thread " << *i << " : received a message" << endl;
- std::cout << "Counter thread " << *i << " : waiting to write" << endl;
- counter++;
- std::cout << "Counter thread " << *i << " : now adding to counter, counter value = "<< counter << endl;
- counterMutex.unlock();
- return NULL;
- }
- int main()
- {
- //std::cout << "Welcome to" << args[0] << " Synchronization" << endl;
- // NUMBER_OF_THREADS = args[1];
- freopen("yaraDeadOutput.txt","w",stdout);
- srand(time(NULL));
- for(int i = 0; i < MAXIMUM_SIZE; i++)
- buffer[i] = -1;
- pthread_t counter,mmonitor,collector;
- pthread_create(&counter,NULL,helpCounter,NULL);
- pthread_create(&mmonitor,NULL,helpMonitor,NULL);
- pthread_create(&collector,NULL,helpCollector,NULL);
- pthread_join(counter,NULL);
- pthread_join(mmonitor,NULL);
- pthread_join(collector,NULL);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement