TimeLinux1

Friday, May 27, 2011

Linux in Depth - 2

... continued ...

-Linux process management is similar to Unix but there are some imp differences.
-In Unix, process creation is a two step process:
    . a new process which is exact copy of the parent is created using the fork() system call.
    . the child is then allowed to run its own program using the exec() system call.
-the two steps are required for the following reasons:
    . step 1, fork(), is reqd bec otherwise the child will cause the termination of the parent process.
    . step 2, exec(), is reqd bec otherwise the child will continue running the same process as parent.
-in step 1, fork(), the child inherits the environment of the parent. eg: signals, address space, open files etc.
-in step 2, exec(), the child changes  the environment specific to the program to be run.
-To identify and manage a process the following properties are used by the os:
    . pid        -  for identification
    . credentials    -  for permissions
-also, a process may have one or more threads of execution within it.
-So the above was the Unix model of process management.

-in Linux, things are a little different.
-Linux does not differentiate between processes and threads.
-instead it refers to them as a third term called 'task'.
-task refers to a flow of control within a program. it resembles Unix threads.
-besides providing fork(), linux has a clone() system call that creates a task (like unix threads).
-one can think of clone() as a superset of fork().
-If the parent and child have the same env (signal, addr spc, files etc) then clone is equiv to fork.
-the main reason for this new model in Linux is efficiency.
-In Linux, the clone() call causes the context of a program to be stored in independent subcontexts.
-this independence of subcontexts helps in sharing resources between multiple processes.
-this means fewer resources can support more processes--which leads to increased efficiency in process mgmt.

-Linux filesystem behavior is similar to that of Unix.
-anything that can handle an input, output stream of data is treated as a file.
-as such, devices are treated as files as well and managed by special device files.
-device files may be of three types - block, character and network.
-block devices - allow random access to data blocks. eg hard disks, optic disks, flash etc.
-char  devices - allow serial access to data. eg keyboard, mouse, display
-netw  devices - allow access to data via kernel. eg remote servers, disks etc.

-the kernel is informed of an event via traps (by procs) and interrupts (by devices).
-the processes are informed of an event via signals.
-signals allow processes to talk to each other. this is called interprocess communication.
-signals also are used by kernel to talk to processes.
-signals do not transfer data but only info about events.
-besides signals, a pool of semaphores are also used for interprocess communication.
-like signals, semaphores do not transfer data but only info about events.
-the actual data is transfered between process using pipes and shared memory.

---xxx--

Tuesday, May 24, 2011

Linux in Depth - 1

-linux looks and feels like unix--compatibility with unix has been a major design goal of linux.
-linux kernel was created and released by in 1991 on intel 80386, a 32 bit processor, by Linus Torvalds.
-first linux kernel version 0.01 was released May 14, 1991. It had no networking and limited device drivers.
-version 1.0 was released Mar 14, 1994. It had networking.
-current major version of linux kernel is version 2.6 that was released in 2003.
-while the kernel code for linux was written from scratch, non kernel code was modeled on GNU, X, BSD etc.
-in recent times, even BSD has taken Linux code in return; eg: pc sound h/w devices, certain math-libraries etc.
-linux in NOT a public domain s/w.
-in public domain s/w the author gives away the copyrights.
-in linux, copyrights are still held by various authors.
-rather linux is free s/w in that while maintaining authors copyrights it permits anyone to read, modify and redistribute it.
-while re-distributing, they have to give away the binaries plus the source code.
-note that its ok to charge a price for the redistribution.
-this means linux is free as in freedom but not necessarily in price/money.

-three components of a linux system:
    . kernel    - does h/w abstraction eg: cpu scheduling, virtual memory etc.
    . system libr    - system calls, for apps to talk to kernel
    . system utils    - individual system procs like daemons that have a specific task
-the processor has two modes - privileged and non privileged.
-the kernel code executes in the processors privileged mode.
-the other  code executes in the processors nonpriv    mode.
-the 'other' code is everything non kernel code. it could be a system code or user code.
-in privileged mode, the code has full control of system h/w.
-the switch between modes occurs due to system calls which triggers a 'context switch'.
-context switch (cs) is the switch of the processor between two processes.
-the two processes may be in different modes altogether - eg: privileged and unprivileged modes.
-during cs, the state of the current process must be saved in PCB before next process is scheduled.
-during cs, cpu is idle. so if the time spent in cs is minimized, system performance improves.
-cs is h/w dependent. ie, by better design, cs can be made more efficient.

