USP Unit 1,2,3 Notes
USP Unit 1,2,3 Notes
Version 1.0
UNIX and ANSI Standards
Developed by Ken Thomson and Dennis Ritchie
SVR 4.0 created by SUN Macro systems and AT &T to set a UNIX
system standard for the industry.
Two standards
•POSIX by Portable Operating System Interface Standard
•ANSI C by American National standard Institute
Version 1.0
The ANSI C Standard
1. Function prototyping:
* ANSI C adopts c++ style of function prototyping where the function
definition and declaration include function names,arguments ‘s data
types and return data types
Version 1.0
* K&R C :
data-type function-name (parameter
name,……………………..)
Version 1.0
2. support of the Constant and volatile data type qualifiers
* Present in ANSI C not in K&R C
* const-implies data cant be changed
/*here printf cant change the
value of x */
int printf(const char * x,….)
{
/* body of printf */
}
Version 1.0
Volatile key word specifies that the value of some variables
may change asynchronously
EX : char get_io()
{
volatile char* io_port=0x7777;
char ch=*io_port;
ch = *io_port;
}
Version 1.0
Wide character support and internationalization
* ANSI C supports to store characters which occupy more
than one
byte Ex : korean character set
Version 1.0
SETLOCALE
#include <locale.h>
Char setlocale (int category, const char* locale);
Category Locale
LC_CTYPE en_US //US
LC_MONETARY fr_FR //French
LC_NUMERIC de_DE //German
LC_TIME C
LC_ALL POSIX
Version 1.0
3. Permit function pointers to be used without dereferencing
foo(14.11,”Hello World”);
funcptr(14.11,”Hello World “); ---in ANSI
(*funcptr)(14.11,”Hello World “); -in KR&C
Version 1.0
ANSI C also defines a set of CPP symbols which may be used in user
programs
Version 1.0
program to illustrate the use of these symbols
#include <stdio.h>
int main()
{
#if __STDC__ == 0 && !defined(__cplusplus)
printf("cc is not ANSI C compliant\n");
#else
printf(" %s compiled at %s:%s. This statement is
at line %d\n",
__FILE__, __DATE__, __TIME__, __LINE__);
#endif
return 0;
}
Version 1.0
THE ANSI/ISO C++STANDARD
• Bjarne stroustrap wrote The annotated C++ Manual ,which became the base
for ANSI C++ standard
Version 1.0
Differences between ANSI C AND ANSI C++
Version 1.0
THE POSIX STANDARDS
Because of many UNIX vendors ,each UNIX version provide its own
set of API’s
Version 1.0
POSIX.1c : specifies multi threaded programming interface
Version 1.0
To ensure program confirms to POSIX.1 standard user should
define
_POSIX_SOURCE as
1. #define _POSIX_SOURCE OR
2. CC -D _POSIX_SOURCE *. C
Version 1.0
_POSIX_C_SOURCE : its value
indicating POSIX version
_POSIX_C_SOURCE value----Meaning
198808L---- First version of POSIX.1
compliance
Version 1.0
Program that shows the posix version
#define _POSIX_SOURCE
#define _POSIX_C_SOURCE 199309L
#include <iostream.h>
#include <unistd.h>
int main()
{
#ifdef _POSIX_VERSION
cout << "System conforms to POSIX: " << _POSIX_VERSION << endl;
#else
cout << "_POSIX_VERSION is undefined\n";
#endif
return 0;
}
Version 1.0
POSIX ENVIRONMENT
Version 1.0
THE POSIX FEATURE TEST MACROS
_POSIX_JOB_CONTROL
• — The system supports BSD type job control
_POSIX_SAVED_ID
• — keeps saved set-UID and set-GID
_POSIX_CHOWN_RESTRICTED
— If -1 user may change ownership of files owned by them else only users with
Version 1.0
_POSIX_NO_TRUNC
• If -1 then any long path name is automatically truncated to
NAME_MAX else an error is generated
_POSIX_VDISABLE
• — If -1 then there is no dissabling character for special characters for
all terminal devices otherwise the value is the disabling character value
Version 1.0
Program to print POSIX-defined configuration
options supported on any given system
#define _POSIX_SOURCE
#define _POSIX_C_SOURCE 199309L
#include <iostream.h>
#include <unistd.h>
int main()
{
#ifdef _POSIX_JOB_CONTROL
cout << "System supports job control\n";
#else
cout << "System does not support job control\n";
#endif
Version 1.0
#ifdef _POSIX_SAVED_IDS
Version 1.0
#ifdef _POSIX_CHOWN_RESTRICTED
Version 1.0
#ifdef _POSIX_NO_TRUNC
#endif
}
Version 1.0
#ifdef _POSIX_VDISABLE
return 0;
Version 1.0
Limits checking at compile time <limits.h>
Version 1.0
_POSIX_ARG_MAX 4096
max size, in bytes of arguments that can be
passed to an exec function call
_POSIX_NGROUP_MAX 0
max number of supplemental groups to which
a process may belong
_POSIX_PATH_MAX 255
max number of characters allowed in a
pathname
Version 1.0
_POSIX_NAME_MAX 14
max number of characters allowed in a
filename
_POSIX_LINK_MAX 8
max number of links a file may have
_POSIX_PIPE_BUF 512
max size of block of data that can be
automatically read from or written
to a pipe file
Version 1.0
_POSIX_MAX_INPUT 255
max capacity, in bytes, of a terminal’s
input queue
_POSIX_MAX_CANON 255
max capacity, in bytes, of a terminal’s
canonical input queue
_POSIX_SSIZE_MAX 32767
max value that can be stored in a
ssize_t- typed object
_POSIX_TZNAME_MAX 3
max number of characters in a time zone name
Version 1.0
Limits checking at run time
Version 1.0
Example illustrating use of sysconfig and
pathconfig
int res;
if(res=sysconf(_SC_OPEN_MAX))==-1)
perror(“sysconf”);
else
cout<<res;
res=pathconf(“/”,_PC_PATH_MAX);
cout <<“ Max path name “ <<res+1;
res=fpathconf(0,_PC_CHOWN_RESTRICTED);
cout <<“chown restricted for stdin”<<res;
Version 1.0
THE POSIX.1 FIPS STANDARD
Job control :
• _POSIX_JOB_CONTROL must be defined
Saved set-UID and set-GID :
• _POSIX_SAVED_IDS must be defined
Long path name is supported
• _POSIX_NO_TRUNC != -1
_only authorised user can change ownership
• _POSIX_CHOWN_RESTRICTED != -1
Version 1.0
_POSIX_VDISABLE should be defined
NGROUP_MAX
• – value should be at least 8
Read and write APIs should return the number of bytes transferred
after the APIs have been interrupted by signals
Version 1.0
THE X/OPEN STANDARDS
The X/Open CAE specifications have broader scope than POSIX and
ANSI
Version 1.0
QUESTIONS
Version 1.0
QUESTIONS
(10)
1. Maximum path length
2. Maximum characters in a file name
3. Maximum number of open files per process
Version 1.0
QUESTIONS
Version 1.0
CHAPTER 5
Version 1.0
UNIX AND POSIX APIs
Version 1.0
Common functions
Version 1.0
POSIX API’s
Version 1.0
UNIX and POSIX Development Environment
Version 1.0
API COMMON CHARACTERISTICS
Version 1.0
API COMMON CHARACTERISTICS
EPERM : a API was aborted because the calling process does not
have super user privilege.
Version 1.0
API COMMON CHARACTERISTICS
Version 1.0
API COMMON CHARACTERISTICS
ENOEXEC : a API could not execute a program via one of the exec
API
ECHILD : a process does not have any child process which it can
wait on
Version 1.0
Calling an API is time consuming ?
Version 1.0
CHAPTER 6
Version 1.0
UNIX FILES
Files are building blocks in an operating system
Version 1.0
File Types
File Types
Character Soft
Block Hard
Version 1.0
FILE TYPES
Regular file
Directory file
Fifo file
Character device file
Block device file
Link file
Socket file
Version 1.0
Regular file
Version 1.0
Directory file
Version 1.0
Device Files
Two types
1. Block device file
Physical device which transmits data a block at a time
EX: hard disk drive, floppy disk drive
Version 1.0
FIFO file
mknod /usr/prog/fifo_pipe p
Version 1.0
Symbolic link file
cat -n /usr/ravi/slink
/*will print contents of /usr/satish/original file*/
Version 1.0
UNIX and POSIX file systems
Version 1.0
Common UNIX files
Version 1.0
UNIX and POSIX file attributes
Version 1.0
UNIX and POSIX file attributes
Version 1.0
Attributes of a file that remain unchanged
File type
File inode number
File system ID
Major and minor device number
Version 1.0
File attributes that are changed using UNIX commands or system
calls
Version 1.0
File attributes that are changed using UNIX commands or system
calls
Version 1.0
Inodes in UNIX system
Version 1.0
mapping of filenames to inode number
65 ..
95 abc
234 a.out
Version 1.0
Application Program Interface to Files
Version 1.0
Struct stat data type<sys/stat.h>
Struct stat
{ dev_ts st_dev; /* file system ID */
ino_t st_ino; /* File inode number */
mode_t st_mode; /* file type and access flags */
nlink_t st_nlink; /*Hard link count */
uid_t st_uid; /* File user ID */
gid_t st_gid; /* File group ID */
dev_t st_rdev; /* Major and Minor device numbers */
off_t st_size; /*File size in number of bytes */
time_t st_atime; /* Last access time */
time_t st_mtime; /* last modofication time */
time_t st_ctime /* Last status change time */
};
Version 1.0
UNIX KERNEL SUPPORT FOR FILES
Version 1.0
Kernel support to open system call
1. The kernel will search the file descriptor table and look for
first unused entry and index to the entry is returned as file
descriptor of the opened file.
2. The kernel will scan the file table in its kernel space , to
find an unused entry that can be assigned to reference the
file
3. If an unused entry is found in the file table,then the
following events will occur:
Version 1.0
Kernel support to open system call
c. The file table entry will contain the current file pointer of
the open file.
rw
rc=1 rc=2
w
rc=1
Version 1.0
Data Structure for File Manipulation
Inode
2 Status flags Information
O_CREAT,O_EXCL,
3 ptr O_TRUNC,O_APPEN Reference
D,O_SYNC count
4
Offset pointer
5
Reference count
It checks the file table entry to make sure that the file is
opened with appropriate mode.
Version 1.0
Kernel support : read system call
The kernel will check the type of file in the inode record
and invokes an appropriate driver function to initiate the
actual data transfer with a physical file.
Version 1.0
Kernel support : close system call
Version 1.0
Relationship between C stream pointers and file
descriptors
fopen fuction calls open system call to perform all the actual
opening
Version 1.0
Directory files
Directoryfunction Purpose
opendir Opens a directory file (returns DIR *)
readdir Reads next record from file
closedir closes a directory file
rewinddir Sets file pointer to beginning of file
Version 1.0
Hard and symbolic links
114 . 515 .
65 .. 65 ..
95 old.c 345 new.c
234 a.out 325 Fun.c
Version 1.0
Difference : cp and ln command
Version 1.0
Difference : hard link and symbolic link
Cannot link files across file Can link files across file system
systems
Increase hard link count of the Does not change hard link count
linked inode of the linked inode
Version 1.0
UNIX File APIs
Version 1.0
Open system call
name
Access_mode : An integer which specifies how file is to be
accessed by calling process
Version 1.0
Access mode flag Use
O_APPEND
O_CREAT
O_EXCL
O_TRUNC
O_NONBLOCK
O_NOCTTY
O_SYNC
Version 1.0
Access modifier flags
O_APPEND : appends data to end of file
O_TRUNC : if the file already exists, discards its contents
and sets file size to zero
O_CREAT : creates the file if it does not exist
O_EXCL : used with O_CREAT only. This flag causes
open to fail if the file exists
O_NONBLOCK : specifies that any subsequent read or
write on the file should be non
blocking
Version 1.0
Ex : int fd;
fd=open(“/etc/passwd”,O_RDONLY);
fd=open(“foo.txt”,O_WRONLY|O_APPEND); like cat>>temp.c
fd=open(“../foo.txt”,O_WRONLY|O_TRUNC); like cat> temp.c
fd=open(“foo.txt”,O_WRONLY|O_CREAT|O_TRUNC,0644)
fd=open(“foo.txt”,O_WRONLY|O_CREAT|O_TRUNC,
S_IRUSR|S_IWUSR|S_IRGRP|S_IOTH
Prototype:
mode_t umask ( mode_t new_umask);
mode_t old_mask = umask (S_IXGRP|S_IWOTH|
S_IXOTH);
/*removes execute permission from group and write&execute
permission from others*/
the file is created with bit wise ANDing the ones compliment
of the calling process umask value
Actual_permission = permission & ~umask_value
1.0
Version Actual_permission=0557&(~031)=0546
Creat
#include <sys/types.h>
#include <unistd.h>
Int creat (const char* pathname, mode_t mode)
#include <sys/types.h>
#include <unistd.h>
ssize_t read (int fdesc ,void* buf, size_t size);
Ex :
#define BUFSIZE 100
int n; char buf[BUFSIZE];
while((n=read(fd,buf,BUFSIZE) >0)
Version 1.0
Write system call
#include <sys/types.h>
#include <unistd.h>
ssize_t write (int fdesc ,void* buf, size_t size);
Version 1.0
Write system call
Ex:
# define BUFSIZE 8192
Int n; cahr buf[BUFSIZE];
N=write(fd,buf,BUFSIZE);
Version 1.0
Close system call
#include <unistd.h>
int close (int fdesc);
Version 1.0
fcntl system call
F_SETFL : sets or clears control flags that are specified in the third
argument (allowed flags are O_APPEND & O_NDELAY).
Version 1.0
fcntl system call CONTD
int cur_flags=fcntl(fdesc,F_GETFL);
int fcntl(fdesc,F_SETFL,cur_flag|O_APPEND|
O_NONBLOCK);
Version 1.0
fcntl system call CONTD
The read and write system calls are always relative to the
current offset within a file.
the lseek system call is used to change the file offset to a
different value
Lseek allows a process to perform random access of data
on any opened file.
lseek is incompatible with FIFO files,character device files
and symlink files.
Prototype :
#include <sys/types.h>
#include <unistd.h>
Off_t lseek (int fdesc , off_t pos, int whence)
Version 1.0
lseek system call
Version 1.0
link system call
The link function creates a new link for existing file.
The function does not create a new file ,rather it creates a
new pathname for an existing file.
Prototype :
#include <unistd.h>
int link (const char* cur_link , const char* new_link) ;
/*test_ ln.C */
#include <iostream.h>
#include <stdio.h>
#include <unistd.h>
int main ( int argc, char* argv[] )
{
if ( argc!=3 ) {
cerr << "usage: " << argv[0] << " <src_file>
<dest_file>\n";
return 0;
}
Version 1.0
Implementation of mv command of UNIX
#include<iostream.h>
#include<unistd.h>
#include<string.h>
int main(int argc,char * argv[])
{
if(argc !=3 || !strcmp(argv[1],argv[2]))
cerr <<“usage :” <<argv[0] <<“old_link>
<new_link>\n”;
else
if(link(argv[1],argv[2]) ==0)
return unlink(argv[1]);
return -1;
}
Version 1.0
READING THE INODE :Stat,fstat
#include <sys/types.h>
#include <unistd.h>
int stat (const char* path_name, struct stat* statv)
int fstat (const int fdesc, struct stat* statv)
Version 1.0
READING THE INODE :Stat,fstat contd
Struct stat
{ dev_t st_dev;
ino_t st_ino;
mode_t st_mode;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
dev_t st_rdev;
off_t st_size;
time_t st_atime;
time_t st_mtime
time_t st_ctime
};
both the functions return value 0 on success and -1 on
fail.
The possible error values are pathname/file descripor is
invalid,process lacks the permission,the function
interrupted.
Version 1.0
READING THE INODE :Stat,fstat contd
Version 1.0
READING THE INODE :Stat,fstat contd
Version 1.0
READING THE INODE :Stat,fstat contd
Version 1.0
Implementation of UNIX ls -l command
Version 1.0
Implementation of UNIX ls -l command
#ifndef MAJOR
#define MINOR_BITS 8
#define MAJOR(dev) ((unsigned)dev >>
MINOR_BITS)
#define MINOR(dev) ( dev &
MINOR_BITS)
#endif
Version 1.0
Implementation of UNIX ls -l command
Version 1.0
Implementation of UNIX ls -l command
Version 1.0
Implementation of UNIX ls -l command
Version 1.0
Implementation of UNIX ls -l command
Version 1.0
Implementation of UNIX ls -l command
Version 1.0
Implementation of UNIX ls -l command
ofs << ' ' << pw_p->pw_name << ' ' <<
gr_p->gr_name << ' ';
Version 1.0
Implementation of UNIX ls -l command
if ((statv.st_mode&S_IFMT) == S_IFCHR ||
(statv.st_mode&S_IFMT)==S_IFBLK)
ofs << MAJOR(statv.st_rdev) << ','
<< MINOR(statv.st_rdev);
else ofs << statv.st_size;
/* show file size or major/minor no. */
ofs << ' ' << ctime (&statv.st_mtime); /*
print last modification time */
ofs << ' ' << path_name << endl;
/* show file name */
}
Version 1.0
Implementation of UNIX ls -l command
Version 1.0
Access system call
#include <unistd.h>
int access (const char* path_name, int flag);
The flag argument contains the new access permission and any
special flags to be set on the file.
The flag argument is same as in the open API.
Version 1.0
Chmod, fcmod system call
Flag value can be specified as an octal integer value in UNIX, or
constructed from the manifested constants defined in
<sys/stat.h>
/* chmod.C*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
Version 1.0
Implementation of chmod command
void change_mode()
{
int flag = (S_IWGRP | S_IROTH | S_IXOTH);
struct stat statv;
if (stat("/usr/satish/unix.doc",&statv))
perror("stat");
else
{
flag = (statv.st_mode & ~flag) | S_ISUID;
if (chmod ("usr/joe/funny.book", flag))
perror("chmod");
}
}
Version 1.0
chown, fchown and lchown system calls
#include <unistd.h>
#include <sys/types.h>
int chown (const char* path_name, uid_t uid, gid_t gid);
int fchown (int fdesc, uid_t uid, gid_t gid);
int lchown (const char* path_name, uid_t uid, gid_t gid);
#include <iostream.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
Version 1.0
Implementation of chown command
if (UID == (uid_t)-1)
cerr<< "Invalid user name\n";
else
for (int i=2; i < argc; i++) /* do for each file specified */
if (stat(argv[i],&statv)==0)
{
if (chown(argv[i], UID, statv.st_gid)) perror("chown");
} else
perror("stat");
return 0;
}
Version 1.0
Utime system call
• The function modifies the access and modification time stamps of a file
#include <unistd.h>
#include <sys/types.h>
#include <utime.h>
int utime (const char* path_name, struct utimbuf* times);
•The times argument specifies the new access time and modification time for
the file.
•The struct utimbuf is defined in <utime.h> header as
Struct utimbuf {
Time_t actime; /* access time*/
Time_t modtime; /* modification time */
}
Version 1.0
Utime system call contd
Version 1.0
/* Emulate the UNIX touch program */
#include <iostream.h>
#include <stdio.h>
#include <sys/types.h>
#include <utime.h>
#include <time.h>
Version 1.0
FILE AND RECORD LOCKING
Version 1.0
Shared and exclusive locks
Version 1.0
A read lock allows processes to set overlapping read locks
but not write locks. Other processes are allowed to lock
and read data from the locked regions.
Version 1.0
If a file lock is not mandatory, it is an advisory. An advisory
lock is not enforced by a kernel at the system call level
Version 1.0
Advisory locks
Version 1.0
FCNTL file locking
Cmd_flag Use
F_SETLK Sets a file lock. Do not block if this
cannot succeed immediately.
Version 1.0
For file locking, the third argument is an address of a struct flock-
typed variable.
This flock specifies a region of a file where the lock is to be set,
unset or queried.
struct flock
{
short l_type;
short l_whence;
off_t l_start;
off_t l_len;
pid_t l_pid;
};
Version 1.0
l_type and l_whence fields of flock
Version 1.0
l_whence value Use
Version 1.0
The l_len specifies the size of a locked region beginning
from the start address defined by l_whence and l_start. If
l_len is 0 then the length of the lock is imposed on the
maximum size and also as it extends. It cannot have a –ve
value.
Version 1.0
LOCK PROMOTION AND SPLITTING
Version 1.0
Mandatory locks can be achieved by setting the following
attributes of a file.
Turn on the set-GID flag of the file.
Turn off the group execute right of the file.
Version 1.0
Program to illustrate the locking mechanism
#include <iostream.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
int main (int argc, char* argv[])
{
struct flock fvar;
int fdesc;
Version 1.0
while (--argc > 0) { /* do the following for each file
*/
if ((fdesc=open(*++argv,O_RDWR))==-1)
{
perror("open"); continue;
}
fvar.l_type = F_WRLCK;
fvar.l_whence = SEEK_SET;
fvar.l_start = 0;
fvar.l_len = 0;
Version 1.0
/* Attempt to set an exclusive (write) lock on the entire file */
while (fcntl(fdesc, F_SETLK,&fvar)==-1)
{
/* Set lock fails, find out who has locked the file */
while (fcntl(fdesc,F_GETLK,&fvar)!=-1 &&
fvar.l_type!=F_UNLCK)
{
cout << *argv << " locked by " << fvar.l_pid<< " from " <<
fvar.l_start << " for "<< fvar.l_len << " byte for " <<
Version 1.0
(fvar.l_type==F_WRLCK ? 'w' : 'r') << endl;
if (!fvar.l_len) break;
fvar.l_start += fvar.l_len;
fvar.l_len = 0;
} /* while there are locks set by other processes */
} /* while set lock un-successful */
Version 1.0
// Lock the file OK. Now process data in the file
/* Now unlock the entire file */
fvar.l_type = F_UNLCK;
fvar.l_whence = SEEK_SET;
fvar.l_start = 0;
fvar.l_len = 0;
if (fcntl(fdesc, F_SETLKW,&fvar)==-1)
perror("fcntl");
}
return 0;
} /* main */
Version 1.0
Directory File APIs
Version 1.0
Directory File APIs
To create a directory
int mkdir (const char* path_name , mode_t mode);
Version 1.0
Difference between mkdir and mknod
Version 1.0
Directory File APIs
Version 1.0
DIRECTORY RELATED FUNCTIONS
Opendir:
DIR* opendir (const char* path_name);
This opens the file for read-only
Readdir:
Dirent* readdir(DIR* dir_fdesc);
Version 1.0
DIRECTORY RELATED FUNCTIONS
Closedir :
int closedir (DIR* dir_fdesc);
Rewinddir :
void rewinddir (DIR* dir_fdesc);
Version 1.0
DIRECTORY RELATED FUNCTIONS
rmdir API:
int rmdir (const char* path_name);
Version 1.0
Device file APIs
Version 1.0
Device file APIs
To create:
int mknod(const char* path_name, mode_t
mode, int device_id);
Version 1.0
MAJOR AND MINOR NUMBERS
Version 1.0
Device file APIs
Version 1.0
FIFO File APIs
To create:
int mkfifo( const char* path_name, mode_t mode);
Version 1.0
How is synchronization provided?
Version 1.0
FIFO File APIs
Version 1.0
FIFO File APIs
Version 1.0
FIFO File APIs
Version 1.0
Uses of the fds argument are:
fds[0] is a file descriptor to read data from the
FIFO file.
Version 1.0
Symbolic Link File APIs
Version 1.0
Symbolic Link File APIs
To create :
int symlink (const char* org_link, const char*
sym_link);
Version 1.0
Symbolic Link File APIs
Version 1.0
QUESTIONS
Version 1.0
QUESTIONS
Version 1.0
QUESTIONS
Version 1.0
QUESTIONS
Version 1.0