Sunday, June 10, 2012

free command explained

Memory in the system can be viewed with free command


# free -m
                    total       used       free     shared    buffers     cached
Mem:           498         85        413          0          5         32
-/+ buffers/cache:         47        451

The  -/+ buffers/cache is calculated as follows
  • used uses -
  • free uses +
  • used -/+ = used - buffers - cached = 85 - 5 - 32 = 47 (~ 48)
  • free -/+ = free + buffers + cached = 413 + 5 + 32 = 450 (~ 451)

Monday, June 4, 2012

Process, Process States and Context Switch


A process is a execution of a program(program is,  say, an executable file) by CPU and many processes appear to execute simultaneously as the kernel schedules them for execution, and several processes may be instances of one program. The kernel loads an executable file into memory during an exec system call, and the loaded process consists of at least three parts, called regions: text, data and the stack
  • The text region corresponds to program code,
  • The data region corresponds to variables in the code
  •  But the stack region is automatically created and it's size is dynamically adjusted by the kernel at run time. The stack consists of logical stack frames that are pushed when calling a function and popped when returning. Because a process in Unix system can execute in two modes, kernel or user, it uses a separate stack for each mode.
A processor(CPU) can execute only one process at a time and the process my be executing in user mode or kernel mode

Process States

The lifetime of a process can be divided into set of states, each with certain characteristics that describe the process. 

Here is the complete set of process states
  1. The process is currently executing in user mode
  2. The process is currently executing in kernel mode
  3. The process is not executing but ready to run as soon as the kernel(scheduler) schedules it
  4. The process is not executing and ready to run, but the process must be swapped into main memory before the kernel can schedule it to execute.
  5. The process is sleeping and resides in main memory. A process puts itself into sleep when it can no longer continue executing , such as when it is waiting for an I/O to complete. When a process wakes up from sleep state, it is transitioned into "ready to run" state,where it is eligible for later scheduling; it does not execute immediately. Sleeping process do not consume CPU resources.
  6. The process is sleeping, but has been swapped into secondary storage to make room for other processes in main memory.
  7. The process is returning from kernel mode to user mode, but the kernel preempts it and does a context switch to schedule another process.
  8. The process is newly created and is in transition state - the process exists, but it is not ready to run, nor is it sleeping. This state is the start state for all processes.
  9. The process executed the exit system call and is in the zombie state. The process no longer exists, but it has an entry in process table. zombie state is the final state of a process and it consumes no cpu resources.
Context Switch

Context switch occurs when a kernel decides it should execute another process. When doing a context switch from one process to another process, the kernel saves enough information about the current executing process so that it can later switch back to it and resume it's execution.
  • Moving between a user and kernel mode is a change in mode and not a context switch.
  • When a process is executing in kernel mode, context switch to another process cannot occur. Thus, processes running in kernel mode cannot be preempted by other processes.
Kernel permits context switch under four circumstances
  • When a process puts itself into sleep
  • When a process exits
  • When a process returns from kernel mode to user execution mode, but is not the most eligible process to run
  • When a process returns to user mode after kernel completes handling an interrupt(an interrupt is always handled in the context of currently executing process), but is not the most eligible process to run.
Procedure  for context switch
  • Decide whether to do a context switch, and whether a context switch is permissible now.
  • Save the context of "old" process.
  • Find the "best" process to schedule for execution by using process scheduling algorithm and restore it's context.

How to find the context switches happening in the system?

The field "cs" in vmstat command under "system" gives the interrupts being processed in the system.

Interrupts and Exceptions

Unix/Linux systems allow devices such as I/O peripherals or the system clock to interrupt the CPU asynchronously. On receipt of an interrupt, the kernel saves it's current context, determines the cause of interrupt and services it. 

So what do we mean by "kernel saves it's current context" here?  

When a system is executing a process, the system is said to be executing in the context of a process. When the kernel decides to execute another process, it does a context switch, so that the system can execute in the context of another process. The kernel services interrupts in the context of the interrupted process even though it may not have caused the interrupt. The interrupted process may have been executing in user mode or kernel mode. The kernel saves enough information about the interrupted process so that it can later resume it's execution. An Interrupt is handled in the context of current process(the interrupt is handled in kernel mode) and the kernel does not spawn or schedule a special process to handle interrupts. Thus while handling an interrupt no context switch happens.

What happens if multiple devices send interrupts at same time to the kernel OR if an interrupt  is received during the execution of a critical region of a process?
  • The hardware usually prioritizes the devices according to the order that the interrupts should be handled. So when the kernel receives an interrupt, it services higher priority interrupts  by blocking out lower priority interrupts. 
             Typical interrupt levels from lower priority(left) to higher priority (right)

             Software Interrupts ---> Terminals --> Network Devices --> Disk --> System Clock ---> Machine Errors
  • Setting processor execution levels to mask off interrupts while handling critical regions of a process - If kernel receives an interrupt while executing a critical region of code, say like manipulating a linked list which involves pointers, kernel will raise the processor execution level so that interrupts at that level and lower levels are masked off, allowing only higher-level interrupts. Critical regions of code are usually small and infrequent.
What is an exception and how does it differ from interrupt?

An exception condition refers to unexpected events caused by a process, such as addressing illegal memory, executing privileged instructions, dividing by zero, and so on. 
  • They are distinct from interrupts, which are caused by events that are external to a process. 
  • Exception occurs in the middle of execution of an instruction and the system attempts to restart the instruction after handling the exception; interrupts are considered to happen between the execution of two instructions, and the system continues with the next instruction after servicing the interrupt.
How to find the interrupts being processed in the system?

The field "in" in vmstat command under "system" gives the interrupts being processed in the system.

Saturday, June 2, 2012

Reset root password in FreeBSD



  • On FreeBSD boot menu, press space bar to pause the default booting 
  • Boot FreeBSD in single user mode, by typing number 4 key
  • Enter full pathname of shell or RETURN for /bin/sh:
  • mount -u /
  • mount -a
  • Type passwd to reset the password
  • Renter the new password and confirm it
  • Reboot the machine and login with the new password