This repository has been archived on 2026-05-11. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
prog-104-p-06-2030/RecapThoseAnimals/Fundamentals/process/fork.c
T
2026-05-07 00:38:33 +02:00

53 lines
1.1 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "fork.h"
int is_prime(size_t n)
{
int status;
pid_t pid = fork();
if(pid < 0) return -1;
//Child
if(pid == 0){
for(size_t i = 2; i < n; i ++){
if(n % i == 0 && i != n){
printf("Child speaking: my number %zu is not a prime number\n", n);
printf("We both say hi!\n");
exit(0);
}
}
printf("Child speaking: my number %zu is a prime number\n", n);
printf("We both say hi!\n");
exit(1);
}
//daddy milk
else{
if(waitpid(pid, &status, 0) == -1) return -1;
if(WIFEXITED(status)){
if(WEXITSTATUS(status) == 1) printf("Parent speaking: stop wasting my time of course %zu is a prime number\n", n);
else printf("Parent speaking: stop wasting my time of course %zu is not a prime number\n", n);
}
printf("We both say hi!\n");
}
return WEXITSTATUS(status);
}
/*
int salute(int num_guests, char *guests[])
{
return 0;
}*/