Question
This is the orginal question and code Write a C/C++ program using fork() system call that allows a parent and a child processes to cooperate
This is the orginal question and code
Write a C/C++ program using fork() system call that allows a parent and a child processes to cooperate on carrying some computations. The program should get two (2) numbers from the user of the program. The child process should compute and display the sum and difference of the two numbers; the parent process should multiply and divide the two numbers and display the results. Your program should avoid any exception or interrupt.
My Teacher has given feed back and said
1.The program has a bug double to float conversion
2. It doesnt check for exception &
3.The execution of program creates zombie and an orphan processes
HOW CAN I FIX THESE THREE THINGS ????? MY ORIGINAL CODE IS BELOW
#include
int main() {
int num1,num2; puts("Please Type Your First Number: "); scanf("%d",&num1); puts("Please Type Your Second Number : "); scanf("%d",&num2); puts(" Thank You: "); int counter = 0; pid_t pid = fork();
if (pid == 0) { // child process int sum = num1 + num2; int diff = num1 - num2; printf(" The Sum of %d and %d is %d and The Diff is %d",num1,num2,sum,diff); } else if (pid > 0) { // parent process int mul = num1 *num2; float div = num1/(num2*1.0); printf(" The Multiplication of %d and %d is %d and The Division is %f",num1,num2,mul,div); } else { // fork failed puts("fork() failed! "); return 1; }
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started