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

  Cross-cultural opportunities and conflicts in canada

Short Paper on Cross-cultural Opportunities and Conflicts in Canada.

  Sociology theory questions

Sociology are very fundamental in nature. Role strain and role constraint speak about the duties and responsibilities of the roles of people in society or in a group. A short theory about Darwin and Moths is also answered.

  A book review on unfaithful angels

This review will help the reader understand the social work profession through different concepts giving the glimpse of why the social work profession might have drifted away from its original purpose of serving the poor.

  Disorder paper: schizophrenia

Schizophrenia does not really have just one single cause. It is a possibility that this disorder could be inherited but not all doctors are sure.

  Individual assignment: two models handout and rubric

Individual Assignment : Two Models Handout and Rubric,    This paper will allow you to understand and evaluate two vastly different organizational models and to effectively communicate their differences.

  Developing strategic intent for toyota

The following report includes the description about the organization, its strategies, industry analysis in which it operates and its position in the industry.

  Gasoline powered passenger vehicles

In this study, we examine how gasoline price volatility and income of the consumers impacts consumer's demand for gasoline.

  An aspect of poverty in canada

Economics thesis undergrad 4th year paper to write. it should be about 22 pages in length, literature review, economic analysis and then data or cost benefit analysis.

  Ngn customer satisfaction qos indicator for 3g services

The paper aims to highlight the global trends in countries and regions where 3G has already been introduced and propose an implementation plan to the telecom operators of developing countries.

  Prepare a power point presentation

Prepare the power point presentation for the case: Santa Fe Independent School District

  Information literacy is important in this environment

Information literacy is critically important in this contemporary environment

  Associative property of multiplication

Write a definition for associative property of multiplication.

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