______ Operating Systems (CS 273 (OS), Fall 2020)
Home
>>     < >




Operating Systems

CS 273 (OS), Fall 2020

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 write

      OS creates an internal data structure for each open file
      User and System parts of a process

    • file 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

    • Source code mopen.c

      • How to call a system call - read(), write(); try_open()

    • Programming with system calls (mopen.c)

  • 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 variable NAME is thought of as having the value value.

  • 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 new lpr process is programmed to check for an environment variable PRINTER, and will use the value of PRINTER 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 and tcsh shells, the command

        % setenv X val
    
    sets the environment variable named X to have the value val.

    Environment variables are assigned differently in the bash, sh, and ksh shells:

        $ export X
        $ set X=val 
    
    (note that these shells typically use $ for a prompt character). The export command only needs to be entered once per shell, and declares that set commands involving X should change the environment.

  • In a C or C++ program, the optional third argument of main() becomes automatically filled with the address of the environment array when that third argument is included. See reverse.c in Lab 1 for an example of including that third argument for main(). The third argument has type char **, 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.

    Shell command:
        % reverse a b c
    
    Header for main() in code reverse.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 calls open(), read(), write(), and exit() 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.,