A university assigns student IDs of the form 1, 2, 3, ... such that if n students are currently enrolled, then the next student to enroll will receive and ID of n + 1. Consider the following algorithm that accomplishes this. // Global variable storing number of students currently enrolled current_student_count = 0 // Function that reads the above global variable, calculates new ID, and increments the global count assign_new_id(): count = current_student_count new_id = count + 1 current_student_count = count + 1 return new_id (a) If two different threads run the above code in parallel to enroll two different students, it is possible for the two students to receive the same ID, and for the current_student_count to have a wrong value. Explain how this is possible. (b) Modify the code above so that the problem in (a) does not happen.

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter6: Modularity Using Functions
Section6.4: A Case Study: Rectangular To Polar Coordinate Conversion
Problem 9E: (Numerical) Write a program that tests the effectiveness of the rand() library function. Start by...
icon
Related questions
Question

A university assigns student IDs of the form 1, 2, 3, ... such that if n students are currently enrolled, then the next student to enroll will receive and ID of n + 1. Consider the following algorithm that accomplishes this. // Global variable storing number of students currently enrolled current_student_count = 0 // Function that reads the above global variable, calculates new ID, and increments the global count assign_new_id(): count = current_student_count new_id = count + 1 current_student_count = count + 1 return new_id (a) If two different threads run the above code in parallel to enroll two different students, it is possible for the two students to receive the same ID, and for the current_student_count to have a wrong value.

Explain how this is possible. (b) Modify the code above so that the problem in (a) does not happen.

Expert Solution
steps

Step by step

Solved in 2 steps with 2 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L