philosophers_prep/rendu/deadlock_demo.c

129 lines
4.1 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* deadlock_demo.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ruiferna <ruiferna@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/08 18:39:44 by ruiferna #+# #+# */
/* Updated: 2025/10/08 19:26:47 by ruiferna ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/time.h>
#include <unistd.h>
#include <stdlib.h>
typedef struct s_info
{
pthread_mutex_t mutex_a;
pthread_mutex_t mutex_b;
} t_info;
void *deadlock_1(void *arg)
{
t_info *shared;
shared = (t_info *) arg;
printf("Thread 1: Locking mutex_a first\n");
pthread_mutex_lock(&shared->mutex_a);
printf("Thread 1: Got mutex_a, sleeping before trying mutex_b\n");
usleep(100000);
printf("Thread 1: Now trying to lock mutex_b (potential deadlock)\n");
pthread_mutex_lock(&shared->mutex_b);
printf("Thread 1: Got both mutexes, releasing them\n");
pthread_mutex_unlock(&shared->mutex_a);
pthread_mutex_unlock(&shared->mutex_b);
return (NULL);
}
void *deadlock_2(void *arg)
{
t_info *shared;
shared = (t_info *) arg;
printf("Thread 2: Locking mutex_b first\n");
pthread_mutex_lock(&shared->mutex_b);
printf("Thread 2: Got mutex_b, sleeping before trying mutex_a\n");
usleep(100000);
printf("Thread 2: Now trying to lock mutex_a (potential deadlock)\n");
pthread_mutex_lock(&shared->mutex_a);
printf("Thread 2: Got both mutexes, releasing them\n");
pthread_mutex_unlock(&shared->mutex_b);
pthread_mutex_unlock(&shared->mutex_a);
return (NULL);
}
void *nolock_1(void *arg)
{
t_info *shared;
shared = (t_info *) arg;
printf("Thread 1: Attempting to lock mutex_a first\n");
pthread_mutex_lock(&shared->mutex_a);
printf("Thread 1: Locked mutex_a successfully\n");
usleep(100000);
printf("Thread 1: Attempting to lock mutex_b second\n");
pthread_mutex_lock(&shared->mutex_b);
printf("Thread 1: Locked mutex_b successfully - both mutexes acquired\n");
pthread_mutex_unlock(&shared->mutex_b);
printf("Thread 1: Released mutex_b\n");
pthread_mutex_unlock(&shared->mutex_a);
printf("Thread 1: Released mutex_a - task completed\n");
return (NULL);
}
void *nolock_2(void *arg)
{
t_info *shared;
shared = (t_info *) arg;
printf("Thread 2: Attempting to lock mutex_a first (same order as thread 1)\n");
pthread_mutex_lock(&shared->mutex_a);
printf("Thread 2: Locked mutex_a successfully\n");
usleep(100000);
printf("Thread 2: Attempting to lock mutex_b second\n");
pthread_mutex_lock(&shared->mutex_b);
printf("Thread 2: Locked mutex_b successfully - both mutexes acquired\n");
pthread_mutex_unlock(&shared->mutex_b);
printf("Thread 2: Released mutex_b\n");
pthread_mutex_unlock(&shared->mutex_a);
printf("Thread 2: Released mutex_a - task completed\n");
return (NULL);
}
int main(int ac, char **av)
{
pthread_t thread1;
pthread_t thread2;
t_info shared;
if (ac != 2)
return (0);
pthread_mutex_init(&shared.mutex_a, NULL);
pthread_mutex_init(&shared.mutex_b, NULL);
int mode = atoi(av[1]);
if (mode != 0 && mode != 1)
{
printf("Insert either a 0 (deadlock) or a 1 (no lock)!\n");
pthread_mutex_destroy(&shared.mutex_a);
pthread_mutex_destroy(&shared.mutex_b);
return (0);
}
switch (mode)
{
case 0:
pthread_create(&thread1, NULL, deadlock_1, &shared);
pthread_create(&thread2, NULL, deadlock_2, &shared);
break;
case 1:
pthread_create(&thread1, NULL, nolock_1, &shared);
pthread_create(&thread2, NULL, nolock_2, &shared);
break;
}
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
pthread_mutex_destroy(&shared.mutex_a);
pthread_mutex_destroy(&shared.mutex_b);
return (0);
}