CS计算机代考程序代写 Carleton University School of Computer Science

Carleton University School of Computer Science
COMP 3000 (WINTER 2021) OPERATING SYSTEMS ASSIGNMENT 4 (SOLUTIONS)
Questions – part 2 [8] (Kernel Modules)
The following questions (where applicable) are based on the original ones in Tutorial 7:
1. [4] Based on ones.c, make a device driver that, when read, always outputs the last byte of what has been written. The new should behave as follows:
/dev/ones
– At the beginning it still outputs ‘1’s like before, as no data has been written
– You should be able to write arbitrary bytes to it, e.g., echo -n “here are some bytes” > /dev/ones
– Once written to, /dev/ones should start outputting the last byte it has received. In the example above, it will always output “ssssssss…” (one ‘s’ at a time).
– Note that the last here refers to the very last (most recent) byte. If you later echo -n “updated” > /dev/ones, when you read from /dev/ones you should see “dddddd…”
Just define a char global variable that “remembers” the last byte. Each write() just takes the last byte without even considering previous bytes and each just output the global variable. Keep in mind you need to use get_user() or to get that last byte. See the code here.
2. [2] If you get the error “Permission denied” (even as root or with sudo) when trying to write to /dev/ones, which line in the code could be the cause? Include that line after the change has been made to fix that error [1/2] in your answer and briefly explain why [1/2].
*mode = 0666;
The previous 0444 means nobody can write to /dev/ones.
3. [1] With the permission issue above fixed, what do you need to add to ones.c so that when you write to /dev/ones you won’t get “Invalid argument”? Or put another way, what can prevent “Invalid argument” from happening when /dev/ones is written to?
Hint: try to trigger this error before making the changes for P2Q1.
The original ones code only has .read = ones_read which means it only responds to read operations hence giving “Invalid argument” when write is requested. Adding .write = ones_write and its corresponding implementation solves the problem.
4. [1] What code in the causes to be created? Include that line of source code in
ones.c
/dev/ones
copy_from_user()
read()
your answer here.
ones_device = device_create(ones_class, NULL, MKDEV(ones_major, 0), NULL, DEVICE_NAME);
Bonus question – [4] (no partial marks)
The following question is based on the original newgetpid in Tutorial 7:

Modify so that if you write a process ID to it, it will output the information on the provided process, specifically: PPID, EUID and EGID. To make this work, you’ll need to:
/dev/newgetpid
o As with P2Q1, receive text written from the user space the same way.
kstrtoint_from_user()
Then, you can verify your implementation this way:
2
o Convert the written text to an integer. Check out . o Find the right task struct. See the implementation of the kill system call.
o > (assuming 3356 is the PID of a running process). o then should output “PPID=xxxx, EUID=yyyy, EGID=zzzz”.
echo -n “3356”
/dev/newgetpid
cat /dev/newgetpid
There should be no program crash, hang, or message flooding, or other destructive bugs.
See the code here.