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.
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)...
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...
Can you add more comments explaining what this code does? i commented what I know so...
Can you add more comments explaining what this code does? i commented what I know so far #include<stdio.h> #include<pthread.h> #include<semaphore.h> #include<unistd.h> sem_t mutex,writeblock; int data = 0,rcount = 0; int sleepLength = 2; // used to represent work void *reader(void *arg) { int f; f = ((int)arg); sem_wait(&mutex); // decrement by 1 if rcount = rcount + 1; if(rcount==1) sem_wait(&writeblock); sem_post(&mutex); printf("Data read by the reader%d is %d\n",f,data); //shows current reader and data sleep(sleepLength); // 1 second of "work" is...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT