Skip to main content

Posts

Showing posts from September, 2009

Representation of File System in the Kernel

Representation of data on a floppy and hard disk may consistently differ, but representing them in the kernel is almost same.VFS mounts the file system through the function register_filesystem(struct file_system_type *fs), Exampleregister_filesystem(&ext2_fs_type);MountingBefore a file can be accessed, the file system containing the file should be mounted. Done by the call using mount or function mount_root() Every mounted file system is represented by a super_block structure. The function read_super()of the VFS is used to initialize the superblock Managing the file system Will issue some flags during the mounting like MS_RDONLY //File system is read onlyMS_REMOUNT //flags have been changedSuperblock OperationsSuper block provides functions needed to access the file system and its processing. Some of the operation includesread_inode();// must execute and Responsible for filling the submitted inode structurewrite_inode(); //must execute and Used to store information about the …

Linux File System

Linux supports many number of file systems, that’s why Linux is accepted quickly as one of the best Operating systems. Linux has the unified interface called as Virtual File system(VFS) which serves as the interface defined in between the OS kernel and the different file systems as illustrated below.The Virtual file System Supplies the applications with the system calls for file managementMaintains the internal structuresPasses tasks on to the appropriate actual file systemPerformance of default actionsBasic PrinciplesTwo main factors taken into consideration while designing any file systemSpeed of access of data and Facility for Random access is made possible by means of the block oriented devices which are divided into specific number of equal sized blocks.When using the blocks, linux uses a buffer cache to speed up the process in random access.In Linux/Unix, the data is stored in a hierarchical file system containing not only files and directories, but also the device files,

Sockets for Inter process communication

Socket programming interfaces provides communication via a network as well as locally on a single computer. Example is INET daemon which waits for incoming network service requests and then call the appropriate service program using the socket file descriptor as standard input and output.Implementation of Unix domain socketsRepresented by a kernel data structure socketSocket specific functions like Socket(), setsockout() The functions are implemented with a single system call socketcall which calls all the necessary functions by reference to the first parameter. The file operation read(), write(), poll(), ioctl(), lseek(), close() are called directly Operations of unix domain sockets long sys_socket(int family, int type, int protocol);// creates a socket file descriptor long sys_connect(int fd, struct sockaddr * uservaddr, int addrlen); //bind the socket to the unix domain address with specified lengthlong sys_listen(int fd, int backlog);//checks whether any connections are being…

Debugging using ptrace

Ptrace is a system call provided in unix for one process to take control of another process to debug errors or bugs in the systemThe process under control can be run step by step and its memory can be read and modified.int sys_ptrace(long request, long pid, long addr, long data);the function processes various request defined in the parameter request and pid indicates the process id of the process to be controlled.Using the request PTRACE_TRACEME, a process can specify that its parent process controls iut via ptrace().

System V IPC

