You are on page 1of 16

Introduction to the UNIX System

 A digital computer system consists of the hardware and the software.

Hardware
 The hardware consists of the physical components.
 The following are some of the hardware components
- One or more processors or central processing units (CPU).
- The memory.
- The input system: consists of the input devices (keyboard, mouse. . . etc) and their controllers.
- The output system: consists of the output devices (monitor, printer . . . etc) and their controllers.
- The storage media and the storage devices. Examples of storage media are the hard disk, CD,
DVD, flash disk and tapes.
 The processor (CPU) interprets (decodes) and executes the instructions of a program.
 The main memory holds the program(s) being executed and their data.
 An input device is used to transmit data from the outside world into the computer whereas an output
device is used to transmit information from the computer to the outside world.
 A storage media is used to hold programs and data for future use inside the computer, and a storage
device (or drive) is used to read and write on a storage medium.
 Each input/output/storage device has a controller that is used to control its operations. Furthermore,
an input device controller translates the signals of that device into bits whereas an output device
controller translates bits from the computer into the signals of that device. A storage device
controller does the translation in both ways. Device controllers are also used for communication
with the CPU.

Software
 The software is the collection of programs that a computer can execute.
 There are two major classes of software:
 system programs and
 Application programs.

©2013 Gilbert Ndjatou Page 41


 An application program is used to perform a specific task or to solve a particular problem for its
user. Examples of application programs follow:
 Word-processing programs: programs to create documents with formatting features.
 Data base management system: programs to create tables of information and to provide for
easy access and update of information in those tables.
 Spreadsheet program: programs to create electronic spreadsheets.
 System programs are used for the operations of the computer. Examples of system programs are
editors, compilers, linkers, loaders, and operating systems.

Editor
- reads characters from the keyboard and places them into a file.

Compiler
- translates a high-level language program source module into machine language.
- creates an object file (module) that contains the machine language code translation of the source
module.

Linker editor or linker


- combines separately translated object modules and the library functions called in a program into
a single load (or executable) module.
- adjusts the displacements in modules other that the first, that are to be relative to the start of the
final load module.
- resolves external references by providing the pointers needed for the external references in each
object module.

Loader
- determines how much memory is needed for a program load module and the address of the first
instruction to be executed in the program (program entry point).
- copies the load module into the memory.
- adjusts relative references to memory locations (that depend on the starting address of the
program) by adding a constant to them.
- initializes the registers that are necessary for the execution of the program.
- transfers the control of the CPU to the program for execution.

Operating system (is the most fundamental of all system programs)


- manages the system resources (main memory, disks, printers, CPU, . . etc), and
- provides an interface to the hardware that is easy to understand and use in other programs.

©2013 Gilbert Ndjatou Page 42


 Popular operating systems are Windows XP and 7, LINUX, and UNIX.
 The interface to the hardware is provided in Windows XP, Vista and 7 by using Application
Program Interfaces (API). It is provided in LINUX and UNIX by using system calls.
 API and system calls are called in a program in the same way that library functions are called.

UNIX Operating System


 The UNIX operating system is a timesharing system:
It allows many users to connect and work on a minicomputer or a mainframe by using a personal
computer or a terminal (keyboard and monitor) as follows:
- The hard disk and other storage media on the computer are subdivided into pieces and a piece of
the storage media is allocated to each user of the system.
- Each user can execute one or more programs simultaneously on the system
(multiprogramming)
- The memory of the computer is subdivided into many pieces, and each executing program is
loaded into one of the pieces.
- UNIX switches the CPU from one executing program to another so that eventually, all the
instructions of an executing program will be executed.
 The UNIX operating system includes the kernel, the standard utility programs, and the libraries
(including system calls).

UNIX Kernel
 The UNIX kernel is the program that is loaded from the disk into the main memory when the
computer is first turned on.
 It always stays in the main memory and runs until the system is turned off or crashes.
 The UNIX kernel contains codes to do the following:
- share the processor(s) and the main memory between competing executing programs
- process all system calls
- handle peripherals (input/output system and storage media/devices).

©2013 Gilbert Ndjatou Page 43


The standard utility programs include the following:
- one or more command processors (shells)
- filters (a program that takes its input from the standard input, does some processing on it, and writes
its output to the standard output)
- program development tools such as compilers, and editors
- text processing programs
- file and directory manipulation commands
- system administration programs
- Miscellaneous programs.

The UNIX Shell


 A shell or command line processor is a program that accepts commands from a user and executes
them.
 There are two types of commands in UNIX:
 Build-in commands that are recognized and executed internally by the shell, and
 Commands that are file names of utility and other programs.
 A shell works as follows:
 When it starts up, it initializes itself, then displays a prompt character ($ or %) on the screen,
and waits for the user to type a command line.
 When the user types a command line, it extracts the first word from it and if it is a build-in
command, it executes it and then redisplays the prompt and waits for the user to type the next
command. Otherwise, it assumes that it is the name of a program file to be executed (or run).
 In order to execute a program file, it searches for that program file in the file system, and if it is
found, it is executed; otherwise it displays an error message, then a prompt, and waits for the
user to type the next command line.
 It executes a program file by suspending itself until that program terminates; at which point it
displays the prompt and then waits for the user to type the next command line.

Example: with the command line mv filea fileb


The shell will invoke the program mv with the two arguments, filea and fileb.

 The most popular shells are:


- Bourne shell (sh)
- Korn shell (ksh)
- C shell (csh)
- Bourne Again shell (bash).
©2013 Gilbert Ndjatou Page 44
 All these shells have a common set of core functionality; but they each have some specialized
features, and the Korn shell is a super-set of the Bourne shell.
 Each shell has its own programming language which is mainly used for manipulating files and
programs in execution.

Note: Many UNIX systems also have a graphical user interface (X Windows) that can be used to
interface with the system.

Files and Directories in a UNIX System


 All the storage media used on a UNIX system are set up by the system administrator and are
organized into a directory hierarchy called file system.
 A directory (or subdirectory) is a file that contains information about other files and directories
stored in the storage media.
 Each user is assigned a subdirectory in the file system (by the system administrator) that corresponds
to the media space where he/she can store his/her files and directories. This directory is referred to
as the user’s home directory.
 A user cannot add its own storage medium (flash disk or floppy disk) in a UNIX system.
 A user can read and or write into another user’s home directory only if he/she is given the
permission to do so.
 A UNIX file is a sequence of 0 or more bytes. UNIX has no way to distinguish an ASCII file from a
Binary file or any other file.
 The name of a file in a UNIX system can be up to 255 ASCII characters, except the NULL
character.
 By convention, many programs expect a file name to consist of a base name followed by a dot
(which counts as a character) and an extension.

Examples: lab1.cpp prog.f90 prog.java.z

 The name of a user’s home directory is set by the system administrator to his or her username.
 Files and subdirectories in the same directory cannot have the same filename.
 However, files and subdirectories in different directories can have the same filename.
 The largest directory in a UNIX system is the root directory and is denoted by the forward slash /

Example:
A UNIX directory hierarchy in which the names of directories are underlined follows:

©2013 Gilbert Ndjatou Page 45


/ (root)

faculty usr students dev students1 bin tmp

glass ndjatou

myfile programs documents

myfle
prog.cpp

 On way to specify a file name in UNIX is to use its absolute path.


 A file or a directory absolute path is the list of the names of all the directories (separated with the
forward slash /) that leads to that file/directory, starting with the root.

Examples of Absolute Path Names:


the absolute path name of the file prog.cpp is: /faculty/ndjatou/programs/prog.cpp
the absolute path name of the directory documents is: /faculty/ndjatou/documents
the absolute path name of the file myfile in directory glass is: /faculty/glass/myfile

 The directory from which a user is working is referred to as that user’s working directory.
 When a user logs into a UNIX system, his/her working directory is automatically set by the shell
