Question

In: Computer Science

Can someone please write clear and concise comments explaining what each line of code is doing...

Can someone please write clear and concise comments explaining what each line of code is doing for this program in C. I just need help tracing the program and understand what its doing. Thanks

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

int join(char *com1[], char *com2[]) {

   int p[2], status;

   switch (fork()) {
       case -1:
           perror("1st fork call in join");
           exit(3);
       case 0:
           break;
       default:
           wait(&status);
           return(status);
   }
  
   //child process's code
   if (pipe(p) == -1){
       perror("pipecall in join ");
       exit(4);
   }

   switch (fork()) {
       case -1:
           perror("2nd fork call failed in join");
           exit(5);
       case 0: //the writing process (grandchild procss)
           dup2(p[1], 1); //redirect stdout to the pipe, writing end.
              
           close(p[0]);
           close(p[1]);

           execvp(com1[0], com1);

           perror("1st execvp call failed in join");
           exit(6);
       default: //the "parent" process's code
           dup2(p[0], 0); //redirect stdin to the pipe, reading end

           close(p[0]);
           close(p[1]);

           execvp(com2[0], com2);

           perror("2nd execvp call failed in join");
           exit(7);
       }
}

int main (){

   char *one[4]={ "ls", "-l", "/usr/lib",NULL};
//   char *two[3]={"grep", "^d", NULL};

   char *two[3]={"more", NULL};

   int ret;

   ret=join(one, two);

   printf("\n\njoin returned.\n");
   exit(0);
}

Solutions

Expert Solution

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

// function created to join two commands
int join(char *com1[], char *com2[]) {

int p[2], status;

// switch is used according process id which is generated by fork
// basically it should be switch(pid){}
switch (fork()) {
case -1:
perror("1st fork call in join"); // if fork returns -1 it means error
exit(3);
case 0:
break; // here code is executed by the first parent
default:
wait(&status); // it is executed by the parent process
return(status);
}
  
//child process's code
if (pipe(p) == -1){
perror("pipecall in join ");
exit(4);
}

switch (fork()) {
case -1:
perror("2nd fork call failed in join");
exit(5);
case 0: //the writing process (grandchild procss)
dup2(p[1], 1); //redirect stdout to the pipe, writing end.
  
close(p[0]);
close(p[1]);
  
execvp(com1[0], com1);        /* functions provide an array of pointers to null-terminated strings that represent the argument list
                                       available to the new program. The first argument, by convention, should point to the filename associated
                                       with the file being executed. The array of pointers must be terminated by a NULL pointer.*/

perror("1st execvp call failed in join");
exit(6);
default: //the "parent" process's code
dup2(p[0], 0); //redirect stdin to the pipe, reading end

close(p[0]);
close(p[1]);

execvp(com2[0], com2);

perror("2nd execvp call failed in join");
exit(7);
}
}

int main (){

char *one[4]={ "ls", "-l", "/usr/lib",NULL};// char array passed with command status and path
// char *two[3]={"grep", "^d", NULL};

char *two[3]={"more", NULL}; // second char array woth command as input

int ret;

ret=join(one, two);// join function calling

printf("\n\njoin returned.\n");
exit(0);
}

Explanation : Comments ha added . this program represent the facility of join command in linux or Unix. But here as arguments of execvp() function proper file name ha not provided a the syntax for this function is

int execvp(const char *file, char *const argv[]);

Hence there is a need of some modification in program .


Related Solutions

Can someone please add clear and concise comments thoroughly explaining each line of code below. Just...
Can someone please add clear and concise comments thoroughly explaining each line of code below. Just need help understanding the code, don't need to modify it. The purpose of the code is to count the frequency of words in a text file, and return the most frequent word with its count. It uses two algorithms: Algorithm 1 is based on the data structure LinkedList. It maintains a list for word frequencies. The algorithm runs by scanning every token in the...
Comment the code explaining what each line is doing. The first line has been done for...
Comment the code explaining what each line is doing. The first line has been done for you. x=[0 0 -1 2 3 -2 0 1 0 0]; %input discrete-time signal x dtx= -2:7; y=[1 -1 2 4]; dty=8:11; z=conv(x,y); dtz=6:18; subplot(1,3,1), stem(dtx,x) subplot(1,3,2), stem(dty,y) subplot(1,3,3), stem(dtz,z)
Econometrics: Can someone please give a clear, concise and intuitive explanation of the rank of a...
Econometrics: Can someone please give a clear, concise and intuitive explanation of the rank of a matrix and how to find the rank using examples. WITHOUT REFERENCE TO ECHELON FORM.
PLEASE WRITE CLEAR AND CONCISE CODE Subject is Unix system programming Write code that downloads all...
PLEASE WRITE CLEAR AND CONCISE CODE Subject is Unix system programming Write code that downloads all of the log files ending in `.log` in an S3 bucket and counts the total number of HTTP errors in those logs. Log lines are in the format `{"path": "/", status: 200}` or `{"path": "/", status: 404}`, for example. (This is JSON, and you can process it as such if you choose.) Use the bucket `class6-logs`, the access key ID "AKIASUMBPHIPY6DLZ4C5", and the secret...
Assembly language: please comment on every line of code explaining each part. include head comments describing...
Assembly language: please comment on every line of code explaining each part. include head comments describing what your program does. Assignment 3A - A program that adds and subtracts 32-bit numbers After installing the assembler on the computer, enter the following program, save it, assemble it and run it. Do not forget to add a comment with your name in it. You will hand in a listing (e.g., addsum.asm) that should include your name ________________________________________ TITLE Add and Subtract (AddSum.asm)...
Please write with structure, not class! Add comments for clear understanding. Write code without using scope...
Please write with structure, not class! Add comments for clear understanding. Write code without using scope resolution operators. Write an ADT to represent a doctor's medical registration number (a non-negative integer), number of patients (a non-negative integer), and income (non-negative, in euros). A doctor's information should be represented by a structure with 3 fields: medical_reg and num_patients of type int, and income of type float. Your ADT should define four interface functions to carry out the following operations: ยท Construct...
Can someone please complete the following code(Java). The stuff in comments are the things you need...
Can someone please complete the following code(Java). The stuff in comments are the things you need to look at and code that package mini2; import static mini2.State.*; /** * Utility class containing the key algorithms for moves in the * a yet-to-be-determined game. */ public class PearlUtil { private PearlUtil() { // disable instantiation } /**    * Replaces all PEARL states with EMPTY state between indices    * start and end, inclusive.    * @param states    * any...
Okay, can someone please tell me what I am doing wrong?? I will show the code...
Okay, can someone please tell me what I am doing wrong?? I will show the code I submitted for the assignment. However, according to my instructor I did it incorrectly but I am not understanding why. I will show the instructor's comment after providing my original code for the assignment. Thank you in advance. * * * * * HourlyTest Class * * * * * import java.util.Scanner; public class HourlyTest {    public static void main(String[] args)     {        ...
1. Write code in mips that will play battleships. Include comments in code on what each...
1. Write code in mips that will play battleships. Include comments in code on what each part is doing.
Basically, the code already functions properly. Could you just write in method header comments explaining what...
Basically, the code already functions properly. Could you just write in method header comments explaining what the method does and what the parameters are? Some of them are already done. Additionally could you change the variables and method names to make them more descriptive of what they're actually holding? Thank you! import java.util.Scanner; /** * This class contains the entire program to print out a yearly calendar. * * @author * @author TODO add your name here when you contribute...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT