Our program will select a secret number from 1 to 100 and we will try to guess t
ID: 3603303 • Letter: O
Question
Our program will select a secret number from 1 to 100 and we will try to guess that number To implement our program we will use IPC The CHILD process is the one that selects the number and in the PARENT process, which is used so we can interact with the program, we try to guess the secret number . I give you here the compiled code that the CHILD is supposed to run. (read on how to access this binary) The CHILD process reads from the standard input and writes to the standard output. Because of this fact, you can run the CHILD code on the command line to see how this program behaves! However, we are to communicate with this CHILD program via the A4.c, that you need to complete I do NOT give you the source code of the CHILD, I only give you the binary (make sure you name it A4 CHILD) You can copy it from delphi . ~1eond/A4 /A4 CHILD - * Have fun and see how fast you can guess the secret number :) HERE is where I will post the source of the A4_CHILD (after you submit your work) #include Winclude int main) int pid; int ni char buf[101); CREATE FIRST PIPE CREATE SECOND PIPE // FORK) if I AM THE CHILD / duplicate file descriptor 0 to point to FIRST pipe I CLOSE ends of FIRST pipe you don't need anymore / duplicate file descriptor 1 to point to SECOND pipe /CLOSE ends of SECOND pipe you don't need anymore ". /A4-CHILD" char 0); exec1p(". /A4. CHILD", perror("execlp") exit(-3); *) , / PARENT PROCESS COMES HERE close(pfd A[0])1 close (pfd BI1) while 1 char NUMI100]: int r=0; printf("Enter a Number: "); fflush (stdout) scanf("S",NUM) SENDNUMto child process READ FROM CHILD THE RESPONSE into the variable buf and store the return value from read) into the variable r if(r> 0) buf [r] = 0'; printf("%s ", fflush(stdout) buf); Reading from child: read() returned %d ", printf(" [PARENT] break; r); return(0)Explanation / Answer
Please go threw this code and comments.
/* I don't know what exactly you want to do . but i done what you want in these line.*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
int main()
{
int pid;
int n;
int pfd_A[2]; //creat pipe fd
int pfd_B[2];
pipe(pfd_A); //call 2 pipes
pipe(pfd_B);
pid= fork(); //created child
if(pid == 0) // in chlid
{
dup2(pfd_A[0],0); //duplicate in stdin
close(pfd_A[1]); //close other ends
dup2(pfd_B[1],1); //duplicate in stdout
close(pfd_B[0]); //close other ends
execlp("./A4_CHILD","./A4_CHILD",(char *) 0);
perror("execlp");
exit(-3);
} else {
char buf[100];
close(pfd_A[0]);
close(pfd_B[1]);
while(1) {
char NUM[100];
int r=0;
printf("Enter a Number: ");
fflush(stdout);
scanf("%s",NUM);
write(pfd_A[1],NUM,strlen(NUM)); //write in pipe
read(pfd_B[0],buf,sizeof(buf)); //read form pipe
if( r > 0) {
buf[r]='';
printf("%s ",buf); //print buffer
fflush(stdout);
}
else {
printf("[PARENT] Reading form clild: read() returned %d ",r); //otherwise give error
break;
}
}
}
return(0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.