extended code to advanced input functionality
This commit is contained in:
parent
58cf91a06d
commit
0fc6c74996
@ -1,23 +1,28 @@
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <sys/semaphore.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
sem_t my_semaphore;
|
||||
atomic_int semaphore_value = 5;
|
||||
sem_t *my_semaphore;
|
||||
|
||||
int main(){
|
||||
sem_init(&my_semaphore, 0, 5);
|
||||
int sem_val;
|
||||
sem_getvalue(&my_semaphore, &sem_val);
|
||||
my_semaphore = sem_open("/my_sem", O_CREAT, 0644, atomic_load(&semaphore_value));
|
||||
int sem_val = atomic_load(&semaphore_value);
|
||||
printf("Initial semaphore value: %d\n", sem_val);
|
||||
|
||||
sem_post(my_semaphore);
|
||||
atomic_fetch_add(&semaphore_value, 1);
|
||||
sem_post(my_semaphore);
|
||||
atomic_fetch_add(&semaphore_value, 1);
|
||||
sem_post(my_semaphore);
|
||||
atomic_fetch_add(&semaphore_value, 1);
|
||||
|
||||
sem_val = atomic_load(&semaphore_value);
|
||||
printf("Semaphore value after extra posts: %d\n", sem_val);
|
||||
|
||||
sem_post(&my_semaphore);
|
||||
sem_post(&my_semaphore);
|
||||
sem_post(&my_semaphore);
|
||||
|
||||
sem_getvalue(&my_semaphore, &sem_val);
|
||||
printf("Semaphore value after extra posts: %d\n", sem_val);
|
||||
|
||||
sem_destroy(&my_semaphore);
|
||||
sem_close(my_semaphore);
|
||||
sem_unlink("/my_sem");
|
||||
return 0;
|
||||
}
|
||||
|
BIN
OS/C/theory/sync/semexpos
Executable file
BIN
OS/C/theory/sync/semexpos
Executable file
Binary file not shown.
@ -4,8 +4,8 @@
|
||||
int counter = 0;
|
||||
|
||||
void* increment(void* arg) {
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
counter++;
|
||||
for (int i = 0; i < 1000000; i++) {
|
||||
counter++; // No synchronization, will lead to race condition
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
@ -1,32 +1,33 @@
|
||||
#include <stdatomic.h>
|
||||
#include <stdio.h>
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <sys/semaphore.h>
|
||||
#include <dispatch/dispatch.h>
|
||||
|
||||
int counter = 0; // shared resource
|
||||
sem_t semaphore; // semaphore declaration
|
||||
atomic_int counter = 0; // shared resource
|
||||
dispatch_semaphore_t semaphore; // semaphore declaration
|
||||
|
||||
void* increment(void* arg) {
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
sem_wait(&semaphore); // lock
|
||||
counter++; // critical section
|
||||
sem_post(&semaphore); // unlock
|
||||
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER); // lock
|
||||
atomic_fetch_add(&counter, 1); // critical section
|
||||
dispatch_semaphore_signal(semaphore); // unlock
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main() {
|
||||
pthread_t t1, t2;
|
||||
sem_init(&semaphore, 0, 1); // initialize semaphore with value 1
|
||||
semaphore = dispatch_semaphore_create(1); // initialize semaphore with value 1
|
||||
|
||||
pthread_create(&t1, NULL, increment, NULL);
|
||||
pthread_create(&t2, NULL, increment, NULL);
|
||||
pthread_join(t1, NULL);
|
||||
pthread_join(t2, NULL);
|
||||
|
||||
sem_destroy(&semaphore);
|
||||
dispatch_release(semaphore);
|
||||
|
||||
printf("Final Counter Value (with semaphores): %d\n", counter);
|
||||
printf("Final Counter Value (with semaphores): %d\n", atomic_load(&counter));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -2,14 +2,15 @@
|
||||
#include <pthread.h>
|
||||
#include <semaphore.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
int counter = 0; // shared resource
|
||||
atomic_int counter = 0; // shared resource
|
||||
sem_t *semaphore; // semaphore declaration
|
||||
|
||||
void* increment(void* arg) {
|
||||
for (int i = 0; i < 100000; i++) {
|
||||
sem_wait(semaphore); // lock
|
||||
counter++; // critical section
|
||||
atomic_fetch_add(&counter, 1); // critical section
|
||||
sem_post(semaphore); // unlock
|
||||
}
|
||||
return NULL;
|
||||
@ -37,7 +38,7 @@ int main() {
|
||||
sem_close(semaphore);
|
||||
sem_unlink("/mysem");
|
||||
|
||||
printf("Final Counter Value (with semaphores): %d\n", counter);
|
||||
printf("Final Counter Value (with semaphores): %d\n", atomic_load(&counter));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user