-linux kernel code is a single monolithic binary, ie all the code is part of a single address space.
-it includes code for scheduler, virtual mem, device drivers, filesystem and networking.
-the main reason for putting all this code in one address space is 'performance'.
-bec all kernel code is in a single addr space, no context switch is reqd during os internal tasks.
-eg when a proc calls an os func or h/w interrupt occurs etc, no context switch is reqd.
-despite being monolithic, linux kernel is modular.
-non-essential code is kept outside of the kernel in the form of modules.
-these modules can be dynamically loaded or unloaded.
-modules run in kernel mode and have full access to the device they drive.
-two imp implications of modules outside of the kernel:
    . only module code needs to be recompiled (instead of whole kernel) if any changes made
    . module code can be distributed on non-GPL proprietary licenses
-in the above, the 2nd point (proprietary modules) would not be possible if module were inside the kernel.
-this is because the kernel is under GPL and forces all code in it to be GPLed.
-this is important because even though the modules run in kernel mode, they need not be under GPL like the kernel code.

... continued ...

Sunday, May 22, 2011

OS Concepts - Memory Management - 3

... continued from previous discussion ...

-virtual memory:
-contiguous memory allocation, paging, segmenting all have a common goal - multiprogramming.
-all of the above use swapping of processes in and out of memory to achieve the goal of multiprogramming.
-however, the shortcoming is that they all swap the 'entire' process between mem and disk and back.
-this is where another concept of 'virtual memory' or 'vm' comes in.
-the central idea behind virtual memory is that an entire process need not be swapped in and out of memory.
-instead a process can be divided up into smaller chunks and those smaller chunks can be swapped.
-in other words, virtual memory allows execution of processes that are not completely im memory.
-infact, vm allows existence of programs much larger than physical memory.
-to achieve this the os considers physical memory plus a part of storage as one logical chunk of memory.
-this logical chunk of mem+disk is called virtual memory.
-vm also allows sharing of memory between processes.
-virtual memory is divided up into uniform sized frames.
-the frames are mapped to the logical pages which are what the cpu operates on.
-the logical pages to physical frames mapping is stored in the page table.
-the page table is the software construct that exists in the memory mgmt unit (mmu) that is a hardware component.
-remember, the structure of a process address space is comprised of:
    . text         -program file
    . data         -global vars, constants
    . heap        -dynamic memory location
    . stack        -temp area for functions, params, vars etc
    . empty space    -between heap and stack for growth of either
-in the above, the empty space between the heap and stack is made up of virtual memory.
-so as needed, page-frames from this empty-space are swapped in out.
-the implementation of vm is not without its pitfalls.
-if implemented incorrectly, it can degrade system performance.
-usually this happens in the case of page thrashing.
-thrashing is the condition in which the system is spending more time swapping and less time executing.
-this happens when the scheduler is overwhelmed with the page-frame swapping requests of running processes.

---xxx--

OS Concepts - Memory Management - 2

... continued from our previous discussion ...

-fragmentation - is a condition in which address spaces in memory are not contiguous.
-in other words, the address spaces are interspersed by blocks of memory that are unused.
-these blocks are unused as they are not sufficiently large to house address spaces belonging to any process.
-fragmentation causes inefficiency in memory usage.

-paging is a method to avoid or minimize fragmentation.
-paging divides:
    .physical memory into units called 'frames'
    .logical  memory (in cpu registers) into units called 'pages'
-the size of frames and pages are defined by the hardware and are in powers of 2 bytes. eg 512 bytes, 1 KB etc..
-frames and pages are usually same size.
-when a process requests data from disk, a frame is requested (from disk) and loaded as page (in memory).
-since the size of page=size of frame, the blocks fit exactly and there is no wastage of mem (ie fragmentation).
-a structure in the MMU called 'page table' keeps track of which frame is loaded as which page.
-note: MMU is a hardware device while page table is a logical construct within mmu.

-segmentation is another method of memory management.
-in segmentation, size of memory unit is not as important as content (as opposed to paging).
-in a way the segmentation is a users perspective of the memory.
-each segment is defined by a name and a length or offset.
-the segment name (by user) to physical address (in memory) is mapped in a 'segment table'.

