Principles of Computer Systems
Winter 2021
Stanford University
Computer Science Department
Instructors: Chris Gregg and
Nick Troccoli
Creating processes and running other programs
Inter-process communication
Signals
Race Conditions
fork()
pid_t pidOrZero = fork();
// both parent and child run code here onwards
printf("This is printed by two processes.\n");
waitpid()
A function that a parent can call to wait for its child to exit:
pid_t waitpid(pid_t pid, int *status, int options);
The function returns when the specified child process exits.
execvp
is a function that lets us run another program in the current process.
int execvp(const char *path, char *argv[]);
execvp()
It runs the executable at the specified path, completely cannibalizing the current process.
To run another executable, we must specify the (NULL-terminated) arguments to be passed into its main function, via the argv parameter.
execvp
has many variants (execle
, execlp
, and so forth. Type man
execvp
for more). We rely on execvp
in CS110.
mysystem
mysystem is our own version of the built-in function system.
static int mysystem(char *command) {
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
char *arguments[] = {"/bin/sh", "-c", command, NULL};
execvp(arguments[0], arguments);
// If the child gets here, there was an error
exitIf(true, kExecFailed, stderr, "execvp failed to invoke this: %s.\n", command);
}
// If we are the parent, wait for the child
int status;
waitpid(pidOrZero, &status, 0);
return WIFEXITED(status) ? WEXITSTATUS(status) : -WTERMSIG(status);
}
mysystem
first-shell-soln.c
first-shell
int main(int argc, char *argv[]) {
char command[kMaxLineLength];
while (true) {
printf("> ");
fgets(command, sizeof(command), stdin);
// If the user entered Ctl-d, stop
if (feof(stdin)) {
break;
}
// Remove the \n that fgets puts at the end
command[strlen(command) - 1] = '\0';
int commandReturnCode = mysystem(command);
printf("return code = %d\n", commandReturnCode);
}
printf("\n");
return 0;
}
Our first-shell program is a loop in main that parses the user input and passes it to mysystem.
first-shell-soln.c
first-shell
Takeaways
fork
, execvp
, and waitpid
to do this!stsh
("Stanford shell") with much of the functionality of real Unix shells.Shells have a variety of supported commands:
Let's make an updated version of mysystem called executeCommand.
static void executeCommand(char *command, bool inBackground) {
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// If we are the child, execute the shell command
char *arguments[] = {"/bin/sh", "-c", command, NULL};
execvp(arguments[0], arguments);
// If the child gets here, there was an error
exitIf(true, kExecFailed, stderr, "execvp failed to invoke this: %s.\n", command);
}
// If we are the parent, either wait or return immediately
if (inBackground) {
printf("%d %s\n", pidOrZero, command);
} else {
waitpid(pidOrZero, NULL, 0);
}
}
second-shell-start.c
static void executeCommand(char *command, bool inBackground) {
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// If we are the child, execute the shell command
char *arguments[] = {"/bin/sh", "-c", command, NULL};
execvp(arguments[0], arguments);
// If the child gets here, there was an error
exitIf(true, kExecFailed, stderr, "execvp failed to invoke this: %s.\n", command);
}
// If we are the parent, either wait or return immediately
if (inBackground) {
printf("%d %s\n", pidOrZero, command);
} else {
waitpid(pidOrZero, NULL, 0);
}
}
Line 1: Now, the caller can optionally run the command in the background.
second-shell-start.c
static void executeCommand(char *command, bool inBackground) {
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// If we are the child, execute the shell command
char *arguments[] = {"/bin/sh", "-c", command, NULL};
execvp(arguments[0], arguments);
// If the child gets here, there was an error
exitIf(true, kExecFailed, stderr, "execvp failed to invoke this: %s.\n", command);
}
// If we are the parent, either wait or return immediately
if (inBackground) {
printf("%d %s\n", pidOrZero, command);
} else {
waitpid(pidOrZero, NULL, 0);
}
}
Lines 11-16: The parent waits on a foreground child, but not a background child.
second-shell-start.c
int main(int argc, char *argv[]) {
char command[kMaxLineLength];
while (true) {
printf("> ");
fgets(command, sizeof(command), stdin);
// If the user entered Ctl-d, stop
if (feof(stdin)) {
break;
}
// Remove the \n that fgets puts at the end
command[strlen(command) - 1] = '\0';
if (strcmp(command, "quit") == 0) break;
bool isbg = command[strlen(command) - 1] == '&';
if (isbg) {
command[strlen(command) - 1] = '\0';
}
executeCommand(command, isbg);
}
printf("\n");
return 0;
}
In main, we must add two additional things:
Note that a background child isn't reaped! This is a problem - one we'll learn how to fix soon.
second-shell-start.c
The pipe system call populates the 2-element array fds with two file descriptors such that everything written to fds[1]
can be read from fds[0]
. Returns 0 on success, or -1 on error.
int pipe(int fds[]);
pipe()
The pipe system call populates the 2-element array fds with two file descriptors such that everything written to fds[1]
can be read from fds[0]
. Returns 0 on success, or -1 on error.
int pipe(int fds[]);
pipe()
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
int result = pipe(fds);
// Write message to pipe (assuming here all bytes written immediately)
write(fds[1], kPipeMessage, strlen(kPipeMessage) + 1);
close(fds[1]);
// Read message from pipe
char receivedMessage[strlen(kPipeMessage) + 1];
read(fds[0], receivedMessage, sizeof(receivedMessage));
close(fds[0]);
printf("Message read: %s\n", receivedMessage);
return 0;
}
pipe-demo.c
Tip: you learn to read before you learn to write (read = fds[0], write = fds[1]).
The pipe system call populates the 2-element array fds with two file descriptors such that everything written to fds[1]
can be read from fds[0]
. Returns 0 on success, or -1 on error.
int pipe(int fds[]);
pipe()
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
int result = pipe(fds);
// Write message to pipe (assuming here all bytes written immediately)
write(fds[1], kPipeMessage, strlen(kPipeMessage) + 1);
close(fds[1]);
// Read message from pipe
char receivedMessage[strlen(kPipeMessage) + 1];
read(fds[0], receivedMessage, sizeof(receivedMessage));
close(fds[0]);
printf("Message read: %s\n", receivedMessage);
return 0;
}
$ ./pipe-demo
Message read: Hello, this message is coming through a pipe.
pipe-demo.c
Tip: you learn to read before you learn to write (read = fds[0], write = fds[1]).
pipe
can allow processes to communicate!
int pipe(int fds[]);
The pipe system call populates the 2-element array fds with two file descriptors such that everything written to fds[1]
can be read from fds[0]
. Returns 0 on success, or -1 on error.
pipe()
Here's an example program showing how pipe works across processes (full program link at bottom).
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
pipe()
Make a pipe just like before.
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
pipe()
The parent must close all its open FDs. It never uses the Read FD so we can close it here.
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
pipe()
Write to the Write FD to send a message to the child.
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
pipe()
We are now done with the Write FD so we can close it here.
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
pipe()
We wait for the child to terminate.
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
pipe()
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
Key Idea: when we call fork, the child gets a copy of the parent's file descriptor table. Any open FDs in the parent at the time fork is called must be closed in both the parent and the child.
pipe()
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
More specifically, this duplication means the child's file descriptor table entries point to the same open file table entries as the parent. Thus, the open file table entries for the two pipe FDs both have reference counts of 2.
pipe()
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
The child must close all its open FDs. It never uses the Write FD so we can close it here.
pipe()
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
Read from the Read FD to read the message from the parent.
pipe()
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
We are now done with the Read FD so we can close it here. Also print the received message.
pipe()
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
Key Idea: the child gets a copy of the parent's file descriptor table. Any open FDs in the parent at the time fork is called must be closed in both the parent and the child.
Here, right before the fork call, the parent has 2 open file descriptors (besides 0-2): the pipe Read FD and Write FD.
pipe()
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
Key Idea: the child gets a copy of the parent's file descriptor table. Any open FDs in the parent at the time fork is called must be closed in both the parent and the child.
Therefore, when the child is spawned, it also has the same 2 open file descriptors (besides 0-2): the pipe Read FD and Write FD.
pipe()
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
Key Idea: the child gets a copy of the parent's file descriptor table. Any open FDs in the parent at the time fork is called must be closed in both the parent and the child.
We should close FDs when we are done with them. The parent closes them here.
pipe()
static const char * kPipeMessage = "Hello, this message is coming through a pipe.";
int main(int argc, char *argv[]) {
int fds[2];
pipe(fds);
size_t bytesSent = strlen(kPipeMessage) + 1;
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// In the child, we only read from the pipe
close(fds[1]);
char buffer[bytesSent];
read(fds[0], buffer, sizeof(buffer));
close(fds[0]);
printf("Message from parent: %s\n", buffer);
return 0;
}
// In the parent, we only write to the pipe (assume everything is written)
close(fds[0]);
write(fds[1], kPipeMessage, bytesSent);
close(fds[1]);
waitpid(pidOrZero, NULL, 0);
return 0;
}
Key Idea: the child gets a copy of the parent's file descriptor table. Any open FDs in the parent at the time fork is called must be closed in both the parent and the child.
We should close FDs when we are done with them. The child closes them here.
pipe()
This method of communication between processes relies on the fact that file descriptors are duplicated when forking.
This is the core idea behind how a shell can support piping between processes
(e.g. cat file.txt | uniq | sort). Let's see how this works in a shell.
Idea: what happens if we change FD 1 to point somewhere else?
0 | 1 | 2 | 3 |
---|
Terminal
File
0 | 1 | 2 |
---|
Terminal
int main() {
printf("This will print to the terminal\n");
close(STDOUT_FILENO);
// fd will always be 1
int fd = open("myfile.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
printf("This will print to myfile.txt!\n");
close(fd);
return 0;
}
Idea: what happens if we change FD 1 to point somewhere else?
0 | 1 | 2 |
---|
Terminal
int main() {
printf("This will print to the terminal\n");
close(STDOUT_FILENO);
// fd will always be 1
int fd = open("myfile.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
printf("This will print to myfile.txt!\n");
close(fd);
return 0;
}
Idea: what happens if we change FD 1 to point somewhere else?
0 | 1 | 2 |
---|
Terminal
myfile.txt
int main() {
printf("This will print to the terminal\n");
close(STDOUT_FILENO);
// fd will always be 1
int fd = open("myfile.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
printf("This will print to myfile.txt!\n");
close(fd);
return 0;
}
Idea: what happens if we change FD 1 to point somewhere else?
0 | 1 | 2 |
---|
Terminal
myfile.txt
int main() {
printf("This will print to the terminal\n");
close(STDOUT_FILENO);
// fd will always be 1
int fd = open("myfile.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);
printf("This will print to myfile.txt!\n");
close(fd);
return 0;
}
Idea: what happens if we change FD 1 to point somewhere else?
0 | 1 | 2 |
---|
Terminal
Idea: what happens if we change a special FD to point somewhere else?
Could we do this with a pipe?
0 | 1 | 2 |
---|
pipe READ
Process 1
Process 2
pipe WRITE
Why would this be useful?
I/O redirection and pipes allow us to handle piping in our shell: e.g. cat file.txt | sort
0 | 1 | 2 |
---|
Terminal
0 | 1 | 2 |
---|
pipe READ
cat
sort
pipe WRITE
cat
sort
uniq
terminal in
terminal out
pipe1
pipe2
Process | stdin | stdout |
---|---|---|
cat | terminal | pipe1[1] |
sort | pipe1[0] | pipe2[1] |
uniq | pipe2[0] | terminal |
int pipe1[2]; int pipe2[2]; pipe(pipe1); pipe(pipe2);
I/O redirection and pipes allow us to handle piping in our shell: e.g. cat file.txt | sort | uniq
One last issue; how do we "connect" our pipe FDs to STDIN/STDOUT?
dup2 makes a copy of a file descriptor entry and puts it in another file descriptor index. If the second parameter is an already-open file descriptor, it is closed before being used.
int dup2(int oldfd, int newfd);
Example: we can use dup2 to copy the pipe read file descriptor into standard input!
dup2(fds[0], STDIN_FILENO);
Second key detail: execvp consumes the process, except for the file descriptor table!
To practice this piping technique, let's implement a custom function called subprocess.
subprocess_t subprocess(char *command);
subprocess is the same as mysystem, except it also sets up a pipe we can use to write to the child process's STDIN.
It returns a struct containing:
Next time: introducing signals
The program below takes an arbitrary number of filenames as arguments and attempts to publish the date and time. The desired behavior is shown at right:
static void publish(const char *name) {
printf("Publishing date and time to file named \"%s\".\n", name);
int outfile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
dup2(outfile, STDOUT_FILENO);
close(outfile);
if (fork() > 0) return;
char *argv[] = { "date", NULL };
execvp(argv[0], argv);
}
int main(int argc, char *argv[]) {
for (size_t i = 1; i < argc; i++) publish(argv[i]);
return 0;
}
publish.c
myth62:~$ ./publish one two three four
Publishing date and time to file named "one".
Publishing date and time to file named "two".
Publishing date and time to file named "three".
Publishing date and time to file named "four".
However, the program is buggy!
Because the child processes (and only the child processes) should be redirecting, we should open, dup2, and close in child-specific code. A happy side effect of the change is that we never muck with STDOUT_FILENO in the parent if we confine the redirection code to the child. Solution:
static void publish(const char *name) {
printf("Publishing date and time to file named \"%s\".\n", name);
if (fork() > 0) return;
int outfile = open(name, O_WRONLY | O_CREAT | O_TRUNC, 0644);
dup2(outfile, STDOUT_FILENO);
close(outfile);
char *argv[] = { "date", NULL };
execvp(argv[0], argv);
}
publish.c
Let's implement a custom function called captureProcess, like subprocess except instead of setting up a pipe to write to the child's STDIN, it's a pipe to read from its STDOUT.
subprocess_t captureProcess(char *command);
It returns a struct containing:
Let's implement a custom function called captureProcess, like subprocess except instead of setting up a pipe to write to the child's STDIN, it's a pipe to read from its STDOUT.
subprocess_t captureProcess(char *command) {
int fds[2];
pipe(fds);
pid_t pidOrZero = fork();
if (pidOrZero == 0) {
// We are not reading from the pipe, only writing to it
close(fds[0]);
// Duplicate the write end of the pipe into STDOUT
dup2(fds[1], STDOUT_FILENO);
close(fds[1]);
char *arguments[] = {"/bin/sh", "-c", command, NULL};
execvp(arguments[0], arguments);
exitIf(true, kExecFailed, stderr, "execvp failed to invoke this: %s.\n", command);
}
close(fds[1]);
return (subprocess_t) { pidOrZero, fds[0] };
}
captureProcess.c