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

HW#2: implement 'command(3)'


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:

main() {
	char out[BUFSIZ], err[BUFSIZ];
	if (command("ls -l", out, BUFSIZ-1, err, BUFSIZ-1) == -1) {
	out[BUFSIZ-1] = '\0';
	err[BUFSIZ-1] = '\0';
	printf("stdout:\n%s\n", out);
	printf("stderr:\n%s\n", err);

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 9.x system, not generate any diagnostic messages or superflous output, generate meaningful error messages to stderr, etc. etc.


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). That file will contain the above main function as provided to you as well as your implementatin of the command function.

The due date for this assignment is 16:00:00 Eastern time on 2022-11-07. Note that for this assignment there is no resubmission.

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]