-in certain computer architectures, both paging and segmentation can coexist.
-Intel architecture is an example where both the methods can coexist.
-on Intel arch, linux has 6 segments for kernel code, kernel data, user code, user data, tasks and a default.
-linux also has 3 level paging strategy for supporting other cpu archs.

... continued ...

Friday, May 20, 2011

Xoom - Motorola, what were you thinking?

I'm a bit late in commenting about this but better late than never. Actually I wanted the initial hype to die down a little bit before I took my assessment.
OK. So lets talk about Motorola Xoom.
Its a new tablet from Motorola based on Google's Honeycomb. It was launched earlier this year with a lot of fanfare and hype prior to its launch. Ads touting Xoom to be the next thing in mobile computing, the best thing that happened since the advent of stone tablets, ads mimicking the 1984 Superbowl ad and what not..
So all in all, the folks in Motorola spent a lot of time in the Marketing Blitz.
Then came the product itself and the next thing we know, its a dud.
Its a dud because, despite all the hype and hoopla, its not selling as it was expected and as it should be.
Why not? First off, the folks who decide product design at Motorola need to be fired. What is great about a tablet that is heavier, thicker and smaller than the competition? Why tout it as the Best thing to have happened since human beings adopted use of tablets? I can't understand. Perhaps you do. No wonder they are unable to sell the product based on the sheer appeal of the tablet. The competition beats it in the look and appeal.
Secondly, Motorola, you need to fire the folks who set the pricing. Why on earth would you introduce that is more expensive, actually at least $100 more than the similar model from the competition. Why on earth will anyone buy a tablet for $800 (Wifi + 3G), or $600 (Wifi only) which are pricier and yet have no physical look or appeal better than their competition? Simply does not make sense.
Looks like the Motorola management has less control of the marketing and product design aspects.
As a result the first REAL tablet we thought could do better than the competition, actually is a BIG disappointment.
Learn Motorola, Learn...

OS Concepts - Memory Management - 1

Memory Management:
-towards the goal of maximizing cpu utilization, multiple programs need to be stored in memory.
-memory consits of a large array of words or bytes each with its own address.
-the cpu fetches instructions from the memory according to the value of the program counter.
-the cpu can fetch instructions directly only from:
    . registers built into the cpu
    . main memory.
-in other words, for instructions to be fetched by cpu, they must be brought to the above two first.
-note that cpu cannot directly access secondary storage like disks.
-since registers are built into the cpu, their access is as fast as the cpu itself.
-main memory on the other hand is accessed via the memory bus and therefore is much slower to access.
-to bridge this, a fast memory between cpu and main memory called 'cache' is used.
-apart from speed issues, the multiple processes residing in memory need to be protected from each other.
-this is done using the concept of memory address space.
-address space protection requires hardware support in the cpu.
-memory address space is like an isolated container for the processes.
-the address space is bounded by a base-register and a limit-register.
-base -register defines the smallest address for the address space.
-limit-register defines the range of addresses above the base register, available to the address space.
-the base and limit registers are part of the cpu h/w.
-the cpu compares the address generated in user mode with the registers assigned to a process.
-if there is a mismatch, then the process is trying to access memory outside its address space and that must be stopped.
-this is done by trapping the process in the os and sending an fatal error to the user process.
-the 'os' here is the kernel that runs in kernel mode.
-the os or kernel has unrestricted view and access to the memory assigned to the kernel and the users.
-note: the memory has a fixed area assigned for os kernel space and the remainder for user space.

-swapping is the event in which a process is moved temporarily out of memory to disk or secondary storage.
-this allows the scheduler to put another process in ready queue and available for cpu to work upon.
-address space assigned to a process is usually contiguos.
-if this becomes not possible (eg due to excessive swapping), fragmentation of memory arises.
-the cpu register does not know the absolute memory address of a process.
-this is because cpu register has much smaller size than main memory.
-the address used by the cpu is called 'logical address'.
-the address used by the mem is called 'physical address'.
-the mapping of logical to physical address is managed by the 'mmu' (memory management unit).
-this concept of logical address mapped to a separate physical address is central to proper memory management.

... continued ...

Thursday, May 19, 2011

