53 lines
1.1 KiB
C
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;
|
|
}*/
|