Write producer and consumer programs that communicate

Assignment Help Other Subject
Reference no: EM133381290

Operating System Principles

Assignment - Multiprocessing and Shared Memory

In this assignment, you will write producer and consumer programs that communicate through a bounded buffer in shared memory. The code that you will need to write will be based on the code discussed in class for forking a child process, creating a shared memory block and implementing a bounded buffer accessed by a producer and a consumer. Write your code in the given producer_template.c and consumer_template.c source files, and then rename them to producer.c and consumer.c. Follow the instructions provided in these files. Note that the low-level functions for reading from and writing to the shared memory block are already provided to you. Please use the provided functions; do not write your own implementation of an already provided function.

As you can see, in the given producer template.c source file, the producer process is the parent process that creates (forks) a child process and loads the consumer executable into it. The consumer executable will be generated by compiling consumer.c (after you write the required code in it). After writing all the required code in both consumer.c and producer.c, use the following commands to compile the two programs and build the two executables:
gcc producer.c -lrt -o producer
gcc consumer.c -lrt -o consumer

The producer program (parent) takes the following three command-line arguments (all integers):
1. The size of the bounded buffer (bufSize). This is the number of items that can fit in the bounded buffer. This cannot exceed 600. Your program should check if the value of this-command line argument is within the valid range (2 to 600); if not, it should print an appropriate error message and terminate.
2. The number of items to produce/consume (itemCnt). Your program should work correctly for any itemCnt that is greater than 0. However, in an interesting test case, itemCnt will be much larger than bufSize. If your program works only if itemCnt is less than bufSize, you won't get any credit, as you won't be solving the bounded-buffer problem.
3. A seed for the random number generator (randSeed). This will allow us to test the program using different data.

The producer program creates a shared memory block with a fixed size of 4K. Let the name of the shared memory block be OS_HW1_yourName, where "yourName" should be your actual full name. The purpose of this is to avoid interfering with other students' work if you happen to be running your programs on the same shared server. The producer then uses this block to communicate with the consumer. The block consists of a header followed by the bounded buffer with the given bufSize. The header contains the following four integers in order:
1. bufSize
2. itemCnt
3. in: the index of the next item to be produced
4. out: the index of the next item to be consumed

The main in the producer program first calls the function InitShm() to create the shared memory block and initialize its header with the above four integer values. It then forks a child process and loads the consumer executable into it. The parent process calls the function Producer() to actually generate the items and write them into the bounded buffer. The producer must generate itemCnt random integers in the range 0 to 3000. Simply call the provided GetRand() function to do that. The key point is that when the bounded buffer is full, the producer must wait until the consumer has consumed at least one item.

The consumer program, in turn, performs the following main steps:

1. Opening and mapping the shared memory block created by the producer. The shared memory block name must match the name of the block created by the producer: OS_HW1_yourName.
2. Reading the four integer values from the header.
3. Reading all the items written by the producer to the shared memory buffer (their count is itemCnt). The key point is that when the bounded buffer is empty, the consumer must wait until the producer has produced at least one more item.

The producer/consumer code that you will need to write will be very similar to the bounded-buffer producer/consumer pseudo-code given in class, but make sure that you do the following:
1. Whenever you read any of the shared variables (in and out), you must read the latest value from the shared buffer using the provided GetIn() and GetOut() functions.
2. Whenever you update any of the shared variables (in and out), you must update their values in the shared buffer using the provided SetIn() and SetOut() functions.
3. Reading from and writing into the shared buffer should be done using the provided ReadAtBufIndex() and WriteAtBufIndex() functions.
4. Use the provided GetRand() function to generate a random number in the above-specified range.

Reference no: EM133381290

Questions Cloud

Culminating argument paper : West Coast University - culminating argument paper is a restructuring of the first two papers with the addition of an abstract, introduction, and conclusion
Do you think this conforms with the principles set : Do you think this conforms with the principles set out in Buddhism? Why or why not? How do you think karma and compassion come into play in this story
How will visualize the data as a result of data analytics : How will you visualize the data as a result of data analytics and how will you use SQL to achieve the data you need?
Somatic symptom disorder might interpret catastrophically : What are some examples of normal physical symptoms that someone with somatic symptom disorder might interpret catastrophically?
Write producer and consumer programs that communicate : CSC139 Operating System Principles, California State University - write producer and consumer programs that communicate through a bounded buffer in shared
Do you consider either of these a noncash scheme : Is taking a box of paper clips from the supply cabinet the same as using a company car on a driving vacation? Do you consider either of these a noncash scheme?
Relationship between antecedent conditions and behavior : How does a psychology experiment demonstrate a cause-and-effect relationship between antecedent conditions and behavior?
Explain how mr. ahmad advise his staff on how to account : Explain how Mr. Ahmad advise his staff on how to account for each transaction below along with reasons to educate him. A customer purchased the shoes from
Genetic defects in their family : Should partners be obligated to inform each other of any genetic defects in their family?

Reviews

Write a Review

Other Subject Questions & Answers

  Explain critical manufacturing organization

Describe two to three challenges you will have when implementing these guidelines and practices

  Discuss what you have drawn from reviewing literature

Gives a quick idea of the topic of the literature review, such as the central theme or organizational pattern - Discuss what you have drawn from reviewing literature so far. Where might the discussion proceed?

  Analyze points of conflict that would arise with children

In light of the differing styles and expectations, analyze points of conflict that would arise with the children when the parents swapped households and why

  Compare sculptures of ancient india and greece

Select any two (2) of these rulers: Ramses II, Shihuangdi, Constantine, Ashoka, Pericles, or Charlemagne. Select rulers from different cultures.

  Lab value is predictive of postoperative morbidity-mortality

Which of the following lab values is most predictive of postoperative morbidity and mortality?

  Identify and briefly discuss two types of legislation

Identify and briefly discuss two types of legislation that most direct affect employee wage rates in Ontario

  Discuss food deserts

Discuss Food Deserts in this Discussion Thread focusing on why they exist in the first place and how we can improve the situation for generations to come.

  Propose an evidence-based practice intervention

They are having to do manual chart audits instead of using the reporting tools that are a part of the electronic health record (EHR). After reviewing their exam

  Brief introduction that identifies issue you have chosen

Give a brief introduction that identifies the issue you have chosen and why. Describe in three separate paragraphs how each perspective would view or explain the issue. Include elements of culture and how humans learn, develop, and become integrated ..

  Write a brief description in which you describe the place

Think about a place today or in your past that you would call "your favorite place." Write a brief description in which you describe this place as vividly.

  How can nation overcome problems of race relations

How can our nation overcome problems of race relations in establishing a fair and equal society? How have conditions for African Americans changed?

  Explain three financial goals have for covering expenses

Explain three financial goals you have for covering your college expenses. For example, you might want to consider work study or taking out another loan.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd