29
Systems Programming/ C and UNIX Alice E. Fischer February 12, 2019 Alice E. Fischer Systems Programming – Lecture 2. . . 1/29 February 12, 2019 1 / 29

Systems Programming/ C and UNIX

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Systems Programming/ C and UNIX

Systems Programming/ C and UNIX

Alice E. Fischer

February 12, 2019

Alice E. Fischer Systems Programming – Lecture 2. . . 1/29 February 12, 2019 1 / 29

Page 2: Systems Programming/ C and UNIX

Outline

1 Booting into Linux

2 The Command Shell

3 Defining a Command Language

Alice E. Fischer Systems Programming – Lecture 2. . . 2/29 February 12, 2019 2 / 29

Page 3: Systems Programming/ C and UNIX

Booting into Linux

Booting into Linux

The BIOSThe bootdisk

The boot sectorThe boot loader

The kernelRun systemd

Install system servicesOpen a desktop or shell

https://opensource.com/article/17/2/linux-boot-and-startup

Alice E. Fischer Systems Programming – Lecture 2. . . 3/29 February 12, 2019 3 / 29

Page 4: Systems Programming/ C and UNIX

Booting into Linux

Start in the BIOS: Find and read the boot sector.

The Basic Input/Output System is stored in the compute’s firmware.

Run the POST (Power On Self Test) to ensure that the hardwarecomponents have been initialized correctly.

The order in which to search for devices is controlled by aconfiguration file.

Locate the first attached device that has a boot sector.

The boot program must be very small because it AND the partitiontable must fit into the first 512-byte sector on the boot device.

Load that boot sector into RAM and transfer control to it.

It loads the next stage of the boot loader, called GRUB.

Alice E. Fischer Systems Programming – Lecture 2. . . 4/29 February 12, 2019 4 / 29

Page 5: Systems Programming/ C and UNIX

Booting into Linux

Read, execute the boot loader

The current Linux bootloader is GRUB2: GRand Unified Bootloader 2

GRUB2 is located between the end of the boot sector and the firstpartition on the disk drive.

This space is 31,744 bytes long (62 512-byte sectors).

It is just smart enough to find the kernel and load it into memory.

This code contains a few common dilesystem drivers (EXT, FAT,NTFS).

There are always at least two versions of the Linux kernel on the disk:the newest one and the prior version.

The prior version is there to revert to when the new one.did notinstall correctly or has a bug.

GRUB allows the user to choose from among several kernel images orforeign OS kernels.

Alice E. Fischer Systems Programming – Lecture 2. . . 5/29 February 12, 2019 5 / 29

Page 6: Systems Programming/ C and UNIX

Booting into Linux

Read and decompress the kernel, give it control.

The files for Stage 2 of the boot loader are stored in /boot/grub2and its subdirectories.

The code consists mostly of runtime kernel modules, loaded asneeded.

It lets the user select which kernel to load.

GRUB stage 2 loads the selected kernel into memory and turnscontrol over to it.

All of the kernels are in a self-extracting, compressed format to savespace.

When the kernel begins running, its first job is to extract the full codefrom the compressed format.

When extraction is done, the kernel runs systemd, which starts upand initializes the session.

Alice E. Fischer Systems Programming – Lecture 2. . . 6/29 February 12, 2019 6 / 29

Page 7: Systems Programming/ C and UNIX

Booting into Linux

Run systemd.

This initialization code was formerly, this was called init.

It is the mother of all processes and is responsible for making the hostmachine ready to use. A configuration file lists the required services.This typically includes:

Mounting the filesystems.

Starting system daemons (sshd, cupsd, xinetd, crond, named,systemd-logind, apcupsd, etc. )

Starting system services (the Xorg server, postgress server, mailserver, power manager)

Presenting a login window.

Alice E. Fischer Systems Programming – Lecture 2. . . 7/29 February 12, 2019 7 / 29

Page 8: Systems Programming/ C and UNIX

The Command Shell

More on the Command Shell

QuotesCompiling Commands

Search PathsChanging your Environment

Shell Resource Files

Alice E. Fischer Systems Programming – Lecture 2. . . 8/29 February 12, 2019 8 / 29

Page 9: Systems Programming/ C and UNIX

The Command Shell

More on the Command Shell

When you log into the system, or double-click your terminal-windowicon, Unix will create a shell process for you. It executes in your userspace, not kernel space

This process displays a prompt in the window and waits for you toenter a command.

It then reads and parses your command line.

The shell will look at the directories in your search path to find a shellscript or executable program that matches your command name. Thefirst match, from the left end of the search path, will be used.

The shell then forks off another process to execute your command,and sends the argc and argv that you typed to your new process.

When your process finishes, control returns to your shell process andit displays another command prompt.

Alice E. Fischer Systems Programming – Lecture 2. . . 9/29 February 12, 2019 9 / 29

Page 10: Systems Programming/ C and UNIX

The Command Shell

Quotes: bash shell

Quoting removes the special meaning, to the shell, of special charactersand reserved words. It can be used to prevent parameter expansion.

There are three mechanisms: the escape character (\), single quotes, anddouble quotes.

The escape character is a backslash. It preserves the literal value ofthe following char. A \<newline> is treated as a line continuation.

Enclosing characters in single quotes preserves the literal value ofeach character. A single quote may not occur between single quotes.

Enclosing characters in double quotes is trickier. It preserves theliteral value of characters within the quotes, with many exceptionsand special cases. Use it for scripting and to quote single quotes.

"You can’t do this with single quotes."

’You can’\’’t do this with single quotes.’

Alice E. Fischer Systems Programming – Lecture 2. . . 10/29 February 12, 2019 10 / 29

Page 11: Systems Programming/ C and UNIX

The Command Shell

Compiling Commands

Compile-time search path (open log.txt, search for ... ).

Preprocess only: c++ -E myCode.cpp

Compile only (don’t link): c++ -c myCode.cpp

Compile and link all:c++ -o myCommand -std=c++14 myCode.cpp myPackage.cpp

Compile and link in C:cc -o myCommand myCode.c myPackage.c

Load and run a program:./myCommand [any necessary switches and arguments]

Load and run if you have extended your path:myCommand [any necessary switches and arguments]

Alice E. Fischer Systems Programming – Lecture 2. . . 11/29 February 12, 2019 11 / 29

Page 12: Systems Programming/ C and UNIX

The Command Shell

Useful C/C++ flags:

-E: Preprocess only

-c : Compile only (don’t link)

-Wall : display all warnings (highest level of warnings)

-v : verbose; print all commands used during compilation.

--version : Display version number and copyright information foryour compiler.

Alice E. Fischer Systems Programming – Lecture 2. . . 12/29 February 12, 2019 12 / 29

Page 13: Systems Programming/ C and UNIX

The Command Shell

The Environment

Your environment is a set of strings that is global to your processes. Itcontains varied information about you and your hardware, and it influencesmany things.

An initial set of definitions and settings is installed when you installyour system.

However, you can add to that set of definitions by using a .bashrc file.

When the .bashrc file EXPORTs something, it goes into theenvironment and will be inherited by all commands executed in thatshell.

PATH, your list of search paths, is defined in the environment.

When you use a shell to execute a command, the shell’s environmentis inherited by the shell process it forks off. This new process executesin the same environment as its parent shell.

Alice E. Fischer Systems Programming – Lecture 2. . . 13/29 February 12, 2019 13 / 29

Page 14: Systems Programming/ C and UNIX

The Command Shell

Understanding your Environment

Typically, each string in the environment has the form“NAME=setting”.

Within a program, you can access the environment using getenv()

or through the global variable char** environ.

You may create a new environment variable in a shell:setenv name value (tcsh) orexport name=value (bash)

You may remove an environment variable:unsetenv( name ) (tcsh) orunset name (bash)

Example: Add the working directory to your search PATH:bash-3.2$ PATH=$PATH:.

bash-3.2$ export PATH

Alice E. Fischer Systems Programming – Lecture 2. . . 14/29 February 12, 2019 14 / 29

Page 15: Systems Programming/ C and UNIX

The Command Shell

Search Paths

When you execute a command from the shell, the shell-process must findthe definition of that command on your machine.

To do so, it uses a search path that was loaded when you logged on.

The search path lists several directories on your machine where shellscripts and executable code are stored.

Initially, the search path lists the directories used for executables whenthe system was installed.

To see your current search path in your environment, type this:echo "$PATH"

printenv PATH

Type a $ in front of the name of an environment variable or a shellvariable to get its value.

My current search path is: /usr/bin /bin /usr/sbin /sbin

/usr/local/bin /usr/X11/bin

Alice E. Fischer Systems Programming – Lecture 2. . . 15/29 February 12, 2019 15 / 29

Page 16: Systems Programming/ C and UNIX

The Command Shell

Search Paths – Continued

There is no “special” place for storing the UNIX commands.

Each installation will have a default search path that is appropriatefor the way its resources are organized.

Built-in commands are typically found in these places:/bin: the most fundamental commands./usr/bin: most commands and utility programs./sbin: commands for system administrators.Some commands are built into the shell.

Initially, your current working directory is NOT on the search path.To execute a command you just compiled, you need to change thesearch path or specify the current working directory as part of thecommand name:./myCommand myarg1 myarg2

Alice E. Fischer Systems Programming – Lecture 2. . . 16/29 February 12, 2019 16 / 29

Page 17: Systems Programming/ C and UNIX

The Command Shell

Changing your environment.

A UNIX system has a predefined standard environment that defines a longlist of basic variables, including who you are and what shell you use.

Environment variables have upper-case names like LOGNAME, USER

One of those is PATH, your default search path.

To see your environment, type printenv

In addition, your shell will load a resource file each time you open ashell window.

The result will be that all the default variables are loaded first,followed by the ones you defined.

Alice E. Fischer Systems Programming – Lecture 2. . . 17/29 February 12, 2019 17 / 29

Page 18: Systems Programming/ C and UNIX

The Command Shell

Creating a Shell Resource File

If you have never created your own shell resource file, you don’t haveone. Instead, the default profile will be used when you open a shell

Use a text editor to create your own resource file, then store the filein your home directory with the name .bashrc (or .cshrc).

If you have trouble giving the correct name to this file, create a file inyour home directory with any name, then use the shell and the mv

command to change the name.

Thereafter, the commands in this file will be loaded for you everytime you open a new shell.

To see this file in a shell, execute cd to go to your home directory,then type cat .bashrc

Alice E. Fischer Systems Programming – Lecture 2. . . 18/29 February 12, 2019 18 / 29

Page 19: Systems Programming/ C and UNIX

Defining a Command Language

Defining a Command Language

Each application defines its own commandsUse getopt() to analyze the argument vector

Defining short and long optionsUsing getopt_long()

Alice E. Fischer Systems Programming – Lecture 2. . . 19/29 February 12, 2019 19 / 29

Page 20: Systems Programming/ C and UNIX

Defining a Command Language

Each Application Defines its own Legal Commands

A program receives an argument vector, entered by the user, when itbegins execution. Every program knows its own needs.

Many programs do not take input from the command line.

Some programs require arguments to work.

Some have optional arguments, in addition to or instead of requiredones.

The first thing a program must do is determine whether it has enoughcommand line arguments and whether they make sense.

If not, it must print a usage comment to inform the user of theproblem and possible solutions.

Alice E. Fischer Systems Programming – Lecture 2. . . 20/29 February 12, 2019 20 / 29

Page 21: Systems Programming/ C and UNIX

Defining a Command Language

Use getopt() to Analyze the Argument Vector.

Command shells provide tools for extracting the switches, or options, andtheir parameters from argv.

Without automation, this is an annoying and picky job because of theneed for validation and for handling errors.

The getopt function automates the process.

int getopt( int argc, char* argv[], const char* opts );

argc and argv are the parameters received from the shell by main().opts is a string containing all the short switch options that yourprogram supports: "i:avRou"

An option letter followed by a colon requires a parameter, which will bestored in optarg.

Alice E. Fischer Systems Programming – Lecture 2. . . 21/29 February 12, 2019 21 / 29

Page 22: Systems Programming/ C and UNIX

Defining a Command Language

Using getopt(): options.c.

for (;;) {

ch = getopt(argc, argv, "i:-abRou");

if( ch == -1 ) break;

...

getopt() returns the next known option character in the optstring.

It will search argv for all the switches in the command line.

Each time you call it, the next switch will be returned. (This is achar, even though the type is technically int.)

The order of the items on the command line will be changed so thatall the switch arguments precede all the non-switches. The globalvariable optind contains the subscript of the first non-switchargument.

Alice E. Fischer Systems Programming – Lecture 2. . . 22/29 February 12, 2019 22 / 29

Page 23: Systems Programming/ C and UNIX

Defining a Command Language

getopt() Return Values

On return from getopt(),

optarg points to an option argument, if it is expected,

optind contains the index to the next argv argument for asubsequent call to getopt().

optopt saves the last known option character returned by getopt().

getopt() returns -1 when the argument list is exhausted.

If getopt()encounters a character not found in optstring or if itdetects a missing option argument, it returns ‘?’ (question mark).optopt is set to the character that caused the error.

Alice E. Fischer Systems Programming – Lecture 2. . . 23/29 February 12, 2019 23 / 29

Page 24: Systems Programming/ C and UNIX

Defining a Command Language

Using getopt_long().

This function extends getopt() by allowing long switches with doubledashes.

Your P3 must use getopt_long() to read and validate the switchesthat are given on the command line. Then it must store thatinformation in its own variables and, finally, print out a summary.

int getopt_long( int argc, char* argv[],

const char* optstring, struct option longOpts[],

int* codeP );

longOpts is a table containing all the information about long switches.If there is a short equivalent for the long switch, that char will bereturned (otherwise 0).If not, the subscript of the correct line of longOpts will be stored incodeP.

If a switch requires an argument, it may be preceded by either an =or a space. An optional argument must be preceded an = .

Alice E. Fischer Systems Programming – Lecture 2. . . 24/29 February 12, 2019 24 / 29

Page 25: Systems Programming/ C and UNIX

Defining a Command Language

Defining short and long Options

Each program defines its own command language like this:

A string defines the short switches, as in: "abo:u".

A structure defines the long switches.

In this example, two short switches (b and o) also have long names,and two do not (a and u).

Two long switches (recursive and debug) do not have short synonyms.

struct option longOpts[] = {

{ "verbose", no_argument, NULL, ’b’},

{ "recursive", no_argument, NULL, 0 },

{ "output", required_argument, NULL, ’o’},

{ "debug", optional_argument, NULL, 0 },

{ NULL, 0, NULL, 0 }

};

The last line of the structure must be a row of 0’s.Alice E. Fischer Systems Programming – Lecture 2. . . 25/29 February 12, 2019 25 / 29

Page 26: Systems Programming/ C and UNIX

Defining a Command Language

Using getopt_long_only().

This extends getopt() by allowing long switches with single dashes.

One can still use the double dash syntax, as with getopt_long().

If using the single dash, the first letter of the long switch name mustnot be the same as any short switch.

Alice E. Fischer Systems Programming – Lecture 2. . . 26/29 February 12, 2019 26 / 29

Page 27: Systems Programming/ C and UNIX

Defining a Command Language

Errors with getopt() and getopt_long_only().

Technically, all of the switches are supposed to precede all of thenon-switch arguments. If you write them in the wrong order, thegetopt() functions will change the order to put non-switches at theend.

Invalid switch name: If a switch is on the command line but NOT inthe set of defined switches, getopt() returns ’?’ as an error code.Your program must check for and handle the error.

It is customary to output a usage message when the form of thecommand line is wrong. See the example program for the usual formof a usage message.

If a required switch argument is missing, the next thing on thecommand line will be taken as its argument.

Alice E. Fischer Systems Programming – Lecture 2. . . 27/29 February 12, 2019 27 / 29

Page 28: Systems Programming/ C and UNIX

Defining a Command Language

The getopt_long() Demo Program. options.c

Lines to look at:

Line 6 includes the header file for getopt_long().Lines 14 through 20 define the set of long switches. The columns inthe table are:

the switch nameargument-requirementsan alternate way to return the option (just write NULL), andthe equivalent short option letter, or zero.

The loop lines 23 checks that there are at enough args are provided tosupply the single non-switch required argument.

The loop on lines 25. . . 47 processes the options returned bygetopt_long().

Use a switch statement to process your options.

In this program, all I do is print the switch information. However,normally, each case would set a boolean flag-variable to indicate thatthe switch is on, and save / process switch-arguments appropriately.

Alice E. Fischer Systems Programming – Lecture 2. . . 28/29 February 12, 2019 28 / 29

Page 29: Systems Programming/ C and UNIX

Defining a Command Language

Homework P1

In the man pages, look up ten commands from the list below. Chooseunfamiliar commands, and choose two from each column. Turn in a1-sentence description of what each command is used for.

alias wc whoami who whichtouch uniq chmod chown unamecp diff df sudo uptimefinger gcc grep touch stringskill less ln ls xxdmkdir more passwd mv manps pwd rm rsync pingslogin ssh sort head tail

Alice E. Fischer Systems Programming – Lecture 2. . . 29/29 February 12, 2019 29 / 29