Introduction Write a small C program using ordinary pipes to provide and accept
ID: 672901 • Letter: I
Question
Introduction Write a small C program using ordinary pipes to provide and accept passwords by the parent and child processes, respectively. Problem Specification Design a program using ordinary pipes in which there are two processes (a parent and a child). The parent process asks the user to enter a candidate password for his “username” in order to access Application X. (We do not care what application is X.) In response, the parent sends the password to the child. The child checks the strength of the user's password, and returns one of the following three decisions:
Decision Description STRONG PASSWORD If the password has more than 8 characters and contains uppercase and lower-case letters as well as numbers. Examples: OPSlmr183, 187jklPNR, etc.
WEAK PASSWORD If the password has more than 8 characters and does not mix upper-case letters, lower-case letters, and numbers. Examples: AAAAAAAAA, 123456789, aaaaaacccc, EERReeeee, 7878EEEE, hhhh4545, etc.
SHORT PASSWORD If the password has fewer than 8 characters.
The program requires using two pipes: one for sending the password from the parent to the child, and the other for sending the child’s decision to the parent.
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
int main()
{
int fd[2], fd2[2], nbytes, i = 0, flag1 = 0, flag2 = 0, flag3 = 0;
pid_t childpid;
char string[] = "Hello, world! ";
char parentwrite[80], childwrite[80];
char parentreadbuffer[80], childreadbuffer[80];
pipe(fd);
pipe(fd2);
if((childpid = fork()) == -1)
{
perror("fork");
exit(1);
}
if(childpid == 0)
{
close(fd[1]);
close(fd2[0]);
nbytes = read(fd[0], childreadbuffer, sizeof(childreadbuffer));
if(strlen(childreadbuffer) < 8){
strcpy(childwrite, "SHORT PASSWORD");
}
else{
flag1 = 0;
for(i = 0; i < strlen(childreadbuffer); ++i){
if(childreadbuffer[i] <= 'z' && childreadbuffer[i] >= 'a'){
flag1 = 1;
break;
}
}
for(i = 0; i < strlen(childreadbuffer); ++i){
if(childreadbuffer[i] <= 'Z' && childreadbuffer[i] >= 'A'){
flag2 = 1;
break;
}
}
for(i = 0; i < strlen(childreadbuffer); ++i){
if(childreadbuffer[i] <= '9' && childreadbuffer[i] >= '0'){
flag3 = 1;
break;
}
}
if(flag1 && flag2 && flag3){
strcpy(childwrite, "STRONG PASSWORD");
}
else{
strcpy(childwrite, "WEAK PASSWORD");
}
}
write(fd2[1], childwrite, (strlen(childwrite)+1));
exit(0);
}
else
{
close(fd[0]);
close(fd2[1]);
strcpy(parentwrite, "OPSlmr183");
write(fd[1], parentwrite, (strlen(parentwrite)+1));
nbytes = read(fd2[0], parentreadbuffer, sizeof(parentreadbuffer));
printf("%s", parentreadbuffer);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.