Kumar Gaurav
Kumar Gaurav

Reputation: 1317

What's difference between lock (spinlock etc) and blocking

This may seems a little absurd, but i'm confused.

  1. What's exactly difference between a locking and blocking operations?
  2. When do we use Block and when do we use Lock?
  3. When we can use spinlock,semaphore etc to put calling process to sleep (i suppose it happens, please mention if i'm wrong) then why do we create wait_queues to put process to sleep (i expect this is blocking).

Please answer.

Upvotes: 1

Views: 2506

Answers (1)

Federico
Federico

Reputation: 3892

When we can use spinlock,semaphore etc to put calling process to sleep (i suppose it happens, please mention if i'm wrong)

It is wrong, when you use spinlock, semaphore etc you are not putting the proccess to sleep. In spinlock case, it is dangerous if you sleep while you get a spinlock.

Locking is necessary in order to avoid concurrent access. Let say that you are writing a printer driver. The driver receive the text form an application and it prints out the text on the paper. What if you have two applications that need to print at the same time? If you do no use a locking policy, you will read on the same page a mixed text from two different applications. In this case, for example, you can use a semaphore or a mutex. When the firt application access the driver, the driver gets the mutex (semaphore) until the application finish to send text. When the second application access the driver, the driver cannot proceed because another process has the mutex active. So, the second printing process can sleep until the first one is over. When the first printing process release the mutex, the second one start to print.

Your user space application can use a blocking, or non blocking approach (man open(2)). When the driver is busy with the first printing process and the second printing process occurs, according to the application choice, the driver can block the second process, or return immediatly. If it is blocking, the driver will use a wait_queues that put the process to sleep until someone will do a wakeup (the first process when it is over). If it is non blocking, the driver return an error code (for example: EBUSY, EAGAIN) and the control return the the application; now the application can decide to re-try later, sleep a while or discard print and return the control to the user

Spinlock are used for low level concurrency. For example, variable used by different process at the same time. In this case we must guarantee that two different processer do not write on the same variable at the same time because the result is unpredictable. So we will do something like the following on the variable to protect

[get the spin lock]
[chage the variable(s)]
[release the spin lock]

This guarantee that only one process at time will modify the variable protected with spinlock. It is dangerous to use spinlock while sleeping because a spinlock does not sleep. When a process found an active spinlock, it does not sleep but it waits in a while loop until someone release the spinlock. If you sleep with an active spinlock, there is someone else that cannot sleep because of this.

Anyway, heres an useful link LDD3 Chapter 5.

Upvotes: 2

Related Questions