Everyone:

This week, we will review system calls, which are the interfaces through which user applications can request services from the operating system kernel. Instead of manipulating data directly on a hard drive or directly controlling a CPU, we use the abstractions of a file and process to perform our desired actions. The OS virtualizes the physical resources of a hard drive and a CPU and allows us to access these virtual objects through system calls such as open and fork.

These abstractions make it easier for us to program large applications by hiding the low-level details of interacting with hardware and by allowing our programs to be portable to different machines. Of course, with any layer of abstraction there is some trade-off, and this week we will examine how operating systems virtualize the CPU via processes and how the operating system provides time sharing, different forms of multitasking, and what happens during a context switch.

TL;DR

For this reading assignment, you are to review basic system calls related to processes, submit your responses to the Reading 02 Quiz, and write a program that uses basic file system calls.

Reading

The readings for this week are:

  1. Operating Systems: Three Easy Pieces

    1. Dialogue
    2. Processes
    3. Process API
    4. Direct Execution

Optional References

  1. Beej's Guide to Unix IPC

    1. A fork() Primer
    2. Signals

  2. System Programming Wiki

    1. Processes
    2. Signals

Quiz

Once you have done the readings, answer the following Reading 02 Quiz questions:

Program

For this week's program, you are to modify the program.c to implement a basic version of the sha1sum utility:

Usage: program [file1 file2 ...]

For each argument given to program, compute the SHA1 digest of the contents of each file argument and display it.

The purpose of this exercise is for you to review using basic I/O system calls such as open, read, and close. If you need a refresher, please read Files and Directories from the course textbook.

Example

$ ./program Makefile  # Compute SHA1 of Makefile
50bb7f7ccf1ca089f3c5eaff5fe95e56ddbe53a5  Makefile

$ echo $?             # Check exit status
0

$ ./program asdf      # Handle invalid files

$ echo $?             # Check exit status
1

Requirements

Hints

To help you get started, the instructor suggests you review the suggestions from Generate SHA hash in C++ using OpenSSL library StackOverflow post and what you did last semester in Homework 08 for the hash_from_file. Note, we are interested in computing the SHA1 digest.

OpenSSL Deprecation

Note that in OpenSSL 3.0, the SHA1_Init, SHA1_Update, and SHA1_Final functions are deprecated and you should instead use the SHA1 function to compute the raw SHA1 digest of a buffer of bytes.

You will want your code to be structured in the following manner:

function sha1sum_file(path, cksum):
    Open file for reading

    Stat file to get meta-data (ie. file length)

    Allocate data buffer with enough space for length of file

    Read data from file into data buffer

    Compute raw SHA1 digest of data in buffer

    Convert raw SHA1 digest into hexadecimal digest

    Close file

    Deallocate data buffer

function main(arguments):
  For each argument:
      Compute hexadecimal SHA1 Digest using sha1sum_file
      Display hexadecimal SHA1 Digest

Note: To link properly to the SHA1 function, you will need to add -lcrypto to the LIBS variable in your Makefile.

Note: You will want to return 0 if all arguments were able to be processed successfully. Otherwise, return the number of files that failed as the exit status.

Submission

To submit you work, follow the same process outlined in Reading 01:

$ git checkout master                 # Make sure we are in master branch

$ git pull --rebase                   # Make sure we are up-to-date with GitHub

$ git checkout -b reading02           # Create reading02 branch and check it out

$ cd reading02                        # Go into reading02 folder
$ $EDITOR answers.json                # Edit your answers.json file

$ ../.scripts/check.py                # Check reading02 quiz
Checking reading02 quiz ...
      Q1 0.50
      Q2 0.50
      Q3 0.20
      Q4 0.30
      Q5 0.70
      Q6 0.50
      Q7 0.30
   Score 3.00 / 3.00
  Status Success

$ git add answers.json                # Add answers.json to staging area
$ git commit -m "Reading 02: Quiz"    # Commit work

$ $EDITOR program.c                   # Edit your program.c file

$ make test-program                   # Check reading02 program
Testing reading02 program...
 I/O System Calls                                             ... Success
 I/O Functions                                                ... Success
 Memory Functions                                             ... Success
 SHA1 Functions                                               ... Success
 program                                                      ... Success
 program (valgrind)                                           ... Success
 program Makefile                                             ... Success
 program Makefile (valgrind)                                  ... Success
 program Makefile README.md                                   ... Success
 program Makefile README.md (valgrind)                        ... Success
 program Makefile README.md program.c                         ... Success
 program Makefile README.md program.c (valgrind)              ... Success
 program Makefile README.md program.c asdf                    ... Success
 program Makefile README.md program.c asdf (valgrind)         ... Success
 program Makefile README.md /bin/ls /bin/bash                 ... Success
 program Makefile README.md /bin/ls /bin/bash (valgrind)      ... Success
   Score 3.00 / 3.00
  Status Success

$ git add program.c                   # Add program.c to staging area
$ git add Makefile                    # Add Makefile to staging area
$ git commit -m "Reading 02: Code"    # Commit work

$ git push -u origin reading02        # Push branch to GitHub

Pull Request

Once you have committed your work and pushed it to GitHub, remember to create a pull request and assign it to the appropriate teaching assistant from the Reading 02 TA List.