CPU Scheduling Notes

cpu scheduling:
-the objective of multiprogramming is to maximize cpu utilization.
-to do this a process is run on the cpu until it must wait.
-during the wait time, another process is picked up from a pool of processes in memory to run on the cpu.
-this picking action is done by the cpu-scheduler.
-in real terms it is the kernel threads not processes that are being scheduled.
-thus process scheduling and thread scheduling are often used interchangeably.
-the queue of processes in memory that are ready to run is called 'ready-queue'.
-note: the ready-queue (that the scheduler chooses from) is not necessarily a fifo queue.
-the entries in the ready-queue are pcb (process control blocks) of processes.
-scheduling can be either preemptive or cooperative.
-in cooperative scheduling a process keeps running until it must wait or it completes its task.
-cooperative scheduling is also called as non-preemptive scheduling.
-in preemptive  scheduling a process runs when it changes from either running or waiting state to ready state.
-most contemporary os these days use preemptive scheduling.
-preemptive scheduling is more complex, requires kernel and hardware support.
-once the scheduler has selected a process to run, another component the dispatcher comes into play.
-the dispatcher transfers the control of cpu from the outgoing to the incoming process.

-on systems that support threads, the scheduling is done at the thread level.
-further, the scheduling is only done for the kernel threads, not user threads.
-user threads are managed and served by thread library and the kernel is unaware of them.

-on systems with multiple cpus, the scheduling is done on each cpu individually.
-this way each cpu can share some of the kernel and user processes and is called 'symmetric multiprocessing' (smp).
-most modern os like linux, unix, windows are smp systems.
-on systems with multiple cores and multiple cpus, the cores can handle distinct threads.
-multicore systems consume less power than multiprocess systems.
-virtualized systems behave as multiprocessor systems in terms of scheduling.
-there the vm software presents virtual cpus to each vm env.
-linux, unix and windows all use priority based preemptive scheduling.
-linux had non-preemptive scheduling prior to kernel 2.6.
-since 2.6, the kernel was made fully preemptive.

Wednesday, May 18, 2011

Linux: Interprocess Communication

-interprocess communication or ipc:
-ipc is the mechanism of sharing data between multiple processes.
-the idea behind ipc is - efficiency - system speed, resource utilization.
-two fundamental models of ipc:
    . shared memory
    . message passing
-shared mem - a region of mem is marked to be shared bet processes.
-mesg  pass - processes talk and share data via mesgs.
-shared mem is faster, more complex to implement. kernel intervention not reqd.
-mesg  pass is slower, less complex to implement. requires kernel intervention.
-certain shared mem terms in linux/unix - shmget(), shmat(), shmmax, shmmin, shmsize etc..
-while ipc can be useful for communication between processes in a system, other mechanisms are needed for remote proc comm.
-a client server system is an example of remote process communication.
-three models of client server system are:
    . sockets
    . remote procedure call (rpc)
    . pipes (ordinary or named)
-sockets are an endpoint of communication - usually a 'host-ip:port' pair.
-in a client server socket system, there is a pair of sockets on the client and server side.
-rpc is a call from a local app to a remote app using at the kernel layer.
-pipes allow two processes to talk and share their data.
-ordinary pipes are one way - like parent child - ie child inherits the data and atrributes of the parent.
-named pipes are two way - no parent child - ie procs can exist independent of each other.
-in linux, named pipes are called fifo and created using mkfifo() system call.
-once created they can be operated with regular system calls like open(), read(), write(), close().

OS Concepts - 3

... continued ...

-threads:
-threads are subset of processes ie a flow of control within a process.
-threads are the fundamental unit of cpu utilization.
-a process can have one or more threads ie the threads of a process share the same address space in mem.
-threads increase efficiency - a multithreaded process can do multiple things simultaneously.
-eg: a web browser process may have one thread displaying graphics while another gets data from disk.
-a thread can listen for client requests and spawn a new thread for the req; then go back to listening.
-multithreading becomes more efficient on multicore chips.
-in that, each thread can run independently on a different core in parallel.
-note: the os sees each core within a chip as a separate processor.
-most os kernels these days have multiple threads.
-eg: linux has a multithreaded kernel that among other things do free memory management.
-threads exist in both the user and kernel space.
-threads in the user space interact with threads in kernel space via system calls.
-the relationship between threads in user space and kernel space may be many-one, many-many, one-one.
-the one-one model is more common in contemporary os.
-threads are implemented via three main type of libraries.
-the three libraries are:
    . pthreads    -for linux, unix
    . win32 api    -for windows world
    . java threads    -for jvm implementations.
