UNIX System Programming
INTRODUCTION
r . S i nha l uti o ns
Amit
K r ant So
E x u be
Nex-G .
td
Pvt. L
January 21, 2012
User UNIX Interface: SHELL
2
Provides command line as an interface
between the user and the system
Is simply a program that starts
automatically when you login
Uses a command language
Allowsprogramming (shell scripting) within
the shell environment
Uses variables, loops, conditionals, etc.
Accepts commands and often makes system
calls to carry them out
January 21, 2012
Various UNIX shells
3
sh (Bourne shell)
ksh (Korn shell)
csh (C shell)
tcsh
bash
Differences mostly in scripting details
January 21, 2012
The Korn Shell (ksh)
4
We will be using ksh as the standard shell for
examples in this class
Language is a superset of the Bourne shell
(sh)
January 21, 2012
Login scripts
5
You don’t want to enter aliases, set
environment variables, set up command
line editing, etc. each time you log in
All of these things can be done in a script
that is run each time the shell is started
For ksh:
~/.profile - is read for a login shell
~/.kshrc
For tcsh
~/.login
~/.cshrc
January 21, 2012
Example .profile (partial)
6
# set ENV to a file invoked each time sh is started
for interactive use.
ENV=$HOME/.shrc; export ENV
HOSTNAME=`hostname`; export HOSTNAME
PS1="$USER@$HOSTNAME>"
alias 'll'='ls -l'
alias 'la'='ls -la'
alias 'ls'='ls -F'
alias 'rm'='rm -i'
alias 'm'='more'
set -o vi
echo ".profile was read"
January 21, 2012
stdin, stdout, and stderr
7
Each shell (and in fact all programs)
automatically open three “files” when they
start up
Standard input (stdin): Usually from the keyboard
Standard output (stdout): Usually to the terminal
Standard error (stderr): Usually to the terminal
Programs use these three files when reading
(e.g. cin), writing (e.g. cout), or reporting
errors/diagnostics
January 21, 2012
Redirecting stdout
8
Instead of writing to the terminal, you can tell
a program to print its output to another file
using the > operator
>> operator is used to append to a file
Examples:
man ls > ls_help.txt
Echo $PWD > current_directory
cat file1 >> file2
January 21, 2012
Redirecting stderr
9
Instead of writing errors to the terminal,
you can tell a program to write them to
another file using the:
ksh: 2> operator
tcsh: >& operator
Examples (suppose j is a file that does not
exist)
{ajax} ls j
ls: j: No such file or directory
{ajax} ls j >& hello.txt
{ajax} cat hello.txt
ls: j: No such file or directory
January 21, 2012
Redirecting stdin
10
Instead of reading from the terminal, you can
tell a program to read from another file using
the < operator
Examples:
Mail user@domain.com < message
interactive_program < command_list
January 21, 2012
Pipes and filters
11
Pipe: a way to send the output of one
command to the input of another
Filter: a program that takes input and
transforms it in some way
wc - gives a count of words/lines/chars
grep - searches for lines with a given string
more
sort - sorts lines alphabetically or
numerically
January 21, 2012
Examples of filtering
12
ls -la | more
cat file | wc
man ksh | grep “history”
ls -l | grep “dkl” | wc
who | sort > current_users
January 21, 2012
UNIX Tutorial
13
http://www.ee.surrey.ac.uk/Teaching/Unix/
Google will give you many links
January 21, 2012
UNIX Filesystem
14
The filesystem is your interface to
physical storage (disks) on your machine
storage on other machines
output devices
etc.
Everything in UNIX is a file (programs,
text, peripheral devices, terminals, …)
There are no drive letters in UNIX! The
filesystem provides a logical view of the
storage devices
January 21, 2012
Working directory
15
The current directory in which you are
working
pwd command: outputs the absolute path
(more on this later) of your working directory
Unless you specify another directory,
commands will assume you want to operate
on the working directory
January 21, 2012
Home directory
16
A special place for each user to store personal
files
When you log in, your working directory will
be set to your home directory
Your home directory is represented by the
symbol ~ (tilde)
The home directory of “user1” is represented
by ~user1
January 21, 2012
UNIX file hierarchy
17
Directories may
bin users tmp
contain plain files
or other directories
dkl kangli
Leads to a tree
structure for the
filesystem foo.txt csci1730
Root directory: /
bar.c abcd
January 21, 2012
Path names /
18
Separate directories
bin users tmp
by /
Absolute path
start at root and follow the dkl kangli
tree
e.g. /users/dkl/foo.txt
foo.txt csci1730
Relative path
start at working directory bar.c abcd
.. refers to level above; . refers to working dir.
If /users/dkl/csci1730 is working dir, all these
refer to the same file
../foo.txt ~/foo.txt ~dkl/foo.txt
January 21, 2012
Types of files
19
Plain (- in the first bit)
Most files
Includes binary and text files
Directory (d)
A directory is actually a file
Points to another set of files
Link (l): A pointer to another file or
directory
Special: e.g. peripheral devices
January 21, 2012
Creating links
20
ln –s <curr_file> <link_name>
This command creates a symbolic link
The file “link_name” will be a pointer to the
“curr_file” which may be in another directory or
even on another physical machine
January 21, 2012
File permissions
21
Permissions used to allow/disallow access to
file/directory contents
Read (r) 4, write (w) 2, and execute (x) 1
For owner, group, and world (everyone)
chmod <mode> <file(s)>
chmod 700 file.txt (only owner can read,
write, and execute)
chmod g+rw file.txt
January 21, 2012
Looking at file contents
22
cat <filename(s)>
“concatenate”
output the contents of the file all at once
more <filename(s)>
Output the contents of a file one screen at a time
Allows forward and backward scroll and search
January 21, 2012
Getting help on UNIX commands
23
These notes only give you the tip of the
iceberg for these basic commands
man <command_name> shows you all the
documentation for a command
apropos <keyword> shows you all the
commands with the keyword in their
description
January 21, 2012
The UNIX System
24
Kernel – Heart of the OS
Process scheduling
I/O control (accesses)
Shell – Interpreter between the user and the
computer
Tools and applications
Accessible from shell
Can be run independently of shell
January 21, 2012
UNIX System Programming
25
Programs make system calls (also called
supervisor calls to invoke kernel.
A system call is essentially a procedure call into
the operating system
The procedure call is protected
Types of system calls
File I/O
Process management
Inter-process communication (IPC)
Signal handling
January 21, 2012
System Calls (Library calls)
26
System calls
Interface to the kernel
Program
Code
Library fread
User Space read user
read
kernel
Kernel Space
January 21, 2012
Basic file I/O
27
Processes keep a list of open files
Files can be opened for reading, writing
Each file is referenced by a file descriptor
(integer)
Three files are opened automatically
FD 0: standard input
FD 1: standard output
FD 2: standard error
January 21, 2012
File I/O system call: open()
28
fd = open(path, flags, mode)
path: string, absolute or relative path
flags:
O_RDONLY - open for reading
O_WRONLY - open for writing
O_RDWR - open for reading and writing
O_CREAT - create the file if it doesn’t exist
O_TRUNC - truncate the file if it exists
O_APPEND - only write at the end of the file
mode: specify permissions if using O_CREAT
January 21, 2012
File I/O system call: close()
29
retval = close(fd)
Close an open file descriptor
Returns 0 on success, -1 on error
January 21, 2012
File I/O system call: read()
30
bytes_read = read(fd, buffer,
count)
Read up to count bytes from file and place
into buffer
fd: file descriptor
buffer: pointer to array
count: number of bytes to read
Returns number of bytes read or -1 if error
January 21, 2012
File I/O system call: write()
31
bytes_written = write(fd, buffer,
count)
Write count bytes from buffer to a file
fd: file descriptor
buffer: pointer to array
count: number of bytes to write
Returns number of bytes written or -1 if error
January 21, 2012
System call: lseek()
32
retval = lseek(fd, offset, whence)
Move file pointer to new location
fd: file descriptor
offset: number of bytes
whence:
SEEK_SET - offset from beginning of file
SEEK_CUR - offset from current offset location
SEEK_END - offset from end of file
Returns offset from beginning of file or -1
January 21, 2012
UNIX File access primitives
33
open – open for reading, or writing or create an
empty file
creat - create an empty file
close –
read - get info from file
write - put info in file
lseek - move to specific byte in file
unlink - remove a file
remove - remove a file
fcntl - control attributes assoc. w/ file
January 21, 2012
File I/O using FILEs
34
Most UNIX programs use higher-level I/O
functions
fopen()
fclose()
fread()
fwrite()
fseek()
These use the FILE datatype instead of
file descriptors
Need to include stdio.h
January 21, 2012
Using datatypes with file I/O
35
All the functions we’ve seen so far use raw
bytes for file I/O, but program data is usually
stored in meaningful datatypes (int, char,
float, etc.)
fprintf(), fputs(), fputc() - used to
write data to a file
fscanf(), fgets(), fgetc() - used to read
data from a file
January 21, 2012
36
January 21, 2012