Tuesday, July 17, 2012

Operating System concepts ... Semaphore

Hi All ,

You might be waiting for the part2 of cracking the code , are you ?.. Ok.. we will come to that later... Now lets discuss some operating system concepts.. like semaphore.

It seems that you know semaphores..is it ? Ok then let me ask some questions

1. what is a semphore?
2. Is a semaphore is a hardware/software stuff?
3. Have you ever used the semaphore in real-time programming till now?

Yeah... I think most of you can answer the first two questions.... And the third question is of course can be answered by most you people. If you are one among those who answered Yes to question no 3.. this post may not be so interesting to you.. however you can proceed reading further... This post mainly intended to those people who are thinking that they haven't done something in real-time (especially undergraduate students .... Once upon a time I used to think like that in my graduation :)... oops I told you one of secret.. ).

Now we will start with answering the questions above :

A semaphore can be a hardware or software tag variable . It is used to find the status of a common resource ( you might be thinking common to what ?? ... just try yourself you will get the answer .. ) . So the primary purpose of the semaphore is locking the resource being used . So whenever a process needs a common resource it checks the semaphore and if it is locked wait for the lock to be released by another process.

Yeah ... I got it ... Now you are thinking what if there are many no instance for a single resource type.. aren't you ? . Of course the solution is  as simple as a child can answer it... Thats the beauty of computer science.. common sense is enough to learn the concept... What a child would do to give chocolates to his friends ? ( of course he wont be giving any way !!) he counts on the no of chocolates remaining in his hand each time he gives a chocolate to one of his friend... thats it we need to count the no of resources available and when the resources are no more available make the semophore locked .... as simple as such .



Thought for your minds : How many types of semaphores are there??


Do not scroll down ... take some time and answer the question.

Yes !! you are right... there are two types of semaphores ..

1. Binary semphore
2. Counting semaphore


I think by now you can correlate the examples.


Now I will give you an real time example of the situation where in you need to have a semaphore ?? Ok.. I know we are not talking about where the semaphore is used in OS...



Suppose you are the CXO of one the biggest  companies (wowww.. or just think you are a start up who can not afford more money )  and you have purchased a software( now onwards called as application) . The twist here is that the software requires a separate license ( I am trying to be as simple as possible... in this process I might leave out some details ) for each user who is using to that application and you got only two licenses.And here is the requirement :

1. Every day 100 reports have to be generated using that application.
2. As you are aware of CS concepts you want it to be done simultaneously.. not just in queue..I mean running one after one sequentially which is not feasible in the given scenario.
3. And now you decided to give this project to some XYZ who would be giving suggestions as what to do next .




Now for the time being change your role to programmer ( its difficult I know from CXO to programmer.. ) .

Now take a minute and analyze the requirements:


As you might be knowing software engineering is very funny... Client doesn't know what he wants clearly... he says something and the tech guys understand some else and they will eventually come up with some novel thing  !!! So its better to invest time in requirements analysis. Don't you agree with me ah?

1. Don't you think that your client is missing something ??
2. If you think so what could be those missing parts??

Ok think about it in your free time . Now analyze the situation there are 100 reports to be generated. And with your knowledge you got to know that each job will take around 3 to 4 hours... the process can release the license after 30 min are so ( think of the situations. How could the process be able to release the license without completing its job??) . The problem is that if you do it manually you have to do it in sequential manner which means you need to spend around 300-400 hours !!!


But you are so innovative and came up with the solution . Guess what the solution could be??

Yeah you are a programmer and you aware of multi-threaded programming and batch processing related things .So obviously you suggestion is developing a program  which automates the process of generating the reports using the available license.

Hmmm... are you sleepy ? bored ?? you might be if you are not following the requirements thoroughly.. or else if you have not given a thought about the requirements analysis..

If you do analysis of the requirements ... ah.. yes .. you are right  !!!.. we can use a counting semaphore here and automate the process using threads .Here semaphore is used to track the no of available licenses .. and checking if one license is available for the next thread ...



I know .. its would be like walking through hell .. if I explain the set of requirements and what the procedure we followed to solve this problem. However I am confident enough that you will have a nice understanding of the semaphore ... and how we can use it build complex systems . Yeah I forgot your CXO  appreciated your effort very well !!!


There is a saying "if you are going through Hell just keep going to see what lies ahead of it " . So if you are so interested just check out some license based applications like crystal report etc . If you are stuck with something  I am always here to help you . Happy learning .

No comments:

Post a Comment