Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please read Lab Exercise 9.1 - Background on Textbook: p. 363 -> Lab 9.1 first.

ID: 3607458 • Letter: P

Question

Please read Lab Exercise 9.1 - Background on Textbook: p. 363 -> Lab 9.1 first. After reading the background, please work on the following problem: The source process reads information from a file (Therefore, you need to prepare a small text file for testing purpose) The source process calls fork) to create the filter process. a. b. C. n from the source process (passed via an anonymous pipe), converts the uppercase characters to lowercase ones and vice versa, and then prints out the converted characters to the screen. How to create an anonymous pipe: pipe0 API.

Explanation / Answer

Make a file named as "in.txt" in the root folder where you run the code.

The code is as follows

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <ctype.h>

#define FILE "in.txt"

int main()
{

    int fd1[2];

    char input_str[100];

    int fhd = open(FILE, 'r');
    if(fhd < -1){
        printf("File %s not found.", FILE);
    }
    int l = read(fhd, input_str, 100);
    input_str[l] = '';
    close(fhd);

    pid_t p;

    if (pipe(fd1)==-1)
    {
        perror("Pipe Failed" );
        return 1;
    }

    p = fork();
    if (p < 0)
    {
        perror("fork Failed" );
        return 1;
    }

    // Parent process
    else if (p > 0)
    {
      

        close(fd1[0]);
        int k = strlen(input_str);
        write(fd1[1], input_str, k+1);
        close(fd1[1]);

        // Wait for child to send a string
        wait(NULL);
    }

    // child process
    else
    {
        close(fd1[1]); // Close writing end of first pipe

        // Read a string using first pipe
        char str[100];
        read(fd1[0], str, 100);

        int k = strlen(str);
        for (int i=0; i < strlen(str); i++)
            str[i] = toupper(str[i]);

        str[k] = '';   // string ends with ''
      
        printf("CHILD: %s ", str);

        close(fd1[0]);
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote