Mini Project 3
Concurrency, distributed shortest paths and symlinks in xv6
Before you start
This is an individual assignment.
Deadlines
Submit all parts of the assignment by 11:59 pm, November 10. No extensions will be provided.
Additionally, doubts asked on the doubt document past 11:59 PM, November 7 will not be answered.
Submission
You will be provided a template repository on code.iiit.ac.in. This repo will contain the base files that you will need to work on. Any other Github or Forked Repositories will not be considered as submission. Make sure to work on the private repository allocated to you under the osn organisation.
You will need to commit iteratively and coherently as you go. Your commit history will be evaluated based on the time a commit was pushed, i.e. the server time. So if you made a commit 2 days ago and pushed it today, we will assume you made that commit today itself. So push as often as you can!
Commits on only the main branch will be evaluated. If you’re planning to work on other branches and merge onto main, take care to NOT do the following:
- Squash merge
- Rebase
- Delete the branch after merging
Like with mini project 2, your commit message must clearly describe the specific functionality implemented in that particular commit.
Making a bunch of big commits right before the deadline or commits without a clear purpose and random names will result in penalization.
You can refer to the resource on git for more details.
Your repository has the following structure:
mini-project3/
├── concurrency/
│ ├── airport-troubles/
│ └── birthday-boy/
├── xv6/
│ ├── user/
│ ├── kernel/
│ ├── Makefile
│ └── ...
├── ai-usage.md
└── README.md The README.md must contain build instructions, run instructions, assumptions you made, and known bugs.
AI Usage
You are NOT allowed to use AI agents to generate code (or the report) for any part of this assignment. You may use AI chatbots for understanding concepts and debugging errors.
Submit the prompts used in a document named ai-usage.md. You must submit only links to your chat; no screenshots are allowed. Faking links or failing to submit the AI usage document will lead to strict penalties.
Grading
You will be graded based on both your implementation and an in-person evaluation with a TA. You are expected to understand the code that you submit.
Doubt Doc
Doubts AnswersAll the best! Have fun :)
Airport troubles [80 marks]
The Intergalactic Institute of Misinformation Technology is ready for the 3067 batch of UG-1s. Double sharing rooms have been converted to triple sharing, and mess food has been upgraded (for a few days). Students from all the nearby galaxies are flying in. Advancements in technology have caused the institute to have its very own airport, Gotham Intergalactic Airport. Spaceship GB-420 has just landed there. Before the students can start their new lives, they have to get through immigration and collect their bags from the baggage belt. Can you help the students of Spaceship GB-420 get through the arrivals section?
For this section, you will be simulating this arrivals area. Every person in the airport must be modelled as a thread. Their jobs are:
- Students enter the immigration hall, scan their passport at one of the scanners, receive a stamp, leave the hall, collect their bag from the belt and exit.
- The immigration officer enters the hall, approves all the students in it at once and leaves.
- Baggage handlers load bags from the spaceship onto the belt. The handler queues loading Student
i’s bag onto the belt. If the queue has space, it’s loaded immediately. Else, it’s blocked until a spot on the belt is freed up.
There are 2 baggage handlers, the belt holds at most 2 bags, and every student has exactly one bag. Any number of students can be in the hall at once. The hall has 3 passport scanners, and each serves one student at a time.
If every scanner is in use, a student waits in the hall until one is free. If several students are waiting, any one of them may take the free scanner.
Constraints [10 marks for satisfying each]
- While the officer is in the hall, no one may enter and students may not leave.
- The officer must wait until every student in the hall has scanned their passport, including those waiting for a scanner. She then approves everyone in the hall as one batch (this counts as one action for the purpose of the assignment).
- A student can receive a stamp only after being approved. The officer leaves right after approving, without waiting for the stamps to be issued. If there is no one to approve, she still approves an empty batch and leaves.
- Students cannot leave the hall until they have received a stamp.
Note: You can assume every student eventually receives a stamp, so no student is left waiting in the hall after the officer’s last visit.
- A handler cannot load a bag until its owner has received a stamp. Any handler may load any ready bag, in any order. If the belt is full, a handler waits with her bag until a student collects one.
- A student can collect only their own bag, and only after leaving the hall. If their bag is not on the belt yet, they wait until a handler loads it. After collecting it, they leave the airport.
- Every bag whose owner receives a stamp is loaded and collected exactly once. No other bag is loaded.
- Every action takes 1 time unit, except
scans passport, which takes 2 time units.
The action times are just symbolic. Your aim is to run a simulation of the actions, which means you need not actually wait for 2 seconds for scanning a passport. Any calls to usleep or similar that we find in your code will be considered suboptimal solutions and may be penalized.
Input format
First line: a single integer N containing the number of lines that will be entered next.
Next N lines: three space-separated values each, <time> <actor> <id>.
<actor> can be one of Student or Immigration officer. Baggage handler isn’t part of the input since their action times are dependent on the stamp receipt times.
Output format
Output each action in the format <time> <actor> <id> <action>.
Lines with the same timestamp must be processed in the order in which they are given. There is only one immigration officer, with the ID 1. The baggage handlers have IDs 1 and 2.
The actors will have the following actions:
- Students:
enters,scans passport,receives stamp,leaves hall,collects bag - Officer:
enters,approves,leaves - Handlers:
loads bag for Student <i>
Example run
Input:
7
0 Student 1
0 Student 2
0 Student 3
1 Student 4
3 Immigration officer 1
4 Student 5
9 Immigration officer 1 Output:
0 Student 1 enters
0 Student 2 enters
0 Student 3 enters
1 Student 1 scans passport
1 Student 2 scans passport
1 Student 3 scans passport
1 Student 4 enters
3 Student 4 scans passport
3 Immigration officer 1 enters
5 Immigration officer 1 approves
6 Student 1 receives stamp
6 Student 2 receives stamp
6 Student 3 receives stamp
6 Student 4 receives stamp
6 Immigration officer 1 leaves
7 Baggage handler 1 loads bag for Student 1
7 Student 1 leaves hall
7 Baggage handler 2 loads bag for Student 2
7 Student 2 leaves hall
7 Student 3 leaves hall
7 Student 4 leaves hall
7 Student 5 enters
8 Student 1 collects bag
8 Student 2 collects bag
8 Student 5 scans passport
9 Baggage handler 1 loads bag for Student 3
9 Baggage handler 2 loads bag for Student 4
9 Immigration officer 1 enters
10 Student 3 collects bag
10 Student 4 collects bag
10 Immigration officer 1 approves
11 Student 5 receives stamp
11 Immigration officer 1 leaves
12 Baggage handler 1 loads bag for Student 5
12 Student 5 leaves hall
13 Student 5 collects bag Note
Given the non-deterministic nature of threads, one input may have more than one output. The above examples illustrate only one possible output of the input.
Important
Please follow the I/O format strictly as your solution might be autograded.
The students of GB-420 thank you whole-heartedly for your help. They will now go on to face larger hurdles at the Intergalactic Institute of Misinformation Technology.
Symbolic links in xv6 [60 marks]
In this exercise, you will be implementing symbolic links (a.k.a soft links/symlinks/shortcuts) in xv6. This is a mechanism that lets a file “point” to another file, meaning that opening, reading from and writing to this symlink will have the same effect as dealing with the original.
For this exercise, you won’t need to implement symlinks to directories or devices. Symlinks to files is sufficient.
Here’s an example of what your implementation would look like when finished:
$ echo "a" > file1
$ ln -s file1 file2
$ cat file2
"a"
$ echo "b" >> file2
$ cat file1
"a"
"b"
$ rm file1
$ cat file2
// error Your solution (for the kernel part) is correct if it passes usertests -q and symlinktest. Userspace programs will be manually tested.
The symlink system call [20 marks]
Implement an int symlink(char* target, char* path) which creates a symlink at path pointing to target. Dangling symlinks are allowed, so the target path need not exist for a successful call.
The call must return 0 if successful, and -1 if a failure occurred.
The open system call [20 marks]
You will need to modify the open system call to follow symlinks. Also, add an O_NOFOLLOW flag to avoid following symlinks. This will come in handy when building the readlink program later. If the symlink destination is also a symlink, you must keep following until you reach a file which is not a symlink. To avoid infinite loops, traverse up to a maximum depth of 10.
As far as this exercise is concerned, open will be the only system call that needs to follow symlinks.
The ln user program [10 marks]
Modify the ln user program to handle soft linking. If called with the -s flag, create a soft link. Otherwise, stick to the usual hard linking behaviour.
Example usage
ln -s README.md README # Creates a soft link called README pointing to README.md ln README.md README # Creates a hard link called README pointing to README.md You may assume that the -s flag, if present, will only be passed as the second argument.
The readlink user program [10 marks]
This program must take in the path of a symlink and print the path of the file it points to. The xv6 kernel won’t compile without this. If the given path is not of a symlink, print out a corresponding error message and exit with a non-zero status.
Example usage
$ readlink README
README.md $ readlink README.md
# Prints an error message Hints
- Add a new inode type (
T_SYMLINK) tokernel/stat.hto represent a symbolic link.symlinktestwon’t compile before that. - Think about where you’ll store the destination path for a symlink (the inode’s data blocks are a convenient choice).
Birthday boy [30 marks]
It’s your roommate’s birthday and you are tasked with bringing him to JC at 11:55PM while your friends set up the cake. You remain with him in his lab till 11:40, but to your dismay, he leaves the lab at 11:45 and goes missing. This is the second year in a row that this has happened.
You and your friends come to the conclusion that he would have gone to the location closest to his lab. You decide to trace all possible places where he could have gone to and find the shortest path there. To speed up the process, you need to parallelize the shortest-path finding algorithm using your knowledge of threads.
The Bellman-Ford algorithm [10 marks]
Modelling the campus as a graph with V vertices and E edges, your task is to find the shortest path from a given node N to any other node using the Bellman-Ford algorithm. You are required to use threads to speed up the algorithm.
You may assume that the graph has no negative cycles.
Input format
First line: space-separated integers V E N T which are the number of vertices, number of edges, the source node and the number of threads which will be spawned, respectively.
Then follow E lines with two space-separated integers each: S D which are the endpoints of the edge. S and D are zero-indexed identifiers for the vertices. Edges are bidirectional.
Output format
V space-separated integers, where integer i is the length of the shortest path from N to i.
Example
Input:
6 7 0 4
0 1
0 2
1 3
2 3
3 4
1 2
4 5 Output:
0 1 1 2 3 4 Performance analysis [20 marks]
You are also required to write a report (should be in the README.md of the repository root) analyzing the performance of this algorithm. You need to show how the runtime varies with V, E and the number of cores.
Your report should contain:
- A plot showing runtime of the algorithm on the y-axis and
Von the x-axis, for a fixed number of cores. This runtime must be averaged over a few graphs with the sameVandEbut different edge sets. - In the same plot, also plot a graph of the expected complexity. For example, if the complexity is , then plot on the graph. Tweak so that it best matches your experimental values. Show what value of you ended up with.
- A plot showing the runtime on the y-axis and
Ton the x-axis. FixVandE. The runtime needs to be averaged over different edge sets. - Observations on the above plot. Why does it look the way it does?
Note
Watermark the plots that you create with the part before @ in your IIIT email (username from username@{students,research}.iiit.ac.in).
For example, with matplotlib:
plt.text(
0.95, 0.95, "username",
ha="right", va="top",
transform=plt.gca().transAxes,
fontsize=10, color="gray", alpha=0.7,
)