Wednesday, May 27, 2009

Linux Notes

: Chapter 1:

Q. What is Kernel in Linux ?

Ans : The Kernel is the core program that runs programs and manages hardware devices, such as disks and printers. It execute the commands which provide by environment. Kernel provides an interface between shell and hardware.

Q2. Define the features of Linux ?

Ans : 1. Multi-tasking :

Linux supports true preemptive multi-tasking. All processes run entirely independently of each other. No process needs to be concerned with making processor time available to other processes.

Multi-user access :

A multi-user system is a computer that is able to concurrently and independently execute several applications belonging to two or more users.

Multi-processing :

Linux also runs on multi-processor architectures. This means that the O. S. can distribute several applications across several processors.

Architecture independence (Portability) :

Linux runs on several hardware platforms, from the Amiga to the PC to DEC Alpha workstations. Such hardware independence is achieved by no other serious O. S.

Demand load executables :

Only those parts of a program actually required for execution are loaded into memory. When a new process is created using fork(), memory is not requested immediately, but instead the memory for the parent process is used jointly by both processes.

Paging :

Linux provide a very important concept of paging. Despite the best efforts to use physical memory efficiently, it can happen that the available memory is fully taken up.

Dynamic cache for hard disk :

Linux dynamically adjusts the size of cache memory in use to suit the current memory usage situation.

Shared Libraries :

Libraries are collections of routines needed by a program for processing data. There are a number of standard libraries used by more than one process at the same time.

Memory protected mode :

Linux uses the processor’s memory protection mechanisms to prevent the process from accessing memory allocated to the system kernel or other processes.

Support for national keyboards and fonts :

Under Linux, a wide range of national keyboards and character sets can be used : for example, the Latin1 set defined by the International Organization for Standardization (ISO) which also includes European special characters.

Different file systems :

Linux supports a variety of file systems. The most commonly used file system at present is the Second Extended (Ext2) File system. This supports filenames of up to 255 characters and has a number of features making it more secure than conventional Unix file systems.

Q. Define the file structure of Linux ?

Ans: The file structure of any O. S. is includes the arrangement of files & folders. Linux organizes files into a hierarchically connected set of directories. Each directory may contain either files or other directories. Because of the similarities to a tree, such a structure is often referred to as a tree structure and also called parent-child structure.

The Linux file structure branches into several directories beginning with a root directory, /. Within the root directory several system directories contain files and programs that are features of the Linux system. These system directories as follows :-

/ root : Begins the file system structure, called the root

/fs : The virtual file system interface in in the fs directory. The

implementations of the various file systems supported by LINUX are

held in the respective subdirectories.

/home : Contains users’ home directories

/bin : Holds all the standard commands and utility programs

/usr : Holds those files and commands used by the system; this directory

breaks down into several sub-directory

/usr/bin : Holds user-oriented commands and utility programs

/usr/sbin : Holds system administration commands

/usr/lib : Holds libraries for programming languages

/usr/doc : Holds Linux documentation

/usr/man : Holds the online manual Man files

/usr/spool : Holds spooled files, such as those generated for printing jobs and

network transfers

/sbin : Holds system administration commands for booting the system

/var : Holds files that vary, such as mailbox files

/dev : Holds file interfaces for devices such as the terminals and printers

/etc : Holds system configuration files and any other system files.

/init : contains all the functions needed to start the kernel. Like start_kernel().

/net : contains the implementations of various network protocols and the

code for sockets to the UNIX and Internet domains.

/arch : architecture -dependent code is held in the subdirectories of arch/

/mm : contains Memory management sources for the kernel.

Q2. Define the Kernel Architecture ?

Ans : Most Unix kernels are monolithic : each kernel layer is integrated into the whole kernel program and urns in Kernel Mode on behalf of the current process. Microkernel operating systems demand a very small set of functions from the kernel, generally including a few synchronization primitives, a simple scheduler, and an interprocess communication mechanism. Although Microkernels oriented O. S. are generally slower than monolithic ones, since the explicit message passing between the different layers of the O. S. might have some theoretical advantages over monolithic ones.

Define the process and task_structure ?

Ans : The concept of a process is fundamental to any multiprogramming operating system. A process is usually defined as an instance of a program in execution; thus, if 16 users are running vi at once, there are 16 separate processes ( although they can share the same executable code).

Each & every process have some unique information, which store in task_struct type process descriptor, which is the object of task_struct.

Struct task _struct

{

volatile long state;

long counter;

long priority;

unsigned long signal;

unsigned long blocked;

unsigned long flags;

int errno;

int debugreg[8];

struct task_struct *next_task;

struct task_struct *prev_task;

struct mm_struct mm;

int pid, uid,gid;

struct fs_struct fs;

long utime, stime, cutime, cstime, start_time;

}

state field of the task_struct describes what is currently happening to the process. The following are the possible process states :

TASK_RUNNING : The process is either executing on the CPU or waiting to be executed.

TASK_INTERRUPTIBLE : The process is suspended (sleeping) until some condition becomes true. Raising a hardware interrupt, releasing a system resource the process is waiting for, or delivering a signal are examples of conditions that might wake up the process, that is put its state back to TASK_RUNNING.

TASK_UNINTERRUPTIBLE: In this state process is uninterruptible of any hardware interrupt, or any signal.

TASK_STOPPED: Process execution has been stopped : the process enters this state after receiving a SIGSTOP, SIGTSTP, SIGTTIN, or SIGTTOU signal.

TASK_ZOMBIE : Process execution is terminated, but the parent process has not stopped. The kernel cannot discard the data contained in the dead process task_struct because the parent could need it.