program to his/her home directory.
 Pathnames can also be specified relative to a user’s working directory.
 A file or a directory relative pathname is a pathname that is specified relative to a user’s working.

Examples of Relative Path Names (with working directory ndjatou):


the relative path name of the file prog.cpp is: programs/prog.cpp
the relative path name of the directory documents is: documents

 When you type a command in a UNIX system, you may do the following:
 use a period (.) to represent your current working directory
 use two periods (..) to represent the parent of your current working directory in the directory
hierarchy.

©2013 Gilbert Ndjatou Page 46


Example:
With ndjatou being my working directory, the relative pathname of the file myfile in the
subdirectory glass of the directory hierarchy above is: . ./glass/myfile

UNIX Users
 Every person needs a username and a password to log into a UNIX system.
 Each user is also identified in a UNIX system by a non-negative integer called user ID (UID) that is
assigned by the system administrator.
 The superuser has the username root and the UID 0.
 The superuser has a special power and may access all the resources in the system.
 Only the system administrator knows the password to log in as the super user.
 A UNIX user is also assigned (by default) to a group (of users) by the system administrator.
 A group is simply an arbitrary collection of users which offers a straightforward method for
controlling projects involving several users.
 A group is also identified by a non-negative integer called group ID (GID) which is assigned by the
system administrator.
 Each UNIX user belongs to at least one group, possibly more.
 Information about a UNIX system groups is held in the file /etc/group.
 In a typical UNIX system, entries in the password file (/etc/passwd) are used to hold the following
information about a user:
 the username
 a place marker for the user’s password
 the uid
 default gid
 optional comment field
 user’s home directory
 the pathname of the user’s default shell.

©2013 Gilbert Ndjatou Page 47


How to Log into a UNIX System
 You need a PC or a terminal to connect and to log into a UNIX computer.
 If you are using a PC, you also need a software that will connect your PC to the UNIX computer.
One of these software is called putty. This software can be downloaded for free from the internet.

Using Putty to Log Into Our UNIX computer


- After you start putty, it displays its configuration window. Continue as follows:
1. Type the host name of your UNIX computer: cs.wpunj.edu
2. Select the SSH protocol.
3. Click on the OPEN button.
- You are now connected to the UNIX machine (assuming that everything went well)
1. Type your username then press the ENTER key.
2. Type your password: do not worry if the cursor is not moving.
- You are now logged into the UNIX machine (assuming that everything went well). The shell
program is waiting for you to type a command line.

How to Log Out of a UNIX System


 You can log out of a UNIX system by pressing CTRL-D (^D) at the shell prompt.

Setting Your Password


 After you have logged into a UNIX computer, you can use the passwd command (discussed in the
UPU book in page 22) to change your password.

C++ Programming in a UNIX System


 The following editors are available on our UNIX computer:
- vi (UPU book, pages 57-69) The most popular editor
- emacs (UPU book, pages 69-75)
- pico The one we will use in this class.
 You access the pico editor by typing one of the following command lines:
pico <ENTER> If you want to create a new document
or
pico <file-specification> <ENTER> If you want to retrieve an existing document
©2013 Gilbert Ndjatou Page 48
Compiling and Linking a C++ Program (UPU book, pages 388-392)
 Suppose that your program consists of just one source file named gpacomputation1.cpp that you
want to compile, link, and execute. This can be done in the following two ways:
I. The command line g++ gpacomputation.cpp <ENTER> calls the compiler to compile the
source file gpacomputation2.cpp (from your current working directory) and:
1. if there are any errors, they are displayed on the screen and the shell program redisplays its
prompt.
2. If there are no errors in the program, the linker is called to create the executable file a.out in
your current working directory. a.out is the default executable file name in a UNIX system.
You execute your program by typing the command line: a.out <ENTER>

II. The command line g++ gpacomputation.cpp -o gpacomputation.bin <ENTER>


calls the compiler and then the linker in the same way that the command line above does it. But
the linker will create the executable file named gpacomputation.bin instead of a.out.
You execute it by typing the command line: gpacomputation.bin <ENTER>

Hands-On Practice U1
List of commands Discussed
Command purpose Reference

passwd Changes a user’s password UPU book in page 22


pwd displays the current working directory UPU book, page 24
ls Displays the content of a directory and info. about directories and UPU book, pages 27-28
files
mkdir Creates a new directory UPU book, pages 31-32
cp Copies a file UPU book, pages 33-34
cd Changes the current working directory UPU book, pages 32-33
mv Renames a file UPU book, page 31
cat Displays the content of a file UPU book, pages 27 and 29
lp Sends the content of a file to a printer UPU book, pages 37 and 39
rm Deletes a file UPU book, pages 27 and 29
rmdir Deletes a directory UPU book, page 35

©2013 Gilbert Ndjatou Page 49


1. Use putty to log onto the department’s UNIX computer (after your first logon you may change your
assigned password to the one that you like and will remember easily).
2. Use the pwd command (UPU book, page 24) to display your current working directory. What is it?
pwd <ENTER>
3. Use the ls command (UPU book, pages 27-28) to display the content of your home/current directory.
Type each of the following command lines:
ls <ENTER> list all the files(whose names do not start with a period) in the current working directory in alphabetical order

ls . <ENTER> list all the files(whose names do not start with a period) in the current working directory in alphabetical order

ls –a . <ENTER> list all the files in the current working directory in alphabetical order

ls –l . <ENTER> list the files in the current working directory with their attributes

4. Use the mkdir command (UPU book, pages 31-32) to create the directories cs2400 and others in
your home directory. (NB: you may use the ls command to make sure that you did it properly).
mkdir cs2400 <ENTER>

mkdir others <ENTER>

ls –l . <ENTER>

5. Use the cp command (UPU book, pages 33-34) to copy the files
/faculty/ndjatou/cs2400/gpacomputation.cpp and /faculty/ndjatou/cs2400/gpacomputation.h to the
directory cs2400 that you have created in step 4. (NB: you may use the ls command to make sure
that you did it properly).
cp /faculty/ndjatou/cs2400/gpacomputation.cpp cs2400 <ENTER>

cp /faculty/ndjatou/cs2400/gpacomputation.h cs2400 <ENTER>

ls –l cs2400 <ENTER>

6. Use the cd command (UPU book, pages 32-33) to make the directory others created in step 4 your
current working directory. (NB: you may use the pwd command to make sure that you did it
properly).
cd others <ENTER>

pwd <ENTER>

7. Use the cp command (UPU book, pages 33-34) to copy the files gpacomputation.cpp and
gpacomputation.h from your directory cs2400 into the directory others. (NB: you may use the ls
command to make sure that you did it properly).
cp ../cs2400/gpacomputation.cpp . <ENTER>

cp ../cs2400/gpacomputation.h . <ENTER>

or
cp ../cs2400/gpacomputation.cpp gpacomputation.cpp <ENTER>

cp ../cs2400/gpacomputation.h gpacomputation.h <ENTER>

©2013 Gilbert Ndjatou Page 50


You may also use the absolute pathnames for the destination files
ls –l . <ENTER>

8. Use the mv command (UPU book, page 31) to rename the file gpacomputation.cpp (in the directory
others which is your current working directory) gpacomputation1.cpp. (NB: you may use the ls
command to make sure that you did it properly).
mv gpacomputation.cpp gpacomputation1.cpp <ENTER>
ls –l . <ENTER>

9. Use the cd command (UPU book, pages 32-33) to make your home directory your current working
directory. (NB: you may use the pwd command to make sure that you did it properly).
cd <ENTER>
pwd <ENTER>
10. Use the cp command (UPU book, pages 33-34) to copy the file /faculty/ndjatou/cs2400/instructions
to the directory others. (NB: you may use the ls command to make sure that you did it properly).
cp /faculty/ndjatou/cs2400/instructions others <ENTER>

