Advanced UNIX
Advanced UNIX
Overview
File System
Shell Programming
Process Management
Inter Process Communication
System Administration
Utilities
Overview
Characteristics of Unix System
Time-sharing
Multi-user, multi-process system
Written in high level language
Hierarchical file system
Consistent file formats
Architecture of Unix System
System Kernel
Utilities and application software
File Structure
Kernel Functions
Schedule Processes
Keep track of files
Control hardware devices
File System
File System
Hierarchical Structure
Consistent treatment of file data
Ability to create and delete files
Dynamic growth of files
Treatment of peripheral devices as files
File System
Types of Files
Regular files
Directories
Special files
Pipes
Named Pipes
I/O devices
File System
File System Layout
#include <fcntl.h>
close ( fd )
int fd;
STAT
sh (bourne shell)
csh
ksh (korn shell)
bash(bourne again shell)
Input/Output Redirection
Create Files
Append to files
Use files as input to the shell
Truncate files
Input/Output Redirection
Redirector Operators
e.g. age=12
Quotes
Single Quotes No evaluation whatsoever
E.g. if expr $a = $b
or if expr $str = abc
a=`expr $a + 1`
Foreground/Background Execution
Background processing by placing an & at the end
Background processes run effectively wrapped around by
nohup
loops run in background enclosed in parentheses
fg command moves a background job into foreground
READ
Main input mechanism for a shell program
Gets a line from the standard input and can store the value
in a variable
Can read a file line by line
Control Structures
If ..then ..else
If <condition>
then
<command>
[elif <condition>
<command>
else
<command>]
fi
Case
case word in
pattern1) action1 ;;
pattern2|pattern3) action2 ;;
.......................................
.......................................
*) default action ;;
esac
While .. do
while <condition>
do
commands
done
test
test <expression>
or
cp command
example
cp file1 file2
Copy Directories
cp -r|R command
Recursively copies the files under a directory and its
subdirectories
tar command
Use tar cvf to create a tar of the subdirectories and files
under a given directory
e.g. tar cvf tarname path of files/subdirectories
Copy Directories
sort +n -m
sorts the nth field, stops sorting at the mth field
Process Status
e.g.
time progname
Uptime
Software Interrupts
Classification of Signals
Termination of a process
Process induced exceptions
Unrecoverable conditions during system calls
Unexpected error condition during a system call
Tracing execution of a process
Handling Signals
#include <signal.h>
signal(sig,function)
int sig;
void (*func)()
Signal allows the calling process to control
signal processing.
Signal Syntax
Some values of sig
SIGHUP hangup
SIGINT interrupt
SIGQUIT quit
SIGKILL kill
SIGFPE floating point exception
SIGBUS bus error
SIGPIPE write on a pipe with no reader
SIGTERM software termination
Signal Syntax
Pipes
Named Pipes
Messages
Shared Memory
Semaphores
Sockets
Pipes
pipe (file_descriptors)
where file_descriptors is a pointer to an array of 2 integers
which are the file descriptors for reading and writing the
pipe
Named Pipes
Similar to pipes
Have directory entries
open system call used to open named pipes
mknod system call to create named pipes
Unrelated processes can also communicate using named
pipes
MESSAGES
cmd IPC_STAT
IPC_SET
IPC_RMID
SHM_LOCK
SHM_UNLOCK
SEMAPHORE
Task Command
Add User useradd
Add Group groupadd
Change password passwd
Listing users and groups logins, listusers
Modify user attributes usermod
Modify group attributes groupmod
Remove user userdel
Remove group groupdel
Communicating with Users
#makedemo
prog : x.o y.o z.o
cc x.o y.o z.o -o prog
x.o : x.c defs.h
cc x.c
y.o : y.c defs.h
cc y.c
z.o : z.c defs.h
cc z.c
MAKE
Macros
Macro Definition
name = value
Example
OBJECTS = x.o y.o z.o
prog : $(OBJECTS)
cc $(OBJECTS) -o prog
SYSTEM UTILITIES
Grep
Options
-n prints the line numbers
-v inverts the sense of the test
-i case insensitive search
Grep
Example
Scans a set of input lines, searching for lines that match any
set of patterns and take a specified action for each pattern on
each line that matches the pattern
AWK
Usage
{ print }
prints all the records
{print $1 $3}
prints the first and third fields of each of the input line
AWK
BuiltIn Variables
cos(x)
log(x)
exp(x)
rand(x)
sin(x)
sqrt(x)
srand(x)
AWK
BuiltIn String Functions