It is considered good practice for programs to always

This preview shows page 30 - 33 out of 35 pages.

It is considered good practice for programs to always be able to handle failures to allocate memory, either by checking the pointer value (if nothrow) or by catching the proper exception.Dynamic memory in CC++ integrates the operators newand deletefor allocating dynamic memory. But these were not available in the C language; instead, it used a library solution, with the functions malloc, calloc, reallocand free, defined in the header<cstdlib>(known as <stdlib.h>in C). The functions are also available in C++ and can also be used to allocate and deallocate dynamic memory.Note, though, that the memory blocks allocated by these functions are not necessarily compatible with those returned bynew, so they should not be mixed; each one should be handled with its own set of functions or operators.
Peterson's algorithmFrom Wikipedia, the free encyclopediaPeterson's algorithm(AKA Peterson's solution) is a concurrent programmingalgorithmfor mutual exclusionthat allows two processes to share a single-use resource without conflict, using only shared memory for communication. It was formulated by Gary L. Petersonin 1981.[1]While Peterson's original formulation worked with only two processes, the algorithm can be generalized for more than two,[2]as shown below.Contents[hide] 1The algorithmo1.1Mutual exclusiono1.2Progresso1.3Bounded waitingo1.4Filter algorithm: Peterson's algorithm for more than two processes2Note3Footnotes4See also5External linksThe algorithm[edit]The algorithm uses two variables, flagand turn. A flag[n]value of trueindicates that the process nwants to enterthe critical section. Entrance to the critical section is granted for process P0 if P1 does not want to enter its critical section or if P1 has given priority to P0 by setting turnto 0.ool flag[2] = {false, false};nt turn;0: flag[0] = true;0_gate: turn = 1;while(flag[1] && turn == 1){// busy wait}// critical section...// end of critical sectionflag[0] = false;P1: flag[1] = true;P1_gate: turn = 0;while(flag[0] && turn == 0){// busy wait}// critical section...// end of critical sectionflag[1] = false;
The algorithm does satisfy the three essential criteria to solve the critical section problem, provided that changesto the variables turn, flag[0], and flag[1]propagate immediately and atomically. The while condition works even with preemption.[1]The three criteria are mutual exclusion, progress, and bounded waiting.[3]Since turn can take on one of two values, it can replaced by a single bit, meaning that the algorithms requires only three bits of memory.[4]:22Mutual exclusion[edit]P0 and P1 can never be in the critical section at the same time: If P0 is in its critical section, then flag[0] is true. In addition, either flag[1] is false (meaning P1 has left its critical section), or turn is 0 (meaning P1 is just now trying to enter the critical section, but graciously waiting), or P1 is at label P1_gate (trying to enter its critical section, after setting flag[1] to true but before setting turn to 0 and busy waiting). So if both processes are in theircritical sections then we conclude that the state must satisfy flag[0] and flag[1] and turn = 0 and turn = 1. No

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture