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