ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
I am in the midst of an interesting discussion...
how while( 1 ) locks the CPU,
i am of the opinion that since it is executing in too tight a loop to allow cpu to send an interrupt to remove the process from run queue....
my friend does not seem totally convinced....please see if anyone has some explanation or pointers in this matter...
you wont necessarily lock the CPU, but you can lock up your program if that isnt running as a seperate thread. The CPU itself wont spend all of its time in 1 program no matter what that one program is doing.
yeah that was actually my understanding also...but i was having some doubts...i was trying to corelate this with the spin locks....
which also execute a very tight loop (atomically (and hence, similar to while(1)));
ill be talking about kernel threads when i say threads in this post
the spin locks are a synchronization method in kernel..
mainly used in SMP (multi processing environments)...
these locks are test and set locks in which if one thread, 'thread1' has the lock, and some other thread wants to acquire the lock, the second thread keeps testing the lock by 'spinnin' around it, that is, testing whether the lock has been freed by the by in a very tight loop, this is all atomically done, and this code has the potential to lock the CPU, if executed in a UP (uniprocessor ) environment,
something like this code :
spinlock
{
while(1)
{
if (lock not set)
spin_lock();
}
}
heres how the CPU might get locked in the UP enviroment, when using spin locks,
suppose thread one has the lock and is about to execute its critical section, at that very moment , a thread with higher priority tries to obtain the lock and starts 'spinning', now since this is the higher priority thread, the first thread never gets a chance to execute the critical section, and thus, cant free the lock, thus, causin the cpu to lock, as the higher priority thread always keeps spinning around that lock.
this is where i saw a similarity between while 1 and the spin locks...
btw, i think certain workarounds exist to make spin locks work on UP systems, like specifying the spin time, ifa thread does not get a lock in certain amount of time, it is removed from the run queue,
Erm, was your original post about kernel programming too? If so you should really say so - spinlocks are not used in user mode programming except implicitly via other locking primitives....
umm, i am not talking about user land or kernel land, i am just trying to draw a parallel between a spin lock and a while (1)(not fully but to a certain extent.. )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.