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);
}
}
DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma
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. Thisprogram will have two classes, a LineItem class and a Transaction class. Th
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
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
Get Free Quote!
342 Experts Online