forked from open-education-hub/operating-systems
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
compute/lab: Replace D code with C in
race-condition
For better understanding and reasons Signed-off-by: cristian-vijelie <[email protected]>
- Loading branch information
1 parent
245bbf0
commit d0ee552
Showing
14 changed files
with
247 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
content/chapters/compute/lab/support/race-condition/c/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
race_condition | ||
race_condition_mutex | ||
race_condition_atomic | ||
race_condition_atomic2 | ||
race_condition_tls |
4 changes: 2 additions & 2 deletions
4
content/chapters/compute/lab/support/race-condition/c/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
BINARY = race_condition_tls | ||
BINARIES = race_condition race_condition_tls race_condition_mutex race_condition_atomic race_condition_atomic2 | ||
LDLIBS = -lpthread | ||
include ../../../../../../common/makefile/single.mk | ||
include ../../../../../../common/makefile/multiple.mk |
50 changes: 50 additions & 0 deletions
50
content/chapters/compute/lab/support/race-condition/c/race_condition.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#include <stdio.h> | ||
#include <pthread.h> | ||
|
||
#include "utils/utils.h" | ||
|
||
#define NUM_ITER 10000000 | ||
|
||
static int val; | ||
|
||
void *increment_var(void *arg) | ||
{ | ||
(void)arg; | ||
|
||
for (size_t i = 0; i < NUM_ITER; i++) | ||
val++; | ||
|
||
return NULL; | ||
} | ||
|
||
void *decrement_var(void *arg) | ||
{ | ||
(void)arg; | ||
|
||
for (size_t i = 0; i < NUM_ITER; i++) | ||
val--; | ||
|
||
return NULL; | ||
} | ||
|
||
int main(void) | ||
{ | ||
int rc; | ||
pthread_t tids[2]; | ||
|
||
rc = pthread_create(tids, NULL, increment_var, NULL); | ||
DIE(rc < 0, "pthread_create"); | ||
rc = pthread_create(tids + 1, NULL, decrement_var, NULL); | ||
DIE(rc < 0, "pthread_create"); | ||
|
||
rc = pthread_join(tids[0], NULL); | ||
DIE(rc < 0, "pthread_join"); | ||
rc = pthread_join(tids[1], NULL); | ||
DIE(rc < 0, "pthread_join"); | ||
|
||
printf("var = %d\n", val); | ||
|
||
return 0; | ||
} |
52 changes: 52 additions & 0 deletions
52
content/chapters/compute/lab/support/race-condition/c/race_condition_atomic.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#include <stdio.h> | ||
#include <pthread.h> | ||
#include <stdatomic.h> | ||
|
||
#include "utils/utils.h" | ||
|
||
#define NUM_ITER 10000000 | ||
|
||
static int val; | ||
|
||
void *increment_var(void *arg) | ||
{ | ||
(void)arg; | ||
|
||
for (size_t i = 0; i < NUM_ITER; i++) | ||
atomic_fetch_add(&val, 1); | ||
|
||
return NULL; | ||
} | ||
|
||
void *decrement_var(void *arg) | ||
{ | ||
(void)arg; | ||
|
||
for (size_t i = 0; i < NUM_ITER; i++) | ||
// TODO: Use `atomic_fetch_sub` to implement `var -= 1` atomically. | ||
; | ||
|
||
return NULL; | ||
} | ||
|
||
int main(void) | ||
{ | ||
int rc; | ||
pthread_t tids[2]; | ||
|
||
rc = pthread_create(tids, NULL, increment_var, NULL); | ||
DIE(rc < 0, "pthread_create"); | ||
rc = pthread_create(tids + 1, NULL, decrement_var, NULL); | ||
DIE(rc < 0, "pthread_create"); | ||
|
||
rc = pthread_join(tids[0], NULL); | ||
DIE(rc < 0, "pthread_join"); | ||
rc = pthread_join(tids[1], NULL); | ||
DIE(rc < 0, "pthread_join"); | ||
|
||
printf("var = %d\n", val); | ||
|
||
return 0; | ||
} |
51 changes: 51 additions & 0 deletions
51
content/chapters/compute/lab/support/race-condition/c/race_condition_atomic2.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#include <stdio.h> | ||
#include <pthread.h> | ||
#include <stdatomic.h> | ||
|
||
#include "utils/utils.h" | ||
|
||
#define NUM_ITER 10000000 | ||
|
||
static atomic_int val; | ||
|
||
void *increment_var(void *arg) | ||
{ | ||
(void)arg; | ||
|
||
for (size_t i = 0; i < NUM_ITER; i++) | ||
val++; | ||
|
||
return NULL; | ||
} | ||
|
||
void *decrement_var(void *arg) | ||
{ | ||
(void)arg; | ||
|
||
for (size_t i = 0; i < NUM_ITER; i++) | ||
val--; | ||
|
||
return NULL; | ||
} | ||
|
||
int main(void) | ||
{ | ||
int rc; | ||
pthread_t tids[2]; | ||
|
||
rc = pthread_create(tids, NULL, increment_var, NULL); | ||
DIE(rc < 0, "pthread_create"); | ||
rc = pthread_create(tids + 1, NULL, decrement_var, NULL); | ||
DIE(rc < 0, "pthread_create"); | ||
|
||
rc = pthread_join(tids[0], NULL); | ||
DIE(rc < 0, "pthread_join"); | ||
rc = pthread_join(tids[1], NULL); | ||
DIE(rc < 0, "pthread_join"); | ||
|
||
printf("var = %d\n", val); | ||
|
||
return 0; | ||
} |
62 changes: 62 additions & 0 deletions
62
content/chapters/compute/lab/support/race-condition/c/race_condition_mutex.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// SPDX-License-Identifier: BSD-3-Clause | ||
|
||
#include <stdio.h> | ||
#include <pthread.h> | ||
|
||
#include "utils/utils.h" | ||
|
||
#define NUM_ITER 10000000 | ||
|
||
pthread_mutex_t mutex; | ||
|
||
static int val; | ||
|
||
void *increment_var(void *arg) | ||
{ | ||
(void)arg; | ||
|
||
// TODO: wrap the whole `for` statement in the critical section and measure | ||
// the running times. | ||
for (size_t i = 0; i < NUM_ITER; i++) { | ||
pthread_mutex_lock(&mutex); | ||
val++; | ||
pthread_mutex_unlock(&mutex); | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
void *decrement_var(void *arg) | ||
{ | ||
(void)arg; | ||
|
||
// TODO: wrap the whole `for` statement in the critical section and measure | ||
// the running times. | ||
for (size_t i = 0; i < NUM_ITER; i++) { | ||
pthread_mutex_lock(&mutex); | ||
val--; | ||
pthread_mutex_unlock(&mutex); | ||
} | ||
|
||
return NULL; | ||
} | ||
|
||
int main(void) | ||
{ | ||
int rc; | ||
pthread_t tids[2]; | ||
|
||
rc = pthread_create(tids, NULL, increment_var, NULL); | ||
DIE(rc < 0, "pthread_create"); | ||
rc = pthread_create(tids + 1, NULL, decrement_var, NULL); | ||
DIE(rc < 0, "pthread_create"); | ||
|
||
rc = pthread_join(tids[0], NULL); | ||
DIE(rc < 0, "pthread_join"); | ||
rc = pthread_join(tids[1], NULL); | ||
DIE(rc < 0, "pthread_join"); | ||
|
||
printf("var = %d\n", val); | ||
|
||
return 0; | ||
} |
2 changes: 1 addition & 1 deletion
2
content/chapters/compute/lab/support/race-condition/c/race_condition_tls.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 0 additions & 3 deletions
3
content/chapters/compute/lab/support/race-condition/d/.gitignore
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.