subject

The following program is designed to echo a message from one child process to another, with upper and lowercase letter reversed. The program reliably outputs just the message "h" and then quits. Explain in a very clearly why this is the output. Suggest how the program could be corrected to produce the expected output. #include

#include

#include

#include

#include

#include

int main()

{

int fd1[2];

int fd2[2];

char message[] = "Hello, world!\n";

char buffer[100];

pid_t pid1;

pid_t pid2;

if (pipe(fd1) == -1)

{ return 1;

}

if (pipe(fd2) == -1)

{ return 1;

}

pid1 = fork();

pid2 = fork();

if (pid1 < 0)

{ return 1;

}

if (pid2 < 0)

{ return 1;

}

if (pid1 > 0)

{

close(fd2[0]);

write(fd2[1], message, strlen(message)+1);

close(fd2[1]);

close(fd1[1]);

read(fd1[0], buffer, 100);

close(fd1[0]);

printf("%s", buffer);

}

else if (pid2 > 0) {

close(fd2[1]);

read(fd2[0], buffer, 100);

close(fd2[0]);

close(fd1[0]);

for (int i=0; i
if(islower(buffer[i])) {

buffer[i] = toupper(buffer[i]);

}

else {

buffer[i] = tolower(buffer[i]);

}

sleep(1);

write(fd1[1], &buffer[i], 1);

}

close(fd1[1]);

}

return 0;

}

ansver
Answers: 3

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 20:00
What is the term for water wave that is created by an underwater earthquake
Answers: 1
question
Computers and Technology, 23.06.2019 01:50
Free points just awnser this. what should i watch on netflix
Answers: 2
question
Computers and Technology, 24.06.2019 15:30
What type of forensic evidence was recovered during the bomb set off at the new mexico facility on the video that was similar to the evidence obtained at the boston bombings and how did the evidence allow the researchers to connect other pieces of evidence to the same bomb?
Answers: 2
question
Computers and Technology, 24.06.2019 16:00
This isn't about school but every time it tells me to watch an ad to unlock the answer to a question it prompts a survey and it just keeps loading. so i haven't been able to get answers for my tests in like a week.
Answers: 2
You know the right answer?
The following program is designed to echo a message from one child process to another, with upper an...
Questions
question
Social Studies, 30.11.2020 06:30
question
Mathematics, 30.11.2020 06:30
question
Mathematics, 30.11.2020 06:30
question
Chemistry, 30.11.2020 06:30
question
Social Studies, 30.11.2020 06:30
Questions on the website: 13722360