or
cp /faculty/ndjatou/cs2400/instructions others/instructions <ENTER>

ls –l . <ENTER>
11. Use the cat command (UPU book, pages 27 and 29) to display the content of the file instructions
that you have just copied into the directory others in step 10.
cat others/instructions <ENTER>
12. Follow the instructions provided in the file instructions.
13. The lp command (UPU book, pages 37 and 39) is used to print the content of the file
gpacomputation2.cpp on the UNIX machine default printer as follows:
lp gpacomputation2.cpp <ENTER> Use default printer(jennifer)

or
lp –d jennifer gpacomputation2.cpp <ENTER> Use jennifer printer

However, our UNIX system does not have a printer: we will therefore use an ftp software (such as
WinSCP) to download a UNIX file to our PC and then print it from there.
14. Use the rm command (UPU book, pages 27 and 29) to delete all the files in the directory others.
(NB: you may use the ls command to make sure that you did it properly).
You can delete one file at a time as follows:
rm gpacomputation1.cpp <ENTER>
rm instructions <ENTER>
. . .
Ls –l . <ENTER>

©2013 Gilbert Ndjatou Page 51


Or you can delete all the files at once by using the file substitution wildcard * as follows:
rm * <ENTER>

ls –l . <ENTER>

15. Use the cd command (UPU book, pages 32-33) to make your home directory your current working
directory. (NB: you may use the pwd command to make sure that you did it properly).
cd <ENTER>

pwd <ENTER>

16. Use the rmdir command (UPU book, page 35) to delete the directory others from your home
directory. (NB: you may use the ls command to make sure that you did it properly).
rmdir others <ENTER>

ls –l <ENTER>

Note: The command line in Step 7 to copy the file ../cs2400/gpacomputation.cpp to the current working
directory and the command line in step 8 to rename it gpacomputation1.cpp could be combined into
one command line as follows:
cp ../cs2400/gpacomputation.cpp gpacomputation1.cpp <ENTER>

Input/Output Redirection
 In a C++ program, all the input data read using a cin statement are taken from the standard input,
and all the output data printed using a cout statement are sent to the standard output.
 In a UNIX system, the standard input is set by default to the keyboard, and the standard output is set
by default to the monitor.
 You can set the standard input to a different file by using input redirection as follows:
gpacomputation1.bin < gpacomputation1.input <ENTER>
- the input of the program gpacomputation1.bin will be read from the file gpacomputation1.input
(from your current working directory) instead of the keyboard.
 You can set the standard output to a different file by using output redirection as follows:
gpacomputation1.bin > gpacomputation1.out <ENTER>
- the output of the program gpacomputation1.bin will be written into the file gpacomputation1.out
(that will be created in your current working directory) instead of the monitor.
 The command line gpacomputation1.bin < gpacomputation1.in > gpacomputation1.out <ENTER>
executes the program gpacomputation1.bin that reads its input from the file gpacomputation1.in (in
your current working directory) and writes its output into the file gpacomputation1.out (that will be
created in your current working directory).

©2013 Gilbert Ndjatou Page 52


Hands-On Practice U2
1. Use putty to log onto the department’s UNIX computer.
2. Change your current working directory to CS2400.
3. Copy the file gpacomputation.cpp from the directory CS2400 to the file gpacomputation1.cpp in the
same directory as follows:
cp gpacomputation.cpp gpacomputation1.cpp <ENTER>
4. Use pico to edit the file gpacomputation1.cpp as follows:
- Change the programmer’s name to your name.
- Delete the following three cout statements from your program ( you do not need a prompt to
type the input data of your program):
cout << endl << “Enter the student\’s first name, last name, and ID number:\t”;
cout << endl << “Enter the first course number, credit and gpv:\t”;
cout << endl << “Enter the second course number, credit and gpv:\t”;

