Question

The following code is meant to capture the output of an executable in a file and then read in and print out the first 100 bytes of that file. Select all of the bugs in the code. Assume that all system calls succeed and that sizeof(char)-1. pid_t pid = fork(); if (pid == 0) { int in-open(file.txt, READONLY) dup2(STDOUT FILENO, in); execl(./something, something, hello!); l else char buffer[100] int in = open(file.txt. READ-ONLY): read(buffer, in, 100); The parent does not wait for the child to finish. Two processes cannot have read permissions to a file simultaneously. The order of the arguments to read) is incorrect. read requires a buffer of type byte. The file descriptor in is declared twice.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

1. The parent does not wait will be taken care by the wait call

pid_t pid = fork();
int in // Removes the redeclarion of in

if (pid == 0){
   in = open("file.txt",READ_ONLY);
   dup2(STDOUT_FILENO,in);
   execl("./something", "something","hello!");
}
else {
      char buffer[100]
      wait(0);    // This will also stop the simultaneous access to the file
      in = open("file.txt",READ_ONLY);
      read(buffer,in,100)
}

Add a comment
Know the answer?
Add Answer to:
The following code is meant to capture the output of an executable in a file and...
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
  • Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this...

    Directions: use only the signal mechanism system calls don’t use ( wait() or pipe() )in this problem. You can still read/write from/to a file. You must use ( kill() and pause() ) system calls. rewrite code below to do kill and pause system calls #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() { int pid; // pid: used to keep track of the child process int x...

  • Error: Unable to save the file in the folder...... Whats is wrong with this code.? The...

    Error: Unable to save the file in the folder...... Whats is wrong with this code.? The below code checks for the HTTP/1.1 200 OK. and if its true it divides tha pathname and hostname and after first slash in the pathname, it saves as the downloading link and have to save in the folder. but it 's not getting saved... int bytes sent; bytes sent send(sockfd, request strlen(request),0) int byte received; char buffer 10000J; bzero buffer 10000); //to make sure...

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use: 0 st...

    In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use: 0 standard input (stdin) 1 standard output (stdout) 2 standard error (stderr) By default, the command interpreter (shell) reads keyboard input from file descriptor 0 (stdin) and writes output to file descriptor 1 (stdout), which appears on the screen. As you explored in Lab 2, input/output can be...

  • Convert C to C++ I need these 4 C file code convert to C++. Please Convert...

    Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1;    FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){    numout = fread(&num, sizeof(int), 1, file);    c...

  • 2. capture This solution to this problem will require using some system calls that will be...

    2. capture This solution to this problem will require using some system calls that will be discussed in week 3. This program should accept the name of a file that includes a list of commands (one per line) to execute. Each line of the file should be executed by a subprocess and the output written to the file capture.txt For example, given the command file: /bin/ls-1 /bin/cat apple banana usr/bin/wc -1-wcanteloupe The output saved to capture.txt might be: W1 schubert...

  • Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also,...

    Edit the code (shell.c) given to do the tasks asked! will rate for correct answer! Also, include a screen shot of the output and terminal window of each command you used. Read carefully to do this task please. shell.c code given below. #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <sys/wait.h> #include <string.h> int main() { int PID; char lineGot[256]; char *cmd; while (1){ printf("cmd: "); fgets(lineGot, 256, stdin); // Get a string from user (includes \n) cmd = strtok(lineGot, "\n");...

  • Consider a file of records of the following type: typedef struct { int id; // unique...

    Consider a file of records of the following type: typedef struct { int id; // unique identifier char desc[100]; // description of item float price; // wholesale price } Record; And the following variables: int fd; // file descriptor, open on file for read/write Record rec; // record which is set to appropriate values If the file is non-empty and fd is currently positioned at the end-of-file, which of the following pairs of statements will update the 11th record in...

  • I have the following code....from the previous lab....the above needs to be added to what is...

    I have the following code....from the previous lab....the above needs to be added to what is already existing. ALSO MODIFY SEMAPHORES TO USE pthreads instead of the pipe constructs P() & V() #include <stdio.h> #include <string.h> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> #include <stdlib.h> #include <sys/stat.h> void printStat(char *filename); //Main int main(int argc, char *argv[]) { //Process Id (storing)    pid_t pid;    int j;    //printf("Welcome to Project Three\n”);    // For loop*/    for (j = 1; j...

  • Hello, how can i compile this C code using option -std=c99 or -std=gnu99 #include <stdio.h> #include...

    Hello, how can i compile this C code using option -std=c99 or -std=gnu99 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #include <unistd.h> #include <sys/types.h> #include <sys/wait.h> #include <sys/stat.h> #include <fcntl.h> static char* args[512]; pid_t pid; int command_pipe[2]; static void waiting(int n); static int command(int input, int first, int last) { int fd[2]; int flag=0; pipe( fd );   pid = fork(); if (pid == 0) { for(int i=0;args[i]!=0;i++) { if(args[i][0]=='>') { fd[1]=open(args[i+1], O_CREAT|O_TRUNC|O_WRONLY, 0644); flag=1; } if(args[i]=='>>' ) { fd[1]=open(args[i+1],...

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
Active Questions
ADVERTISEMENT