Operating Systems 2021F: Assignment 2

From Soma-notes
Revision as of 10:03, 14 October 2021 by Soma (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Please submit the answers to the following questions via Brightspace by October 14, 2021 by 10 AM. There are 20 points in 9 questions.

Submit your answers as a plain text file following this template. Name your answer file "<username>-comp3000-assign2.txt" (where username is your MyCarletonOne username).

Your answers will be parsed by a script in order to help with grading so please preserve the format of the template. Make sure the file remains a plain text file! No other formats will be accepted.

Don't forget to include what outside resources you used to complete each of your answers, including other students, man pages, and web resources. You do not need to list help from the instructor, TA, or information found in the textbook.

Getting Started

First, download and run countdown.sh. You can make it executable after downloading by running "chmod a+x countdown.sh". After this, it should run like any other executable.

Next, download and compile 3000capture.c as usual. Try the following to get an idea of how 3000capture and countdown.sh work:


 ./3000capture /bin/ls /dev/null -la
 cat 3000capture.log

 cat > bc-input.txt
 ^D   # type ctrl-D to end the file
 ./3000capture /usr/bin/bc bc-input.txt -l
 cat 3000capture.log

 ./3000capture ./countdown.sh /dev/null
 cat 3000capture.log


  1. [2] If we read from /dev/null, what data do we get? How can you verify this?
  2. [2] 3000capture requires commands to be fully qualified pathnames. How can you modify 3000capture so that if a command doesn't start with a /, 3000capture searches the current PATH to find the right program? If PATH isn't defined it should search a default set of directories.
  3. [2] What is the purpose of the fflush() call in parent() at line 36? Why isn't it needed elsewhere in parent()?
  4. [2] How could you fix 3000capture so it sends its output to the file specified by the CAPTURE environment variable? If there is no CAPTURE environment variable, it should use the same default as it does currently.
  5. [2] How many processes does countdown.sh create, and how many executables does it run? How can you verify this using bpftrace?
  6. [2] What is child_exited used for? Why is it needed?
  7. [2] How can you create a shell script that sends a SIGTERM signal to whatever process that ran it (its parent)? What happens when you run 3000capture on it?
  8. [2] How can you verify that the shell script from the previous question is actually sending the right signal using bpftrace?
  9. [4] How could you change 3000capture so that when it receives a SIGUSR1 signal it
    • Prints "SIGUSR1 received, shutting down."
    • Sends a SIGTERM signal to its child
    • Waits at least one second
    • If the child has terminated, it reports how the child has terminated, whether from a signal or whether it returned normally.
    • If the child has not terminated, it reports "Child still running, aborting." and exits with a return value of -10.



/* 3000capture.c */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <signal.h>

int result_pid = -1;
int result_status = 0;
int child_exited = 0;

void signal_handler(int the_signal)
        if (the_signal == SIGCHLD) {
                result_pid = wait(&result_status);
                if (result_pid == -1) {
                child_exited = 1;

int parent(pid_t child_pid, char *command)
        int count = 0;
        const int bufsize = 256;
        char buf[bufsize];


        while (!child_exited) {
                write(1, ".", 1);

        if (result_pid == child_pid) {
                count += snprintf(buf + count, bufsize,
                                  "Child %d terminated", child_pid);
                if (WIFEXITED(result_status)) {
                        count += snprintf(buf + count, bufsize,
                                          " normally with status %d",
                } else if (WIFSIGNALED(result_status)) {
                        count += snprintf(buf + count, bufsize,
                                          " with signal %d",
                return 0;
        } else if (result_pid == -1) {
                printf("No child to wait for.\n");
                return -1;
        } else {
                printf("wait returned unknown value %d\n", result_pid);
                return result_pid;

int child(char *command, char *input_fn, char *output_fn, char *argv[])
        extern char **environ;
        int fd;

        fd = creat(output_fn, 0644);
        if (fd == -1) {
                fprintf(stderr, "Could not create %s for output.\n",
        dup2(fd, 1);

        fd = open(input_fn, O_RDONLY);
        if (fd == -1) {
                fprintf(stderr, "Could not open %s for input.\n",
        dup2(fd, 0);
        execve(command, argv, environ);
        fprintf(stderr, "execve of %s failed.\n", command);

int main(int argc, char *argv[], char *envp[])
        char *command;
        char *input_fn;
        char *output_fn = "3000capture.log";
        pid_t pid;
        struct sigaction signal_handler_struct;
        memset(&signal_handler_struct, 0, sizeof(signal_handler_struct));
        signal_handler_struct.sa_handler = signal_handler;
        if (sigaction(SIGCHLD, &signal_handler_struct, NULL)) {
                fprintf(stderr, "Couldn't register SIGCHLD handler.\n");

        if (argc < 3) {
                        "Usage: %s <command> <input file> [command args]\n"
                        "By default saves data to %s; set CAPTURE to change.\n",
                        argv[0], output_fn);
        command = argv[1];
        input_fn = argv[2];

        pid = fork();

        if (pid == -1) {
                fprintf(stderr, "Fork failed.\n");
        } else if (pid == 0) {
                argv[2] = command;
                return child(command, input_fn, output_fn, argv + 2);
        } else {
                return parent(pid, command);



for i in `seq 10 -1 1`; do
    echo ">>>>  $i  <<<<"
    sleep 1

echo "Blastoff!"


Assignment 2 solutions