C language wait () semaphores part signal () semaphores part code

Source: Internet
Author: User
Tags semaphore code

Http://blog.csdn.net/raykid13/archive/2008/10/16/3087858.aspx

The semaphore structure is expressed in C as follows:

    1. TypedefStruct{
    2. IntValue;// Records the semaphore Value
    3. StructProcess * List;// Store the process waiting for this semaphore
    4. } Semaphore;

Wait () semaphoresCodeAs follows:

    1. Wait (semaphore * s ){
    2. S-> value --;
    3. If(S-> value <0 ){
    4. AddThisProcess to s-> list;
    5. Block ();
    6. }
    7. }

Part of the signal () semaphore code is as follows:

    1. Signal (semaphore * s ){
    2. S-> value ++;
    3. If(S-> value <= 0 ){
    4. Remove a process P from S-> list;
    5. Wakeup (P );
    6. }
    7. }

1. the bounded-buffer problem:

Full is initialized to 0, empty is initialized to N, and mutex is 1

  1. Do{
  2. Wait (full );
  3. Wait (mutex );
  4. ...
  5. // Remove an item from buffer to nextc
  6. ...
  7. Signal (mutex );
  8. Signal (empty );
  9. ...
  10. // Consume the item in nextc
  11. ...
  12. }While(True );

II. The readers-writers problem:

WRT Initialization is 1, readcount Initialization is 0, mutex is 1

Writer operation:

 

    1. Do{
    2. Wait (WRT );
    3. ...
    4. // Writing is saved med
    5. ...
    6. Signal (WRT );
    7. }While(True );

Reader operation:

 

  1. Do{
  2. Wait (mutex );// Ensure that operations with signal (mutex) are not interrupted by other readers
  3. Readcount ++;
  4. If(Readcount = 1)
  5. Wait (WRT );
  6. Signal (mutex );
  7. ...
  8. // Reading is already med
  9. ...
  10. Wait (mutex );
  11. Readcount --;
  12. If(Readcount = 0)
  13. Signal (WRT );
  14. Signal (mutex );
  15. }While(True );

III. The dining-philosophers problem:

All chopstick [5] is initialized to 1.

  1. Do{
  2. Wait (chopstick [I]);
  3. Wait (chopstick [(I + 1) % 5]);
  4. ...
  5. // Eating
  6. ...
  7. Signal (chopstick [I]);
  8. Signal (chopstick [(I + 1) % 5]);
  9. ...
  10. // Thinking
  11. ...
  12. }While(True );

But the biggest problem with this solution is its deadlock. So I think we should add a semaphore mutex and initialize it to 1:

  1. Do{
  2. Wait (mutex );
  3. Wait (chopstick [I]);
  4. Wait (chopstick [(I + 1) % 5]);
  5. Signal (mutex );
  6. ...
  7. // Eating
  8. ...
  9. Wait (mutex );
  10. Signal (chopstick [I]);
  11. Signal (chopstick [(I + 1) % 5]);
  12. Signal (mutex );
  13. ...
  14. // Thinking
  15. ...
  16. }While(True );

In this way, it is ensured that a philosopher cannot pick up any chopsticks during the time when he picks up two chopsticks, thus undermining the hold and wait features of the four features required for deadlock, this avoids deadlocks.

 

Finally, the deadlock has four features:

1. Mutual exclusion;

2. Hold and wait;

3. No preemption;

4. Circular wait;

When all four conditions are met, a deadlock may occur.

Related Article

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.