The counter variable holds the time in ‘ticks’ for which the process can still run before a mandatory scheduling action is carried out. The schedular uses the counter value to select the next process.

The priority holds the priority of a process.

The signal variable contains a bit mask for signals received for the process.

The bolcked contains a bit mask for all the signals the process plans to handle later.

flags contains the system status flags.

errono contains the error code if generated.

debugreg[8] assigns the debugger to that err code.

*next_task and *prev_task all processes are entered in a doubly linked list with the help of these two components.

mm_struct mm the data for each process needed for memory management are collected, mm_struct store those data.

Every process has its own process ID number , pid, user ID, uid, goup ID, gid.

The file-system-specific data are stored in fs_struct fs.

The utime and stime variables hold the time the process has spent in User Mode and System Mode, cutime and cstime contain the totals of the corresponding times for all child processes, start_time contains the time at which the current process was generated.

.

Q. What is the output of command ps ?

Ans : ps command output which processes are running at any instant. Linux assigns a unique number to every process running in memory. This number is called process ID or simply PID.

PID TTY TIME COMMAND

2269 tty01 0:05 sh

2396 tty01 0:00 ps

PID : Process ID

TTY : Terminal Id Which The Processes Were Launched

TIME : The Time That Has Elapsed since the Processes Were Launched

COMMAND : The Names Of The Processes.

Chapter 4 : Memory Management

Q Define the architecture - independent memory model in Linux ?

Ans : Memory Management is primarily concerned with allocation of main memory to requests processes. Two important features of memory management function are : Protection and Sharing. Memory management activity in a Linux kernel. Some of main issues related to memory management are :

Pages of Memory :

The physical memory is divided into pages. The size of a memory page is defined by the PAGE_SIZE macro. For the x86 processor, the size is set to 4 KB, while the Alpha processor uses 8 KB.

Virtual address space :

A process is run in a virtual address space. In the abstract memory model, the virtual address space is structured as a kernel segment plus a user segment. Code and data for the kernel can be accessed in the kernel segment, and code and data for the process in the user segment. A virtual address is given by reference to a segment selector and the offset within the segment. When code is being processed, the segment selector is already set and only offsets are used. In the kernel, however, access is needed not only to data in the kernel segment but also to data in the user segment, for the passing of parameters. For this purpose, the put_user() and get_user() functions are defined.

Programmers casually refer to a memory address as the way to access the contents of a memory cell. In x86 Micro processors, we have three kind of address.

(i) Logical Addresses :

Included in the machine language instructions to specify the address of an operand or of an instruction. Each logical addresses consists of a segment and an offset that denotes the distance from the start of the segment to the actual address.

(ii) Linear Address :

A single 32 bit unsigned integer that can be used to address upto 4 GB, that is upto 232 memory cells. Linear addresses are usually represented in hexa decimal notation; Their values ranges from 0x00000000 to 0xffffffff.

(iii) Physical Address :

Physical address is used to address memory cells included in memory chips. They correspond to the electrical signals sent along the address pins of the microprocessor to the memory bus. Physical Address are represented as 32 bit unsigned integer.

Converting the Linear address :

Linux adopted a three - level paging model so paging is feasible on 64 bit architectures. The x86 processor only supports a two - level conversion of the linear address. While Alpha processor supports three-level conversion because the Alpha processor supports linear addresses with a width of 64 bits.

Three level paging model defines three types of paging table :

Page (Global) directory

Page middle directory

Page Table

Page Global Directory :

Page Global Directory includes the addresses of several page middle directory. It is of 12 bit length. Different functions available for modification of Page Global directory are :

(i) pgd_alloc () : Allocates a Page Directory and filles with 0.

(ii) pgd_bad() : Can be used to test whether the entry in Page Directory is valid. (iii) pgd_clear() : Delete the entry in page directory.

(iv) pgd_free() : Releases the page of memory allocate to page directory.

(v) pgd_none() : Tests whether the entry has been initialized.

Page Middle Directory :

It includes the address of several Page Tables. It is of 13 bit length. Functions used for handling Page Middle directory are :

(i) pmd_alloc() : Allocates a Page Middle directory to manage memory in

user area.

(ii) pmd_bad() : Test whether the entry in the Page Middle directory is valid.

(iii) pmd_clear() : Deletes the entries in the page middle directory is valid.

(iv) pmd_free() : Releases a Page Middle Directory for memory in user segment. (v) pmd_offset(): Returns the address of an entry in the page middle directory to

which the address in argument is allocated.

(vi) pmd_none() : Tests whether the entry in the page middle directory has been

set.

Page Table :

Each Page Table entries points to page frames. It is of 25 bits length. The ‘dirty’ attribute is set when the contents of the memory page has been modified. A page table entry contains a number of flags which describe the legal access modes to the memory page and their state :

PAGE_NONE : No physical memory page is referenced by page table entry.

PAGE_SHARE : All types of Access are permitted.

PAGE_COPY : This macro is historical & identical to PAGE_READONLY.

PAGE_READONLY: Only read and execute access is allowed to this Page of

memory.

PAGE_KERNEL : Access to this page of memory is only allowed in the kernel

segment.

Following are some functions have been defined to mainpulate the page table entries and their attributes :

(i) mk_pte() : Returns a page table entry generated from the memory address

of a page and a variable of the pgprot_t type.

(ii) pte_alloc() : Allocates new page table.

(iii) pte_clear() : clears the page table entry.

(iv) pte_dirty() : checks whether ‘dirty’ attributes is set.

(v) pte_free() : Releases the page table.