Professional Documents
Culture Documents
Unix Fundamentals & System Programming
Unix Fundamentals & System Programming
Unix Fundamentals & System Programming
&
System Programming
History of UNIX
Ken Thompson of AT&T Bell Laboratories designed UNIX in late 1960s Two versions of UNIX that emerged are AT&T Unix and BSD Unix
In 1989, AT&T and Sun Microsystems joined together and developed system V release 4 (SVR4)
Two of the main standards mainly in use are POSIX(Portable Operating System Interface) and X/open standard. In 1988, MIT formed Xconsortium which developed vendor-neutral Xwindow System.
Copyright Datagrid Technologies UNIX Basics 2
File system
dev
bin
tmp
home
etc
var
lib
usr
sh
console lp0 ls user1
spool
bin
src
Commands
File name
cat
cat command takes the input from the keyboard, and sends the output to the monitor We can redirect the input and output using the redirection operators
$ cat>file1 Type the content here <ctrl d> $ cat file1 Displays the content of the file $cat>>file1 (will append to the content of the file)
grep
grep -Global Regular Expression Printer is used for searching regular expressions Syntax
grep <options> <pattern> <filename(s)>
grep options
-c displays count of the number of occurrences
-n
-v -i
10
Patterns
* - matches 0 or more occurrence of the previous character [^pqr] - Matches a single character which is not p ,q or r ^pqr -Matches pqr at the beginning of the line pqr$ -Matches pqr at the end of the line .- Matches any one character
11
12
Can also specify the line number from which the data has to be displayed $ tail +5 file1
13
Filter command - tr
tr - translate filter used to translate a given set of characters usage :
$tr [a-z] [A-Z] <filename
14
Filter command - tr
Useful options for tr -s char
Squeeze multiple contiguous occurrences of the character into single char
-d char
Remove the character
15
Shell Scripts
when a group of commands have to be executed regularly, they should be stored in a file, and the file executed as a shell script or a shell program. it is not mandatory using the .sh extension for shell scripts; makes them easy to match with the wild cards. shell script needs to have execute permission when invoked by its name.
By passing the shell script name as an argument to the shell. For example:
$sh script1.sh If the shell script is assigned execute permission, it can be executed using its name. For example: $./script1.sh
19
parameters are specified after the name of the shell script when invoking the script.
Within the shell script, parameters are referenced using the predefined variables $1 through $9. In case of more than 9 parameters, other parameters can be accessed by shifting.
20
Built-in variables
Following are built-in variables supported
$0, $1$9 $* $@ - positional arguments - all arguments - all arguments
21
----------------------script2.sh-------------------------echo Total parameters entered: $# echo First parameter is : $1 echo The parameters are: $* shift echo First parameter is : $1 ------------------------------------------------------------
$script2.sh a b 123
$1 $2 $3
$*=a b
123
$#=3
22
The expression can be formed using a combination of shell variables and the operators supported by the test command. These operators provide facility to compare numbers, string and logical values, file types and file access modes.
23
24
25
26
Combining conditions
It is possible to combine conditions by using following operators:
-a (logical AND operator) -o (logical OR operator) ! (logical NOT operator)
27
28
29
30
System Programming
open
Name: open and create a file or device Prototype: #include<sys/types.h> #include<fcntl.h> int open(const char *path_name, int access_mode, mode_t permissions);
Description: open function establishes a connection between a process and a file. existing file create a new file Return value:- return value is a file descriptor Otherwise return -1 failure
1st argument: Pathname 2nd argument:Access modes O_RDONLY O_WRONLY O_RDWR Access Modifiers O_CREAT O_EXCL O_APPEND O_TRUNC 3rd argument Permision S_IRWXUSR S_IRWGRP S_IROTH S_IRWXU
Inode table
244 rc=1
new.dat
3 user1
new.dat
fd=open(new.dat,O_RDONLY,0) user1 f1 184 new.dat 244
f1
Read
Name: read-reading data from a file Prototype #include<sys/types.h> #include<unistd..h> Size_t read(in fdesc,void *buf, size_t size); Return value: return number of bytes of data successfully read and stored in the buffer. Otherwise returns -1 failure
Name: write-writing data into a file Prototype #include<sys/types.h> #include<unistd..h> ssize_t write (in fdesc, void *buf, size_t size);
Write
Return Value: return number of bytes of data successfully write and stored in the buffer. Otherwise returns -1 failure
Name: lseek: random access Prototype #include <unistd.h> off_t lseek(int fd, off_t pos, int whence); Pos byte offset to be added Whence SEEK_CUR SEEK_SET SEEK_END File pointer=whence value+pos
lseek:
Inode table
244 rc=1
new.dat
Fp=0+20
fd=open(new.dat,O_RDONLY,0) lseek(fd,20,SEEK_SET)
Name fork - create a new process Prototype #include <unistd.h> pid_t fork(void);
fork
Description The fork() function shall create a new process. The new process (child process) shall be an exact copy of the calling process (parent process) except as detailed below: The child process shall have a unique process ID.
Pid=422 P1
Pid=425
Pid=425
Int main() { printf(parent); pid=fork() // pid=0 printf(after fork): }
P2
RETURN VALUE Upon successful completion, fork() shall return 0 to the child process and shall return the process ID of the child process to the parent process. Both processes shall continue to execute from the fork() function. Otherwise, -1 shall be returned to the parent process, no child process shall be created, and errno shall be set to indicate the error.
wait
Name wait, waitpid - wait for the parent process Prototype #include <sys/types.h> #include <sys/wait.h> pid_t wait(int *status); pid_t waitpid(pid_t pid, int *status, int options);
Pid=422 P1
P2
Pid=425
Int main() { printf(parent); pid=fork() Pid=425 If(pid==0) printf(child pid=%d,getpid()): else $./a.out { parent wait(); Child pid=425 printf(parent pid=%d,getpid()); Parent pid=422 } $ } Int main() { printf(parent); pid=fork() If(pid==0) Pid=0 printf(child pid=%d,getpid()): else { wait(); printf(parent pid=%d,getpid()); } }
system
Name system - execute a shell command Prototype #include <stdlib.h> int system(const char *command); Return Value The value returned is -1 on error (e.g. fork(2) failed), and the return status of the command otherwise.
stat/lstat
Name To retrieve property of a file prototype #include <unistd.h> #include <sys/stat.h> #include <sys/types.h> int stat(const char *path, struct stat *buf); int lstat(const char *path, struct stat *buf);
struct stat ( dev_t st_dev; /* inode's device */ ino_t st_ino; /* inode's number */ mode_t st_mode; /* inode protection mode */ nlink_t st_nlink; /* number of hard links */ uid_t st_uid; /* user ID of the file's owner */ gid_t st_gid; /* group ID of the file's group */ dev_t st_rdev; /* device type */ )
Using the S_*() macros on the st_mode member lets you find out type of file S_ISBLK(mode) S_ISCHR(mode) S_ISDIR(mode) S_ISFIFO(mode) S_ISLNK(mode) S_ISREG(mode)