CSCI-3753: Operating SystemsFall 2018
Anh NguyenDepartment of Computer Science University of Colorado Boulder
PA2 – LKM
•How to code a LKM?•module_init()•module_exit()
•How to install the module?•Makefile: obj-m:=xyz.o•make –C /lib/modules/$(uname -r)/build M=$PWD modules
•How to run the module?• sudo insmod xyz.ko
•How to remove the module?• sudo rmmod xyz
CSCI 3753 Fall 2018 4
PA2 – Character Device Driver
• A device driver is a program that operates or controls a particular type of device that is attached to a computer.• A driver communicates with
the device through the computer bus or communications subsystem to which the hardware connects. •When a calling program invokes
a routine in the driver, the driver issues commands to the device. • Once the device sends data back to the
driver, the driver may invoke routines in the original calling program.
CSCI 3753 Fall 2018 5
PA2 – Character Device Driver
•To work with device drivers, we have to work with the corresponding device files.•These files are stored in /dev folder.•To create device file for a device driver
• sudo mknod –m 777 /dev/simple_character_device c 240 0• mknod was originally used to create the character and
block devices that populate /dev/.• Device files • a storage device (block): cd-roms, hard drives, etc.• a device use for other purpose (character): /dev/zero,
/dev/null, or any other device not used to store info • "b" means block, and "c" means character
CSCI 3753 Fall 2018 6
mknod –m <permission> <device_file_location> <type of driver> <major number> <minor number>
Process
• A software program consist of a sequence of code instructions and data stored on disk.• A program is a passive entity
• A process is a program actively executing from main memory within its own address space
CSCI 3753 Fall 2018 8
Code
Data
Program P1
Other…
Process State
•New: The process is being created. •Running: Instructions are being executed. •Waiting: The process is waiting for some event to
occur (such as an I/O completion or reception of a signal). •Ready: The process is waiting to be assigned to a
processor.•Terminated: The process has finished execution.
CSCI 3753 Fall 2018 9
Process Cooperation
•Reasons for providing an environment that allows process cooperation: • Information sharing: providing an environment to allow
concurrent access to such information • Computation speedup: break a particular task into
subtasks executing in parallel with the others to run faster•Modularity: dividing the system functions into separate
processes or threads • Convenience: working on many tasks at the same time
•Cooperating processes needs IPC mechanism !!!
CSCI 3753 Fall 2018 11
IPC in Linux
•There are two fundamental models of IPC: •Message passing• Communication takes place by means of messages
exchanged between the cooperating processes.• Advantages:• Useful for exchanging small amounts of data,
because no conflicts need be avoided • Easy to implement in a distributed system
• Disadvantages:• Time-consuming as typically implemented using
system calls
CSCI 3753 Fall 2018 12
IPC in Linux
•There are two fundamental models of IPC: •Shared memory• A region of memory is shared by cooperating
processes is established.• Processes can then exchange information by reading
and writing data to the shared region.• Advantages:• Fast as system calls are required only to establish
shared-memory• Useful for exchanging large amounts of data
• Disadvantages:• Complicated to implement in a distributed system
CSCI 3753 Fall 2018 13
Shared Memory
•One process creates a shared memory segment that other processes (if permitted) can access.
• If succeeds, it returns the shared memory segment ID. • It is also used to get the ID of an existing shared
segment (from a process requesting sharing of some existing memory portion).
CSCI 3753 Fall 2018 14
#include <sys/ipc.h>#include <sys/shm.h>int shmget(key_t key, size_t size, int shmflg);
Shared Memory
•Once created, a shared segment can be attached to a process address space.
• If succeeds, it returns a pointer, shmaddr, to the head of the shared segment associated with a valid shmid. •Once attached, a process can read or write to the
segment, as allowed by the permission requested in the attach operation.
CSCI 3753 Fall 2018 15
#include <sys/types.h> #include <sys/shm.h>void *shmat(int shmid, const void *shmaddr, int shmflg);
Shared Memory
•To detach the shared memory segment located at the address indicated by shmaddr
CSCI 3753 Fall 2018 16
int shmdt(const void *shmaddr);
Shared Memory Example
•Goal: to illustrate the usage of shared memory between two processes in Linux. •The server process creates a shared memory
segment and writes some characters in it. It then waits for the first character in the shared memory to become '*', and then exits. •The client process reads characters from this shared
memory and prints them out on the terminal, writes '*' at the beginning of the shared memory segment, and then exits.
CSCI 3753 Fall 2018 17