Deploying the best E-Resources for Software Engineering Students

We at IT Engg Portal, provide all the Computer and IT Engineering students of Pune University with well compiled, easy to learn notes and other E-resources based on the curriculum

Power Point Presentations and Video Lectures for Download

We provide the most recommended power point presentations and Video Lectures from the most prominent Universities for most of the difficult subjects to ease your learning process

Bundling Codes for your Lab Practicals

Deploying the best of available E-Resources for Tech Preparation (Campus Placements)

The Complete Placement Guide

Our Team has worked hard to compile this E-Book for all students heading for Campus Placements. The book is a complete solution for Technical Preparation for Campus Placements.

Pune University's most viewed website for Computer and IT Engineering

With more than 4,00,0000 pageviews from 114 countries over the globe, we are now the most viewed website for Ebooks and other E- Resources in Computer and IT Engineering

Showing posts with label Operating Systems. Show all posts
Showing posts with label Operating Systems. Show all posts

Sunday, July 29, 2012

Mutex and Semaphore : A Deeper Explanation - Operating Systems : TE [Comp - Sem 6] [I.T. Sem 5]


Let us first understand, what is Mutex aka Mutual Exclusion.
Mutex is the short-form for mutual exclusion object. In computer programming, a mutex is a program object that allows multiple program threads to share the same resource, such as file access, but not simultaneously. When a program is started, a mutex is created with a unique name. After this stage, any thread that needs the resource must lock the mutex from other threads while it is using the resource. The mutex is set to unlock when the data is no longer needed or the routine is finished.


Mutual exclusion, in computer science, refers to the problem of ensuring that no two processes or threads (henceforth referred to only as processes) can be in their critical section at the same time. Here, a critical section refers to a period of time when the process accesses a shared resource, such as shared memory. The problem of mutual exclusion was first identified and solved by Edsger W. Dijkstra in his seminal 1965 paper titled: Solution of a problem in concurrent programming control.
A simple example of why mutual exclusion is important in practice can be visualized using a singly linked list
In such a linked list the removal of a node is done by changing the “next” pointer of the preceding node to point to the subsequent node (e.g., if node i is being removed then the “next” pointer of node i-1 will be changed to point to node i+1). In an execution where such a linked list is being shared between multiple processes, two processes may attempt to remove two different nodes simultaneously resulting in the following problem: let nodes i and i+1 be the nodes to be removed; furthermore, let neither of them be the head nor the tail; the next pointer of node i-1 will be changed to point to node i+1 and the next pointer of node iwill be changed to point to node i+2. Although both removal operations complete successfully, node i+1 remains in the list since i-1 was made to point to i+1 skipping node i (which was made to point to i+2). This problem can be avoided using mutual exclusion to ensure that simultaneous updates to the same part of the list cannot occur.

Now, let us understand another very similar terminology called  Semaphore.


What is a Semaphore ?
In programming, especially in Unix systems, semaphores are a technique for coordinating or synchronizing activities in which multiple processes compete for the same operating system resources. A semaphore is a value in a designated place in operating system (or kernel) storage that each process can check and then change. Depending on the value that is found, the process can use the resource or will find that it is already in use and must wait for some period before trying again. Semaphores can be binary (0 or 1) or can have additional values. Typically, a process using semaphores checks the value and then, if it using the resource, changes the value to reflect this so that subsequent semaphore users will know to wait.
Semaphores are commonly use for two purposes: 
  • to share a common memory space and 
  • to share access to files. 
Semaphores are one of the techniques for interprocess communication (IPC). The C programming language provides a set of interfaces or "functions" for managing semaphores.

 In simple terms, a semaphore is a variable or abstract data type that provides a simple but useful abstraction for controlling access by multiple processes to a common resource in a parallel programming environment. 
     A useful way to think of a semaphore is as a record of how many units of a particular resource are available, coupled with operations to safely (i.e., without race conditions) adjust that record as units are required or become free, and if necessary wait until a unit of the resource becomes available. Semaphores are a useful tool in the prevention of race conditions; however, their use is by no means a guarantee that a program is free from these problems. 
     Semaphores which allow an arbitrary resource count are called counting semaphores, while semaphores which are restricted to the values 0 and 1 (or locked/unlocked, unavailable/available) are called binary semaphores (same functionality that mutexes have).
.




What are the differences between Mutex and Semaphore?  and
When to use mutex and when to use semaphore?

