write a C program for integer basic arithmetics Using Unix s

write a C program for integer- basic arithmetics. (Using Unix system calls, fork(), wait(), read() and write() )

-Prompts the message \"This program makes simple arithmetics\",

-Gets in an infinite loop then

1. Writes the message \"Enter an arithmetic statement, e.g., 34 + 132 > \",

2. Reads the whole input line,

3. Forks and

– the parent writes the message \"Created a child to make your operation, wait- ing\" then calls wait() to wait for its child.

– the child process calls the function childFunction(char *) and never returns.

4. The child, through childFunction(char *line),

– writes the message \"I am a child working for my parent\"

– usessscanf()toconverttheinputlineintoaninteger,acharacterandaninteger,

respectively.

– in case of wrong statement, the child calls exit(50)

– in case of division by zero, the child calls exit(100)

– in case of a wrong op the child calls exit(200)

– otherwise, it performs the appropriate arithmetic operation,

– uses sprint() to create an output buffer consisting of n1 op n2 = result,

– writes the output bufferto the screen

– calls exit(0)

5. Once the child terminates, the parent checks the returned status value and if it is 50, 100 or 200, writes \"Wrong statement\", \"Division by zero\" or \"Wrong operator\", respectively.

6. The parent goes back to 1.

tip:All reads/writes must be done using read()/write() && use the returned value of sscanf() for detecting a \"Wrong statement\"

Solution

#include <stdio.h>

#include<string.h>

#include<sys/types.h>

#define MAX_COUNT 200

#define BUF_SIZE 100

void childProcess(char [],char []);

void ParentProcess(void);

void main(void)

{

pid_t pid1,pid2, pid;

int status;

int i;

char buf[BUF_SIZE];

pirntf(\"*** parent is about to fork process 1 ***\ \");

if((pid1 = fork())<0) {

printf(\"Failed to fork process 1\ \");

exit(1);

}

else if(pid1==0)

ChildProcess(\"First\", \" \");

printf(\"*** Parent is about to fork process 2 ***\ \");

exit(1);

}else if (pid2 == 0)

ChildProcess(\"First\", \" \");

printf(\"*** Parent is about to fork process 2 ***\ \");

exit(1);

}

else if(pid2 == 0)

ChildProcess(\"Second\", \" \");

ParentProcess();

sprintf(buf, \" *** Parent enters waiting status...\ \");

write(1, buf, strlen(buf));

pid=wait(&status);

sprintf(buf, \"*** Parent enters waiting status ....\ \");

write(1, buf, strlen(buf));

pid = wait(&status);

sprintf(buf, \" *** Parent detects process %d was done ***\ \", pid);

write(1, buf, strlen(buf));

pid = wait(&status);

sprintf(buf, \"*** Parent detects process %d was done ***\ \", pid);

write(1, buf, strlen(buf));

pid = wait(&status);

printf(\"*** Parent detects process %d is done ***\ \", pid);

printf(\"*** Parent exits ***\ \");

exit(0);

#define QUAD(X) (x*x*x*x)

void ParentProcess(void)

{

int a,b,c,d;

int abcd, a4b4c4d4;

int count = 0;

char buf[BUF_SIZE];

sprintf(buf, \"Parent is about to compute the Armstrong numbers\ \");
write(1, buf, strlen(buf));
for (a = 0; a <= 9; a++)
for (b = 0; b <= 9; b++)
for (c = 0; c <= 9; c++)
for (d = 0; d <= 9; d++) {
abcd = a*1000 + b*100 + c*10 + d;
a4b4c4d4 = QUAD(a) + QUAD(b) + QUAD(c) + QUAD(d);
if (abcd == a4b4c4d4) {
sprintf(buf, \"From parent: \"
\"the %d Armstrong number is %d\ \",
++count, abcd);
write(1, buf, strlen(buf));
}
}
sprintf(buf, \"From parent: there are %d Armstrong numbers\ \", count);
write(1, buf, strlen(buf));
}

void ChildProcess(char *number, char *space)
{
pid_t pid;
int i;
char buf[BUF_SIZE];

pid = getpid();
sprintf(buf, \"%s%s child process starts (pid = %d)\ \",
space, number, pid);
write(1, buf, strlen(buf));
for (i = 1; i <= MAX_COUNT; i++) {
sprintf(buf, \"%s%s child\'s output, value = %d\ \",
space, number, i);
write(1, buf, strlen(buf));
}
sprintf(buf, \"%s%s child (pid = %d) is about to exit\ \",
space, number, pid);
write(1, buf, strlen(buf));
exit(0);
}

write a C program for integer- basic arithmetics. (Using Unix system calls, fork(), wait(), read() and write() ) -Prompts the message \
write a C program for integer- basic arithmetics. (Using Unix system calls, fork(), wait(), read() and write() ) -Prompts the message \
write a C program for integer- basic arithmetics. (Using Unix system calls, fork(), wait(), read() and write() ) -Prompts the message \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site