Semaphore and PV operations

Source: Internet
Author: User
From: orders. 1. semaphore Concept 1. semaphore type definition each semaphore must record at least two pieces of information: the semaphore value and the process queue waiting for the semaphore. Its type is defined as follows: (expressed in Pascal-like languages)
  1. Semaphore = record
  2. Value: integer;
  3. Queue: ^ PCB;
  4. End;

PCB is the process control block and the data structure created by the operating system for each process.

S. value> = 0, S. queue is empty; S. value <0, S. the absolute value of value is S. number of waiting processes in the queue; 2. PV primitive can perform two primitive operations on a semaphore variable: P operation and V operation, which are defined as follows:
  1. Procedure P (var s: samephore );
  2. {
  3. S. value = S. Value-1;
  4. If (S. value <0) asleep (S. Queue );
  5. }
  6. Procedure V (var s: samephore );
  7. {
  8. S. value = S. Value + 1;
  9. If (S. value <= 0) wakeup (S. Queue );
  10. }
Two standard processes are used: asleep (S. queue); the PCB of the process that executes this operation enters S. at the end of the queue, the process changes to the waiting state wakeup (S. queue. the queue header processes wake up and insert into the ready queue S. when the initial value is 1, it can be used to achieve mutual exclusion of processes. P and V Operations are non-disruptive program segments, called primitives. If semaphores are treated as shared variables, PV operations are the critical section. Multiple processes cannot be executed at the same time, which is generally ensured by hardware. A semaphore can only be set to an initial value once. In the future, only P or V operations can be performed on it. As you can see, the semaphore mechanism must have public memory and cannot be used in distributed operating systems. This is its biggest weakness. Ii. instance 1. producer-consumer problem (with buffer) Description: A warehouse can store K items. Each time a producer produces a product, the product is placed in a warehouse, and production stops when the Warehouse is full. Each time a consumer consumes an item from the warehouse, the consumer stops consuming the item when the Warehouse is empty. A: Process: producer-producer process. The data structure of the consumer-consumer process is buffer: array [0 .. k-1] of integer; In, out: 0 .. k-1;-in records the first empty buffer, out records the first non-empty buffer S1, S2, mutex: semaphore;-S1 control buffer is not satisfied, S2 control buffer is not empty, mutex protects the critical zone; initialize S1 = K, S2 = 0, mutex = 1
  1. Producer (producer process ):
  2. Item_type item;
  3. {
  4. While (true)
  5. {
  6. Produce (& item );
  7. P (S1 );
  8. P (mutex );
  9. Buffer [in]: = item;
  10. In: = (in + 1) mod k;
  11. V (mutex );
  12. V (S2 );
  13. }
  14. }
  15. Consumer (Consumer process ):
  16. Item_type item;
  17. {
  18. While (true)
  19. {
  20. P (S2 );
  21. P (mutex );
  22. Item: = buffer [out];
  23. Out: = (out + 1) mod k;
  24. V (mutex );
  25. V (S1 );
  26. Consume (& item );
  27. }
  28. }
2. First-class read-Writer Problem description: Some readers and some writers read and write the same blackboard. Multiple readers can read the blackboard at the same time, but only one writer can at a time. Readers cannot use the blackboard at the same time. Different requirements on blackboard priority enable readers-to-writers to fall into several categories. The first type of problem specifies that the reader has a high priority and only allows the writer to use the blackboard when there are no readers. Answer: Process: writer process, reader-reader process data structure: read_account: integer; r_w, mutex: semaphore;-r_w controls who uses the blackboard, mutex protects the critical section, the initial value is 1.
  1. Reader-(Reader process ):
  2. {
  3. While (true)
  4. {
  5. P (mutex );
  6. Read_account ++;
  7. If (read_account = 1) P (r_w );
  8. V (mutex );
  9. Read ();
  10. P (mutex );
  11. Read_account --;
  12. If (read_account = 0) V (r_w );;
  13. V (mutex );
  14. }
  15. }
  16. Writer process ):
  17. {
  18. While (true)
  19. {
  20. P (mutex );
  21. Write ();
  22. V (mutex );
  23. }
  24. }
3. Philosophers Problem description: There are five philosophers in a room. Their life is thinking and eating. There is a round table in the room with a dish of macaroni in the middle (assuming there is no limit to the number of macaroni ). There are five chairs around the table. Each of the five philosophers has a fork. The philosophers must use both the left and right forks when eating. Answer: Process: data structure and process in total by philosopher-philosophers: State: array [0 .. 4] of (think, hungry, eat); Ph: array [0 .. 4] of semaphore;-each philosopher has a semaphore whose initial value is 0 mutex: semaphore;-mutex protects the critical section. Initial Value = 1
  1. Procedure test (I: 0 .. 4 );
  2. {
  3. If (State [I] = hungry) and (State [(I + 1) mod 5] <> eating)
  4. And (State [(I-1) mod 5] <> eating ))
  5. {State [I] = eating;
  6. V (pH [I]);
  7. }
  8. }
  9. Philosopher (I: 0 .. 4 ):
  10. {
  11. While (true)
  12. {
  13. Think ();
  14. P (mutex );
  15. State [I] = hungry;
  16. Test (I );
  17. V (mutex );
  18. P (pH [I]);
  19. Eat ();
  20. P (mutex );
  21. State [I] = think;
  22. Test (I-1) mod 5 );
  23. Test (I + 1) mod 5 );
  24. V (mutex );
  25. }
  26. }

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.