added OS theory code
This commit is contained in:
parent
05121a72a9
commit
2313ae0e01
6 changed files with 96 additions and 0 deletions
21
OS/C/theory/sync/withoutsemaphores.c
Normal file
21
OS/C/theory/sync/withoutsemaphores.c
Normal file
|
@ -0,0 +1,21 @@
|
|||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
|
||||
int counter = 0;
|
||||
|
||||
void* increment(void* arg) {
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
counter++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main() {
|
||||
pthread_t t1, t2;
|
||||
pthread_create(&t1, NULL, increment, NULL);
|
||||
pthread_create(&t2, NULL, increment, NULL);
|
||||
pthread_join(t1, NULL);
|
||||
pthread_join(t2, NULL);
|
||||
printf("Final Counter Value (without semaphores): %d\n", counter);
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue