Introduction to Unix

Unix Operating System

 

An interface between user(s) and a computer

 

 

        Large software program (or collection of programs) to...
         - control tasks of various devices (i.e. input, screen, disks, ...)
         - control execution of other programs or "processes"
         - assign computer resources (memory, disk, I/O) to application programs
         - shields the user from the complexity of the hardware
         - an example (reading data from disk file)
         - software program also called kernel
 

Why the need?
    Imagine writing a program to...
        - read from a file on disk
        - allocate some memory
        - print the information to the screen
        - Unix provides C language system calls for users (i.e. open, read, write)
 

What's the shell?
    - user interface to Unix
    - command-line interpreter to Unix
    - simple, easy to replace
    - many versions of shells (csh, ksh, bsh, bash)
    - version dependent on specification in passwd file
    - useful for customizing user environment (aliases, paths, environment variables, ...)
    - customization via an "invisible" file in home directory (.bashrc, .cshrc, ...)
 

 

File system
    - Organized as a hierarchical, tree structure in a similar manner to Windows folders

    - A Unix “directory” is similar to a Windows “folder”

    - Unix commands are used to move within this hierarchy of directories

    - For example at the command line in your terminal window, type the following and press return

    - Remember to just type what is after the command line prompt, represented below as [dbock@shaula:~]$

    [dbock@shaula:~]$ cd

    - Many Unix commands are short abbreviations for a task

    - This command, for example, stands for “change directory”

    - When entered by itself (without arguments), this will move you to your home directory

    - To see where you are in the file system, type the following:

    [dbock@shaula:~]$ pwd

    - The output of this command (short for “print working directory”) will show you “where you are” in the file system

    - This is also known as the absolute pathname of a location within the file system

    - This is denoted as a sequence of / characters and folder (or directory) names

    - An example might be something like: /home/staff/dbock

                - The first / represents the top of the hierarchical file system also called the root

    - Each name following / represents a directory one-level down the hierarchical file system

    - Think of it like a tree starting at the root with each branch as a directory

    - This is exactly how folders are organized within Windows, folders within folders within folders

    - The Unix file system is similar and uses / characters and folder names to represent your current location in the tree

    - To see the contents of the directory you are currently residing within, type the following command

    [dbock@shaula:~]$ ls

    - This command (short for “list”), shows you the current contents (files and possibly other directories)

    - This is the content of the directory you are currently in (or the current “working directory”)

    - Some important directory pathnames with regard to software development and this course are listed below

    - You can go to these (and any directory) by giving the pathname after the cd command

    /usr/lib              contains libraries (collections of pre-compiled functions) to link with programs

  /usr/include          contains header files (containing declarations for library functions)

    /home/staff/dbock     instructor’s home directory (containing assignments and submission directories)

 

Commonly used Unix commands
    - The Unix Quick reference contains a list of some of the more commonly used Unix commands
    - Practice and experiment with each of these commands

    - Practice making folders of your own in your home directory with the “Unix exercise” below

 

 

Programmer interface to Unix
    - System libraries (in
/usr/lib) contain functions that programmers can use to access devices

    - Examples include functions to read and write files, allocate memory, control processes, or access devices
    - In this way, these libraries provide a “programmatic interface” to the Unix devices and hardware resources

    - In other words, programmers can access hardware from an executable program by using such functions in their programs
    - An example  might be a program that opens a file, read information in the file, and writes it back out to another file

 

Unix exercise
- Practice using Unix commands from the command-line prompt

- Get comfortable navigating around the Unix file system

- Practice the following exercise:

Go to your home directory

[dbock@shaula:~]$ cd

Print the current (working) directory

[dbock@shaula:~]$ pwd

Make a directory in your home directory named csc125

[dbock@shaula:~]$ mkdir csc125

List the contents of the current directory

[dbock@shaula:~]$ ls

Change directory to your new csc125 directory

[dbock@shaula:~]$ cd csc125

Make a directory under your new csc125 directory named labs

[dbock@shaula:~]$ mkdir labs

Copy sampleLab.c from my directory /home/staff/dbock/csc125/labs to your new labs directory

[dbock@shaula:~]$ cp /home/staff/dbock/csc125/labs/sampleLab.c labs

Change directory to your labs directory

[dbock@shaula:~]$ cd labs

List the contents in your labs directory (you should see a copy of sampleLab.c)

[dbock@shaula:~]$ ls

 
 

Writing programs

Editors

- C/C++ programs are written using programs called editors

- A variety of editors exist for the Unix operating system

- Some editors include pico, vi, emacs

- Typing the name of an editor and a filename will load the file in the editor

- If you have used a Unix editor before, feel free to use it for this course

- Feel free to try any editor you wish, whatever works best for you

- If you have the time, I strongly suggest looking into emacs (discussed below)

 

What is Emacs?
- Efficient method of writing/creating source files

- Powerful editor (among other editors such as vi, pico)
- Extremely customizable
- Extensible
- Good for multiple file software development
 

Where can I find documentation on Emacs?
- Online help, tutorial within the program
- Lots of sites with Emacs information (example User's manual)
 

How do I use Emacs?
- Emacs in a separate window
   [dbock@shaula:~]$ emacs sampleLab.c

- Emacs in shell window
   [dbock@shaula:~]$ emacs --no-windows sampleLab.c


 

Emacs tutorial
    [dbock@shaula:~]$ emacs --no-windows

Type: C-h then the letter t

Follow tutorial instructions (great source of Emacs learning)

Get comfortable using Emacs to write programs


 

Write and compile a C program
- Using Emacs or editor of your choice, open your copy of sampleLab.c
- Complete the sample lab as per instructions in sampleLab.c
- Submit your solution to /home/staff/dbock/csc125/students/sampleLab

- Instructions and example in Syllabus under “Submission” heading