Beruflich Dokumente
Kultur Dokumente
報告人 : 陸述人
http://www.ntut.edu.tw/~s4599001
Operating System Linux
• Operating system
– Kernel space/User space
– MMU
– mutex/semaphore/interrupt
– Task structure
• ptrace/mmu/signal/task relation /process id
• file/timer/semaphores/misc
– Main algorithm
• Signal handing
• Interrupt
– Tasklets
– Bottom haves
• Software interrupt –max 32
– Void open_sorftirq(int nr, void (*action) (structure softirq_action *, void *data)
– Raise_softirq()
• Scheduler
– SCHED_FIFO/SCHED_RR/SCHED_OTHER
Mutex
• Definition
– In computer programming, a mutex is a progr
am object that allows multiple program thread
s to share the same resource, such as file acc
ess, but not simultaneously.
• Andrea Steinbach and Angelika Schofer, a
s a master's thesis at Rheinische Friedrich-
Wilhelms University.
Semaphore
• Definition
– A semaphore is a protected variable (or abstract data
type) and constitutes the classic method for restrictin
g access to shared resources (e.g. storage) in a multi
programming environment.
• Sample
– Dining philosophers problem
– do not prevent all deadlocks
• invented by Edsger Dijkstra and first used in the
THE operating system
Semaphore Programming
P(Semaphore s)
{
await s > 0 then s = s-1; /* must be atomic once s > 0 is detected */
}
V(Semaphore s)
{
s = s+1; /* must be atomic */
}
Init(Semaphore s, Integer v)
{
s = v;
}
Operating System in Linux -3
• Driver:
– Modprobe/Insmod/rmmod/lsmod
– Built in kernel
Driver implementation in Linux
• Driver specifications
– Char drivers
• Console
• Serial port
– Block drivers
• DMA transfer
– Network interfaces
• IO port / IO memory
Driver implementation in Linux
• Interrupt
– int request_irq(unsigned int irq,void (*handle
r)(int, void *, struct pt_regs *),unsigned long
flags,const char *dev_name,void *dev_id);
– void free_irq(unsigned int irq, void *dev_id);
Writing Linux Device Driver
Header files
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/version.h>
#include <linux/config.h>
#include <linux/init.h>
#include <linux/types.h>
#include <linux/fs.h>
Writing Linux Device Driver
Initialize register (clear)
static int test_open(struct inode *inodePtr, struct file *fp)
{
printk( "Enter open routine.\n" );
return 0;
}
static ssize_t test_write(struct file *fp, char *buf, size_t len, loff_t *offs
et)
{
printk( "Enter test_write\n" );
return len;
}
Writing Linux Device Driver
Mapping
module_init ( test_init_module );
module_exit ( test_cleanup_module );
Create Environment
• Compiler
– gcc -D__KERNEL__ -DMODULE -I/usr/src/linux-2.4.22/include -c -O -Wall drv.c -o drv.o
• Make node
– mknod /dev/test c 100 0
• Check node
crw-rw-rw- 1 root dialout 4, 64 Jun 30 11:19 test
crw-rw-rw- 1 root dialout 4, 65 Aug 16 00:00 ttyS1
Writing Application
int main(int argc, char *argv[])
{
int fd;
char buf[256] = "Hello World!!";