Help With program using fork() system for child and parent process

This is the orginal question and My 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 feedback and said to fix the following

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 And the new code but I keep getting the same errors

Old Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdio.h>
#include <unistd.h>

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("\n 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("\n The Multiplication of %d and %d is %d and The Division is %f",num1,num2,mul,div);
}
else
{
// fork failed
puts("fork() failed!\n");
return 1;
}


return 0;
}



Please Type Your First Number:                                                                                      
2                                                                                                                   
Please Type Your Second Number :                                                                                    
10                                                                                                                  
 Thank You:                                                                                                         
                                                                                                                    
 The Multiplication of 2 and 10 is 20 and The Division is 0.200000                                                  
 The Sum of 2 and 10 is 12 and The Diff is -8   
 


New Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#define INF 99999999
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("\n The Sum of %d and %d is %d and The Diff is %d",num1,num2,sum,diff);
exit(0);
}
else if (pid > 0)
{
// parent process
wait(NULL);
double div;
int mul = num1 *num2;
if(num2!=0)
{
div = (double)num1/(double)num2;
printf("\n The Multiplication of %d and %d is %d and The Division is %lf",num1,num2,mul,div);
}
else
{
div=INF;
printf("\n The Multiplication of %d and %d is %d",num1,num2,mul);
printf("\n Denominator cannot be zero!Operation division not possible.");

}
}
else
{
// fork failed
puts("fork() failed!\n");
return 1;
}
return 0;
}

return 0;
}


main.cpp: In function ‘int main()’:
main.cpp:23:1: error: ‘exit’ was not declared in this scope
 exit(0);
 ^~~~
main.cpp:23:1: note: suggested alternative: ‘_exit’
 exit(0);
 ^~~~
 _exit
main.cpp:28:1: error: ‘wait’ was not declared in this scope
 wait(NULL);
 ^~~~
main.cpp:28:1: note: suggested alternative: ‘main’
 wait(NULL);
 ^~~~
 main
main.cpp: At global scope:
main.cpp:53:1: error: expected unqualified-id before ‘return’
 return 0;
 ^~~~~~
main.cpp:54:1: error: expected declaration before ‘}’ token
 }
 ^


Last edited on
0. The code is not indented.


¿what's the point of `fork()' if the parent will wait for the child to end to start doing work?

> error: ‘exit’ was not declared in this scope
> error: ‘wait’ was not declared in this scope
missing headers
`exit()' is declared in `cstdlib'
`wait()' in `sys/wait.h'

> error: expected declaration before ‘}’ token
indent your code and will be obvious.

> The execution of program creates zombie and an orphan processes
you only have one child, so it's an orphan zombie.
an orphan zombie will be reaped immediately, ¿what's the issue?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>

#include<stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/wait.h>

#include <sys/types.h>

#define INF 99999999

int main()

{

                int N1,N2;

                puts("Please Type Your First Number: ");

                scanf("%d",&N1);

                puts("Please Type Your Second Number : ");

                scanf("%d",&N2);

                puts(" Thank You: ");

                int counter = 0;

                pid_t pid = fork();

                if (pid == 0)

                {

                                // child process

                                int sum = N1 + N2;

                                int diff = N1 - N2;

                               

                                printf("\n The Sum of %d and %d is %d and The Diff is %d",N1,N2,sum,diff);

                                exit(0);

                }

                else if (pid > 0)

                {

                                // parent process

                                wait(NULL);

                                double div;

                                int mul = N1 *N2;

                                if(N2!=0)

                                {

                                                div = (double)N1/(double)N2;

                                                printf("\n The Multiplication of %d and %d is %d and The Division is %lf",N1,N2,mul,div);

                                }

                                else

                                {

                                                div=INF;

                                                printf("\n The Multiplication of %d and %d is %d",N1,N2,mul);

                                                printf("\n Denominator cannot be zero!Operation division not possible.");

                               

                                }

                }

                else

                {

                                // fork failed

                                puts("fork() failed!\n");

                                return 1;

                }

                return 0;

}




Please Type Your First Number:                                                                                
10                                                                                                            
Please Type Your Second Number :                                                                              
5                                                                                                             
 Thank You:                                                                                                   
                                                                                                              
 The Sum of 10 and 5 is 15 and The Diff is 5                                                                  
 The Multiplication of 10 and 5 is 50 and The Division is 2.000000  
  


The execution of program creates zombie and an orphan processes also didn't seem to be a problem to me, but apparently it is ...
Last edited on
Topic archived. No new replies allowed.