Linux Programming Assignment
Linux Programming Assignment
Matricule:UBa19P0162
School: COLTECH
Question 1
Given that you are using a Debian-based Linux distribution (such as Ubuntu), write a simple
“Hello, world!" kernel module. Clearly show how it is compiled, load and unload. Your report
should include screen shots
Answer
Using a command line terminal in ubuntu, a bash file is created through nano text editor from
the command nano hello.shhttps://www.fritz.ai/object-detection/
This takes you automatically into the nano text editor where the command #!/bin/bash is written.
This command is used to import all bash libraries. The command chmod +x hello.sh is used to
grant write permissions to the file . in the instance where this priviledge is not granted, the hello
world file wont execute as shown below:
It displays a message permission denied indicating the write access was not yet granted.
After granting write access, the file is saved using ctrl + o and runned from terminal using
command ./hello.sh
https://www.fritz.ai/object-detection/
The file compiled successfully and hello world was displayed on screen.
b) Reentrant kernel
c) Multithreaded kernel
d) Microkernel
Answers
This is a type of operating system architecture in which the entire operating system works in the
kernel space.
This monolithic model differs from the other operating system architectures like micro lithic as
this provides the virtual interface alone over the computer hardware which makes it more useful.
The operating system is written as a collection of procedures that are linked together into a
single large executable program. Each procedure in the system is free to call any other process.
Calling any procedure makes the system very efficient
A) Reentrant kernel
a reentrant kernel allows processes (or, more precisely, their corresponding kernel
threads) to give up the CPU. They have no effect on other processes entering kernel
mode. Multiple processor systems may be scheduled together in the case of single-
processor systems.
B) Multithreaded kernel
C) Multithreaded kernel
D) Microkernel
A microkernel is one of the classifications of the kernel. Being a kernel it manages all
system resources. But in a microkernel, the user services and kernel services are
implemented in different address spaces. The user services are kept in user address
space, and kernel services are kept under kernel address space, thus also reduces the
size of kernel and size of an operating system as well.
scull is a char driver that acts on a memory area as though it were a device.
Major numbers
The major number is a small integer that serves as the index into a static array of char drivers
Minor numbers
The major number and minor number tell the kernel how to access the device. A common major
number is assigned to all devices that are being controlled by the same device driver. The minor
number helps to distinguish between the exact device type/controller using the same device
driver.
4)
a)
• User: A “user” is the owner of the file. If you create a new file, then you become the owner of the file.
• Group: Every user is a part of the specific “group.” A group contains multiple users, all of whom will
have the same access permissions to the file.
• Other: All users and groups in the system are considered as “others.”
b)
c)
d).Swap space in Linux is used when the amount of physical memory (RAM) is full. If the system needs
more memory resources and the RAM is full, inactive pages in memory are moved to the swap space.
While swap space can help machines with a small amount of RAM, it should not be considered a
replacement for more RAM. Swap space is located on hard drives, which have a slower access time than
physical memory. Swap space can be a dedicated swap partition (recommended), a swap file, or a
combination of swap partitions and swap files. Note that Btrfs does not support swap space. for example a
system with 2 GB, 8 GB, or 64 GB of system RAM, discretion can be exercised with regard to chosen
swap space and hibernation support. If your system resources allow for it, increasing the swap space may
lead to better performance.
e) The Linux virtual file system or virtual file system generally is a layer that sits on the top of your actual
file system. It allows the user to access different types of file systems, you can think of the virtual file
system as an interface between the kernel and the actual file system
f) •The superblock points to other File System structures, including the inode table, the bitmap of free
blocks, and cylinder groups (extensions of the inode table and further bitmaps).
•An inode stores a file’s metadata (owner, group, permissions, type of file, and pointers to the data disk
blocks.
•A file is one or more names pointing to an inode which points to the file’s data blocks and holds the
file’s metadata -.
•A dentry is a directory entry, with the inode pointer, file name, name length and offset to the next dentry.
The system call getdents() is used to iterate through them.
g). The differences between dentry and inode are that dentry is used to facilitate directory-specific
operations, inode is just a collection of metadata about file or directory. Superblock is the abstraction of
filesystem.
5)
a) A race condition occurs when multiple processes/threads access and manipulate the same data
concurrently and their timing or the ordering of events affects the program’s outcome.
b) Because the thread scheduling algorithm can swap between threads at any time, you don't know the
order in which the threads will attempt to access the shared data. Therefore, the result of the change in
data is dependent on the thread scheduling algorithm, i.e. both threads are "racing" to access/change the
data.
6)
a)Semaphores are integer variables that are used to solve the critical section problem by using two atomic
operations, wait and signal that are used for process synchronization.
Semaphores are implemented in the machine independent code of the microkernel. So they are machine
independent.
b) A binary semaphore is restricted to values of zero or one, while a counting semaphore can assume any
nonnegative integer value.
7)
b) A spinlock enforces a thread trying to access it to wait in a loop. The thread doesn’t perform a task
during this wait time. It only checks if the lock is available or not.
On the other hand, a semaphore achieves process synchronization without busy waiting. Its wait operation
puts the process into sleep. Hence, busy waiting doesn’t waste system resources.
8)
a) Basically, spin-lock shall be used if critical section is small (smaller than the overhead of sleep/wake-
up) and critical section does not call anything that can sleep! A semaphore shall be used if critical section
is bigger and it can sleep.
9) IOCTL is referred to as Input and Output Control, which is used to talking to device drivers. This
system call, available in most driver categories. The major use of this is in case of handling some specific
operations of a device for which the kernel does not have a system call by default.
Some real-time applications of ioctl are Ejecting the media from a “cd” drive, change the Baud Rate of
Serial port, Adjust the Volume, Reading or Writing device registers, etc. We already have the write and
read function in our device driver. But it is not enough for all cases.