Coding Challenge: Lock Management

Test your lock management in python skill by solving the coding challenge presented below.

We'll cover the following

Problem

Suppose you are managing a multi-user file system, and in that system, files can be accessed by multiple users at the same time. All nodes can read and write to the files and doing that concurrently can corrupt the files.

In this challenge, we assume for simplicity that there is only one file demofile.txt which contains a simple counter (an integer). Suppose all the nodes count something (i.e., words in the documents) and add the total count to the file counter.

The nodes are represented by the processes that read the file and add 1 to the counter and store the updated value again to the file. We deploy 50 processes and 100 jobs (each job adds 1 to the counter), so the total count after the process is done should be 100. However, since many processes are running concurrently, the count gets corrupted, and the result is less than 100 because of the race condition.

This unsafe code is provided below. Your task is to change this code to make it process-safe and guarantee the final count to be 100 on every run.

Run the below code by clicking Run and enter command python lock-test.py.

Get hands-on with 1200+ tech skills courses.