-in linux, the distinction between process and threads is blurry.
-instead they are treated as 'tasks'.
-for this, the clone() system call is used instead of a succession of fork() and exec() calls.
-note: when a child process is created first the fork() call is made.
-this creates two identical address spaces (ie processes).
-thereafter the exec() call is made to load a binary image of a program into memory (ie executes it).
-in this manner, the parent-child processes are able to communicate initially and then go separate ways.
-the parent then runs the wait() system call to wait for a return code from the child after the child finishes its work.
-in linux, the clone() system call creates the tasks.
-the task 'points' to the address space of the parent instead of creating a separate address space for the child.

---xxxxx--

Sunday, May 15, 2011

OS Concepts - 2

... continued ...


-system calls are internal command instructions to the kernel.
-they are an interface to the services made available by an operating system.
-user programs use system calls to interact with the kernel and in turn the hardware.
-system calls are generally routines written in C (occassionaly C++ or Assembly).
-Examples of system calls - open(), read(), write(), exec(), fork() etc..
-usually OS, provide a set of APIs that programmers can use without having to dabble with system calls.
-the APIs give a uniform set of commands that internally call system calls
-Examples of APIs are three - Posix APIs (Linux, Unix, Aapl); Win32 (Windows); Java API for JVMs.
-Six types of system calls relate to - process, file, device, info, communication, protection.

-multitasking os usually require interprocess communication.
-interprocess communication can be one of two models - message passing model or shared memory model.
-in mpm, message tokens are passed bet two procs while they are in their own address spaces.
-in smm, two processes agree to share some of their address space in order to communicate.
-mpm is suitable for small amounts of data share, esp between two computers.
-smm is suitable for large data share and is faster.
-smm is more complicated to implement than mpm.

-virtual machines:
-the fundamental idea behind virtual machines is to abstract h/w of a single system into multiple execution env.
-vm is attained via a vm abstraction layer (called hypervisor) for the h/w and cpu scheduling.
-vm tricks the guest os to think that they are in full control of the h/w.
-in this no changes on the guest os are reqd.
-paravirtualization - is another variation of vm.
-in pvm, the guest os is presented with an exec env which is similar but not identical to guest's preferred system.
-in other words, the guest must be modified to run the pvm h/w.
-in pvm only one kernel is insalled and h/w is not virtualized (the kernel env is global env)
-rather the host os and its devices are virtualized (the guest env are local env)
-this provides the processes in the guest os to think that they are the only procs on the system.
-examples of pvm are solaris containers.
-from an implementation perspective, the host os has a kernel mode and a vm env that is in user mode.
-then within the vm env, each guest os has its own 'virtual' kernel and user modes.
-when the guest os makes a system call, it is passed to the hypervisor.
-the hypervisor then changes the register contents and program counter for the vm to simulate a real system call.
-vm require h/w support.
-in vmware, the hypervisor runs in the physical user mode.
-in other words, vmware runs as an application tool on top of the os.
-virtual machines running on top of vmware think that they are running on bare h/w.


-processes:
-a process is a program in execution. it is the unit of work in computing.
-it is a running set of resources like cpu time, memory, files and io devices.
-every process has an address space in memory, where it executes.
-the address space comprises:
    . text         -program file
    . data         -global vars, constants
    . heap        -dynamic memory location
    . stack        -temp area for functions, params, vars etc
    . empty space    -between heap and stack for growth of either
