COMP3000 Operating Systems 2023F Tutorials: Tutorial 1
This first tutorial will prepare you for subsequent tutorials. You will familiarize yourself with the OS environment and, in particular, learn the basics of command-line interaction in Linux (Ubuntu).
Getting Started
The concepts covered below are mostly part of a standard tutorial on UNIX/Linux. However, remember that you are trying to build a conceptual model of how things work. Thus, don't memorize commands; instead, try to understand how things fit together, learn how to find out the answer with that model, and ask questions when things don't work as expected!
Making use of Man pages
The man
(short for manual) command is a way to access the built-in software documentation system, whose entries come with the system and software packages. For example, if you look at files in an installed software package (e.g., dpkg -L ftp
to list files in the ftp package) you can see there are a few files such as /usr/share/man/man1, 5, …
They are the man page entries (sections).
The topics go beyond just software/command manuals. They also include conventions and abstract concepts (e.g., man syscalls
and man man-pages
) that can serve as an info center. There are multiple sections for different purposes, which can be specified as the first argument, e.g., 1 for general commands, 2 for system calls, 3 for library functions, etc. For instance, tee is both a command (man 1 tee
) and a system call (man 2 tee
). A default will be used if the section is not specified.
For any commands mentioned in the tutorials, you can use man
to find the usage. The advantage is that the man page documentation provides an exact match to your current environment, compared to searching the Internet.
Compared to the summary and brief information provided by the man
command, you can also use the info
command for a more detailed explanation for certain topics (just give it a try).
Background
The Shell
The shell, also called "command line" or more commonly "terminal", provides a text interface for running programs. While not as visually pleasing as a graphical interface, the shell provides a clearer representation of the functionality provided by the OS.
It works as an interpreter to the commands the user enters, i.e., taking commands and sending them to the OS for execution. Typical shells include bash
(the default we are using in Ubuntu), ksh
, zsh
, csh
, etc. You will learn how to change the current shell in Tutorial 4. Most modern shells also allow for the creation of a shell script, which is a sequence of commands (programmed in a scripting language). You will see shell scripts as we navigate this course.
File Permissions
Each file on the system (including directories and other special files) has an owner (user), and belongs to a group.
File permissions in Linux are: read, write, and execute, denoted rwx
. Each file has three sets of rwx
permissions. The first set denotes the type of access allowed to the owner of the file (rwxrwxrwx). The second set of permissions denotes the allowed file operations for a group of users (rwxrwxrwx). The third set of permissions denotes the allowed file operations for everyone else (rwxrwxrwx). A file is always owned by someone and is always associated with a group.
Whether or not you will be allowed to access (read from, write to, or execute) a file in Linux/Unix is determined by who (i.e., which user) you are logged in as, and the "rwxrwxrwx" permissions on that file.
Environment & Shell Variables
Environment variables on both Linux and Windows are variable-value pairs that define important context-related information (such as the name of the current user, the current language, the timezone) for applications. The key advantage of environment variables is that they are available right when a program starts - they are given to it by the operating system.
In Linux, these environment variables can be printed on the command line in most shells by referring to the variable name prefixed with a $
sign (e.g., to output the value in the HELLO environment variable, one could type the command echo $HELLO
).
If a variable is private to the shell, it is called a shell variable. In bash, by default all variables are first shell variables. A shell variable becomes an environment variable, if you "export" it. For example:
X="Important Data"
just defines X as a shell variable. However, if you then type:
export X
X will be turned into an environment variable (no longer private), and so every subsequent program run by the shell will also get X. You can combine both in one line:
export X="Important Data"
This is the idiom for setting environment variables normally. You will be able to better understand the differences between shell variables and environment variables in later tutorials and assignments. Typically, you can access shell and environment variables using the same mechanism (the $
sign).
One thing to remember with the above is that spaces are used to separate arguments in bash and most other UNIX shells. Thus, it is an error to type:
export X = "Important Data"
as you now are giving export three arguments, not one.
To delete an environment variable, you can use the command unset
. For example:
unset X
Downloading files using commands
When working with the command line and you need to download a small number of files from a website, you can choose one of the two below:
curl -o hello.c https://people.scs.carleton.ca/~abdou/comp3000/f23/tut1/hello.c wget https://people.scs.carleton.ca/~abdou/comp3000/f23/tut1/hello.c
Use man curl
and man wget
to find out more about these commands, if you are interested.
In later tutorials, this will be useful as you need source files to do your work, for which the URL will be just a hyperlink on this wiki page.
Compiling & Running Programs
To compile a program, use gcc:
gcc -O2 hello.c -o hello
This compiles it with level 2 optimization and without debugging symbols, and the output program is hello.
To run, you have to specify where it is (the single dot . represents your current directory):
./hello
Remember you can always change directories using the cd
command.
Tasks/Questions
- When you have first logged in to a shell, how (i.e., using what commands?) can you find out information about the environment? Consider the following as examples. Try to find out mutiple ways for each (if you can).
- The version of your Linux distribution (in our case, Ubuntu) and the version of your Linux kernel.
- RAM (remaining/total), disk space (remaining/total), and CPU.
- The name (binary path) of the current shell.
- Using the man command, find out what the following commands do: which, pwd, who, whoami, whereis, and whatis.
- Linux commands can be classified as internal (built into the shell) and external (separate program binaries). How can you tell if a specific command (e.g., cd) is internal or external?
- For the program you just compiled above, what if you used hello to run it instead of ./hello (and why)?
- Figure out where at least three external commands reside on the system.
- Making your own commands: the PATH environment variable lists the directories the shell uses to search for external commands. Where can you find documentation on it? How can you add the current directory (whichever directory you are currently in) to PATH? Then, how to make that change permanent? Try to identify multiple ways. You can now revisit the question about "hello" above.
- Look at the permissions of the program binaries of the external commands you have just found above. Who owns them? What group are they in?
- For those same program binaries, figure out what the permission bits mean by reading the man page of chmod (this is the command you could use to change those permission bits).
- What are the owner, group, and permissions of /etc/passwd and /etc/shadow? What are these files used for (and how did you find this out)?
- What does it mean to have execute permission on a directory?
- The ls command can be used to get a listing of the files in a directory. What options are passed to ls to see: the permission bits above; all the files within a directory (including hidden files)? How to make a file hidden?
- Compile and run csimpleshell.c. How does its functionality compare to that of bash? List at least 3 differences.