Please Help Triplet Prodigies Part 1 1 Write a C program cal
Please Help
Triplet Prodigies:
Part 1
1. Write a C program called lastnameTriplets that accepts 2 integer parameters. The program (parent process= P) should create 3 children, C1, C2, and C3, sequentially.
C1 should print its PID, then calculate and print the sum of the 2 integers.
C2 should print its PID, then calculate and print the product of the 2 integers.
C3 should print its PID, then calculate and print the sum the squares of the 2 integers. The parent should wait for all three children to complete their calculations before printing its own PID and exiting.( Only the parent process should create children. )
P creates C1 and waits.
C1 performs sum calculation.
C1 exits.
P creates C2 and waits.
C2 performs product calculation.
C2 exits.
P creates C3 and waits.
C3 performs sum of squares calculation.
C3 exits.
P exits.
Hint: Use the function atoi() to convert an argument passed in through the command line into an integer.
Part 2:
2.) Write a C program called lastnameTripletsExtended that extends the work of part 1 by accepting any
number of integer parameters
Solution
// lastnameTriplets
#include <stdio.h>
int main(int argc , char **argv)
{
//declare ppid to hold parent pid
int ppid;
//variable to hold two inetegers read from command line args
int n1,n2;
//variable used in loop
int i;
//Get parent Id
ppid = getpid();
//check if two integers are entered as argument
if(argc < 3)
{
printf(\"Enter two integers as command lines..\ \");
exit(1);
}
//convert commandline args to integer
n1 =atoi(argv[1]);
n2 =atoi(argv[2]);
//print two numbers
printf(\"n1 = %d n2 = %d \ \",n1,n2);
//create 3 children
for( i = 1 ; i <= 3 ; i++ )
{
create_child(i,ppid,n1,n2);
}
//parnt process waits for all children
if( ppid == getpid())
{
wait(NULL);
}
return 0;
}
void create_child(int i,int ppid,int n1, int n2)
{
int pid;
pid = fork();
//to check we create child of same parent
switch(i)
{
case 1:
if( pid == 0 && ppid == getppid())
{
int sum ;
printf(\"Child c1 with pid %d and parent id %d \ \",getpid(),getppid);
sum = n1+n2;
printf(\"the sum of the 2 integers = %d\ \",sum);
}
//other parent wait for their child
else if(pid > 0)
{
wait(pid);
}
break;
case 2:
if( pid == 0 && ppid == getppid())
{
int prod;
printf(\"Child c2 with pid %d and parent id %d \ \",getpid(),getppid);
prod = n1 * n2;
printf(\"product of the 2 integers =%d \ \",prod);
}
//other parent wait for their child
else if(pid > 0)
{
wait(pid);
}
break;
case 3:
if( pid == 0 && ppid == getppid())
{
int sum;
printf(\"Child c3 with pid %d and parent id %d \ \",getpid(),getppid);
sum = n1 * n1 + n2 *n2;
printf(\"sum the squares of the 2 integers = %d\ \",sum);
}
//other parent wait for their child
else if(pid > 0)
{
wait(pid);
}
break;
default:
break;
}
}
---------------------------------------------------------------------------------------------
output of program1
./a.out 10 25
n1 = 10 n2 = 25
Child c1 with pid 201 and parent id 4195840
the sum of the 2 integers = 35
Child c2 with pid 205 and parent id 4195840
product of the 2 integers =250
Child c3 with pid 207 and parent id 4195840
sum the squares of the 2 integers = 725
-----------------------------------------------------------------------------------------------------------------------------------------------
//lastnameTripletsExtended
#include <stdio.h>
//Array will hold MAX integers ie 100 integers
#define MAX 100
//function declaration to create children
void create_child(int i,int ppid,int arr[], int size);
int main(int argc , char **argv)
{
//declare ppid to hold parent pid
int ppid;
//take array of integres to hold numbers taken from command line argument
int array_num[MAX];
//variable used in loop
int i;
//variable used for counting number of array items
int count = 0;
//Get parent Id
ppid = getpid();
//check if two integers are entered as argument
if(argc < 3)
{
printf(\"Enter two integers as command lines..\ \");
exit(1);
}
//take all the integers entered on integers into array of integers
printf(\"Integers entered are : \");
while(--argc)
{
array_num[count] = atoi(argv[count+1]);
printf(\"%d \\t\",array_num[count]);
count++;
}
//create 3 children
for( i = 1 ; i <= 3 ; i++ )
{
create_child(i,ppid,array_num,count);
}
//parnt process waits for all children
if( ppid == getpid())
{
wait(NULL);
}
return 0;
}
void create_child(int i,int ppid,int arr[], int size)
{
int pid;
pid = fork();
//to check we create child of same parent
switch(i)
{
case 1:
if( pid == 0 && ppid == getppid())
{
int sum = 0;
int i;
printf(\"\ Child c1 with pid %d and parent id %d \ \",getpid(),getppid);
//calculate sum of all integers
for(i = 0 ; i < size; i ++)
{
sum+=arr[i];
}
printf(\"the sum of integers = %d\ \",sum);
}
//other parent wait for their child
else if(pid > 0)
{
wait(pid);
}
break;
case 2:
if( pid == 0 && ppid == getppid())
{
int prod=1;
int i;
printf(\"\ Child c2 with pid %d and parent id %d \ \",getpid(),getppid);
//calculate sum of all integers
for(i = 0 ; i < size; i ++)
{
prod*=arr[i];
}
printf(\"product of integers =%d \ \",prod);
}
//other parent wait for their child
else if(pid > 0)
{
wait(pid);
}
break;
case 3:
if( pid == 0 && ppid == getppid())
{
int sum=0;
int i;
printf(\"\ Child c3 with pid %d and parent id %d \ \",getpid(),getppid);
//calculate sum of all integers
for(i = 0 ; i < size; i ++)
{
sum+=arr[i] * arr[i];
}
printf(\"sum the squares of integers = %d\ \",sum);
}
//other parent wait for their child
else if(pid > 0)
{
wait(pid);
}
break;
default:
break;
}
}
----------------------------------------------------------------
output:
./a.out 3 4 5 6 7 8 9
Integers entered are : 3 4 5 6 7 8 9
Child c1 with pid 172 and parent id 4195840
the sum of integers = 42
Integers entered are : 3 4 5 6 7 8 9
Child c2 with pid 176 and parent id 4195840
product of integers =181440
Integers entered are : 3 4 5 6 7 8 9
Child c3 with pid 178 and parent id 4195840
sum the squares of integers = 280





