Question

Q4) Write a C program named hw3q4.c” that takes three string inputs from the command line arguments that represent file names. The first and second files contain four sorted integer numbers that represent set elements. The main process creates a child process that shares the three files. The child process determines the intersection set of two sets and saves the line: Child process PID: xxxx Intersection of (x, x, x, x) and (y, y, y, y) (z, z, z, z) in the third file, then terminates. Meanwhile, the parent process computes the union of the two sets concurrently with the child process, then it waits till the child process terminates. After that, it saves to the third file the line: Parent process PID: xxxx Union of {x, x, x, x} and {y, y, y, y} = {z, z, z, z, z) and terminates. Use only Unix I/O functions. The program should check for all possible errors and mistakes and display correct and clear error messages. Include the source file to the report. Show the compilation and linking commands in your report. Test your run several times with all search possibilities and show your test results in the report.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code is followed by the output snapshot

/* File: hw3q4.c */
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
    char buf[1024];
    int buflength;

    int s1[4], s2[4];
    int sr[8], srsize; /* result */

    int fd1, fd2, fd3; /* file descriptors */

    pid_t pid;

    int i, j, k;
    int status;
    char * s;
    /* check for command line arguments */
    if(argc < 4)
    {
        sprintf(buf, "Usage: %s <file1> <file2> <file3>\n", argv[0]);
        buflength = strlen(buf);
        write(1, buf, buflength);
        return -1;
    }

    /* open fds and check for errors */
    if((fd1 = open(argv[1], O_RDONLY)) < 0)
    {
        sprintf(buf, "Error opening file: %s\n", argv[1]);
        buflength = strlen(buf);
        write(1, buf, buflength);
        return -2;
    }

    if((fd2 = open(argv[2], O_RDONLY)) < 0)
    {
        sprintf(buf, "Error opening file: %s\n", argv[2]);
        buflength = strlen(buf);
        write(1, buf, buflength);
        close(fd1);
        return -2;
    }

    /* create with read, write permissions */
    if((fd3 = open(argv[3], O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR)) < 0)
    {
        sprintf(buf, "Error opening file: %s\n", argv[3]);
        buflength = strlen(buf);
        write(1, buf, buflength);
        close(fd1);
        close(fd2);
        return -3;
    }

    /* read the file contents */
    buflength = read(fd1, buf, 1024);
    for(i = 0, s = buf; i < 4; i++)
    {
        /* parse an integer */
        s1[i] = 0;
        while(*s != ' ' && buflength > 1)
        {
            s1[i] = s1[i] * 10 + (*s - '0');
            s++;
            buflength --;
        }
        while(*s == ' ' && buflength > 1)
        {
            s++; /* skip the whitespace */
            buflength --;
        }

        /* printf("Integer %d: %d\n", i + 1, s1[i]); */
    }

    buflength = read(fd2, buf, 1024);
    for(i = 0, s = buf; i < 4; i++)
    {
        /* parse an integer */
        s2[i] = 0;
        while(*s != ' ' && buflength > 1)
        {
            s2[i] = s2[i] * 10 + (*s - '0');
            s++;
            buflength --;
        }
        s++; /* skip the whitespace */
        buflength --;

        /* printf("Integer %d: %d\n", i + 1, s2[i]); */
    }

    /* fork a child */
    pid = fork();
    if(pid == 0) /* child */
    {
        /* intersection */
        /* printf("Child\n"); */
        i = j = k = srsize = 0;
        while(i < 4 && j < 4)
        {
            if(s1[i] == s2[j])
            {
                sr[k++] = s1[i];
                i++;
                j++;
            }
            else if(s1[i] < s2[j])
            {
                i++;
            }
            else
            {
                j++;
            }
        }
        srsize = k;

        sprintf(buf, "Child process PID: %d Intersection of {", getpid());
    }
    else /* parent */
    {
        /* printf("Parent\n"); */
        /* union */
        i = j = k = srsize = 0;
        while(i < 4 && j < 4)
        {
            if(s1[i] == s2[j])
            {
                sr[k++] = s1[i];
                i++;
                j++;
            }
            else if(s1[i] < s2[j])
            {
                sr[k++] = s1[i++];
            }
            else
            {
                sr[k++] = s2[j++];
            }
        }
        while(i < 4)
        {
            sr[k++] = s1[i++];
        }
        while(j < 4)
        {
            sr[k++] = s2[j++];
        }
        srsize = k;

        sprintf(buf, "Parent process PID: %d Intersection of {", getpid());
    }

    /* Prepare the text for writing */
    for(i = 0; i < 4; i++)
    {
        if(i == 0)
            sprintf(buf, "%s%d", buf, s1[i]);
        else
            sprintf(buf, "%s,%d", buf, s1[i]);
    }
    sprintf(buf, "%s} and {", buf);

    for(i = 0; i < 4; i++)
    {
        if(i == 0)
            sprintf(buf, "%s%d", buf, s2[i]);
        else
            sprintf(buf, "%s,%d", buf, s2[i]);
    }
    sprintf(buf, "%s} = {", buf);

    for(i = 0; i < srsize; i++)
    {
        if(i == 0)
            sprintf(buf, "%s%d", buf, sr[i]);
        else
            sprintf(buf, "%s,%d", buf, sr[i]);
    }

    sprintf(buf, "%s}\n", buf);
    buflength = strlen(buf);

    if(pid == 0) /* child */
    {
        /* write immediately */
        write(fd3, buf, buflength);
        /* printf("Child done\n"); */
    }
    else
    {
        /* wait for child to finish */
        wait(&status);
        /* write to file */
        write(fd3, buf, buflength);
        /* printf("Parent done\n"); */
    }
    /* close fds */
    close(fd1);
    close(fd2);
    close(fd3);
    return 0;
}

na tYazQunatyat.TCHEGGTOTK SetS umairaz@umairaz:-/CHEGG/fork_ sets gcc hw3q4c.c umairaz@umairaz:-/CHEGG/fork_ sets /a.out a1 a2 a3 umairaz@umairaz:-/CHEGG/fork_ sets /a.out b1 b2 b3 umairaz@umairaz:-/CHEGG/fork_ sets /a.out c1 c2 c3 umairaz@umairaz:-/CHEGG/fork_ sets /a.out d1 d2 d3 umairaz@umairaz:-/CHEGG/fork_sets cat a1 12 18 89 95 umairaz@umairaz:-/CHEGG/fork_sets cat a2 8 18 95 125 umairaz@umairaz:-/CHEGG/fork_sets cat a3 Child process PID: 10072 Intersection of {12,18,89,95) and {8,18,95,125} {18,95) Parent process PID: 10671 Intersection of {12,18,89,95) and {8,18,95,125) = {8,12,18,89 , 95,125) umairaz@umairaz:-/CHEGG/fork_sets cat b1 98 156 196 598 umairaz@umairaz:-/CHEGG/fork_sets cat b2 600 601 655 688 umairaz@umairaz:-/CHEGG/fork_sets cat b3 Child process PID: 10082 Intersection of {98,156,196,598) and {600,601,655,688} {} Parent process PID: 10081 Intersection of {98,156, 196,598) and {600,601,655,688} {98,156, 196,598,600 , 601,655,688) umairaz@umairaz:-/CHEGG/fork_sets cat c1 55 58 99 666 umairaz@umairaz:-/CHEGG/fork_sets cat c2 55 58 99 666 umairaz@umairaz:-/CHEGG/fork_sets cat c3 Child process PID: 10100 Intersection of {55,58,99,666} and {55,58,99,666} {55,58,99,666) Parent process PID: 10099 Intersection of {55,58,99,666} and {55,58,99,666} = {55,58,99,666} umairaz@umairaz:-/CHEGG/fork_sets cat d1 2 3 59 umairaz@umairaz:-/CHEGG/fork_sets cat d2 2 3 9 11 umairaz@umairaz:-/CHEGG/fork_sets cat d3 Child process PID: 10110 Intersection of 12,3,5,9 and f2,3,9,11]£2,3,9 Parent process PID: 10109 Intersection of {2,3,5,9} and {2,3,9,11} = {2,3,5,9,11) umairaz@umairaz:-/CHEGG/fork_sets

