Untitled
unknown
plain_text
13 days ago
794 B
5
Indexable
#include <stdio.h> #include <pthread.h> #include <stdbool.h> #define NUM_ITER 5 volatile bool flag[2] = {false, false}; volatile int turn; void *process(void *arg) { int i = *(int *)arg; int j = 1 - i; // El otro proceso for (int k = 0; k < NUM_ITER; k++) { flag[i] = true; turn = j; while (flag[j] && turn == j); // Espera activa // Sección crítica printf("Proceso %d en la sección crítica\n", i); // Sale de la sección crítica flag[i] = false; } return NULL; } int main() { pthread_t t1, t2; int p0 = 0, p1 = 1; pthread_create(&t1, NULL, process, &p0); pthread_create(&t2, NULL, process, &p1); pthread_join(t1, NULL); pthread_join(t2, NULL); return 0; }
Editor is loading...
Leave a Comment