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
open
for 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 open
Section 2; call syntax;
O_READ, O_WRITE, O_APPEND
O_APPEND
warning
-
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=value
for example,PRINTER=dept-rns202
These strings are treated as "environment variables", in which a variableNAME
is 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
lpr
in a shell; the newlpr
process is programmed to check for an environment variablePRINTER
, and will use the value ofPRINTER
as 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
% printenv
typically prints all the environment strings for that shell process.In the
csh
andtcsh
shells, the command% setenv X val
sets the environment variable namedX
to have the valueval
.Environment variables are assigned differently in the
bash
,sh
, andksh
shells:$ export X $ set X=val
(note that these shells typically use$
for a prompt character). Theexport
command only needs to be entered once per shell, and declares thatset
commands involvingX
should 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.c
in 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 c
Header 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.c
uses system callsopen(), read(), write(),
andexit()
in order to manipulate a given file interactively and quit the program.mopen.c
is 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 -r
Before using a system call in your code, consult the manual page (perhaps by googling
man syscall
, e.g.,