Archive for the ‘1’ Category

Semaphore Vs Spinlocking

It’s been quite a long time since i updated the blog … Not because i was busy but just because i was too lazy or may be i can say i dint find anything useful to write .

Anyway during the device driver class we had a discussion on implementing Reader/Writer problem in the kernel space to understand wait queues and locking . We started realizing that locking is not as easy as it sounds or as easy as it appeared for those who studied these concepts from galvin . During the demo a open question was posted as to use semaphore or a spin lock for ensuring mutual exclusion for the reader/writer problem . So this tutorial is for those who thinks semaphore mechanism and spin locking are the same.

In a semaphore when we call the down(struct semaphore *) or down_interruptible(struct semaphore*) is that, when the shared resource is locked ( Note : Rem that the shared buffer cannot be locked only instructions that access can be locked) the down call will put the calling process to sleep . So can this mechanism be applied for all synchronization . A big NO!!!!!!!!!! . Suppose we r using a semaphore to sync access to a device in a interrupt handler . Can u put the interrupt handler process to sleep ??? The interrupt handler will only be wakened after quite a long time . I think you should be able to guess the consequences . So what do we use here ???? It’s where the Spinlock kicks in…

The spin lock cannot be put into sleep . It will continuously poll for the processor and it will can be used in process like interrupt handlers . So know with this knowledge which one do we use for Reader/Write problem ???? Take this scenario : Initially the circular buffer is empty and the reader tries to read the data from the buffer . It gets the lock and tries to read from the queue . If it is a spinlock it will not relinquish the processor . So writer process cannot even write the data into buffer . So wat happenns the kernel hangs . The only way is switch of the PC DIRECTLY . No other way . So u must understand how sensitive these issues are and how tough it is to write such high privileged code .

1) We know that the 2.4 is non preemptive .Only one kernel process/thread can run . Then why use synchronization constructs .
2) What is the effect of interrupts in spinlocking ???


Why should bootable partition be within 1024 cylinders and how to overcome it??

After spending loadz & loadz of time debugging timing signals in GTKwave finally found sometime to update the blog . It’s 3:30 am in the morn… Thought this article would be worth to write . This article is one of the basics things a undergrad student who has taken OS must know but .. even the most guys even after taking Advanced Operating System course fail to know ….

Most of you while installing the OS must have had a problem saying that the bootable partition must be within 1024 cylinders. What is it exactly ??? For those who dnt know what a cylinder is .. google for it.. First i would like to throw light on boot sequence . When the computer is switched on

1) The BIOS has a program in it which is the first program run by the computer . This program reads the MBR . The MBR has 2 things boot code and MBR entires(info abt partions) . The code in the MBR reads THE FIRST SECTOR in the hard disk which contains the again contains code for a secondary boot loader . This boot loader checks which partion is bootable from the MBR records and loads the OS from that partition . Now where does the problem arise ????

The each MBR record contains 10 bits for cylinder and 8 bits for head(starts from 1) and 6 bits for sector(starts from 1)

so 1024 cylinders 255 head and 63 sectors can be represented ( If you dnt know abt CHS representation google it!!! or betta repeat the OS course )
so 1024*255*63*512( assuming each sector is 512 bytes) = 8GB (approx)
So.. the bootable partition has to be within first 8GB of hard disk 😦 . But we have hard disks of immense capacity eg:320 GB can’t i have bootable partition beyond it!!!!!! Read on!!!!

You must know something about interrupt . When an OS wants to read a chunk of data what it does??? It issues a INT 18(software interrupt & bios interrupt) . Before calling the interrupt it sets the values of specified registers to the CHS (Cylinder,Head,Sector) . After this interrupt data transfer takes place . Again the problem here is there is only provision for 10 bit addr for cylinder , 8 and 6 bits for head and cylinder .

Here comes LBA (Logical block addressing ) . Which assigns a unique number of each and every sector in the disk . CHS ->LBA translation is nothing but for each and every sector of the disk a unique number is assigned . Is LBA responsible for enabling to access boot partions beyond 8 GB no!!!!! Then what is it ???

The CHS used at 13h(BIOS side is ) L-CHS and the one that is used at disk drive is P-CHS . Suppose you want the boot partition to be present upto 4095 cylinder then 12 bits is allocated for cylinder and remaining 6 bits for head . Therefore the L-CHS has specification 1024 cylinders and 256 heads and 63 sectors while the P-CHS has the specifications 4096 cylinders 63 heads and 63 sectors . So when the CHS value at the MBR is sent to the 13h . The which is L-CHS . WHen the disk read command is issued the LCHS is converted to P-CHS thus >1024 cylinders is supported.

The caculations for L-CHS to P-CHS will be uploaded in the next update . These types of BIOS r no longer used . A newer types of bios is used which also supports 42h interrupt the details of which is also be in next blog . Why i need to know all this stuff??? If you want to write an Os , device driver , boot loaderyou can’t wothout these basic fundas!!!!!!!1

Processor Architecture

