Question
This is challanging Operating System program (For C or C++ coder) Objectives: Synchronization mechanisms in multithreading and multiprocessing. Preventing race conditions, locks, busy waiting (spinlocks),
This is challanging Operating System program (For C or C++ coder)
Objectives:
Synchronization mechanisms in multithreading and multiprocessing. Preventing race conditions, locks, busy waiting (spinlocks), data protection in the critical region.
Synchronize simultaneous file writes (A version of the 'readers-writers' problem).
we will use multiple worker processes - each of which needs to add updates to a data structure. This mimics common scenarios where multiple workers will be adding data to a central repository from multiple sources at the same time (OS kernel data structures, database updates, etc.).
We will use a file for the data structure for this assignment - because it is persisted to disk, easy to manage, and easy to verify. But in real life, this could be a file, a linked list, a database table, an array, tree, graph, or any other structure which receives data from multiple processes simultaneously.
Each thread must merge their separate new data with the main data file, keeping the main file in sorted order. So each process must read the main file, read the new file, merge the two, and then write the merged, sorted results back to the main file. This takes a little time, so if there is no locking mechanism, two processes can read the same file virtually instantaneously, merge their separate data sets with the main file, and then try to write their new version of the main file back to disk.
At the very least, we have a race condition where we have two (or more) conflicting versions of the data. This will cause an accidental data deletion when the processes try to write their data, and the second version overwrites the first. A demo will be given in-class as to what can go wrong if we don't use a lock for this type of operation.
What we need is for one process to acquire a lock, open the main file, merge its data, write the main file, and then release the lock so that other processes can proceed. In this way, each process is guaranteed to load updated, correct data to merge with, and no process has 'stale' data to merge with, which would cause the data anomaly (accidental deletion).
What you need to do:
Implement a program usning C, C++ or Java which starts three threads of execution, each of which attempts to open a data file and merge it with the main file, keeping the final results in sorted order. Data for the main file and the three data files have been provided.
You can choose to do this via multithreading, multiprocessing (generated from the same parent process), or by deploying three completely separate processes which are initiated by the operating system.
Implement a locking system, so that only one thread of execution can access the main file (critical region) at a time. This locking system should include some simple mechanism that allows the processes that do not hold the lock to obtain it once it is free. Busy waiting will probably be the easiest approach, and it will be sufficient for this assignment.
Your choices for locking could be mutexes, semaphores, a simple variable flag (like Peterson's solution), a monitor, or file locking (available on both UNIX and Windows).
Check your results against the answers given, to make sure they are correct.
Notes:
Your threads must run simultaneously, and make use of the locking mechanism. There will be 4 individual program three for the input and one is the main file which will print the sorted output .txt program.
You can write the program in any language that you want, and use any method you like to start the three threads of execution, and to implement the lock.
The Three input file will be reade from .txt file and output file will be sorted .txt file
input1.txt | input2.txt | input3.txt | final.txt |
36 | 9 | 0 | 0 |
22 | 66 | 70 | 1 |
91 | 12 | 76 | 1 |
47 | 55 | 42 | 1 |
5 | 65 | 92 | 2 |
78 | 42 | 41 | 3 |
44 | 73 | 33 | 3 |
3 | 82 | 54 | 3 |
7 | 80 | 25 | 4 |
52 | 26 | 1 | 5 |
95 | 70 | 32 | 7 |
61 | 65 | 95 | 7 |
2 | 11 | 12 | 9 |
80 | 68 | 73 | 9 |
50 | 89 | 61 | 11 |
66 | 61 | 23 | 12 |
32 | 7 | 70 | 12 |
33 | 98 | 34 | 12 |
38 | 76 | 50 | 15 |
92 | 37 | 44 | 17 |
74 | 80 | 3 | 17 |
92 | 47 | 12 | 18 |
92 | 1 | 58 | 22 |
26 | 15 | 23 | 23 |
70 | 81 | 41 | 23 |
94 | 25 | 17 | 23 |
68 | 42 | 50 | 25 |
18 | 64 | 34 | 25 |
72 | 49 | 30 | 26 |
89 | 72 | 73 | 26 |
26 | |||
27 | |||
29 | |||
30 | |||
31 | |||
32 | |||
32 | |||
33 | |||
33 | |||
33 | |||
34 | |||
34 | |||
34 | |||
36 | |||
37 | |||
38 | |||
41 | |||
41 | |||
42 | |||
42 | |||
42 | |||
44 | |||
44 | |||
44 | |||
47 | |||
47 | |||
47 | |||
49 | |||
50 | |||
50 | |||
50 | |||
52 | |||
53 | |||
54 | |||
55 | |||
56 | |||
57 | |||
58 | |||
61 | |||
61 | |||
61 | |||
61 | |||
64 | |||
65 | |||
65 | |||
66 | |||
66 | |||
67 | |||
68 | |||
68 | |||
69 | |||
70 | |||
70 | |||
70 | |||
70 | |||
70 | |||
71 | |||
72 | |||
72 | |||
73 | |||
73 | |||
73 | |||
74 | |||
74 | |||
76 | |||
76 | |||
77 | |||
78 | |||
79 | |||
80 | |||
80 | |||
80 | |||
80 | |||
81 | |||
82 | |||
84 | |||
85 | |||
87 | |||
89 | |||
89 | |||
91 | |||
92 | |||
92 | |||
92 | |||
92 | |||
94 | |||
95 | |||
95 | |||
97 | |||
98 |
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started