#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <stdbool.h>
bool is_prime( int num) {
if ( num <= 1 ) return false ;
for ( int i = 2 ; i <= num / 2 ; i++ ) {
if ( num % i == 0 ) return false ;
}
return true ;
}
int main( ) {
pid_t pid1, pid2, parent_pid;
parent_pid = getpid( ) ;
pid1 = fork( ) ;
if ( pid1 == 0 ) {
// Child Process 1
int child1_pid = getpid( ) ;
if ( is_prime( child1_pid) ) {
printf ( "Child 1 (PID %d): My PID is a prime number.\n " , child1_pid
) ; } else {
printf ( "Child 1 (PID %d): My PID is not a prime number.\n " , child1_pid
) ; }
} else {
pid2 = fork( ) ;
if ( pid2 == 0 ) {
// Child Process 2
if ( is_prime( parent_pid) ) {
printf ( "Child 2 (PID %d): My parent's PID (%d) is a prime number.\n " , getpid
( ) , parent_pid
) ; } else {
printf ( "Child 2 (PID %d): My parent's PID (%d) is not a prime number.\n " , getpid
( ) , parent_pid
) ; }
} else {
// Parent Process
wait( NULL) ;
wait( NULL) ;
printf ( "Parent (PID %d): Missions accomplished...\n " , parent_pid
) ; }
}
return 0 ;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDx1bmlzdGQuaD4KI2luY2x1ZGUgPHN5cy93YWl0Lmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0ZGJvb2wuaD4KCmJvb2wgaXNfcHJpbWUoaW50IG51bSkgewogICAgaWYgKG51bSA8PSAxKSByZXR1cm4gZmFsc2U7CiAgICBmb3IgKGludCBpID0gMjsgaSA8PSBudW0gLyAyOyBpKyspIHsKICAgICAgICBpZiAobnVtICUgaSA9PSAwKSByZXR1cm4gZmFsc2U7CiAgICB9CiAgICByZXR1cm4gdHJ1ZTsKfQoKaW50IG1haW4oKSB7CiAgICBwaWRfdCBwaWQxLCBwaWQyLCBwYXJlbnRfcGlkOwoKICAgIHBhcmVudF9waWQgPSBnZXRwaWQoKTsKCiAgICBwaWQxID0gZm9yaygpOwoKICAgIGlmIChwaWQxID09IDApIHsKICAgICAgICAvLyBDaGlsZCBQcm9jZXNzIDEKICAgICAgICBpbnQgY2hpbGQxX3BpZCA9IGdldHBpZCgpOwogICAgICAgIGlmIChpc19wcmltZShjaGlsZDFfcGlkKSkgewogICAgICAgICAgICBwcmludGYoIkNoaWxkIDEgKFBJRCAlZCk6IE15IFBJRCBpcyBhIHByaW1lIG51bWJlci5cbiIsIGNoaWxkMV9waWQpOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIHByaW50ZigiQ2hpbGQgMSAoUElEICVkKTogTXkgUElEIGlzIG5vdCBhIHByaW1lIG51bWJlci5cbiIsIGNoaWxkMV9waWQpOwogICAgICAgIH0KICAgICAgICBleGl0KDApOwogICAgfSBlbHNlIHsKICAgICAgICBwaWQyID0gZm9yaygpOwoKICAgICAgICBpZiAocGlkMiA9PSAwKSB7CiAgICAgICAgICAgIC8vIENoaWxkIFByb2Nlc3MgMgogICAgICAgICAgICBpZiAoaXNfcHJpbWUocGFyZW50X3BpZCkpIHsKICAgICAgICAgICAgICAgIHByaW50ZigiQ2hpbGQgMiAoUElEICVkKTogTXkgcGFyZW50J3MgUElEICglZCkgaXMgYSBwcmltZSBudW1iZXIuXG4iLCBnZXRwaWQoKSwgcGFyZW50X3BpZCk7CiAgICAgICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgICAgICBwcmludGYoIkNoaWxkIDIgKFBJRCAlZCk6IE15IHBhcmVudCdzIFBJRCAoJWQpIGlzIG5vdCBhIHByaW1lIG51bWJlci5cbiIsIGdldHBpZCgpLCBwYXJlbnRfcGlkKTsKICAgICAgICAgICAgfQogICAgICAgICAgICBleGl0KDApOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIC8vIFBhcmVudCBQcm9jZXNzCiAgICAgICAgICAgIHdhaXQoTlVMTCk7CiAgICAgICAgICAgIHdhaXQoTlVMTCk7CiAgICAgICAgICAgIHByaW50ZigiUGFyZW50IChQSUQgJWQpOiBNaXNzaW9ucyBhY2NvbXBsaXNoZWQuLi5cbiIsIHBhcmVudF9waWQpOwogICAgICAgIH0KICAgIH0KICAgIHJldHVybiAwOwp9Cg==