Write a solution for the producer/consumer problem using processes instead

Write a solution for the producer/consumer problem using processes instead of threads. Refer to the solution worked out in the demo and make sure you fully understand what is going on there before beginning this one (source file is attached to this assignment). Turn in all code and a text file called README containing instructions for building and running your programs. All programs must compile and run on os.cs.siue.edu. Descriptions of functions listed below in bold can be found in the man pages (e.g. man fork), though sometimes you may need to specify the correct section of the manual to get the correct page (e.g. man 3 printf). Approach the homework in phases and incrementally grow your solution: start with process creation (e.g. if there’s one producer and one consumer specified, one process -the parent- will fork two others, a producer program and a consumer program), then move on to shared memory stuff (making sure that the children can read what the parent is placing in there), and only then finish up the producer/consumer portion (pretty much the same as the threaded version).

Use the following functions for process creation, etc:

fork – create a new process

execlp – replace current process image with a new process image (i.e. execute a file)

waitpid – allows parent to wait for a child with specified pid

Use the following functions for sharing stuff between processes:

shm_open – create a named chunk of shared memory

ftruncate – to change the size of the named chunk of shared memory

mmap – to map the shared memory chunk into current process’ address space

Example:

Let’s say you want to share a struct, like:

typedef struct shared{

   int some_int_to_share;

   int some_other_int_to_share;

   …

}shared;

In the parent process (the one that starts everything):

int fd = shm_open(“prodconshare”, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);

ftruncate(fd, sizeof(shared));

shared* sharedstruct = mmap(NULL, sizeof(shared), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

In the child process it is similar, but the child process would not create the named chunk if it doesn’t exist and the child process has no need to resize the chunk (so, no ftruncate):

int fd = shm_open(“prodconshare”, O_RDWR, S_IRUSR | S_IWUSR);

shared* sharedstruct = mmap(NULL, sizeof(shared), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

Use the following functions to synchronize the producers and consumers:

sem_init – initialize a semaphore

sem_wait – decrement a semaphore

sem_post – increment a semaphore

(remember that decrementing a semaphore below zero will put the calling process to sleep and sem_post will have to be called by another process to wake it up. You might think about stuffing some semaphores into the shared memory, using a struct to arrange the shared memory chunk’s contents.)

The producers can exit when they’ve produced their whole string, e.g. “hello world”.

For simplicity, the consumers may run indefinitely and be terminated with ctrl+c.

Other useful functions:

malloc – allocate memory

sizeof – get the size of things

printf – print stuff

perror – print error stuff

Share This Post

Email
WhatsApp
Facebook
Twitter
LinkedIn
Pinterest
Reddit

Order a Similar Paper and get 15% Discount on your First Order

Related Questions

Needed ASAP Your reflection will answer these questions: Is sustainability

Needed ASAP  Your reflection will answer these questions: Is sustainability an issue you consider when youpurchase something, why or why not? What have you learned about e-waste? What roledoes e-waste have in your life? What value can this initiative bring to others? This is yourreflection so share your experiences about

“Not in My Backyard” In 1991 Chemical Waste Management (Chem

  “Not in My Backyard” In 1991 Chemical Waste Management (Chem Waste), the country’s largest hazardous waste company, received approval to build California’s first commercial toxic-waste the incinerator at its Kettleman Hills dump site in the San Joaquin Valley.  A few weeks later Chem Waste, the state and the county

Complete and submit your Comprehensive Psychiatric Evaluation, including your differential

  Complete and submit your Comprehensive Psychiatric Evaluation, including your differential diagnosis and critical-thinking process to formulate a primary diagnosis. Incorporate the following into your responses in the template: Subjective: What details did the patient provide regarding their chief complaint and symptomology to derive your differential diagnosis? What is the duration

The HR director approached the HR team requesting a reassessment

 The HR director approached the HR team requesting a reassessment of the current performance appraisal process and expectations. The HR director has broken the department into small groups and has asked each to research and propose a performance appraisal process to move forward with. Once the proposals are submitted, they

Assume you are working at an agency with both inpatient

  Assume you are working at an agency with both inpatient and outpatient services. Mary is a client of yours in inpatient so you will be working with her one-on-one at least once a week in inpatient treatment then she will transfer levels (from Level III to Level II) at

The topic for your writing assignment is below. The writing

The topic for your writing assignment is below.   The writing assignment should be a minimum of 750 words. You should use information from the textbook and outside sources to complete the assignment in the textbook. You must include a Works Cited page and in-text citations. See the Writing Assignment Instructions for

Please answer the following questions: 1. What are the 3

Please answer the following questions: 1. What are the 3 usual response strategies to difficult people? 2. What are the components of active listening? Please create this paper in APA formatting with 2-3 pages of detail information. These 2-3 pages do not include the title page and the reference page.

Web Services Project 4 Page summary paper Investigate alternative tools

Premium Paper Help is a professional writing service that provides original papers. Our products include academic papers of varying complexity and other personalized services, along with research materials for assistance purposes only. All the materials from our website should be used with proper references.

Topic 1. Explain why focus group is a useful qualitative

Topic 1. Explain why focus group is a useful qualitative method of gathering information. Having you as a moderator of a community focus group that will explore knowledge, believes, attitudes and behaviors about your selected health problem: Define your group and the setting for the activity, applying statistical principles of sample size

2500 – 3000 words Homework on the mentioned topic with

2500 – 3000 words Homework on the mentioned topic with introduction and conclusion in Harvard referencing style.  Font size should be 11 or 12 in times new roman with 1.5 line spacing. If plagiarism is noticed the assignment will be marked as zero  Need 20 reference and quality Deadline is

The paper for your term project is due this week.

The paper for your term project is due this week. Your term project for this class is a 15 page marketing plan for the product or service that was approved in your week one proposal. It is to follow APA format and be of a quality graduate level. Don’t forget

evidence based therapeutic paper

Description  Write a 750-1,500 word APA scholarly paper by watching a formal interview conducted by any professional reporter, journalist or talk show host. a. Briefly describe the type of interview and the topic discussed. b. Name 3 therapeutic communication techniques used by the speaker/reporter/host and describe with verbatim examples with

Read through the scenarios below. Choose 2 scenarios and answer the following questions for each scenario.   If you use sources, remember to cite them in A

Read through the scenarios below. Choose 2 scenarios and answer the following questions for each scenario.   If you use sources, remember to cite them in APA format.  You will type the scenario and all your answers and upload them to CANVAS. What is the developmentally appropriate practice in each scenario? Explain your answer. What statements in NAEYC’s

When reading the stories of great heroes, Gods/Goddesses, and monsters,

When reading the stories of great heroes, Gods/Goddesses, and monsters, the Greeks covered a multitude of themes. Like heroism, justice, and vengeance, these themes are seen over and over. But not unlike the concept of love. Being either part or the focus of a great number of Greek myths, the

Nearly 56 million people died globally in 2016. More than

Nearly 56 million people died globally in 2016. More than half of all deaths in low-income countries in 2016 were caused by communicable diseases, maternal causes and nutritional deficiencies.  List the Top 10 Global Causes of Death from 2016. Why is it important to know the reasons individuals die? Choose