Read Chapter 6 – Process Synchronization Create a console-based application that
ID: 2246517 • Letter: R
Question
Read Chapter 6 – Process Synchronization Create a console-based application that spawns two threads: a producer and a consumer. Use any programming language. The producer should open an input file and repeatedly copy values to a circular buffer. The consumer should open an output file and repeatedly copy values from the same circular buffer to the file. For both the producer and consumer, a random number of bytes between 1 and n, inclusive, should be copied each iteration, where n is specified by the user. The number of bytes should be randomized each iteration. If the producer is unable to write to the buffer (because it does not contain enough empty elements), or if the consumer is unable to read from the buffer (because it does not contain enough unread items), then it should proceed to the next iteration, choosing a new random number of bytes to copy. (Exception: Once the producer has already read the entire file, then the consumer does not have to continue generating random numbers until it gets an exact hit; instead, it should write the rest of the buffer to complete the copy.) When the program completes, the output file should be an exact copy of the input file.The syntax of the program should be as follows: copyfile input output n m
where
copyfile is the name of your executable input is the name of the input file output is the name of the output file n is the maximum number of bytes to copy in any given iteration (described above) m is the size of the circular buffer, in bytes Read Chapter 6 – Process Synchronization Create a console-based application that spawns two threads: a producer and a consumer. Use any programming language. The producer should open an input file and repeatedly copy values to a circular buffer. The consumer should open an output file and repeatedly copy values from the same circular buffer to the file. For both the producer and consumer, a random number of bytes between 1 and n, inclusive, should be copied each iteration, where n is specified by the user. The number of bytes should be randomized each iteration. If the producer is unable to write to the buffer (because it does not contain enough empty elements), or if the consumer is unable to read from the buffer (because it does not contain enough unread items), then it should proceed to the next iteration, choosing a new random number of bytes to copy. (Exception: Once the producer has already read the entire file, then the consumer does not have to continue generating random numbers until it gets an exact hit; instead, it should write the rest of the buffer to complete the copy.) When the program completes, the output file should be an exact copy of the input file.
The syntax of the program should be as follows: copyfile input output n m
where
copyfile is the name of your executable input is the name of the input file output is the name of the output file n is the maximum number of bytes to copy in any given iteration (described above) m is the size of the circular buffer, in bytes Read Chapter 6 – Process Synchronization Create a console-based application that spawns two threads: a producer and a consumer. Use any programming language. The producer should open an input file and repeatedly copy values to a circular buffer. The consumer should open an output file and repeatedly copy values from the same circular buffer to the file. For both the producer and consumer, a random number of bytes between 1 and n, inclusive, should be copied each iteration, where n is specified by the user. The number of bytes should be randomized each iteration. If the producer is unable to write to the buffer (because it does not contain enough empty elements), or if the consumer is unable to read from the buffer (because it does not contain enough unread items), then it should proceed to the next iteration, choosing a new random number of bytes to copy. (Exception: Once the producer has already read the entire file, then the consumer does not have to continue generating random numbers until it gets an exact hit; instead, it should write the rest of the buffer to complete the copy.) When the program completes, the output file should be an exact copy of the input file.
The syntax of the program should be as follows: copyfile input output n m
where
copyfile is the name of your executable input is the name of the input file output is the name of the output file n is the maximum number of bytes to copy in any given iteration (described above) m is the size of the circular buffer, in bytes
Explanation / Answer
#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>
#define SIZES 4
#define FULLENGTH 0
#define MT 0
char buff[SIZES];
int nxt = 0;
int nextOut = 0;
sem_t mt_mut;
sem_t full_mut;
void Install(char box)
{
int val;
sem_wait(&mt_mut);
buff[nxt] = box;
nxt = (nxt + 1) % SIZES;
printf("Producing %c ...nxt %d..Ascii=%d ",box,nxt,box);
if(nxt==FULLENGTH)
{
sem_post(&full_mut);
sleep(1);
}
sem_post(&mt_mut);
}
void * Prods()
{
int uu;
for(uu = 0; uu < 10; uu++)
{
Install((char)('A'+ uu % 26));
}
}
void Receive()
{
int box;
sem_wait(&full_mut);
box = buff[nextOut];
nextOut = (nextOut + 1) % SIZES;
printf(" ...Consuming %c ...nextOut %d..Ascii=%d ",box,nextOut,box);
if(nextOut==MT)
{
sleep(1);
}
sem_post(&full_mut);
}
void * Cons()
{
int uu;
for(uu = 0; uu < 10; uu++)
{
Receive();
}
}
int main()
{
pthread_t ptid,ctid;
sem_init(&mt_mut,0,1);
sem_init(&full_mut,0,0);
if(pthread_create(&ptid, NULL,Prods, NULL))
{
printf(" ERROR creating thread 1");
exit(1);
}
if(pthread_create(&ctid, NULL,Cons, NULL))
{
printf(" ERROR creating thread 2");
exit(1);
}
if(pthread_join(ptid, NULL))
{
printf(" ERROR joining thread");
exit(1);
}
if(pthread_join(ctid, NULL))
{
printf(" ERROR joining thread");
exit(1);
}
sem_destroy(&mt_mut);
sem_destroy(&full_mut);
pthread_exit(NULL);
return 1;
}
Rate the answer an upvote.......Thankyou
Hope this helps.....
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.