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 06:30
Requirement types discussed during software development include functional and color scheme nonfunctional and code style constraint and nonfunctional fashionable and functional.
Answers: 2
question
Computers and Technology, 23.06.2019 16:00
What is the biggest difference between section breaks and regular page breaks? section breaks are more difficult to add than page breaks. section breaks make it easier for you to view the document as an outline. section breaks allow you to have areas of the document with different formatting. section breaks are smaller than regular page breaks.
Answers: 2
question
Computers and Technology, 24.06.2019 00:30
Setting up a home network using wireless connections is creating a a. vpn b. lan c. wan d. mini-internet
Answers: 2
question
Computers and Technology, 24.06.2019 08:20
Which type of entity describes a fundamental business aspect of a database? a. linking b. lookup c. domain d. weak
Answers: 3
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
Mathematics, 17.07.2020 14:01
question
English, 17.07.2020 14:01