Concrete understanding of Operating System concepts is required to design/develop smart applications. 
As per operating system terminology, the mutex and semaphore are kernel resources that provide synchronization services (also called as synchronization primitives). 

Let us understand the difference between semaphore and mutex by considering the 'Producer - Consumer Problem'

The Producer-consumer problem:

Note that the content is generalized explanation. Practical details will vary from implementation.

Consider the standard producer-consumer problem. Assume, we have a buffer of 4096 byte length. A producer thread will collect the data and writes it to the buffer. A consumer thread will process the collected data from the buffer. Objective is, both the threads should not run at the same time.

Using Mutex:
   A mutex provides mutual exclusion, either producer or consumer can have the key (mutex) and proceed with their work. As long as the buffer is filled by producer, the consumer needs to wait, and vice versa.
    At any point of time, only one thread can work with the entire buffer. The concept can be generalized using semaphore.

Using Semaphore:
    A semaphore is a generalized mutex. In lieu of single buffer, we can split the 4 KB buffer into four 1 KB buffers (identical resources). A semaphore can be associated with these four buffers. The consumer and producer can work on different buffers at the same time.

Misconception:
    There is an ambiguity between binary semaphore and mutex. We might have come across that a mutex is binary semaphore. But they are not! The purpose of mutex and semaphore are different. May be, due to similarity in their implementation a mutex would be referred as binary semaphore.
  Strictly speaking, a mutex is locking mechanism used to synchronize access to a resource. Only one task (can be a thread or process based on OS abstraction) can acquire the mutex. It means there will be ownership associated with mutex, and only the owner can release the lock (mutex).
  
   Semaphore is signaling mechanism (“I am done, you can carry on” kind of signal). For example, if you are listening songs (assume it as one task) on your mobile and at the same time your friend called you, an interrupt will be triggered upon which an interrupt service routine (ISR) will signal the call processing task to wakeup.


FAQS on Mutex and Semaphore

 1. Can a thread acquire more than one lock (Mutex)?
Yes, it is possible that a thread will be in need of more than one resource, hence the locks. If any lock is not available the thread will wait (block) on the lock.

2. Can a mutex be locked more than once?
A mutex is a lock. Only one state (locked/unlocked) is associated with it. However, a recursive mutexcan be locked more than once (POSIX complaint systems), in which a count is associated with it, yet retains only one state (locked/unlocked). The programmer must unlock the mutex as many number times as it was locked.

3. What will happen if a non-recursive mutex is locked more than once.
Deadlock. If a thread which had already locked a mutex, tries to lock the mutex again, it will enter into the waiting list of that mutex, which results in deadlock. It is because no other thread can unlock the mutex. An operating system implementer can exercise care in identifying the owner of mutex and return if it is already locked by same thread to prevent deadlocks.

4. Are binary semaphore and mutex same?
No. We will suggest to treat them separately, as it was explained signalling vs locking mechanisms. But a binary semaphore may experience the same critical issues (e.g. priority inversion) associated with mutex. We will cover these later article.
A programmer can prefer mutex rather than creating a semaphore with count 1.

5. What is a mutex and critical section?
Some operating systems use the same word critical section in the API. Usually a mutex is costly operation due to protection protocols associated with it. At last, the objective of mutex is atomic access. There are other ways to achieve atomic access like disabling interrupts which can be much faster but ruins responsiveness. The alternate API makes use of disabling interrupts.

6. What are events?
The semantics of mutex, semaphore, event, critical section, etc… are same. All are synchronization primitives. Based on their cost in using them they are different. We should consult the OS documentation for exact details.

7. Can we acquire mutex/semaphore in an Interrupt Service Routine?
An ISR will run asynchronously in the context of current running thread. It is not recommended to query (blocking call) the availability of synchronization primitives in an ISR. The ISR are meant be short, the call to mutex/semaphore may block the current running thread. However, an ISR can signal a semaphore or unlock a mutex.

8. What we mean by “thread blocking on mutex/semaphore” when they are not available?
Every synchronization primitive will have waiting list associated with it. When the resource is not available, the requesting thread will be moved from the running list of processor to the waiting list of the synchronization primitive. When the resource is available, the higher priority thread on the waiting list will get resource (more precisely, it depends on the scheduling policies).

9. Is it necessary that a thread must block always when resource is not available?
Not necessarily. If the design is sure ‘what has to be done when resource is not available‘, the thread can take up that work (a different code branch). To support application requirements the OS provides non-blocking API.
For example POSIX pthread_mutex_trylock() API. When the mutex is not available the function will return immediately where as the API pthread_mutex_lock() will block the thread till resource is available.


