Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The purpose is to learn how to use semaphores to protect a limited size resource. A circular buffer with 15 positions (each position stores 1

The purpose is to learn how to use semaphores to protect a limited size resource. A circular buffer with 15 positions (each position stores 1 character) is to be used to communicate information between two threads (producer and consumer). The producer thread will read characters, one by one from a file and place it in the buffer and continue to do that until the end-of-file (EOF) marker is reached. The name of the file must be mytest.dat when you are submitting the program of course you can use your own file while individually testing your program. There should be no more than 150 characters in the file. The producer must inform the consumer when it has finished placing the last character in the buffer. The producer could do this by placing a special character for example, * in the shared buffer or by using a shared memory flag that the producer sets to true and the consumer reads at the appropriate time Consumer thread will read the characters, one by one, from the shared buffer and print it to the screen. A parent process will create both producer and consumer threads and will wait until both are finished to destroy semaphores. The consumer should run slower than producer. So, place a one second sleep in the consumer thread between reads from the shared memory. Sample Output: The output your program produces will be reconstruction of the original thread contained in mytest.dat. It need not include the * character. Use the following header for your program #define _REENTRANT #include #include #include #include #include #include #include #include You will require 3 semaphores; The buffer should be treated as circular buffer Below is a piece of C code that gives you some idea of how to open file and read from file char newChar; FILE* fp; fp= fopen("mytest.dat", "r"); while(fscanf(fp,"%c",&newChar) != EOF) .. close(fp); To compile program use the command: gcc name_of_program.c -lpthread -lrt The semaphore functions: sem_t sem1; sem_wait(&sem1); sem_post(&sem1); sem_init(&sem1, ,); sem_destroy(&sem1); Useful commands for threads: pthread_t tid1[1]; /* process id for thread 1 */ pthread_t tid2[1]; /* process id for thread 2 */ pthread_attr_t attr[1]; /* attribute pointer array */ fflush(stdout); /* Required to schedule thread independently.*/ pthread_attr_init(&attr[0]); pthread_attr_setscope(&attr[0], PTHREAD_SCOPE_SYSTEM); /* end to schedule thread independently */ /* Create the threads */ pthread_create(&tid1[0], &attr[0], thread1, NULL); pthread_create(&tid2[0], &attr[0], thread2, NULL); /* Wait for the threads to finish */ pthread_join(tid1[0], NULL); pthread_join(tid2[0], NULL); . Terminate threads pthread_exit(NULL);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administration The Complete Guide To Dba Practices And Procedures

Authors: Craig S. Mullins

2nd Edition

0321822943, 978-0321822949

More Books

Students also viewed these Databases questions

Question

Develop a PERT diagram. AppendixLO1

Answered: 1 week ago

Question

=+90 percent of all oil refineries) into several smaller companies

Answered: 1 week ago