CS631 -- Advanced Programming in the UNIX Environment - HW#2

HW#2: implement 'command(3)' (in-class / time limited)

Summary

The objective of this assignment is for you apply the first lessons of Interprocess Communication from class and to write a tool that requires communication between two related processes using the pipe(2) system call.

Problem assignment

Write a function command(3) as described in this manual page. Example usage:

int
main(int argc, char **argv) {
	char out[BUFSIZ], err[BUFSIZ];
	if (command("ls -l", out, BUFSIZ, err, BUFSIZ) == -1) {
		perror("command");
		exit(EXIT_FAILURE);
	}
	printf("stdout:\n%s\n", out);
	printf("stderr:\n%s\n", err);
	return EXIT_SUCCESS;
}

As usual, your program will be well formatted, properly commented (where necessary!), be able to be compiled without any warnings using the '-Wall -Werror -Wextra' flags on a NetBSD 8.x system, not generate any diagnostic messages or superflous output, generate meaningful error messages to stderr, etc. etc.

Hints

Review the code examples from Lecture 08. Also take a look at the source for the system(3) library function, from which you may take code for this assignment.

You will need to:

  • create two pipes, one for stdout, one for stderr of the command to execute
  • ignore SIGINT, SIGQUIT; block SIGCHLD
  • fork(2) a new process
  • dup2(2) the respective file descriptors onto the write-ends of the pipes
  • exec(3) the command in question
  • read(2) from the read-ends of the pipes
  • append output to the respective buffer, careful to not overflow them
  • wait(3) for the process to terminate

Deliverables and Due Date

You will submit a single file named "$username-command.c" (where "$username" is your Stevens username).

The due date for this assignment is the end of class on 2019-10-21; if we do not have enough time to complete the assignment in class, then the submission due date will be 2019-10-28 16:00:00 EDT. However, the assignment is to be completed within an hour. That is, you may start at any time, but please complete the assignemnt within an hour and then submit your work, regardless of whether you've completed the assignment or not.

Please attach the file to an email sent from your @stevens.edu email address to jschauma@stevens.edu with a subject of "[CS631] HW2".


[Course Website]