Three forms of System V IPCSemaphoresMessage QueuesShared MemorySystem V IPC is different from POSIX API, but both are available in the linux kernel.GNU C library in kernel version 2.2 includes the interfaces for shared memory and the semaphore according to POSIX.Access Rights and Numbersstruct kern_ipc_perm{key_t key;  //keyuid_t uid; //Ownergid_t gid;  //Owner uid_t cuid;  //Creatorgid_t cgid; //Creatormode_t mode; //Access Modeunsigned long seq; //counter used to calculate the identifier};the user and group id needs 32bit for the Intel 32 bit architecture, so the kernel supported both the IPC_OLD and IPC_64.Semaphores (System V)Array of semaphores can be setup using the system calls It is always possible to modify a number of semaphores. They can be incremented or decremented in steps greater than 1. Semaphores are created using the following structurestruct sem_array{struct kern_ipc_perm sem_perm; //access permissiontime_t sem_otime; //time of the last semaphore operation time_t s…

Pipes and Named Pipes (FIFO)

Pipes are the classical method of interprocess communication. For example# ls –l | moreThe symbol | indicates a pipe and the shell in the above example runs the processes ls and more which are linked with a pipe. ls writes data to the pipe and more reads it.Named pipes otherwise called as the FIFO (First in First Out) is the other variant of pipe. They can be created like this# mkfifo pathnameExample# mkfifo hello# ls –l helloprw-r- -r- - 1 temp users 0 Aug 28 10.45 hello |there are many similarities between the pipes and the FIFO, but the inode specification for the both are more or less the same. following is the inode specification for the pipe struct pipe_inode_info{wait_queue_head_t wait; //wait queuechar * base; //address of the FIFO bufferunsigned int readers; //no of processes reading at this momentunsigned int writers; //no of processes writing at this momentunsigned int waiting_readers; //no of blocked process reading at this momentunsigned int waiting_writers; //no of block…

Communication Via Files (IPC)

This is one of the oldest way of data exchange in which the information is sent as a file.File needs two type of lockingMandatory LockingRead wirte is blocked during the entire locking period Advisory lockingAllows Read and write even after the lock has been set processes accessing the file for Read and Write has to lock it and release it again Locking Entire FilesThere are two methods for locking the entire fileFirst Method is by the use of several system callslink – a system call create – another system call combination of flags like O_CREAT and O_EXEC with open (system call) lock a fle with open() function using O_CREAT | O_WRONLY | O_TRUNK, but this option fails under the superuser mode Second Methodfcntl – A system call to lock the entire file. It is also useful to lock the file areas. In Linux 2.0 it is called flock() which is not advisable to use.Locking file areasint sys_fcntl(unsigned int fd, unsigned int cmd, unsigned long arg);int sys_fcntl64(unsigned int fd, unsigned int c…

Inter process Communication in Linux Kernel (IPC)

When two processes trying to access a resource is called race condition. Main challenge is to avoid the race condition, the following method is usually adopted to avoid the race conditionuse of shared memory can prevent race condition Use of threads. Threads use the same address space when switching it to other threads(so same page directories and tables), whereas processes uses different address which leads to exchange of page directories and page tables. This is made with the help of POSIX Threads. Forms of IPCResource Sharing (system V Shared memory ) Synchronization (Mutex, Semaphores) Connection oriented data exchange (Virtual Sockets, Pipes, named Pipes(FIFO)) Connectionless data exchange (Signals) Linux implements all forms of IPCSynchronization in the KernelWhen multiple processors are used, how each processor is synchronized in the kernel is what described in this section.Processor can acquire a spinlock whenever it wanted to access a resource.typedef struct{volatile unsigned…

Page Directory, Page table and Memory Mapping Functions

Page Directories and Page Tables
Page directories and the page tables for the user and the kernel segment is managed by the kernel itself.
The short identifier for the page directory is pgd, pmd (page middle directory) and the short identifier for the page table is pte.
The data type for the page directory and the page table is respectively pgd_t, pmd_t and pte_t. 

Functions  relevant to Page directory
pgd_val(), pmd_val() - These functions allow to access the real value of the directory entry (either 32 bit or 64bit).pgd_alloc(), pmd_alloc() - provides the memory page for the respective page directory.  pgd_free(), pmd_free() - The directories are freed.pgd_clear, pmd_clear() - deletes the entry in the page directory.pgd_present, pmd_present() - checks whether the directoryentry refers to the page middle directory or page table, returns apositive result if there is an entry present, else returns a negativevalue.pgd_none(), pmd_none() - just the reverse of pgd_present(), pmd_present()set_p…

Conversion of Linear Address to Physical Address

The Conversion from the linear address to physical address is done by a separate processor or a MMU In architecture Independent model, page conversion is a three level process which takes four steps 1st part is used as an index in the page directory which refers to the page middle directory2nd part serves as an index to the page middle directory, there it refers to the page table3rd Part used as an index to the page table, there it refers to the physical memory4th Part of the address gives the offset within the selected page of memory. The x86 model supports a two level conversion of linear address to physical address. Highmem support helps upto physical memory size of 4GB being the address space of 32bit address, but Intel added four additional address pins to the Pentium Pro and created the physical address extension (PAE). Since the two level conversion supported only 32 bit addresses, the PAE has the control of supporting three level address conversion as in the Linux Archit…

Memory Management (Linux Kernel)

The data related to the processes are to be stored in the memory somehow by the operating system. In the olden days, primary memory is a scarce resource. and the application demands increases, so store the data of a process, it is necessary to use other forms of memory, either we can usePhysical address spacevirtual address spaceMapping the virtual addresses onto the physical addresses is the responsibility of the operating system’s memory management system.Memory protection prevents two processes exchanging data by changing the areas of memory used by both. The program code of a process can be executed in different threads at the same time. In principle, these threads share a common virtual address space.Architecture Independent Memory ModelAccess time is the primary factor used in Physical (main) memory. To minimize the access times, the cache memory versions like Level1 Cache (L1 cache) and level2 cache (L2 Cache) were used.Cache memory improves the performance of Personal Computer…

System Calls

System calls provides a layer between hardware and the user space.Provides abstracted hardware interface for user space system calls ensure security and stability single common layer between user space and rest of the system System calls usually accessed via function calls.for examplecall to the printf () function   //through a function call Access to the C library functions access the kernel and execute the write() function to write on to the screen System calls returns a long variable for success, negative value indicates error Each system call is assigned a syscall number defined in sys_call_table in entry.Ssystem calls are faster than many other operating systems Example System Calls//Example1asmlinkage long sys_getpid){return current->tgid;}Pause:Interrupts the execution of the program until the process is reactivated by a signal.asmlinkage int sys_pause(void){current-> state = TASK_INTERRUPTIBLE;schedule();return ERESTARTNOHAND;}Complex System Callsfork()The fork() functio…

Page Directories and Page Tables in Linux Kernel

Page directories and the page tables for the user and the kernel segment is managed by the kernel itself.
The short identifier for the page directory is pgd, pmd (page middle directory) and the short identifier for the page table is pte.
The data type for the page directory and the page table is respectively pgd_t, pmd_t and pte_t. 

Functions  relevant to Page directory
pgd_val(), pmd_val() - These functions allow to access the real value of the directory entry (either 32 bit or 64bit).pgd_alloc(), pmd_alloc() - provides the memory page for the respective page directory.  pgd_free(), pmd_free() - The directories are freed.pgd_clear, pmd_clear() - deletes the entry in the page directory.pgd_present, pmd_present() - checks whether the directory entry refers to the page middle directory or page table, returns a positive result if there is an entry present, else returns a negative value.pgd_none(), pmd_none() - just the reverse of pgd_present(), pmd_present()set_pgd(), set_pmd() - the kernel …

Main Algorithms of a kernel…

Timer InterruptsThere is one hardware timer that generates interrupts every 10ms and all the software timer synchronizes with it. Usually the timer stored in the variable jiffies. unsigned long volatile jiffies;The variable jiffies is modified by the timer interrupt every 10ms and hence it is declared as volatile.volatile struct timeval xtime:This is the actual time which again modified by the timer interruptOther functions of timer interrupt likedo_timer();updates the jiffiestimer_bh();updates the timer and processing of the timer related functionsupdate_process_time();collects data for the scheduler and decides whether it has to be scheduled.The Schedulerschedule () is function declared in kernel/sched.cThe actions of the scheduler is given below, once the schedule() function is called, Upcoming software interrupts are processed (so interrupts are given higher priority over the other entities in the system) process with highest priority determined (if two tasks has equal priority, t…