Operating Systems
CS 273 (OS), Fall 2020
./ executables
Homework assignment HW1 - submit by "midnight" (anywhere on earth)
Questions on homework? Piazza?
Reading questions?
Chapter 1
Chapter 10
Selected Linux system calls
Some basic file system calls (highlights)
open(), read(), write(), close()Can
openfor read-only, write-only, read or writeOS creates an internal data structure for each open file
User and System parts of a processfile descriptor (relationship to open-file data structure)
lseek()- change current position in file~rab/os/egs/mopen filename [-r|-w|-rw] ...man openSection 2; call syntax;
O_READ, O_WRITE, O_APPENDO_APPENDwarning
-
How to call a system call -
read(), write(); try_open()
File properties
mode bits; protection domains; umask
Directory operations
(hard) links; symbolic links; unlinking; default directory
The environment
As part of process management in Linux, every process possesses an array of strings known as the environment for that process. Each string in the environment has the form
NAME=valuefor example,PRINTER=dept-rns202These strings are treated as "environment variables", in which a variableNAMEis thought of as having the valuevalue.When a process is started using a (running) shell program, that process automatically inherits that shell's environment. This enables that shell to pass information to the new process. For example, you might execute the program
lprin a shell; the newlprprocess is programmed to check for an environment variablePRINTER, and will use the value ofPRINTERas the default destination printer if that environment variable exists.In general, whenever any given process (not necessarily a shell) starts up another new process in Linux, that new process inherits that given process's environment.
A shell command
% printenvtypically prints all the environment strings for that shell process.In the
cshandtcshshells, the command% setenv X valsets the environment variable namedXto have the valueval.Environment variables are assigned differently in the
bash,sh, andkshshells:$ export X $ set X=val(note that these shells typically use$for a prompt character). Theexportcommand only needs to be entered once per shell, and declares thatsetcommands involvingXshould change the environment.In a C or C++ program, the optional third argument of
Shell command:main()becomes automatically filled with the address of the environment array when that third argument is included. Seereverse.cin Lab 1 for an example of including that third argument formain(). The third argument has typechar **, since it holds an array of pointers to (null-terminated arrays of) characters. That array of pointers can have one or more elements; the final element holds a null-pointer, namely the value(char *) 0, which enables a program to detect the end of its environment.% reverse a b cHeader formain()in codereverse.c:int main(int argc, char **argv, char **envp)Memory diagram:
Programming with system calls (mopen.c)
The program
~cs273/egs/mopen.cuses system callsopen(), read(), write(),andexit()in order to manipulate a given file interactively and quit the program.mopen.cis also a model for how to use system calls in a C program.Demo of
egs/mopen
Example:~cs273/egs/mopen try.txt -r -w -rBefore using a system call in your code, consult the manual page (perhaps by googling
man syscall, e.g.,