SoftSys Lecture Notes, Monday 3/1/21 #
Hard Drive Operation #
Binary Flags #
Binary Flags in C #
You may remember code from Homework 2.5 like this:
regex_t re;
ret = regcomp(&re, pattern, REG_EXTENDED | REG_NOSUB);
We’re going to do an exercise to better understand what REG_EXTENDED | REG_NOSUB does here.
Do the following as an exercise:
- Write a C program that includes
regex.h. (The program doesn’t have to do anything useful.) - Use the command
gcc -H <file.c>to see whereregex.hlives on your system. - In
regex.h, what are the values ofREG_EXTENDEDandREG_NOSUB? Do you see other variables nearby that have similar values? (REG_EXTENDED == 0001) (REG_NOSUB == 0004) - What does
REG_EXTENDED | REG_NOSUBequal? Why might you want to calculate this value when callingregcomp?
0001 = 1 0100 = 4 #
0101 = 5 #
“|” -> Bitwise OR operator At a glance, you can see exactly which flags are set to True. You can check to see if a bit is set by using the bitwise AND operator “&”.
Binary Flags in Unix #
Binary flags are used for access control in Unix files.
Read the following zine by the excellent Julia Evans and then answer the question below.

- Read about setuid and setgid here: https://en.wikipedia.org/wiki/Setuid. What are the three things that setgid does for different file types?
- Read about the sticky bit at https://en.wikipedia.org/wiki/Sticky_bit. Describe one example use case in which you might want to use a sticky bit on a directory.
- What is the 4-digit octal code for a file that is (1) sticky but not setuid or setgid, (2) can be read, written, and executed by the owning user, (3) can be read and executed by the owner’s group, and (4) can be read by all other users?
- Read about chmod symbolic modes by running
man chmodor here: https://en.wikipedia.org/wiki/Chmod#Symbolic_modes. What is the symbolic command to create permission that equal the octal code above, starting from unknown permissions? - Create a file called
id.shwith the following contents:#!/usr/bin/env bash id - Make the file executable by adding executable with
chmod, and run it. What do you get? - Now change the owner to root by running
sudo chown root:root id.sh, and runchmodagain to add setuid permissions. Then run the file again. What do you get now?
I/O Performance #
One of the simplest command-line utilities is a program called yes. If you run it, you will see it simply print out y over and over again, once per line. (Use Ctrl-C to stop this if you start running it.)
We’re going to try an exercise to see how it performs.
- First, install the program
pvon your machine by runningsudo apt install pv(or the equivalent command for Mac, probablybrew install pv). - Let’s benchmark the actual version of
yesto see how it performs. Runyes | pv -r > /dev/nullto see how fastyeswrites. (Again, use Ctrl-C to stop execution.) - Now write your own version of this utility called
yes.c. Compile and run it in the same way as above to measure its speed. How does it compare? - Your implementation is probably a bit slower than the built-in
yesprogram, so let’s try to get it a bit faster. Read about thewritesystem call by runningman 2 write(on most systems). Try using this to writeyrepeatedly. - Define a constant in
yes.ccalledCOPIES(you can do this by adding a line like#define COPIES 42at the top of the file). Then modify your implementation so that you create a string containingCOPIEScopies ofy\n(whatyesprints by default), and try measuring the speed of your program again. - If you have time, try playing around with the value of
COPIESto see what kinds of values help make the performance faster or slower.