1.23
2.0
main
test-branch
| Line | |
|---|
| 1 | #include <pthread.h>
|
|---|
| 2 | #include <stdio.h>
|
|---|
| 3 | #include <assert.h>
|
|---|
| 4 |
|
|---|
| 5 | #define SIZE (20)
|
|---|
| 6 | #define EMPTY (-1)
|
|---|
| 7 | #define FULL (-2)
|
|---|
| 8 | #define FALSE (0)
|
|---|
| 9 | #define TRUE (1)
|
|---|
| 10 |
|
|---|
| 11 | typedef struct {
|
|---|
| 12 | int element[SIZE];
|
|---|
| 13 | int head;
|
|---|
| 14 | int tail;
|
|---|
| 15 | int amount;
|
|---|
| 16 | } QType;
|
|---|
| 17 |
|
|---|
| 18 | pthread_mutex_t m;
|
|---|
| 19 | int __VERIFIER_nondet_int(void);
|
|---|
| 20 | int stored_elements[SIZE];
|
|---|
| 21 | _Bool enqueue_flag, dequeue_flag;
|
|---|
| 22 | QType queue;
|
|---|
| 23 |
|
|---|
| 24 | int init(QType *q)
|
|---|
| 25 | {
|
|---|
| 26 | q->head=0;
|
|---|
| 27 | q->tail=0;
|
|---|
| 28 | q->amount=0;
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | int empty(QType * q)
|
|---|
| 32 | {
|
|---|
| 33 | if (q->head == q->tail)
|
|---|
| 34 | {
|
|---|
| 35 | printf("queue is empty\n");
|
|---|
| 36 | return EMPTY;
|
|---|
| 37 | }
|
|---|
| 38 | else
|
|---|
| 39 | return 0;
|
|---|
| 40 | }
|
|---|
| 41 |
|
|---|
| 42 | int full(QType * q)
|
|---|
| 43 | {
|
|---|
| 44 | if (q->amount == SIZE)
|
|---|
| 45 | {
|
|---|
| 46 | printf("queue is full\n");
|
|---|
| 47 | return FULL;
|
|---|
| 48 | }
|
|---|
| 49 | else
|
|---|
| 50 | return 0;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | int enqueue(QType *q, int x)
|
|---|
| 54 | {
|
|---|
| 55 | q->element[q->tail] = x;
|
|---|
| 56 | q->amount++;
|
|---|
| 57 | if (q->tail == SIZE)
|
|---|
| 58 | {
|
|---|
| 59 | q->tail = 1;
|
|---|
| 60 | }
|
|---|
| 61 | else
|
|---|
| 62 | {
|
|---|
| 63 | q->tail++;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | return 0;
|
|---|
| 67 | }
|
|---|
| 68 |
|
|---|
| 69 | int dequeue(QType *q)
|
|---|
| 70 | {
|
|---|
| 71 | int x;
|
|---|
| 72 |
|
|---|
| 73 | x = q->element[q->head];
|
|---|
| 74 | q->amount--;
|
|---|
| 75 | if (q->head == SIZE)
|
|---|
| 76 | {
|
|---|
| 77 | q->head = 1;
|
|---|
| 78 | }
|
|---|
| 79 | else
|
|---|
| 80 | q->head++;
|
|---|
| 81 |
|
|---|
| 82 | return x;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | void *t1(void *arg)
|
|---|
| 86 | {
|
|---|
| 87 | int value, i;
|
|---|
| 88 |
|
|---|
| 89 | pthread_mutex_lock(&m);
|
|---|
| 90 | if (enqueue_flag)
|
|---|
| 91 | {
|
|---|
| 92 | for(i=0; i<SIZE; i++)
|
|---|
| 93 | {
|
|---|
| 94 | value = __VERIFIER_nondet_int();
|
|---|
| 95 | enqueue(&queue,value);
|
|---|
| 96 | stored_elements[i]=value;
|
|---|
| 97 | }
|
|---|
| 98 | enqueue_flag=FALSE;
|
|---|
| 99 | dequeue_flag=TRUE;
|
|---|
| 100 | }
|
|---|
| 101 | pthread_mutex_unlock(&m);
|
|---|
| 102 |
|
|---|
| 103 | return NULL;
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | void *t2(void *arg)
|
|---|
| 107 | {
|
|---|
| 108 | int i;
|
|---|
| 109 |
|
|---|
| 110 | pthread_mutex_lock(&m);
|
|---|
| 111 | if (dequeue_flag)
|
|---|
| 112 | {
|
|---|
| 113 | for(i=0; i<SIZE; i++)
|
|---|
| 114 | {
|
|---|
| 115 | if (empty(&queue)!=EMPTY)
|
|---|
| 116 | if (!(dequeue(&queue)==stored_elements[i])) {
|
|---|
| 117 | ERROR:
|
|---|
| 118 | goto ERROR;
|
|---|
| 119 | }
|
|---|
| 120 | }
|
|---|
| 121 | dequeue_flag=FALSE;
|
|---|
| 122 | enqueue_flag=TRUE;
|
|---|
| 123 | }
|
|---|
| 124 | pthread_mutex_unlock(&m);
|
|---|
| 125 |
|
|---|
| 126 | return NULL;
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | int main(void)
|
|---|
| 130 | {
|
|---|
| 131 | pthread_t id1, id2;
|
|---|
| 132 |
|
|---|
| 133 | enqueue_flag=TRUE;
|
|---|
| 134 | dequeue_flag=FALSE;
|
|---|
| 135 |
|
|---|
| 136 | init(&queue);
|
|---|
| 137 |
|
|---|
| 138 | if (!(empty(&queue)==EMPTY)) {
|
|---|
| 139 | ERROR:
|
|---|
| 140 | goto ERROR;
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | pthread_mutex_init(&m, 0);
|
|---|
| 144 |
|
|---|
| 145 | pthread_create(&id1, NULL, t1, &queue);
|
|---|
| 146 | pthread_create(&id2, NULL, t2, &queue);
|
|---|
| 147 |
|
|---|
| 148 | pthread_join(id1, NULL);
|
|---|
| 149 | pthread_join(id2, NULL);
|
|---|
| 150 |
|
|---|
| 151 | return 0;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.