Hi reader,
Here our task is :
A :Accept number.
C:Find whether it is armstrong.
D:Print Result.
B:Calculate Reverse of the Number.
E:Display Reverse.
Here is the complete Program.
#include<stdio.h>
#include<unistd.h>
main()
{ //initially in A
int num,f1,f2,f3,f4,p1[2],p2[2],p3[2],p4[2],arm,rev,flag=0;
pipe(p1);
pipe(p2);
f1=fork(); //A created B
if(f1>0) //A is working ..
{
f2=fork(); //A created C
if(f2>0) //A is working...
{
printf("Enter a number :");
scanf("%d",&num);
close(p1[0]);
close(p2[0]);
write(p1[1],&num,sizeof(num));
write(p2[1],&num,sizeof(num));
}
if(f2==0) //C is working..
{
pipe(p3);
f3=fork(); //C created D
if(f3>0) //C is working..
{
close(p1[1]);
read(p1[0],&num,sizeof(num));
flag=0;
for(arm=0;num>0;num/=10)
arm+=(num%10)*(num%10)*(num%10);
if(num==arm)flag=1;
close(p3[0]);
write(p3[1],&flag,sizeof(flag));
}
else if (f3==0) //D is working..
{
close(p3[1]);
read(p3[0], &flag, sizeof(flag));
(flag)?printf("It is Armstrong Number"):("It is Not Armstrong Number");
}
}
}
if(f1==0) //B is working..
{
sleep(2);
pipe(p4);
f4=fork(); //B created E
if(f4>0) //B is working..{
close(p2[1]);
read(p2[0],&num,sizeof(num));
for(rev=0;num>0;num/=10)
rev=10*rev+num%10;
close(p4[0]);
write(p4[1],&rev,sizeof(rev));
}
if(f4==0) //E is working..
{
close(p4[1]);
read(p4[0],&rev,sizeof(rev));
printf("Reverse of Number is : %d\n",rev);
}
}
}
Output
Enter a number: 153
It is Armstrong Number
Reverse of Number is :351
Here our task is :
- Create 5 processes.
- Find whether a number is Armstong and Reverse the same using these processes.
A :Accept number.
C:Find whether it is armstrong.
D:Print Result.
B:Calculate Reverse of the Number.
E:Display Reverse.
Here is the complete Program.
#include<stdio.h>
#include<unistd.h>
main()
{ //initially in A
int num,f1,f2,f3,f4,p1[2],p2[2],p3[2],p4[2],arm,rev,flag=0;
pipe(p1);
pipe(p2);
f1=fork(); //A created B
if(f1>0) //A is working ..
{
f2=fork(); //A created C
if(f2>0) //A is working...
{
printf("Enter a number :");
scanf("%d",&num);
close(p1[0]);
close(p2[0]);
write(p1[1],&num,sizeof(num));
write(p2[1],&num,sizeof(num));
}
if(f2==0) //C is working..
{
pipe(p3);
f3=fork(); //C created D
if(f3>0) //C is working..
{
close(p1[1]);
read(p1[0],&num,sizeof(num));
flag=0;
for(arm=0;num>0;num/=10)
arm+=(num%10)*(num%10)*(num%10);
if(num==arm)flag=1;
close(p3[0]);
write(p3[1],&flag,sizeof(flag));
}
else if (f3==0) //D is working..
{
close(p3[1]);
read(p3[0], &flag, sizeof(flag));
(flag)?printf("It is Armstrong Number"):("It is Not Armstrong Number");
}
}
}
if(f1==0) //B is working..
{
sleep(2);
pipe(p4);
f4=fork(); //B created E
if(f4>0) //B is working..{
close(p2[1]);
read(p2[0],&num,sizeof(num));
for(rev=0;num>0;num/=10)
rev=10*rev+num%10;
close(p4[0]);
write(p4[1],&rev,sizeof(rev));
}
if(f4==0) //E is working..
{
close(p4[1]);
read(p4[0],&rev,sizeof(rev));
printf("Reverse of Number is : %d\n",rev);
}
}
}
Output
Enter a number: 153
It is Armstrong Number
Reverse of Number is :351