59 lines
1.7 KiB
C
59 lines
1.7 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* 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);
|
|
}
|
|
|