Operating Systems
Assignment
Lift-simulator
(In C language)
The objective of this programming assignment is to give you experiences in using multiple processes and multiple threads and their inter process / thread communications. You will learn how to create processes and threads and solve some critical section problems. The programs for this Lift-simulator should include the following features.
- There are three elevators, Lift-1, Lift-2, and Lift-3, which are servicing a 20-floor building (Floors 1 to 20). Assume that initially all lifts are in Floor 1. Each lift waits for lift requests from any floor (1 to 20) and serves one request at a time.
- Create a file sim_input to store n requests in the following formats, for n between 50 and 100.
1 5
7 2
3 8
4 11
.
.
.
12 19
20 7
The first number in each request represents the floor where the request is made, and the second number is the destination floor. The two numbers are separated by a space. For example, the first request comes from Floor 1 to go up to Floor 5, while the last request is from Floor 20 to go down to Floor 7.
- Create a task Lift-R that runs a function request() to get one lift request at a time from sim_input, and puts the request into a buffer of sizem ≥ 1. Thus there are at mostmpending lift requests. The function puts a request to the buffer when there is available space. Otherwise, Lift-R is blocked waiting for available space in the buffer.
- Create three tasks to simulate Lift-1, Lift-2, and Lift-3, each of which runs a function lift() to perform operations of each lift. For each request, Lift-1, Lift-2, or Lift-3 removes the request from buffer, goes to the floor where the request is made, and moves to the destination floor. Then the lift waits for the next available request if buffer
1
is empty, or grabs another request from buffer. Since lifts need time to go from one floor to others, you should simulate this event; this can be done, for example, by using sleep(t) if the time needed istseconds. To simplify, our simulator assumes each liftrequires the same time, t seconds, for each request, for t 0. For example, the time to go from Floor 3 to Floor 5 (2 floor-distance), is the same as the time for the lift to go from Floor 9 to Floor 4 (5 floor-distance).
- Consider accessing buffer as the bounded buffer producer-consumer problem, where Lift-R is theproducerand Lift-1, Lift-2, and Lift-3 are theconsumers.
- Create a file sim_out to record all activities of Lift-1, Lift-2, Lift-3, and Lift-R. When Lift-1 that is located in Floor 8, for example, has completed a request from Floor 2 to goto Floor 9, Lift-1 records its activity in sim_out as:
Lift-1 Operation Previous position: Floor 8 Request: Floor 2 to Floor 9 Detail operations:
Go from Floor 8 to Floor 2 Go from Floor 2 to Floor 9 #movement for this request: 13 #request: 5
Total #movement: 70 Current position: Floor 9
Note that the “#movement” is from the lift’s previous position to the floor from which the request is made to the destination floor of the request; for the example we have (8 – 2) + (9 – 2) = 13. The “#request” is the total number of requests that Lift-1 has served, while “Total #movement” is the total number of movements to serve those requests. Each lift keeps these two values in two variables.
Similarly, each time a request is added into buffer, Lift-R prints the following information in sim_out.
——————————————–
New Lift Request From Floor 8 to Floor 2
Request No: 21
——————————————–
The “Request No” is the number of requests that Lift-R has put into buffer.
- File sim_out is shareable among Lift-1, Lift-2, Lift-3 and Lift-R; therefore, accessing the file needs proper mutual-exclusion.
- Your simulator terminates when all requests in sim_input have been put into buffer. The simulator then computes the total number of requests (the sum of #request of all lifts), and the total number of movements (the sum of Total #movement of all lifts). It then prints the results in sim_out:
2
Total number of requests: 100
Total number of movements: 5520
- As part of your program, you have to remove all tasks that have been created to avoid zombies, all other resources, and close all open files.
Part A: Implementation using pthreads (50%)
As threads by default share memory, for this part you need not create shared memory, e.g., for buffer. However, you must address all necessary synchronization issues. Among others, use
pthread_mutex_lock(), pthread_mutex_unlock(), pthread_cond_wait(), pthread_cond_signal(), and pthread_join() in your program.
To test your program, you should run the program as follows:
lift_sim_A m t
Thus, you have to name you executable file lift_sim_A; m is the buffer size, and t is the time required by each lift to serve a request.
Part B: Implementation using processes (30%)
Since processes do not share memory, the parent process needs to create shared memory, e.g., for buffer. You must also address all necessary synchronization issues. You can use either POSIX or System V for semaphores and shared memory. Make sure that the parent process waits for the termination of all child processes and remove the shared memory, and semaphores.
To test your program, you should run the program as follows:
lift_sim_B m t
Thus, you have to name you executable file lift_sim_B; m is the buffer size, and t is the time required by each lift to serve a request.
3
Instruction for assignment
- The assignment should include:
Software solution of your assignment that includes (i) all source code for the programs with proper in-line and header documentation. Use proper indentation so that your code can be easily read. Make sure that you use meaningful variable names, and delete all unnecessary comments that you created while debugging your program; and (ii) readme file that, among others, explains how to compile your program and how to run the program.
Detailed discussion on how any mutual exclusion is achieved and what processes / threads access the shared resources.
Description of any cases for which your program is not working correctly or how you test your program that make you believe it works perfectly.
Sample inputs and outputs from your running programs.
4
The post Operating Systems appeared first on My Assignment Online.