Write a C system program that creates a process chain of 32 processes having the following characteristics. 1. Each process prints two prime numbers that are generated randomly in the range of 1-1000. 2. Each parent must wait for its child process to terminate.
Your answer
#include <stdio.h>
#include <stdlib.h>
int checkPrimeNumber(int n);
int main() {
int c, n;
printf("two prime numbers that are generated randomly in the range of 1-1000");
for (int i = 1; i <= 32; i++) {
int temp = 0;
while (temp != 2) {
int number = 1 + rand() % 1000;
if (checkPrimeNumber(number) == 1) {
temp++;
printf("%d ", number);
}
}
printf("\n");
}
return 0;
}
int checkPrimeNumber(int n) {
int j, flag = 1;
for (j = 2; j <= n / 2; ++j) {
if (n % j == 0) {
flag = 0;
break;
}
}
return flag;
}
Comments
Leave a comment