Multithreading Problems



#include <iostream>
#include<pthread.h>
#include <semaphore.h>
#include <unistd.h>
/*
One thread is printing odd number and other thread is printing even numbers, Schedule both the thread in such a way, final o/p is 1234567890.
*/
using namespace std;
int count = 1;
int i = 0;
bool flag = false;
#define MAX 1000
int counter = 0;

pthread_mutex_t mutex;
pthread_cond_t cond;
void *even(void *data)
{
      while(count++<MAX)
      {
            pthread_mutex_lock(&mutex);
            while(counter%2 != 0)
            {
                  pthread_cond_wait(&cond, &mutex);
            }
            cout<<counter++<<" ";
            pthread_mutex_unlock(&mutex);
            pthread_cond_signal(&cond);
      }
      pthread_exit(0);
}
void *odd(void *data)
{
      while(count++<MAX)
      {
            pthread_mutex_lock(&mutex);
            while(counter%2 == 0)
            {
                  pthread_cond_wait(&cond, &mutex);
            }
            cout<<counter++<<" ";
            pthread_mutex_unlock(&mutex);
            pthread_cond_signal(&cond);
               
      }
      pthread_exit(0);
}

int main()
{
      pthread_t id[2];
     
      pthread_mutex_init(&mutex, 0);
      pthread_cond_init(&cond, 0);
     
      pthread_create(&id[0], NULL, even, NULL);
      pthread_create(&id[1], NULL, odd, NULL);
     
      pthread_join(id[0], NULL);
      pthread_join(id[1], NULL);
     
      return 0;
}
#include <iostream>
#include<pthread.h>
#include <semaphore.h>
/*
One tread print 1, second thread print 2, third thread prints 3, final output should be 123123123123123......
*/
using namespace std;
sem_t sem1;
sem_t sem2;
sem_t sem3;
int count = 1;
int i = 0;
bool flag = false;
#define MAX 1000

void *printOne(void *data)
{
      while(count++<MAX)
      {
            sem_wait(&sem1);
            cout<<"1 ";
            sem_post(&sem2);       
      }
      pthread_exit(0);
}
void *printTwo(void *data)
{
      while(count++<MAX)
      {
            sem_wait(&sem2);
            cout<<"2 ";
            sem_post(&sem3);
      }
      pthread_exit(0);
}
void *printThree(void *data)
{
      while(count++<MAX)
      {
            sem_wait(&sem3);
            cout<<"3 ";
            sem_post(&sem1);
      }
      pthread_exit(0);
}
int main()
{
      pthread_t id[3];
     
      sem_init(&sem1, 0, 1);
      sem_init(&sem2, 0, 0);
      sem_init(&sem3, 0, 0);
     
      pthread_create(&id[0], NULL, printOne, NULL);
      pthread_create(&id[1], NULL, printTwo, NULL);
      pthread_create(&id[3], NULL, printThree, NULL);
     
      pthread_join(id[0], NULL);
      pthread_join(id[1], NULL);
      pthread_join(id[2], NULL);
     
      sem_destroy(&sem1);
      sem_destroy(&sem2);
      sem_destroy(&sem3);
     
      return 0;

}














































































































































Comments

Popular posts from this blog

Kth most frequent Character in a given String

[16 Feb 2020] Given an array where every element occurs three times, except one element which occurs only once.

[17 Feb 2020] Given an array of integers, find the nearest smaller number for every element such that the smaller element is on left side.