What kind of loop is for (;;)?

Found in torvalds/linux-2.6.git -> kernel/mutex.c line 171

I have tried to find it on Google and such to no avail.

What does for (;;) instruct?

Answers


The for(;;) is an infinite loop condition, similar to while(1) as most have already mentioned. You would more often see this, in kernel mutex codes, or mutex eg problem such as dining philosophers. Until the mutex variable is set to a particular value, such that a second process gets access to the resource, the second process keeps on looping, also known as busy wait. Access to a resource can be disk access, for which 2 process are competing to gain access using a mutex such that at a time only one process has the access to the resource.


It literally means "do nothing, until nothing happens and at each step, do nothing to prepare for the next". Basically, it's an infinite loop that you'll have to break somehow from within using a break, return or goto statement.


It is an infinite loop which has no initial condition, no increment condition and no end condition. So it will iterate forever equivalent to while(1).


It loops forever (until the code inside the loop calls break or return, of course. while(1) is equivalent, I personally find it more logical to use that.


It's equivalent to while( true )

Edit: Since there's been some debate sparked by my answer (good debate, mind you) it should be clarified that this is not entirely accurate for C programs not written to C99 and beyond wherein stdbool.h has set the value of true = 1.


it is an infinite for loop.


It is same as writing infinite loop using " for " statement but u have to use break or some other statement that can get out of this loop.


I means:

#define EVER ;;

for(EVER)
{
     // do something
}

Warning: Using this in your code is highly discouraged.


It is functionally equivilent to while(true) { }.

The reason why the for(;;) syntax is sometimes preferred comes from an older age where for(;;) actually compiled to a slightly faster machine code than while(TRUE) {}. This is because for(;;) { foo(); } will translate in the first pass of the compiler to:

lbl_while_condition:
   mov $t1, 1
   cmp $t1, 0
   jnz _exit_while
lbl_block:
   call _foo
   jmp lbl_while_condition

whereas the for(;;) would compile in the first pass to:

lbl_for_init:
   ; do nothing
lbl_for_condition:
   ; always
lbl_for_block:
   call foo;
lbl_for_iterate:
   ; no iterate
   jmp lbl_for_condition

i.e.

 lbl_for_ever:
    call foo
    jmp lbl_for_ever

Hence saving 3 instructions on every pass of the loop.

In practice however, both statements have long since been not only functionally equivalent, but also actually equivalent, since optimisations in the compiler for all builds other than debug builds will ensure that the mov, cmp and jnz are optimised away in the while(1) case, resulting in optimal code for both for(;;) and while(1).


for(;;)

is an infinite loop just like while(1). Here no condition is given that will terminate the loop. If you are not breaking it using break statement this loop will never come to an end.


It's an infinite loop that you'll have to break somehow from within using a break, return or goto statement. or either some interrupt happens otherwise this loop will run infinitely and executes ;(null statement) every time


That was obviously an infinite loop condition.


Need Your Help

How to get IP address of running docker container

docker containers docker-for-mac

I am using Docker for Mac. I am running a nodejs based microservice in a Docker container. I want to test node microservice through the browser. How to get IP address of running docker container?

Changing color of Twitter bootstrap Nav-Pills

twitter-bootstrap nav nav-pills

I'm trying to change the active color (after its clicked it remains twitter's light-blue color) for each tab: