manual: link lock-example1.c
This commit is contained in:
parent
2e9b1812de
commit
37c2a68017
|
@ -209,40 +209,48 @@ obtained with the option `--property`.
|
||||||
|
|
||||||
### Unbounded Loops
|
### Unbounded Loops
|
||||||
|
|
||||||
CBMC can also be used for programs with unbounded loops. In this case,
|
CBMC can also be used for programs with unbounded loops. In this case, CBMC
|
||||||
CBMC is used for bug hunting only; CBMC does not attempt to find all
|
is used for bug hunting only; CBMC does not attempt to find all bugs. The
|
||||||
bugs. The following program (lock-example.c) is an example of a program
|
following program
|
||||||
with a user-specified property:
|
([lock-example.c](https://raw.githubusercontent.com/diffblue/cbmc/develop/doc/cprover-manual/lock-example.c))
|
||||||
|
is an example of a program with a user-specified property:
|
||||||
|
|
||||||
```C
|
```C
|
||||||
_Bool nondet_bool();
|
_Bool nondet_bool();
|
||||||
_Bool LOCK = 0;
|
_Bool LOCK = 0;
|
||||||
|
|
||||||
_Bool lock() {
|
_Bool lock()
|
||||||
if(nondet_bool()) {
|
{
|
||||||
|
if(nondet_bool())
|
||||||
|
{
|
||||||
assert(!LOCK);
|
assert(!LOCK);
|
||||||
LOCK=1;
|
LOCK = 1;
|
||||||
return 1; }
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void unlock() {
|
void unlock()
|
||||||
|
{
|
||||||
assert(LOCK);
|
assert(LOCK);
|
||||||
LOCK=0;
|
LOCK = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main()
|
||||||
|
{
|
||||||
unsigned got_lock = 0;
|
unsigned got_lock = 0;
|
||||||
int times;
|
int times;
|
||||||
|
|
||||||
while(times > 0) {
|
while(times > 0)
|
||||||
if(lock()) {
|
{
|
||||||
|
if(lock())
|
||||||
|
{
|
||||||
got_lock++;
|
got_lock++;
|
||||||
/* critical section */
|
/* critical section */
|
||||||
}
|
}
|
||||||
|
|
||||||
if(got_lock!=0)
|
if(got_lock != 0)
|
||||||
unlock();
|
unlock();
|
||||||
|
|
||||||
got_lock--;
|
got_lock--;
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
_Bool nondet_bool();
|
||||||
|
_Bool LOCK = 0;
|
||||||
|
|
||||||
|
_Bool lock()
|
||||||
|
{
|
||||||
|
if(nondet_bool())
|
||||||
|
{
|
||||||
|
assert(!LOCK);
|
||||||
|
LOCK = 1;
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void unlock()
|
||||||
|
{
|
||||||
|
assert(LOCK);
|
||||||
|
LOCK = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
unsigned got_lock = 0;
|
||||||
|
int times;
|
||||||
|
|
||||||
|
while(times > 0)
|
||||||
|
{
|
||||||
|
if(lock())
|
||||||
|
{
|
||||||
|
got_lock++;
|
||||||
|
/* critical section */
|
||||||
|
}
|
||||||
|
|
||||||
|
if(got_lock != 0)
|
||||||
|
unlock();
|
||||||
|
|
||||||
|
got_lock--;
|
||||||
|
times--;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue