Question

In: Computer Science

•Modify p4.c so that the output file p4.output is created but also displayed to standard output...

•Modify p4.c so that the output file p4.output is created but also displayed to standard output ( the screen ). This should be done by another instance of exec().

•Implement the pipe() command to do the following:

$> grep –o else p4.c | wc –l

p4.c

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

int main(int argc, char *argv[]) {
int rc = fork();
if (rc < 0) {
    // fork failed
    fprintf(stderr, "fork failed\n");
    exit(1);
} else if (rc == 0) {
    // child: redirect standard output to a file
    close(STDOUT_FILENO);
    open("./p4.output", O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU);

    // now exec "wc"...
    char *myargs[3];
    myargs[0] = strdup("wc");   // program: wc (word count)
    myargs[1] = strdup("p4.c"); // arg: file to count
    myargs[2] = NULL;           // mark end of array
    execvp(myargs[0], myargs); // runs word count
} else {
    // parent goes down this path (main)
    int rc_wait = wait(NULL);
}
return 0;
}

Solutions

Expert Solution

Done accordingly. Comment for further help. Please uprate.

CODE:

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

int main(int argc, char *argv[]) {
int rc = fork();
int rc2=fork();
if (rc < 0) {
// fork failed
fprintf(stderr, "fork failed\n");
exit(1);
} else if (rc == 0) {
close(STDOUT_FILENO);
// child: redirect standard output to a file
open("./p4.output", O_CREAT|O_WRONLY|O_TRUNC, S_IRWXU);

// now exec "wc"...
char *myargs[3];
myargs[0] = strdup("wc"); // program: wc (word count)
myargs[1] = strdup("p4.c"); // arg: file to count
myargs[2] = NULL; // mark end of array
execvp(myargs[0], myargs); // runs word count
}else if (rc2==0){
char *myargs0[3];
myargs0[0] = strdup("wc"); // program: wc (word count)
myargs0[1] = strdup("p4.c"); // arg: file to count
myargs0[2] = NULL; // mark end of array
execvp(myargs0[0], myargs0); // runs word count
} else {
// parent goes down this path (main)
int rc_wait = wait(NULL);
}
return 0;
}

Output:


Related Solutions

modify code to write the output as an HTML table to a file in the output...
modify code to write the output as an HTML table to a file in the output directory. The file that is saying to work at : SOURCE CODE IN PERL: print "Enter principal amount: "; $P=; while($P<=0) { print "Principal must be positive. Try again: "; $P=; } print "Enter number of times interest is applied in a year: "; $n=; while($n!=12 && $n!=4 && $n!=2 && $n!=1) { print "It must be 12, 4, 2 or 1. Try again:...
C++ 1. Modify this program to open the file "Customers.dat" so that all data is written...
C++ 1. Modify this program to open the file "Customers.dat" so that all data is written to the end of the file AND to allow the file to be read. 2. Create a method called "getLargestCustomerNumber" and call it after the "Customers.dat" file has been opened. Read all the existing customerNumbers and determine the largest customerNumber - do not assume the last record in the file is the largest number. Use this number as the base when adding new customer...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates...
C Programming: POSIX: Producer / Consumer Modify the code below so that the Producer.c file calculates the Fibonacci sequence and writes the sequence to the shared-memory object. The Consumer.c file should then output the sequence. Producer.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <fcntl.h> #include <sys/shm.h> #include <sys/stat.h> #include <sys/mman.h> #include <zconf.h> int main() { /* The size (in bytes) of shared-memory object */ const int SIZE = 4096; /* The name of shared-memory object */ const char *Obj =...
Modify this program so that it takes in input from a TEXT FILE and outputs the...
Modify this program so that it takes in input from a TEXT FILE and outputs the results in a seperate OUTPUT FILE. (C programming)! Program works just need to modify it to take in input from a text file and output the results in an output file. ________________________________________________________________________________________________ #include <stdio.h> #include <string.h> // Maximum string size #define MAX_SIZE 1000 int countOccurrences(char * str, char * toSearch); int main() { char str[MAX_SIZE]; char toSearch[MAX_SIZE]; char ch; int count,len,a[26]={0},p[MAX_SIZE]={0},temp; int i,j; //Take...
).Modify the project so that records are inserted into the random access file in ascending order...
).Modify the project so that records are inserted into the random access file in ascending order using an insertion sort methodology with the Social Security Number acting as the key value. This requires defining the method compareTo() in the Personal and Student classes to be used in a modified method add() in Database. The method finds a proper position for a record d, moves all the records in the file to make room for d, and writes d into the...
Change the program to modify the output file by making each sentence a new paragraph (inserting...
Change the program to modify the output file by making each sentence a new paragraph (inserting two carriage returns between every sentence. :) Don't over-think this, but you must have worked through and understand how the program works now in order to modify it. Remember, you want the carriage returns between every SENTENCE, not every LINE. How would one do this? I'm not to sure how to make it make a new line after a sentence. Any help will be...
Change the program to modify the output file by making each sentence a new paragraph (inserting...
Change the program to modify the output file by making each sentence a new paragraph (inserting two carriage returns between every sentence. :) Don't over-think this, but you must have worked through and understand how the program works now in order to modify it. Remember, you want the carriage returns between every SENTENCE, not every LINE. How would one do this? I'm not to sure how to make it make a new line after a sentence. Any help will be...
[PYTHON] Modify the examaverages.py program included with this assignment so it will also compute the overall...
[PYTHON] Modify the examaverages.py program included with this assignment so it will also compute the overall average test grade. E.g if there are 3 test each student must take and the user enters the following set of test scores for the two students…: 30, 40, 50 for the first student 50, 60, 70 for the second student …then program will print the average for each student (i.e. 40 for the first student and 60 for the second student – the...
1).Modify the project so that records are inserted into the random access file in ascending order...
1).Modify the project so that records are inserted into the random access file in ascending order using an insertion sort methodology with the Social Security Number acting as the key value. This requires defining the method compareTo() in the Personal and Student classes to be used in a modified method add() in Database. The method finds a proper position for a record d, moves all the records in the file to make room for d, and writes d into the...
In c++, modify this program so that you allow the user to enter the min and...
In c++, modify this program so that you allow the user to enter the min and maximum values (In this case they cannot be defined as constants, why?). // This program demonstrates random numbers. #include <iostream> #include <cstdlib> // rand and srand #include <ctime> // For the time function using namespace std; int main() { // Get the system time. unsigned seed = time(0); // Seed the random number generator. srand(seed); // Display three random numbers. cout << rand() <<...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT