Linux - KernelThis forum is for all discussion relating to the Linux kernel.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
see i know but had a little confusion.I was thinking of a scenario
i have a uniprocessor machine
now one of my kernel threads say A has aquired a spinlock on particular data structure say DS .There can be a situation that it gets scheduled and another thread say B gets scheduled which is operating on the same data structure say DS now since spinlocks are disabled thread B will start operating on DS which can lead to race conditions .Please correct me if I am wrong .I am very new to linux kernel .
The scenario you have mentioned can not occur with spinlocks.
If Process A holds a spinlock, it can not be taken out of the processor because kernel preemption gets disabled while holding a spinlock.
Even if did get preempted while holding the lock, and process B tries to acquire the lock it will fail as process A would be holding it and Process B would spin endlessly on the lock. Because CPU will be kept busy by the Process B and there is no way for process A to come back and release the lock, leading to a perfect situation of a deadlock.
the scenario of which I am talking about is on uniprocessor system where spinlocks are disabled so at first A will not be able to lock.
only kernel preemption gets disabled .As kernel preemtion is disabled so a process in user space cannot be scheduled but what if a kernel thread B gets scheduled .what you say.(A and B both are kthreads so will be in kernel context)
I am not really clear what exactly you are trying to say.
Quote:
spinlocks are disabled so at first A will not be able to lock
when spinlock is disabled, what are you trying to lock ?
If multiple threads have to access the same data you could protect it using semaphore or use atomic operations. Failing to do so is poor programming and there is nothing that kernel can do for that.
hi
what I am trying to say is that spinlocks are disabled on uniprocessor so if I want to use spinlocks in two of my kthreads I will not be ale to .
scenario- global is shared
kthread a //kernel thread
spinlock()//not actual syntax
global++;
spinunlock()//not actual syntax
kthread b //kernel thread
spinlock()//not actual syntax
global++;
spinunlock()//not actual syntax
providing it is uniprocessor now so i cannot use spinlock cause they are disabled .Correct me I am wrong so in such scenararios I am supposed to use other locking schemes.
Last edited by ashish anand; 08-05-2011 at 11:54 PM.
"Silly rabbit ... what's a spinlock for?" And, if you know that, "what does a spinlock have to guard against if there is only one CPU?"
2.Maybe the source of your confusion is that perhaps you're thinking "if spinlocks are disabled on a UP kernel" (and they are), "then I can't use them in my code." But you CAN!
3. If you have a critical section (for example, in a kernel driver), then use spinlocks.
If this code is executed on a UP, the code will effectively be a no-op.
The use of spin_lock_irqsave() will disable interrupts locally and provide the spinlock on SMP. This covers both interrupt and SMP concurrency issues. With a call to spin_unlock_irqrestore(), interrupts are restored to the state when the lock was acquired. With a UP kernel, the above code compiles to the same as:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.