Added process_basics.c exercice, renamed exercices

This commit is contained in:
Rui Ribeiro 2025-10-14 19:54:19 +01:00
parent 2128325237
commit be7c0d17bb
7 changed files with 58 additions and 0 deletions

58
rendu/process_basics.c Normal file
View File

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process_basics.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ruiferna <ruiferna@student.42porto.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/14 17:06:42 by ruiferna #+# #+# */
/* Updated: 2025/10/14 19:53:46 by ruiferna ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#define NUM_CHILDREN 3
int main(int argc, char *argv[]) {
pid_t pids[NUM_CHILDREN];
int i;
i = 0;
while (i < NUM_CHILDREN)
{
pids[i] = fork();
if (pids[i] == -1) {
printf("Error creating process\n");
return 1;
}
if (pids[i] == 0) {
// Child process
int child_id = i + 1;
int j;
j = 0;
while (j <= 5)
{
printf("Child %d (PID: %d): Message %d\n", child_id, getpid(), j);
usleep(100000);
j++;
}
exit(0);
}
i++;
}
// Parent process
i = 0;
while (i < NUM_CHILDREN)
{
waitpid(pids[i], NULL, 0);
i++;
}
printf("Parent: All 3 children have finished\n");
return (0);
}