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...
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.
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.
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)     {        ...
This is a python program. Put comments explaining the code, please. Suppose you have been tasked...
This is a python program. Put comments explaining the code, please. Suppose you have been tasked with writing a Python program (using linked lists) to keep track of computer equipment. For each piece of equipment, we track its name, purchase date, purchase amount, and quantity on hand. Write a program that completes the following tasks: allow the user to add a piece of equipment to the front of the list; allow the user to update the quantity of a piece...
Please include comments on what you are doing.   Using linked lists, write a Python program that...
Please include comments on what you are doing.   Using linked lists, write a Python program that performs the following tasks: store the records for each college found in the input file - colleges.csv - into a linked list. (File includes name and state data fields) allow the user to search the linked list for a college’s name; display a message indicating whether or not the college’s name was in the database allow the user to enter a state's name and...
Can you please explain in detail what each line of code stands for in the Main...
Can you please explain in detail what each line of code stands for in the Main method import java.util.Scanner; public class CashRegister { private static Scanner scanner = new Scanner(System.in); private static int dollarBills[] = {1, 2, 5, 10, 20, 50, 100}; private static int cents[] = {25, 10, 5, 1}; public static void main(String[] args) { double totalAmount = 0; int count = 0; for (int i = 0; i < dollarBills.length; i++) { count = getBillCount("How many $"...
write a half page clear and concise summary of the article and what you learned new...
write a half page clear and concise summary of the article and what you learned new and gleaned on the article. The compliance revolution after the passage of the Sarbanes-Oxley Act of 2002 (SOX) was accomplished in large part with the help of the internal control framework of the Committee of Sponsoring Organizations of the Treadway Commission (COSO). COSO’s framework became part of a worldwide movement to enhance periodic accounting and reporting of financial results. Coupled with the global convergence...
write the code in MATLAB with comments and show the inputs and results of the code...
write the code in MATLAB with comments and show the inputs and results of the code for the question below. Write an .m file in MATLAB, that records audio (you can record your own voice for 20 seconds that was recorded using your phone), then take Fourier transform of the signal (probably FFT).
JAVA PROGRAM: INCLUDE COMMENTS EXPLAINING THE CODE PLEASE Given the following information: 13-inch MacBook Air, 1.6GHz...
JAVA PROGRAM: INCLUDE COMMENTS EXPLAINING THE CODE PLEASE Given the following information: 13-inch MacBook Air, 1.6GHz dual-core Intel Core i5 processor, Turbo Boost up to 2.7GHz, Intel HD Graphics 6000, 8GB memory, 128GB PCIe-based flash storage 13-inch MacBook Air, 1.6GHz dual-core Intel Core i5 processor, Turbo Boost up to 2.7GHz, Intel HD Graphics 6000, 8GB memory, 256GB PCIe-based flash storage 15.6-inch Dell i3552, 1.6GHz Processor, Intel Pentium N3700, HD Laptop, 4 GB memory, DDR3L SDRAM, Windows 10, Black Drive 3...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT