



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




COMPSCI 453/552: Operating Systems
In this assignment, we will write a simple character driver called booga. The directory ∼amit/cs453/lab/linux contains the code for several example character device drivers that were discussed in class. You will need to copy this entire directory and create a subdirectory booga in it.
You will only be able to test this assignment on your own computer.
The booga driver is a simple character driver that supports the open, read and write opera- tions. The driver supports four minor numbers: 0, 1, 2, and 3. The device files are: /dev/booga0, /dev/booga1, dev/booga2, /dev/booga3. You can also create a link from /dev/booga to /dev/booga0. On reading from /dev/booga0, /dev/booga1, /dev/booga2 and /dev/booga3 the driver gives a stream of one of the following phrases:
Note that the driver may only output part of a phrase if the number of bytes requested by the user is not a multiple of the length of the chosen phrase. Also each new phrase is separated from the previous one by a single space.
The driver is unpredictable as to what phrase it says. In order to do this we will use a random number generator. However, we cannot call any standard C library functions in the kernel (e.g. random()). Linux actually has a device driver that generates truly random numbers by observing hardware noise, interrupts and other system activity. In the user space, the random device driver can be accessed via /dev/random. In your device driver code, you will need to include the header file and the function that you will call has the following prototype.
extern void get random bytes(void *buf, int nbytes);
Here is a sample code on how to use the above function.
char randval; get_random_bytes(&randval, 1); choice = (randval & 0x7F) % 4; /* the bitwise AND is to guarantee positive numbers */
On writing to booga devices /dev/booga0, /dev/booga1, /dev/booga2, the booga device driver works like /dev/null (so it pretends to write the buffer but doesn’t actually write to any device). However if a process tries to write to /dev/booga3, it suffers from sudden death! You cannot call the system call kill() from inside the kernel space so you will have to figure out how to kill a process from inside the kernel. Check the kernel sources for ideas. On onyx the kernel source is in the /usr/src/linux/ directory.
Create /proc entries for the booga driver. It reports on the number of bytes read/written since the driver was loaded, number of times it was opened from each supported minor number, and the number of times each of the four strings have been output. If the driver outputs an incomplete phrase, we will still count it as another occurrence of that phrase. Here is a sample output.
[root@kohinoor booga]# cat /proc/driver/booga bytes read = 1400 bytes written = 2000 number of opens: /dev/booga0 = 2 times /dev/booga1 = 1 times /dev/booga2 = 3 times /dev/booga3 = 2 times strings output: booga! booga! = 0 times googoo gaagaa! = 3 times wooga! wooga! = 0 times neka maka! = 2 times [root@kohinoor booga]#
Start off by recursively copying the directory ∼amit/cs453/lab/linux device drivers to your home directory. One of the directories given there is the booga directory.
static int booga_open (struct inode *inode, struct file *filp); static int booga_release (struct inode *inode, struct file *filp); static ssize_t booga_read (struct file *filp, char *buf, size_t count, loff_t *f_pos); static ssize_t booga_write (struct file *filp, const char *buf, size_t count, loff_t *f_pos) static int __init booga_init(void); static void __exit booga_exit(void);
soundlow 300 0 [sound] soundcore 2372 6 [sb sound]
[root@kohinoor booga]# test-booga 0 100 neka! maka! neka! maka! neka! maka! neka! maka! neka! maka! neka! maka! neka! maka! neka! maka! neka! [root@kohinoor booga]# test-booga 0 100 wooga! wooga! wooga! wooga! wooga! wooga! wooga! wooga! wooga! wooga! wooga! wooga! wooga! wooga! wo
[root@kohinoor booga]# ./booga_unload
[root@kohinoor booga]# /sbin/lsmod Module Size Used by nfsd 144060 8 (autoclean) lockd 30984 0 (autoclean) [nfsd] sunrpc 52516 0 (autoclean) [nfsd lockd] 3c59x 19112 1 (autoclean) opl3 11208 0 (unused) sb 33620 0 uart401 5968 0 [sb] sound 57240 0 [opl3 sb uart401] soundlow 300 0 [sound] soundcore 2372 6 [sb sound] [root@kohinoor booga]#
Here is a sample write session.
Usage: test-booga amit@kohinoor booga: test-booga 0 100 write Attempting to write to booga device Wrote 100 bytes. amit@kohinoor booga: test-booga 1 100 write Attempting to write to booga device Wrote 100 bytes. amit@kohinoor booga: test-booga 2 100 write Attempting to write to booga device Wrote 100 bytes. amit@kohinoor booga: test-booga 3 100 write Attempting to write to booga device Killed amit@kohinoor booga:
typedef struct Booga_Dev { char data; unsigned int number; / minor number / unsigned int usage; / keep track of device usage */ }
The space for this structure would need to be allocated (typically in init module()). The data points to a buffer that is allocated and filled in booga read().
The final directory containing all the relevant files must still be submitted on onyx.
Make sure to include a README file in the booga subdirectory that contains your name, date, assignment number and observations about the assignment.
Put all these files in a directory (say ∼/cs453/p2). Change directory to this directory and execute the following command (on onyx) to submit the assignment.
submit amit cs453 2
or