-a process changes state during execution.
-process states could be new, ready, running, waiting, terminated.
-process control block (pcb) is the info that the operating system maintains for every process.
-the process control block is how the os identifies each process and operates on it.
-the process control block contains imp info like state, register address, memory addr, account info etc.
-a process may have multiple threads of execution.
-threads increase efficiency in computing.
-if a program is invoked multiple times, they have different processes and threads but might share some info.
-eg: if a browser is opened multiple times simultaneosly, each will be a separate process they may share some cache info.
-process scheduler is a programming construct of the os.
-the job of process scheduler is to select the process to run on the cpu from a pool of pcbs.
-when scheduled, each resource (cpu, mem, disk) will have its own queue that has multiple processes that wait on that resource.
-processes can be cpu bound or io bound ie one that spends more time on cpu or io operations resp.
-for efficiency a scheduler needs to put a good mix of cpu and io bound procs.
-context switch is the operation during which a cpu switches between procs.
-in context switch the cpu stops and saves its current proc and starts a new one.
-the time spent in context switch is a cpu overhead as the cpu does not do any useful work during this time.

... continued ...

OS Concepts - 1

OS Concepts:

-A very general definition of OS will be that it is:
    . a big program
    . that manages the hardware of a computer
    . on behalf of its users and other programs.
-the definition of an OS varies from the viewpoint it is looked at.
-a users definition of OS will differ from that of a program or that of the system itself.

-kernel - is that part of the OS that is intimately associated with hardware control and operation.
-kernel always runs on a computer.
-system programs are programs that are part of the OS but not part of the kernel.
-application programs are programs that are not part of the OS and are initiated by the user.

-a general purpose computer consists of cpus and device controllers connected via a system-bus.
-the device controllers control devices like memory, disk drives, video, keyboard etc.
-the cpu and device controllers can execute concurrently, competing for memory cycles.
-the memory cycles are in turn dependant on cycles of signals on the system-bus.
-typically the os has a device driver for each device controller.

-the bootstrap program that resides in the firmware, reads and loads the OS from disk to memory.
-once booted the os waits for some event to happen from either the s/w or h/w.
-events generated by h/w are called interrupts.
-events generated by s/w are called traps. traps are signalled by something called 'system calls'.
-the interrupts or traps are basically requests for cpu attention.
-during the event, the cpu stops what it is doing, stores the info in a fixed address in memory and attends to the event.
-once done with the event, it returs to the fixed address to resume its work.

-multiprogramming    - running multiple programs from one or more users simultaneously
-multitasking        - like multiprogramming but the tasks are from a single user.
-the above two ideas follow from that the system keeps many jobs in main memory simultaneously in a job pool.
-then it picks up jobs from the job pool to execute on the cpu. This is done by the scheduler.
-the switching between jobs is expected to be smooth so as to permit an effective end-user experience.

-to distinguish between user code and system code, two modes of os operation are defined:
    . user-mode and
    . kernel-mode
-they are set using a mode bit in the software.
-the mode bit allows the os to distinguish between a code from user or kernel.
-for user-mode,   mode bit is 1.
-for kernel-mode, mode bit is 0.
-eg: when a program run by a user requests a service from the os (via a system call), it changes the mode bit.
-at system boot, the h/w and os start in kernel mode.
-then the os starts user applications in user mode.
-when an event occurs (trap or interrupt), the h/w and os switch from user mode to kernel mode.
-this dual mode of operation protects the system from crashing due to runaway user programs.
-this is because the h/w allows privileged instructions to be run only in kernel mode.
-if an attempt is made by user program to run a privileged h/w instruction, then the h/w traps it to the os.
-the os shows an appropriate error message and the memory space for the user program may be dumped.
-the only way an user program can request h/w service is through a system call.
-note: intel 8088, on which dos ran, did not have the mode bit. so a runaway user program could crash the system.
-note: process failure causes memory to be dumped into a file called 'core dump'.
-note: kernel  failure causes kernel to create a file called 'crash dump'. kernel failure=crash.

-process is a program in execution.
-to become a program in execution, a process needs cpu time, memory, files and io devices.
-a process can have one or more threads in it.
-a thread is a sequence of instructions within a process.
-a single threaded process has one program counter.
-a multi  threaded process has one program counter for each thread.
-a program counter specifies the next instruction to be executed by the cpu.
-many processes can execute concurrently by multiplexing on a single cpu.

-cpu and memory are tighly integrated to work together.
-during process execution, cpu constantly reads and writes instructions from the memory.
-for a program to be executed as a process, it must be mapped and loaded to a fixed address in memory.
-this address in memory is called address space.
-in a multiprogramming system, the address space for each process needs to be isolated from others.
-yet at the same time, the processes must be able to share resources for efficiency.
-these present design and implementation challenges to the os.

... continued ...