os-70-350

os-70-350 Commit Details


Date:2013-12-22 22:05:57 (11 years 8 months ago)
Author:Natalie Adams
Branch:master
Commit:a103db320c888e50b2783116209ea4bfcf0c4ea0
Parents: 41aa65c1141a8d514ebadfb9be61fbc34feeda55
Message:adding working version of peterson's algorithm updating build scripts for Linux examples

Changes:

File differences

petersons-linux/build.sh
1
2
3
4
#!/bin/bash
gcc -o main -pthread main.c
./main
petersons-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
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
80
81
82
#include <stdio.h>
#include <time.h>
#include <stdlib.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];
void * produce(void* data) {
srand (time(NULL));
while (true)
{
flag[0] = true;
turn = 1;
while (flag[1] && turn == 1);
if (counter == BUFFER_SIZE - 1) // make sure the buffer doesn't overflow
{
flag[0] = false;
sleep(5);
continue;
}
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;
}
}
void * consume(void* data) {
int i = 0;
while(true)
{
flag[1] = true;
turn = 0;
while (flag[0] && turn == 0);
if (counter == -1) // producer hasn't produced - make sure there is content to consume
{
flag[1] = false;
continue;
}
if (buffer[counter] == 0)
{
for(i = 0; i < BUFFER_SIZE; i++)
printf("%i\n", buffer[i]);
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--;
flag[1] = false;
sleep(1);
}
}
int main() {
int i;
pthread_t thread1, thread2;
int iret1, iret2;
for(i = 0; i < BUFFER_SIZE; i++)
buffer[i] = -1;
flag[0] = true;
flag[1] = true;
iret1 = pthread_create( &thread1, NULL, produce, NULL);
iret2 = pthread_create( &thread2, NULL, consume, NULL);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
}
petersons-windows/main.c
2525
2626
2727
28
2928
29
30
3031
3132
3233
......
3940
4041
4142
43
4244
4345
4446
......
5153
5254
5355
54
56
57
5558
5659
5760
5861
59
6062
63
6164
65
6266
6367
6468
......
7276
7377
7478
75
76
79
80
7781
while (flag[1] && turn == 1);
if (counter == BUFFER_SIZE - 1) // make sure the buffer doesn't overflow
{
continue;
flag[0] = false;
Sleep(500); // Let the producer catch up
continue;
}
}
DWORD WINAPI consume(void* data) {
int i = 0;
while(true)
{
flag[1] = true;
}
if (buffer[counter] == 0)
{
printf("%i\n", buffer[counter]);
for(i = 0; i < BUFFER_SIZE; i++)
printf("%i\n", buffer[i]);
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;
counter--;
flag[1] = false;
Sleep(50);
}
}
thread1 = CreateThread(NULL, 0, produce, NULL, 0, NULL);
thread2 = CreateThread(NULL, 0, consume, NULL, 0, NULL);
WaitForSingleObject(thread1, 50);
WaitForSingleObject(thread2, 50);
WaitForSingleObject(thread1, INFINITE );
WaitForSingleObject(thread2, INFINITE );
}
racecondition-linux/build.sh
11
22
3
3
44
#!/bin/bash
gcc -o main main.c
gcc -o main -pthread main.c
./main
racecondition-windows/main.c
2121
2222
2323
24
2425
2526
2627
2728
2829
29
30
31
3032
3133
3234
}
DWORD WINAPI consume(void* data) {
int i = 0;
while(1)
{
while (counter == -1);
if (buffer[counter] == 0)
{
printf("%i\n", buffer[counter]);
for(i = 0; i < BUFFER_SIZE; i++)
printf("%i\n", buffer[i]);
printf("Attempting to consume an element that was already consumed counter = %i\n", counter);
exit(1);
}
thread-linux/build.sh
11
22
3
3
44
#!/bin/bash
gcc -o main main.c
gcc -o main -pthread main.c
./main
vcbuild.bat
1313
1414
1515
16
16
1717
goto compile
)
:compile
cl.exe %1.c
cl.exe /D "WIN32" /D "_WINDOWS" %1.c kernel32.lib user32.lib
del %1.obj

Archive Download the corresponding diff file

Branches

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