In: Computer Science
This is a C program to produce a parent/child relationship in which
the parent process repeatedly counts and displays on the screen the number of characters for any user password the child process inputs to the pipe.
The communications will cease once the string “Done” is inputted.
#include<stdio.h> #include<unistd.h> #include<string.h> int main() { int pipefds[2]; int returnstatus; int pid; char password[100]; returnstatus = pipe(pipefds); if (returnstatus == -1) { printf("Unable to create pipe\n"); return 1; } pid = fork(); while(1) { printf("enter the password"); scanf("%s" , password); if(password="Done") { return 0;} else { // child process if (pid == 0) { printf("child Process - Writing to pipe - password is %s\n", password); write(pipefds[1], password, sizeof(password));} else { //parent process read(pipefds[0], readmessage, sizeof(readmessage)); printf("Child Process - Reading from pipe – length of password is %d\n", strlen(readmessage));} }
return 0;
}