Add a comment
Know the answer?
Add Answer to:
Q4) Write a C program named "hw3q4.c” that takes three string inputs from the command line...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C program called test that takes one command line argument, an integer N. When...

    Write a C program called test that takes one command line argument, an integer N. When we run test: ./test N the program will do this: the parent process forks N child processes each child process prints its process ID, exits the parent process waits for all child processes to exit, then exits

  • You are required to write a C program on Unix/Linux in which the parent process creates...

    You are required to write a C program on Unix/Linux in which the parent process creates three child processes, lets them run concurrently, and waits for them to return and prints their exit status. The three child processes are assigned different tasks. Child one is to calculate and display the highest mark of a class of ten students for a unit. Child one is required to get the marks from the standard input (i.e. the keyboard). Child two is to...

  • Write a C or C++ program A8p1.c(pp) that accepts one command line string parameter. Call the...

    Write a C or C++ program A8p1.c(pp) that accepts one command line string parameter. Call the fork function to produce two processes. In the child process print out the lower case version of the string. In the parent process print out the reversed upper case version of the string (e.g. child: “abc”; parent: ”CBA”). You may call the toupper and tolower functions in the header <ctype.h> if you wish. Specify in the output whether the parent or child process is...

  • Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...

    Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...

  • Write a program in C or a script in bash, called “compare” that takes two numbers on the command line and compares them....

    Write a program in C or a script in bash, called “compare” that takes two numbers on the command line and compares them. The program should print the result of the comparison. Specifically, it should print “<x> is <comparison> <y>”, where <x> is the first number, <y> is the second number and <comparison> is one of “equal to”, “greater than” or “less than”. If the two numbers are equal, the program should have an exit status of zero. The exit...

  • In C write a program that must accept one argument from the command line. The argument...

    In C write a program that must accept one argument from the command line. The argument is the name of the file containing the processes (for example: processes.csv).This file is comma-separated with three columns (process ID, arrival time and burst time) with each row for an individual process. You can assume that this file will have a maximum of 10 processes. For example Input: processes.csv ProcessID,Arrival Time,Burst Time 0,1,3 1,0,5 2,9,8 3,10,6 Where the first element is processes, the second...

  • Write a C program, named sortit, that reads three integers from the command line argument and...

    Write a C program, named sortit, that reads three integers from the command line argument and returns the sorted list of the integers on the standard output screen, For instance, >sortit 3 5 7 >The sorted list is 3, 5, 7. >sortit 4 0 9 >The sorted list is 0, 4, 9 >sortit 1 p 9 >sortit: invalid input p. >sortit >usage: sortit num1 num2 num3! 1. The source code of a c file 2. Screenshots that show successful execution...

  • Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++. Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string...

    Program is in C++.   Write a function named wordStatsPlus that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report: the total number of lines; total number of words; the number of unique letters used from A-Z, case-insensitively, and its percentage of the 26-letter alphabet; the average number of words per line (as an un-rounded...

  • 10) Unlike a signal, which conveys only the occurrence of a particular event and contains no...

    10) Unlike a signal, which conveys only the occurrence of a particular event and contains no information content, a pipe can be thought of as a scratch file created by a system call. It can be used as a communications channel between concurrently running processes. The interface call to a pipe is similar to that for any file. In fact, the process reads and writes to a pipe just like any file. Unlike files, however, pipes do not represent actual...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT