Experiment 3: Process Control-2 (Semaphore)

  1. Write producer and consumer programs that require:

  1. The two programs of the producer and the consumer share a warehouse, and the warehouse is an ordinary file (/tmp/store) with a capacity of 100 bytes;

  1. Producers put resources into warehouses, and consumers consume resources from warehouses; resources are numeric characters "1, 2, 3, 4, 5, 6, 7, 8, 9, 0", a resource is a number, 10 Numbers are generated cyclically;

  1. The producer creates a warehouse (/tmp/store) and produces a resource at an interval of 1s. When the warehouse is full (the number of resources reaches 100), the producer cannot continue to produce; the consumer consumes a resource at an interval of 2s. When the warehouse is empty At this time, consumers cannot continue to consume;

  1. Consumers spend each time

  1. 1 resource, first print out the number of resources and the number of vacancies in the warehouse before consumption, then print out the number of resources and the number of vacancies in the warehouse after consumption, and print out the content of the consumed resources;

  1. The producer produces one resource each time, first prints out the number of resources and the number of vacancies in the warehouse before production, then prints out the quantity of resources and the number of vacancies in the warehouse after production, and prints out the content of the produced resources.

  1. After consumers consume resources, they need to delete the consumed resources from the warehouse;

  1. Use semaphores to implement process synchronization and mutual exclusion.

[Hint] There are many solutions to the problem, you can use one or more semaphores, you can also use semaphores + file locks, or use other suitable methods.

operation result:

Guess you like

Origin blog.csdn.net/weixin_52357218/article/details/128619087