Finally decided to do something which i can put in my resume . So I, Rajesh and Ram decided to take up design of the processor . I chose to design the architecture of the processor . I was confused between whether to design the processor to support direct addressing or just immediate addressing . So i first came up with an architecture which would support only immediate addressing otherwise the control unit design would become really tedious . The timing signal generation would be a mammoth task . Then i later decided k let’s include direct addressing also . Now the architecture is ready . The control unit is yet to be designed . The architecture has 2 small mistakes . First is it should be RAM instead of ROM and output port and the data in port must be diff but in the diagram its the same so have to change that :(. Other one you guys have to find and comment . I’m uploading the image of the architecture so you guys can also get some ideas of how to design and to even comment my own design so i can correct it .

I have not included bus to reduce complexity . Instead i have used a register file which is much more easier to generate signals for.


Oppurtunity or Tragedy……

So today my TA duty gets over. For the past couple of days it has been a overload of Altera FPGA kits . Even a day more of fpga kits i would have started hating these kits which i used to love . I , Rajesh , Ramachandran were the hardware lab TA’s . Few days back we got a shock from the prof . One of my team members Rajesh was working in the lab . Prof Shankar asked how the TA job was going .Rajesh told it’s almost over . The prof told that the TA duty for digital ckts lab which we were to take care next semester had been cancelled coz the senate did not approve the course . He asked rajesh whether we guys would like to continue as hardware lab TA’s . The prof told next sem we dnt have to do any work ( When my friend said this i got a immediate smile on my face but it’s was so short lived..) . The prof continued . He said that he was to form a system programming team . He told that he has plans of making device available online .. What??? Online how ????? K The embedded device would be shared in the LAN . So ppl do not need a physical access to the device . Instead the programs which we want to burn would be transferred over the LAN and flushed into the ckts and the o/p’s will be sent back to the LAN .He asked whether we are taking device drivers course next sem . We said yes .Then he asked whether we guys would like to write the device driver for it . We were not able to say yes or no coz we know that we are sure to get banged by the course work next semester . We have planned to take the most heavy courses . We have been searching for a oppurtunity like this . To work under him is like the coolest thing . Not many get that oppurtunity but thinking how we could get time is making us tensed . Still we guys are confused whether to accept the offer or not . SO we decided to meet him and talk to him again . Hope things turns out to be in favour of us……….

Things to ponder…..

Yesterday when i was writing the printf statement in C suddenly something went thro’ my mind.. It’s nothing but the variable arguments i entered in printf . This function is accepts variable arguments then y doesn’t user written function doesn’t accept it ??? A good itch …. I found out that even user written functions to can accept the variables too .. The answer is MACROS

va_list , va_start,va_arg , va_end these macros r responsible for the magic . They are available in stdarg.h

va_list – is used to declare a list that will hold the variable arguments

Eg : va_list list;

va_start – Used to u=initialize the list . va_start ( list created using va_list , name of the first static argument )

va_arg ( list created using va_list , data type ) each time the function is called we get one of the arguments

va_arg (list created using va_list)

#include “stdio.h”
#include “stdarg.h”

void print(char *msg,…) //notice the use of … which indicates that the function will receive variable arguments
int i;
va_list list;
int main()
return 0;

I compliled the program using a gnu c compiler .


Today i watched this movie @ sathyam cinemas……. Ho!!!!!!!! man what a movie… What effects … Simply loved each and every frame… This movie highlighted india and indians…that was kind of nice… I liked it… The main reason for this post is to a recap of what happened before we went to the movie……..

K i was in lab with this stupid TA duty… It was earlier planned that around 8 guys would go for the movie .

Me : Hey Rajesh come on man it’s already 7 lets start for the movie….
Rajesh: Hey sorry man i have a project in man .. i gotto start it otherwise i won’t get sleep..
Me: Hey too much man … we can do tomo let’s party today..
Rajesh : No da u proceed..

I was completely vexed then the next conversation

Me: Hey rama where r u ???? shall we start to movie????
Ram:Macha i’m in my friends house da..Today rajesh told that we will going to movie on someother day so i started

Again disappointed… Then i called Aniket

Me : Hey ani where r u??????
Aniket : I’m in Ascendas with my old friends….

I was like.. What the fuck…………. 3 guys down… I called the remaining

Anuj ,Shanky , Panky.. They were like hey dude come on lets move … I was truely excited . Most of my friends r starting to their native today . They had hired a cab so we joined them and got down at sathyam…. Finally got to watch the movie…. Enjoyed it thorougly … Between we had our dinner at Noodle house forgot to mention abt it . And the special highlight of the night is anuj was about to enter girls rest room and the security guys was like hey where r u going ??? that’s girls bathroom !! What a bulb $#$#%^$^%&

First sem @ IIT ends…

The first sem in IIT ends today… Couldn’t beleive it … Time is flying so fast . It seems like i just finished my BE . Life in this 3 months was really a new experience . Enjoyed each and everyone second here . I got the feel of belonging to the institute which i failed to get in my BE . I feel its my institute and i have to do something in return for everything i get here . One can see a lot of alumini paying back . Whenever i see that ,i feel that i should also do something for the institute someday . We students here get the feeling of being respected .

Hostel life is truely amazing . Nightout , chatting with friends , eat at 2 am , coffee day , late night movies , night outs before assignment submission whoa!!! what a semester … Finally we burnt the midnight oil before the exams . Finally, managed to finish the exams and hoping a decent score even though not a very score 😦 . I have got more than a month holidays now . Thinking what to do … I have a couple of plans in my mind . Dnt know how much i am going to implement . Let’s c….