1.23
2.0
main
test-branch
| Line | |
|---|
| 1 | #include <pthread.h>
|
|---|
| 2 | #include <stdio.h>
|
|---|
| 3 |
|
|---|
| 4 | #define TRUE (1)
|
|---|
| 5 | #define FALSE (0)
|
|---|
| 6 | #define SIZE (5)
|
|---|
| 7 | #define OVERFLOW (-1)
|
|---|
| 8 | #define UNDERFLOW (-2)
|
|---|
| 9 |
|
|---|
| 10 | unsigned int __VERIFIER_nondet_uint(void);
|
|---|
| 11 | static int top=0;
|
|---|
| 12 | static unsigned int arr[SIZE];
|
|---|
| 13 | pthread_mutex_t m;
|
|---|
| 14 | _Bool flag=FALSE;
|
|---|
| 15 |
|
|---|
| 16 | void error(void)
|
|---|
| 17 | {
|
|---|
| 18 | ERROR: ;
|
|---|
| 19 | goto ERROR;
|
|---|
| 20 | return;
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | void inc_top(void)
|
|---|
| 24 | {
|
|---|
| 25 | top++;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | void dec_top(void)
|
|---|
| 29 | {
|
|---|
| 30 | top--;
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 | int get_top(void)
|
|---|
| 34 | {
|
|---|
| 35 | return top;
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | int stack_empty(void)
|
|---|
| 39 | {
|
|---|
| 40 | (top==0) ? TRUE : FALSE;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | int push(unsigned int *stack, int x)
|
|---|
| 44 | {
|
|---|
| 45 | if (top==SIZE)
|
|---|
| 46 | {
|
|---|
| 47 | printf("stack overflow\n");
|
|---|
| 48 | return OVERFLOW;
|
|---|
| 49 | }
|
|---|
| 50 | else
|
|---|
| 51 | {
|
|---|
| 52 | stack[get_top()] = x;
|
|---|
| 53 | inc_top();
|
|---|
| 54 | }
|
|---|
| 55 | return 0;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | int pop(unsigned int *stack)
|
|---|
| 59 | {
|
|---|
| 60 | if (top==0)
|
|---|
| 61 | {
|
|---|
| 62 | printf("stack underflow\n");
|
|---|
| 63 | return UNDERFLOW;
|
|---|
| 64 | }
|
|---|
| 65 | else
|
|---|
| 66 | {
|
|---|
| 67 | dec_top();
|
|---|
| 68 | return stack[get_top()];
|
|---|
| 69 | }
|
|---|
| 70 | return 0;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | void *t1(void *arg)
|
|---|
| 74 | {
|
|---|
| 75 | int i;
|
|---|
| 76 | unsigned int tmp;
|
|---|
| 77 |
|
|---|
| 78 | for(i=0; i<SIZE; i++)
|
|---|
| 79 | {
|
|---|
| 80 | pthread_mutex_lock(&m);
|
|---|
| 81 | tmp = __VERIFIER_nondet_uint()%SIZE;
|
|---|
| 82 | if ((push(arr,tmp)==OVERFLOW))
|
|---|
| 83 | error();
|
|---|
| 84 | pthread_mutex_unlock(&m);
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | void *t2(void *arg)
|
|---|
| 89 | {
|
|---|
| 90 | int i;
|
|---|
| 91 |
|
|---|
| 92 | for(i=0; i<SIZE; i++)
|
|---|
| 93 | {
|
|---|
| 94 | pthread_mutex_lock(&m);
|
|---|
| 95 | if (top>0)
|
|---|
| 96 | {
|
|---|
| 97 | if ((pop(arr)==UNDERFLOW))
|
|---|
| 98 | error();
|
|---|
| 99 | }
|
|---|
| 100 | pthread_mutex_unlock(&m);
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 |
|
|---|
| 105 | int main(void)
|
|---|
| 106 | {
|
|---|
| 107 | pthread_t id1, id2;
|
|---|
| 108 |
|
|---|
| 109 | pthread_mutex_init(&m, 0);
|
|---|
| 110 |
|
|---|
| 111 | pthread_create(&id1, NULL, t1, NULL);
|
|---|
| 112 | pthread_create(&id2, NULL, t2, NULL);
|
|---|
| 113 |
|
|---|
| 114 | pthread_join(id1, NULL);
|
|---|
| 115 | pthread_join(id2, NULL);
|
|---|
| 116 |
|
|---|
| 117 | return 0;
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.