2012/01/24

Input Example

Question:
Write a step by step execution of things that happen after a user presses a key on the keyboard. Use as much detail as possible.

Answer:
1. The keyboard sends a scan code of the key to the keyboard controller (Scan code for key pressed and key released is different).
2. The keyboard controller interprets the scan code and stores it in a buffer.
3. The keyboard controller sends a hardware interrupt to the processor. This is done by putting signal on “interrupt request line”: IRQ 1.
4. The interrupt controller maps IRQ 1 into INT 9.
5. An interrupt is a signal which tells the processor to stop what it was doing currently and do some special task.
6. The processor invokes the “Interrupt handler”. CPU fetches the address of “Interrupt Service Routine” (ISR) from “Interrupt Vector Table” maintained by the OS (Processor use the IRQ number for this).
7. The ISR reads the scan code from port 60h and decides whether to process it or pass the control to program for taking action.

About OS Threads

Priority Inversion
http://en.wikipedia.org/wiki/Priority_inversion

Consider there is a task L, with low priority. This task requires resource R. Consider that L is running and it acquires resource R. Now, there is another task H, with high priority. This task also requires resource R. Consider H starts after L has acquired resource R. Now H has to wait until L relinquishes resource R.

Everything works as expected up to this point, but problems arise when a new task M (which does not use R) starts with medium priority during this time. Since R is still in use (by L), H cannot run. Since M is the highest priority unblocked task, it will be scheduled before L. Since L has been preempted by M, L cannot relinquish R. So M will run till it is finished, then L will run - at least up to a point where it can relinquish R - and then H will run. Thus, in above scenario, a task with medium priority ran before a task with high priority, effectively giving us a priority inversion.

Solution:
Disabling all interrupts to protect critical sections
A priority ceiling
Priority inheritance



Dead Lock
1. Mutual Exclusion: Only one process can use a resource at a given time.
2. Hold and Wait: Processes already holding a resource can request new ones.
3. No Preemption: One process cannot forcibly remove another process’ resource.
4. Circular Wait: Two or more processes form a circular chain where each process is waiting on another resource in the chain.


Preemption
http://en.wikipedia.org/wiki/Preemption_%28computing%29
The period of time for which a process is allowed to run in a preemptive multitasking system is generally called the time slice, or quantum. The scheduler is run once every time slice to choose the next process to run. If the time slice is too short then the scheduler will consume too much processing time.

An interrupt is scheduled to allow the operating system kernel to switch between processes when their time slices expire, effectively allowing the processor’s time to be shared between a number of tasks, giving the illusion that it is dealing with these tasks simultaneously, or concurrently. The operating system which controls such a design is called a multi-tasking system

2012/01/23

How video frame catched.

VSYNC  indicates when a new image is about to start being sent out on the pixel data bus.

HREF indicates when a new line is about to start being sent out on the pixel data bus.

PCLK indicates when there is valid pixel data on the pixel data bus.

The VIP engine latches data into the VIP buffers during the following conditions:
• On the active EDGE of VSYNC (0 = falling-edge, 1 = rising-edge), the VIP engine will count until the number of expected lines are received (i.e. any extraneous lines are ignored).
• On the active LEVEL of HREF (0 = active high, 1 = active low), the VIP engine will count until the number of expected pixels are received (i.e. any extraneous pixels are ignored).

2012/01/17

Thread Condition Wait Coding Pattern

The reason why we need pthread_mutex_lock(&mutex) before pthread_cond_wait(&cond, &mutex) :
Need to protect the condition read/wirte of variables that changes the conditions.
Before we call pthread_cond_wait, we need to check if condition is met or not, if it is met, not call to pthread_cond_wait is needed.

Below sample code can be found at
http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Fapis%2Fusers_78.htm


#define _MULTI_THREADED #include <pthread.h> #include <stdio.h> #include "check.h" /* For safe condition variable usage, must use a boolean predicate and */ /* a mutex with the condition. */ int conditionMet = 0; pthread_cond_t cond = PTHREAD_COND_INITIALIZER; pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; #define NTHREADS 5 void *threadfunc(void *parm) { int rc; rc = pthread_mutex_lock(&mutex); checkResults("pthread_mutex_lock()\n", rc); while (!conditionMet) { printf("Thread blocked\n"); rc = pthread_cond_wait(&cond, &mutex); checkResults("pthread_cond_wait()\n", rc); } rc = pthread_mutex_unlock(&mutex); checkResults("pthread_mutex_lock()\n", rc); return NULL; } int main(int argc, char **argv) { int rc=0; int i; pthread_t threadid[NTHREADS]; printf("Enter Testcase - %s\n", argv[0]); printf("Create %d threads\n", NTHREADS); for(i=0; i<NTHREADS; ++i) { rc = pthread_create(&threadid[i], NULL, threadfunc, NULL); checkResults("pthread_create()\n", rc); } sleep(5); /* Sleep is not a very robust way to serialize threads */ rc = pthread_mutex_lock(&mutex); checkResults("pthread_mutex_lock()\n", rc); /* The condition has occured. Set the flag and wake up any waiting threads */ conditionMet = 1; printf("Wake up all waiting threads...\n"); rc = pthread_cond_broadcast(&cond); checkResults("pthread_cond_broadcast()\n", rc); rc = pthread_mutex_unlock(&mutex); checkResults("pthread_mutex_unlock()\n", rc); printf("Wait for threads and cleanup\n"); for (i=0; i<NTHREADS; ++i) { rc = pthread_join(threadid[i], NULL); checkResults("pthread_join()\n", rc); } pthread_cond_destroy(&cond); pthread_mutex_destroy(&mutex); printf("Main completed\n"); return 0; }

2012/01/03

JavaScript Create Object

Create Object with constructor
http://www.w3schools.com/js/js_objects.asp
function person(firstname,lastname,age,eyecolor) { this.firstname=firstname; this.lastname=lastname; this.age=age; this.eyecolor=eyecolor; this.newlastname=newlastname; } function newlastname(new_lastname) { this.lastname=new_lastname; } var myFather=new person("John","Doe",50,"blue"); var myMother=new person("Sally","Rally",48,"green"); myFather.newlastname("Gu");

Post Code on Blogger

Simplest way to post code to blogger for me: <pre style="background: #f0f0f0; border: 1px dashed #CCCCCC; color: black;overflow-x:...