logo Use CA10RAM to get 10%* Discount.
Order Nowlogo
(5/5)

prints five subsequent lines of letter A on the printer and keeps looping (about 10 times).

INSTRUCTIONS TO CANDIDATES
ANSWER ALL QUESTIONS

C++, Full Assignment description is attached as a file.

Simulate the concurrent execution of two threads using a single program:

Thread A: prints five subsequent lines of letter A on the printer and keeps looping (about 10 times).

Thread B: prints five subsequent lines of letter B on the printer and keeps looping

 

 

// This producer and consumer program is to show you how to create threads and use related system calls.

// Your homework will be a different program.

 

#include <stdio.h>

#include <pthread.h>

 

#define TIMES 10

#define CAPACITY 3

#define ITEMSIZE 20

 

struct Buffer

{

  int item[ITEMSIZE];

  int size;

};

 

void *producer();

void *consumer();

 

pthread_mutex_t mutex;

pthread_cond_t  full_cond;

pthread_cond_t  empty_cond;

struct Buffer buffer;

 

int

main()

{ int i;

  pthread_t tid;

 

  for (i =0; i< ITEMSIZE; i++) buffer.item[i]=-1;

  buffer.size = 0;

 

  pthread_setconcurrency(2);

 

  pthread_create(&tid, NULL, (void *(*)(void *))producer, NULL  );

  pthread_create(&tid, NULL, (void *(*)(void *))consumer, NULL );

 

 

 pthread_exit(0);

 

}

 

void *producer()

{ int i,j;

 

 for (j = 0; j<TIMES; j++)

 {pthread_mutex_lock(&mutex);  

 if (buffer.size == CAPACITY)

    {printf("Buffer is full. Producer is waiting...\n");

     pthread_cond_wait(&full_cond, &mutex);

    }

 else

    {

     printf("Producer adds one more item ...\n");

     buffer.item[buffer.size++] = buffer.size;

     for (i =0;i<buffer.size; i++)

     printf("%d ", buffer.item[i]);

     printf("\n");

     pthread_cond_broadcast(&empty_cond);

     

    }

  pthread_mutex_unlock(&mutex);

}

}

 

void *consumer()

{ int i,j;

 

for (j=0; j<TIMES; j++)

{

  pthread_mutex_lock(&mutex);

 

  if (buffer.size == 0)

    {

      printf("Buffer is empty. Consumer is waiting...\n");

      pthread_cond_wait(&empty_cond, &mutex);

    }

  else

    {

      printf("Consumer consumes an item from buffer...\n");

      buffer.size--;

      for (i=0; i<buffer.size; i++)

      printf("%d ", buffer.item[i]);

      printf("\n");

      pthread_cond_broadcast(&full_cond);

    }

 

   pthread_mutex_unlock(&mutex);

 }

 

}

 

 

 

 

(5/5)
Attachments:

Related Questions

. Introgramming & Unix Fall 2018, CRN 44882, Oakland University Homework Assignment 6 - Using Arrays and Functions in C

DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma

. The standard path finding involves finding the (shortest) path from an origin to a destination, typically on a map. This is an

Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t

. Develop a program to emulate a purchase transaction at a retail store. This program will have two classes, a LineItem class and a Transaction class. The LineItem class will represent an individual

Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th

. SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

. Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of Sea Ports. Here are the classes and their instance variables we wish to define:

1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of

Ask This Question To Be Solved By Our ExpertsGet A+ Grade Solution Guaranteed

expert
Um e HaniScience

730 Answers

Hire Me
expert
Muhammad Ali HaiderFinance

953 Answers

Hire Me
expert
Husnain SaeedComputer science

882 Answers

Hire Me
expert
Atharva PatilComputer science

571 Answers

Hire Me

Get Free Quote!

342 Experts Online