- Save the program in the file with the same name.


5. Use pico to create the input file of your program, gpacomputation1.input as follows:
<your first name> <your last name> <make up an ID number>
CS251 4 4
CS301 3 4
6. Compile and link your program to produce the executable file gpacomputation1.bin.
7. Execute gpacomputation1.bin using I/O redirection: name your output file gpacomputation1.out.
8. Use WinSCP to download the files, gpacomputation1.cpp, gpacomputation1.input and
gpacomputation1.out to your PC and then print and return them.

File Attributes (Pages 41-46)


 Each UNIX file has 8 attributes that you can display by using the ls command with the -l, -s, and –F
options as follows:
ls -lsF <file-specifications>

Example
$ ls -lsF cs2400/gpacomputation.cpp
6 -rw-r--r-- 1 ndjatou faculty 2375 Sep 16 2008 cs2400/gpacomputation.cpp

©2013 Gilbert Ndjatou Page 53


Field # Field Value Meaning

1 6 The number of blocks occupied by the file on the


physical storage
2 -rw-r- - r- - The type and permission mode of the file
3 1 The hard link count of the file
4 ndjatou The username of the owner of the file
5 faculty The group name of the file
6 2375 The size of the file, in bytes
7 Sep 16 2008 The date that the file was last modified
8 Cs2400/gpacomputation.cpp The relative pathname of the file

File Types
 The first character of field #2 indicates the file type. The different types of UNIX files with their
corresponding characters are given in Figure 2.35 in page 44 (UP). The file type of the file in the
example above is regular file.

File Permissions
 When you create a UNIX file, you must specify the read permission (r), write permission (w), and
the execute permission (x) of that file for the owner of the file, the members of the owner’s group
and the other users.
 A file’s permission setting consists of three consecutive groups of three characters as follows:
 The first group of characters is the permission settings for the owner,
 The second is the permission settings for the members of the owner’s group, and
 The last one is the permission settings for the other users of the UNIX system.
 The first character of a group is r if the read permission is given, and a dash otherwise.
 The second character is w if the write permission is given, and a dash otherwise.
 The third character is x if the execute permission is given, and a dash otherwise.
 For the file in the example above, the permission settings are rw-r- - r- -, which means that the
owner of the file can only read and write it, the members of his group and the other users can only
read it.
 The table in Figure 2.38 in page 45 (UP) lists the permission meanings for files and folders.
 You use the chmod command (page 49, UP) to change a file’s permission settings.

©2013 Gilbert Ndjatou Page 54


Obtaining On-Line Help in UNIX
 The UNIX on-line manual contains helpful information about user commands and application
programs; system calls and library functions; file formats; headers, tables, and macros; games and
demonstrations; special file and commands; and device driver and kernel interfaces.
 This information is held into 8 sections (1, 2, 3, 4, 5, 6, 7, and 9) and is accessed by using the man
command discussed in page 18 (UP).

Hands-On Practice U3
1. Display the content of the file /etc/group on the screen (do not print it). How many groups are there
on your UNIX system?
2. Display the content of the password file, /etc/passwd, on the screen (do not print it) and find out the
following information about you held in UNIX:
a. your username
b. a place marker for the user’s password
c. your uid
d. your default gid
e. optional comment field
f. your home directory
g. the pathname of your default shell.

3. Use the groups command discussed in page 47 (UP) to display a list of all the groups to which you
are a member. What are they?
4. Use the chmod command to change the file permissions of your home directory such that nobody
else can access it as follows:
a. Display the current permission settings of your home directory: ls -ld . <ENTER>

b. Update its permission: chmod go-rx . <ENTER>

c. Display again the current permission settings of your home directory: ls -ld . <ENTER>

5. Practice on using the man command to obtain on-line information on user commands, library
functions (system calls), . . . etc.

©2013 Gilbert Ndjatou Page 55


©2013 Gilbert Ndjatou Page 56

You might also like