WELCOME TO ALL
RT LINUX
OPERATING SYSTEM(OS) Definition An operating system (OS) is a computer program that manages the hardware and software resources of a computer It acts as a channel between hardware and user application
OPERATING SYSTEM(OS) The operating system must fulfils two main objectives : Interact with the hardware components servicing all low-level programmable elements included in the hardware platform. Provide an execution environment to the applications that run on the computer system (the so-called user programs).
OS SERVICES Some of the basic OS Services are Process management Memory management Disk and file systems Networking Security - Internal security & External security Graphical user interfaces Device drivers
LINUX Linux also known as GNU/LINUX Created by Linus Torvalds Good example for open source development Source code is freely available to everyone GPL License POSIX Standard OS Linux supports the X Window System GUI
LINUX KERNAL It is a set of code to provides services to outer world Innermost part of the operating system It is in charge of hardware management It provides RR Scheduling technique Disadvantage of RR technique
KERNAL BLOCKS
REAL TIME OS
RTOS is a class of OS intended for real-time applications KERNEL
LINUX KERNEL Ex:-
RT-Linux QNX VxWorks HARDWARE LynxOS.
USER SPACE & KERNEL SPACE
User space is where normal application programs run like GUI based application or UNIX shell Any subroutines or functions forming part of the kernel are considered to be part of kernel space. (modules and device drivers, for example) Application can not interact directly to Kernel space It is possible to through some kernel supported functions
USER SPACE & KERNEL SPACE
Some of the advantages of User Space are Full C library can be linked in. The programmer can run a conventional debugger User-space can be kill easily User memory is swappable
USER SPACE & KERNEL SPACE
Some of the drawbacks of User Space are Interrupts are not available in user space Direct access to memory is difficulty I/O ports accessing is difficulty User memory is swappable Response time is slower The most important devices can’t be handled in user space
USER SPACE & KERNEL SPACE
LOADABLE KERNEL MODULE(LKC) LKC used by the Linux kernel to expand his functionality This can be loaded dynamically No whole kernel recompilation needed Often this used for specific device drivers such as sound cards, video cards etc., LKM consist of two basic functions (minimum) -- init() & cleanup()
LOADABLE KERNEL MODULE(LKC)
LKC Creation
#include int init_module(void) { printk("Hello world\n"); return 0; } void cleanup_module(void) { printk("Goodbye\n"); }
printk() is a print function without help of C function(printf) Accessing public kernal symbols by loading the module.
LOADABLE KERNEL MODULE(LKC)
Compiling & Loading LKC root# gcc -c hello.c root# insmod hello.o
Hello, world (or /var/log/messages we can see) root# rmmod hello Goodbye root#
KERNEL MODULES vs APPLICATION An application performs a single task from beginning to end A module “main” function terminates immediately in order to serve future requests Init Module is a 1st Entry point for kernel module (i.e) “Here I am, and this is what I can do.” The second entry point of a module, cleanup module It should tell the kernel, “I’m not there anymore; don’t ask me to do anything else
INTER PROCESS COMMUNICATION IPC is transfer of data among processes
PROCESS Running instance of a program Each process should be synchronize each other. Concurrency in the Kernel
INTER PROCESS COMMUNICATION Many techniques are used to access the IPC Pipes FIFO Semaphore Shared Memory
PIPES A pipe is a communication device that permits unidirectional communication. Data written to the “write end” of the pipe is read back from the “read end.” Pipes are serial devices Typically, a pipe is used to communicate between two threads
PIPES Example for Pipe creation int pipe_fds[2]; int read_fd; int write_fd; pipe (pipe_fds); read_fd = pipe_fds[0]; write_fd = pipe_fds[1]; read_fd can be read back from write_fd.
FIFO A first-in, first-out (FIFO) file is a pipe that has a name in the file system. FIFOs are also called named pipes. It can be open by multiple processes for reading or writing. Processes can be exchanging data via the FIFO without writing any file system.
FIFO FIFO Creation & Deletion % mkfifo /tmp/fifo
(Creation)
% ls -l /tmp/fifo -- listing FIFO file prw-rw-rw- 1 sri users 0 Nov 09 14:04
p, indicating that this file is actually a FIFO % rm /tmp/fifo
(Deletion)
FIFO FIFO Accessing Access a FIFO just like an ordinary file Example:int fd = open (fifo_path, O_WRONLY); write (fd, data, data_length); close (fd); FILE* fifo = fopen (fifo_path, “r”); fscanf (fifo, “%s”, buffer); fclose (fifo);
REAL TIME FIFO Realtime FIFOs are First-In-First-Out queues Read/Write by Linux processes and RTLinux threads FIFOs are uni-directional Needed modules system/rtl-posixio.o & fifos/rtlfifo.o Device entries in /dev/rtfxx folder Before a realtime FIFO can be used, it must be initialized:
REAL TIME FIFO FIFO Creation & Deletion Include rtl_fifo.h header file int rtf_create(unsigned int fifo, int size); int rtf_destroy(unsigned int fifo); Return Value is 0 for Success
Example iRet_val = rtf_create( 45, 1024); iRet_val = rtf_destroy( 45 );
REAL TIME FIFO FIFO Accessing int id = open("/dev/rtf16", O_RDONLY) close(id); Open Mode -- O_RDONLY, O_WRONLY or O_RDWR
Read / Write Functions rtf_get() - Read data from real-time FIFO int rtf_get (unsigned int fifo, char * buf, int count); rtf_get (16, &Obj, sizeof(Sample) );
REAL TIME FIFO rtf_put() - Write data to a real-time FIFO int rtf_put ( unsigned int fifo, char * buf, int count ); rtf_put (16, &Obj, sizeof(Sample) ); rtf_flush() - Empty a realtime FIFO rtf_flush(unsigned int fifo); rtf_flush(16);
THREADS A mechanism to allow a program to do more than at a time Threads appear to run concurrently kernel schedules them asynchronously Linux implements the POSIX standard thread (known as pthreads) Linking application with -lpthread
THREADS Thread Creation & Deletion Thread ID creation using pthread_t Thread function Thread creation using pthread_create() Thread deletion using pthread_delete_np() Priority can be set for thread scheduling
THREADS Example pthread_t tid_Samp; /* Thread ID creation */ /* Thread creation */ pthread_create(&tid_Samp,NULL,thread_function,NULL); void *thread_function(void *p) { struct sched_param r; r.sched_priority = 1; pthread_setschedparam(pthread_self(), SCHED_FIFO, &r); /* Thread periodic */ pthread_make_periodic_np(tid_Samp,gethrtime(), 1000000000);
THREADS while(1) { pthread_wait_np(); { printk(“Thread testing....\n”); } } return 0; } /* Thread Deletion */ pthread_delete_np(tid_Samp); tid_Samp = NULL;
THREADS RT Thread Structure
SEMAPHORES It can be used for synchronizing processes sem_t sam_sem; /*Creating sem variable */ sem_init(&sam_sem,1,0); /*Initialization of sem */ sem_wait(&sam_sem); /* Wating for semaphore */ sem_post(&sam_sem); /* Posting sem */ sem_destroy(&sam_sem); /* Deleting sem */
CHARACTER DEVICE It allows processes to communicate with device drivers in the kernel and through them with physical devices (modems, terminals, etc.) Major number & Minor number i_major = register_chrdev (0,"DPcPCI7008",&DPcPCI7008_fops); unregister_chrdev(i_major, "DPcPCI7008");
CHARACTER DEVICE Access driver function using fops struct file_operations scull_fops = {
};
read: scull_read, write: scull_write, ioctl: scull_ioctl, open: scull_open, release: scull_release,
DEBUGGING Print messages ( printf(), printk() ) GNU Debugger (GDB) Kernel Source level Debugger (kgdb) Build in kernel Debugger (kdb) Memory tools :- MEMWATCH, YAMD
RTLINUX COMMANDS Starting & Stopping startx
Start the X system (GUI)
halt
Stop all processes and shutdown
reboot Stop all processes and then reboot
-
Mounting File systems mount/umount mount/unmount the file systems mount /dev/hdc1 /mnt/hard_disk mount -t smbfs -o username=xxx //10.5.2.42 /mnt/net umount /mnt/net
RTLINUX COMMANDS ls or ll
Listing files from current directory
cp copy files mv move files rm Remove files mkdir / cd / rmdir Directory commands man Help page view cat
File view
RTLINUX COMMANDS insmod / rmmod dmesg
Module insert/remove
Display kernel messages
ps List current processes kill Kill a specific process rtlinux start / rtlinux stop chmod change mode mc midnight command pwd Display the current path
QUESTIONS
THANK YOU