Skip to content

Instantly share code, notes, and snippets.

@csfelipe
Created February 21, 2021 22:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save csfelipe/814f21fbe6dbb86dc1a20b6630a15f3e to your computer and use it in GitHub Desktop.
Save csfelipe/814f21fbe6dbb86dc1a20b6630a15f3e to your computer and use it in GitHub Desktop.
This is the C program without mutex displayed in the lecture "6.2.4. Example of race condition" in the UC3M course "Cybersecurity: A hands-on approach"
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
void *add_function();
int addMul = 0;
main() {
pthread_t thread;
int ret[70];
int i=0;
for (i=0; i<70; i++) {
ret[i] = pthread_create(&thread, NULL, add_function, NULL);
}
pthread_exit(NULL);
printf("RESULT %d\n", addMul);
exit(EXIT_SUCCESS);
}
void *add_function() {
int i = 0;
for (i=0;i<10;i++) {
int b = 0;
while (b<400) {
addMul++;
b++;
}
printf("Result of adding i=0 | %d\n", addMul);
pthread_exit(NULL);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment