os-70-350

os-70-350 Commit Details


Date:2013-12-21 22:06:44 (11 years 8 months ago)
Author:Natalie Adams
Branch:master
Commit:232afae0a7a41a77810024a97c137a22e182c0d7
Parents: d49bb8012a13ebca5f3b5e837b912e850f1bbba5
Message:adding raceconditions and petersons examples

Changes:

File differences

petersons-windows/build.bat
1
../vcbuild.bat main
petersons-windows/main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#define BUFFER_SIZE 10
#if !defined(true) && !defined(false)
#define true 1
#define false 0
#endif
volatile int counter = -1;
volatile int buffer[BUFFER_SIZE];
volatile int turn;
volatile int flag[2];
DWORD WINAPI produce(void* data) {
srand (time(NULL));
while (true)
{
flag[0] = true;
turn = 1;
//while (counter == BUFFER_SIZE - 1); // make sure the buffer doesn't overflow
while (flag[1] && turn == 1);
if (counter == BUFFER_SIZE - 1)
{
continue;
flag[0] = false;
}
buffer[counter + 1] = rand() % 20 + 1; // yes I know rand is not thread safe - I just need a way to demonstrate a producing/consuming problem - using the same number doesn't help.
printf("PRODUCER value = %i counter = %i\n", buffer[counter + 1], counter + 1);
counter++;
flag[0] = false;
}
}
DWORD WINAPI consume(void* data) {
while(true)
{
flag[1] = true;
turn = 0;
//while (counter == -1); // make sure there is content to consume
while (flag[0] && turn == 0);
if (counter == -1) // producer hasn't produced
{
flag[1] = false;
continue;
}
if (buffer[counter] == 0)
{
printf("%i\n", buffer[counter]);
printf("Attempting to consume an element that was already consumed counter = %i\n", counter);
exit(1);
}
printf("CONSUMER value = %i counter = %i\n", buffer[counter], counter);
counter--;
buffer[counter] = 0;
flag[1] = false;
}
}
int main() {
int i;
HANDLE thread1, thread2;
for(i = 0; i < BUFFER_SIZE; i++)
buffer[i] = -1;
flag[0] = true;
flag[1] = true;
thread1 = CreateThread(NULL, 0, produce, NULL, 0, NULL);
thread2 = CreateThread(NULL, 0, consume, NULL, 0, NULL);
WaitForSingleObject(thread1, 50);
WaitForSingleObject(thread2, 50);
}
racecondition-linux/build.sh
1
2
3
4
#!/bin/bash
gcc -o main main.c
./main
racecondition-linux/main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define BUFFER_SIZE 10
volatile int counter = -1;
volatile int buffer[BUFFER_SIZE];
void * produce(void* data) {
srand (time(NULL));
while (1)
{
while (counter == BUFFER_SIZE - 1);
buffer[counter + 1] = rand() % 20 + 1; // yes I know rand is not thread safe - I just need a way to demonstrate a producing/consuming problem - using the same number doesn't help.
printf("PRODUCER value = %i counter = %i\n", buffer[counter + 1], counter + 1);
counter++;
}
}
void * consume(void* data) {
while(1)
{
while (counter == -1);
if (buffer[counter] == 0)
{
printf("%i\n", buffer[counter]);
printf("Attempting to consume an element that was already consumed counter = %i\n", counter);
exit(1);
}
printf("CONSUMER value = %i counter = %i\n", buffer[counter], counter);
buffer[counter] = 0;
counter--;
}
}
int main() {
int i;
pthread_t thread1, thread2;
int iret1, iret2;
for(i = 0; i < BUFFER_SIZE; i++)
buffer[i] = -1;
iret1 = pthread_create( &thread1, NULL, produce, NULL);
iret2 = pthread_create( &thread2, NULL, consume, NULL);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
return 0;
}
racecondition-windows/build.bat
1
../vcbuild.bat main
racecondition-windows/main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#define BUFFER_SIZE 10
volatile int counter = -1;
volatile int buffer[BUFFER_SIZE];
DWORD WINAPI produce(void* data) {
srand (time(NULL));
while (1)
{
while (counter == BUFFER_SIZE - 1);
buffer[counter + 1] = rand() % 20 + 1; // yes I know rand is not thread safe - I just need a way to demonstrate a producing/consuming problem - using the same number doesn't help.
printf("PRODUCER value = %i counter = %i\n", buffer[counter + 1], counter + 1);
counter++;
}
}
DWORD WINAPI consume(void* data) {
while(1)
{
while (counter == -1);
if (buffer[counter] == 0)
{
printf("%i\n", buffer[counter]);
printf("Attempting to consume an element that was already consumed counter = %i\n", counter);
exit(1);
}
printf("CONSUMER value = %i counter = %i\n", buffer[counter], counter);
buffer[counter] = 0;
counter--;
}
}
int main() {
int i;
HANDLE thread1, thread2;
for(i = 0; i < BUFFER_SIZE; i++)
buffer[i] = -1;
thread1 = CreateThread(NULL, 0, produce, NULL, 0, NULL);
thread2 = CreateThread(NULL, 0, consume, NULL, 0, NULL);
WaitForSingleObject(thread1, INFINITE);
WaitForSingleObject(thread2, INFINITE);
}

Archive Download the corresponding diff file

Branches

Number of commits:
Page rendered in 0.52713s using 14 queries.