Experiment IV, producers and consumers

Source: Internet
Author: User

Experiment IV, producers and consumers

First, Experimental Purpose

1, master the concept of critical areas and the design principles of critical areas;

2, grasp the concept of signal volume, the meaning of PV operation and the application of PV operation to achieve synchronization and mutual exclusion of the process;

3, the analysis process contention resources phenomenon, learning to solve the process of mutual exclusion method.

Second, experimental content and requirements

Analysis of synchronization and mutual exclusion of process, programming to achieve classic process synchronization problem--the simulation of producer consumer problem

Producer-Consumer problem statement:

There is an annular buffer pool that contains n buffers (0~n-1).

There are two types of processes: a set of producer processes and a set of consumer processes, the producer process puts products into the empty buffer, the consumer process takes the product from the full buffer, all processes must access the buffer mutually exclusive, the producer cannot write data to the full buffer, the consumer cannot fetch the data from the empty buffer, That is, producers and consumers must synchronize the allocation and release of resources in a computer system: each process in a computer system can consume or produce a class of resources. When a process in a system uses a resource, it can be thought of as consumption, and the process is called a consumer. When a process frees up resources, it is quite a producer, defining the data structures in the producer consumer problem and initializing them.

Signal volume, initial value.

Write the PV operation.

Write the producer and consumer program, use the signal volume and its PV operation, realize the synchronization and mutual exclusion between producer and consumer.

Simulation shows the effect of synchronization and mutual exclusion between producers and consumers.

Optional: Simulates the effect of reader-writer problems.

Third, experimental methods and results test

Code implementation:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <time.h>

int main () {

int p=0,v=0,run=1,i=0,j,n;

printf ("Input buffer size:");

scanf ("%d", &n);

while (run==1) {

Srand (Time (NULL));

J=rand ()%2+1;

if (1==J)

{

if (i<5&&p==0&&v==0)

{

p++;

v++;

i++;

printf ("Production \ n");

p--;

v--;

}

else {

printf ("The warehouse is full cannot produce \ n");

}

}

Else

{

if (i>0&&p==0&&v==0)

{

p++;

v++;

i--;

printf ("Consumption \ n");

p--;

v--;

}

else {

printf ("No goods at present, so can not be consumed!") \ n ");

}

}

printf ("Whether to continue ———— 1. is 2. No \ n");

scanf ("%d", &run);

}

}

Results test:

Four, Experimental Summary

This experiment is generally simple, the use of random methods of production-consumption to choose. Although can simulate some effects, but also a bit of a flaw, that is, sometimes always appear unable to consume this option, causing the buffer has not been used, the bug will continue to improve.

Experiment IV, producers and consumers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.