References:







Thursday, November 17, 2011

Pune University : TE - IT - 2010 Dec : Question Papers




Question Papers for Third Year Engineering  in
 Information Technology :Pune University
     
Subject
Download Link
Software Engineering
Operating Systems
Theory of Computation
Computer Network Technology

Wednesday, September 28, 2011

What is Mutex? - Explanation and Animation

   
     In computer programming, a mutex (mutual exclusion object) is a program object that is created so that multiple program thread can take turns sharing the same resource, such as access to a file. Typically, when a program is started, it creates a mutex for a given resource at the beginning by requesting it from the system and the system returns a unique name or ID for it. After that, any thread needing the resource must use the mutex to lock the resource from other threads while it is using the resource. 
    If the mutex is already locked, a thread needing the resource is typically queued by the system and then given control when the mutex is locked during the new thread's use of the resource). becomes unlocked (when once more, the the mutex is locked during the new thread's use of the resource).
    Semaphore owns the resource ownership whereas mutex doesn't. Means when the semaphore enters the critical section by setting the sem_flag as one but because of some means(lets say dead lock) it gets blocked inside the critical section waiting for another resource,which is being used by another process/thread.So now no other process can enter the  critical section ...almost deadlock..means no other process can unset the sem_flag to zero..=> implies the process which sets the lock/flag has to unlock the flag.No other process/thread has the permission to unlock..


    On the other hand, mutex doesn't own the ownership means even if it gets blocked inside the crtitical region,any other process can enter the critical section. A semaphore post (or basically unlock) can be performed by  a different thread. However, in the case of mutex, it should be unlocked only by the same thread.

 Lets understand how does the Mutual Exclusion works using this animation -



Thursday, July 14, 2011

Operating System - Lecture Notes

Download Operating System Lectures Notes from our portal. The book can be used as a guide to understand the basic concepts of Operating systems and as a guide for Last Minute Revision.

Operating System - Lecture Notes


Wednesday, June 15, 2011

Operating Systems - TE-IT - 5th sem

Operating systems is a subject which i feel will  be really beneficial for all the Computer and IT engineers. I loved this subject. The subject is really interesting and moreover  very important as it is the most triggered subject  during campus placements.




    Download torrents for Ebooks of Operating Systems:


                                                  




Direct Download




Operating System Concepts - Galvin




Download Now
Alternative Download





 Alternatively, you can  also refer to this books: Modern Operating Systems - Andrew S. Tanenbaum


                                             

Alternative Link (Torrent)





Operating Systems -
Per Breinch Hansen


Download Now
Alternative Download



  The details of the subject disclosed here are with regard to the 2008 Pattern of Pune University:


 * Unit 1 - It is just and introduction to the basics of operating system. Even though it is quite lengthy .... i would never say its TUFF....  its pretty easy to learn the concepts of this Unit. Certain concepts require more examples to actually get the concept into your brain. Simply use Wikepedia to fire your querry.....every aspect will be clear.


*Unit 2 - Interesting Unit... all concepts can be easily understood by using the PPTs provided by Galvin on his official website.....


* Unit 3 -Process Communication and Synchronization- Easy to learn and understand.... u can do this unit by giving a glance to the concepts like you go through a novel.


*Unit 4 -5-> are the most important concepts in the book...u  really need to understand them to learn them.  These units may not have that many algorithms but will contain a lot of theory.




* Unit 6 - > This is again a simple unit...you dont need to actually study anything...all concepts are related to security and ethical issues.... people who often surf the net are already acquainted with a major part f this unit...




 VERDICT : I have mentioned that this subject is easy to learn and understand.... but i never meant that you can get through this subject without actually studying . OS ,even though interesting ...it is really time consuming..!!!!!!


Books:  Two most recommended books are the one s written by Galvin and William Stallings. I preffered William Stallings more ...though most others preffered  Galvin..
At the end both are equally good.


Please don't even accidentally fall prey to Techmax!!!...for OS- i would suggest Technical...if you are not going to use  to Foreign authors under any circumstances


 Operating Systems - Galvin   - is also  a nice book and is being used by many students


DOWNLOAD OPERATING SYSTEMS - BY  GALVIN - EBOOK -HERE.


Video Lectures

Introduction to Operating System


Memory Management
(Part 1)


Memory Management
(Part 2)