os-70-350

os-70-350 Commit Details


Date:2013-12-23 22:23:46 (11 years 8 months ago)
Author:Natalie Adams
Branch:master
Commit:688d807a1eaf0bb5892c35920c626d3ae8e11954
Parents: a103db320c888e50b2783116209ea4bfcf0c4ea0
Message:adding second race condition for both windows and linux

Changes:

File differences

petersons-linux/build2.sh
1
2
3
4
#!/bin/bash
gcc -o main2 -pthread main2.c
./main2
petersons-linux/main2.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
// This example based off of samples by Jerry Breecher - http://web.cs.wpi.edu/~jb/CS502/Class_Example_Code.html
#include <stdio.h>
#define LOOP 999999
#if !defined(true) && !defined(false)
#define true 1
#define false 0
#endif
volatile long gvar = 0;
volatile int turn;
volatile int flag[2];
void * produce(void* data) {
int i = 0;
flag[0] = true;
turn = 1;
while (flag[1] && turn == 1);
for (i = 0; i < LOOP; i++)
gvar++;
flag[0] = false;
}
void * consume(void* data) {
int i = 0;
flag[1] = true;
turn = 0;
while (flag[0] && turn == 0);
for (i = 0; i < LOOP; i++)
gvar--;
flag[1] = false;
}
int main() {
int i;
pthread_t thread1, thread2;
int iret1, iret2;
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);
printf("gvar = %i\n", gvar);
}
petersons-windows/build2.bat
1
../vcbuild.bat main2
petersons-windows/main2.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
// This example based off of samples by Jerry Breecher - http://web.cs.wpi.edu/~jb/CS502/Class_Example_Code.html
#include <stdio.h>
#include <windows.h>
#define LOOP 999999
#if !defined(true) && !defined(false)
#define true 1
#define false 0
#endif
volatile long gvar = 0;
volatile int turn;
volatile int flag[2];
DWORD WINAPI produce(void* data) {
int i = 0;
flag[0] = true;
turn = 1;
while (flag[1] && turn == 1);
for (i = 0; i < LOOP; i++)
gvar++;
flag[0] = false;
}
DWORD WINAPI consume(void* data) {
int i = 0;
flag[1] = true;
turn = 0;
while (flag[0] && turn == 0);
for (i = 0; i < LOOP; i++)
gvar--;
flag[1] = false;
}
int main() {
int i;
HANDLE thread1, thread2;
flag[0] = true;
flag[1] = true;
thread1 = CreateThread(NULL, 0, produce, NULL, 0, NULL);
thread2 = CreateThread(NULL, 0, consume, NULL, 0, NULL);
WaitForSingleObject(thread1, INFINITE );
WaitForSingleObject(thread2, INFINITE );
printf("gvar = %i\n", gvar);
}
racecondition-linux/build2.sh
1
2
3
4
#!/bin/bash
gcc -o main -pthread main.c
./main
racecondition-linux/main2.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
#include <stdio.h>
#define LOOP 20000
volatile int counter = -1;
volatile long gvar = 0;
DWORD WINAPI produce(void* data) {
int i = 0;
for (i = 0; i < LOOP; i++)
gvar++;
}
DWORD WINAPI consume(void* data) {
int i = 0;
for (i = 0; i < LOOP; i++)
gvar--;
}
int main() {
int i;
pthread_t thread1, thread2;
int iret1, iret2;
iret1 = pthread_create( &thread1, NULL, produce, NULL);
iret2 = pthread_create( &thread2, NULL, consume, NULL);
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);
printf("gvar = %i\n", gvar);
}
racecondition-windows/build2.bat
1
../vcbuild.bat main2
racecondition-windows/main2.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
// This example based off of samples by Jerry Breecher - http://web.cs.wpi.edu/~jb/CS502/Class_Example_Code.html
#include <stdio.h>
#include <windows.h>
#define LOOP 20000
volatile int counter = -1;
volatile long gvar = 0;
DWORD WINAPI produce(void* data) {
int i = 0;
for (i = 0; i < LOOP; i++)
gvar++;
}
DWORD WINAPI consume(void* data) {
int i = 0;
for (i = 0; i < LOOP; i++)
gvar--;
}
int main() {
int i;
HANDLE thread1, thread2;
thread1 = CreateThread(NULL, 0, produce, NULL, 0, NULL);
thread2 = CreateThread(NULL, 0, consume, NULL, 0, NULL);
WaitForSingleObject(thread1, INFINITE);
WaitForSingleObject(thread2, INFINITE);
printf("gvar = %i\n", gvar);
}

Archive Download the corresponding diff file

Branches

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