Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use c/c++ Language,write the following code and here is the two sample of the code. You should use those samples (you can use one or

image text in transcribed

Use c/c++ Language,write the following code and here is the two sample of the code. You should use those samples (you can use one or both)

Sample-1

#include #include #include

int buff1[1000], buff2[1000]; sem_t sem1; sem_t sem2; sem_t sem3; int total=0; void* read(){ sem_wait(&sem1); FILE *ptr; ptr = fopen("Number.txt", "r"); int i=0; while(!feof(ptr)) { total=i; fscanf(ptr, "%d", &buff1[i]); i++; } sem_post(&sem2); } void* copy(void* args){ sem_wait(&sem2); int i; for(i=0; i buff2[i]=buff1[i]; } sem_post(&sem3); } void* print(void* args){ sem_wait(&sem3); int i; printf("From Buffer "); for(i=0; i printf("%d ",buff2[i]); } } int main() { pthread_t thread1,thread2,thread3; sem_init(&sem1,0,1); sem_init(&sem2,0,0); sem_init(&sem2,0,0); pthread_create(&thread1,NULL,&read,NULL); pthread_create(&thread2,NULL,copy,NULL); pthread_create(&thread3,NULL,print,NULL); pthread_join(thread1,NULL); pthread_join(thread2,NULL); pthread_join(thread3,NULL); }

Sample-2

semaphore empty1 = 1; semaphore empty2 = 1; semaphore full11 = 0; semaphore full12 = 0; Process_A () { while(1) { wait(empty1); read(next_file(), Buffer_1); signal(full1); } } Process_B () { while(1) { wait(full1); wait(empty2); copy(Buffer_2, Buffer_1); signal(empty1); signal(full2); } } Process_C () { while(1) { wait(full2); print(Buffer_2); signal(empty2); } }

You should use above Structure for solving this problem

Edit the code show output also(code shouldn't have any error)

1 (10) Three processes are involved in printing a file (pictured below). Process A reads the file data from the disk to Buffer 1, Process B copies the data from Buffer 1 to Buffer 2, finally Process C takes the data from Buffer 2 and print it. Process A Process C Process B -> Buffer 1 |-- copy Buffer 2 Read from File print Assume all three processes operate on one (file) record at a time, both buffers' capacity are one record. Write a program to coordinate the three processes using semaphores

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 Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions

Question

Distinguish between formal and informal reports.

Answered: 1 week ago