Documente Academic
Documente Profesional
Documente Cultură
A system call is just what its name implies -- a request for the
File descriptors
The first three are already opened when the process begins
0: The standard input
When the parent process forks a process, the child process inherits the file
descriptors of the parent.
Introduction
- In order to make use of services such as application programs file
creation,process duplication, and interprocess communication,must talk
to the operating system.
- a collection of routines called system calls,which are the programmers
functional interface to the UNIX kernel.
- The UNIX system calls can be loosely grouped into three main categories:
file management
process management
error handling
Files
open
close
chmod
fcntl
read
fstat
write
ftruncate
mknod
ioctl
lseek
unlink
truncate
stat
chown
sync
dup
gethostname
htonl
special
Directory
link
pipe
Sockets
Internet sockets
gethostbyname
dup2
htons
7
accept
bind
inet_addr
connect
inet_ntoa
getdents
listen
socket
KERNEL BASICS
- User programs make use of the kernel via the system call interface.
8
Meaning
0
1
2
standard input
standard output
standard error
10
- For Example,
the printf() library function always sends its output
using file descriptor 1.
the scanf() always reads its input using file descriptor 0.
Fd0
i/p
Fd1
o/p
Fd2
error
File
Many file descriptors, one file
11
Description
open()
create()
read()
Read the content from the file and places it into buffers
write()
close()
lseek()
ioctl()
dup(),dup2()
umask()
5.Close
6.lseek
9.ioctl
7.stat
8.fstat
L1.2
13
int fd;
fd=open(hi.txt,O_CREAT | O_RDWR,0600);
2: int fd ;
fd=open(hello.txt,O_RDWR,0600);
14
MEANING
Open for read only.
Open for write only.
Open for both read and write.
MEANING
Position the file pointer at the end of the file
before each write().
O_CREAT
O_EXCL
O_TRUNC
Creating
a File
- To create a file, use the O_CREAT flags as part of the mode flags and
supply the initial file-permission flag settings as an octal value.
int fd;
fd = open( filename, O_CREAT | O_RDWR, 0600);
16
int fd=open(m.txt,O_RDWR);
write(fd,HELLO WORLD,10);
18
MEANING
SEEK_SET
SEEK_CUR
SEEK_END
Eg:
20
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
main()
{
int fd;
fd = open(hi.txt, O_CREAT | O_RDWR, 0600 );
write( fd, HELLOWORLD!, 6);
UNLLOW
21
*/
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
main()
{
int fd1,fd2;
char buf[1024];
long int n;
close(fd2);
}
22
*/
File Status
The i-node data structure holds all the information about a file
except the file's name and its contents.
Sometimes your programs need to use the information in the i-node
structure to do some job. You can access this information with the
stat() and fstat() system calls.
System Call : int stat( const char* path, struct stat* buf );
int fstat( int fd, struct stat* buf ) ;
path : represents the name and the relative or absolute path of file.
stat() fills the buffer buf with information about the file name.
The stat structure is defined in /usr/include/sys/stat.h.
24
Output :
Cat hi.txt
abcd
Space
Fstat output
Stat output
Inode number :160000000
File size: 11
ioctl Function:
28
umask Function:
The umask function sets the file mode creation mask for the process and
returns the previous value.
#include <sys/stat.h>
Syntax: mode_t umask(mode_t newmask);
Returns: previous file mode creation mask.
The newmask argument is formed as the bitwise OR of any of the nine
constants like S_IRUSR, S_IWUSR, and so on.
29
Eg:
o/p:
#include<stdio.h>
main()
{
int fd;
fd=open(m.txt,O_CREAT|O_RDWR,0777);
umask(0002);
close(fd);
}
ls l m.txt
-rwx-rwx-r-x
Description
fopen()
fclose()
fflush()
gets() reads from standard input until it reads the new line
character end-of-file
fgets() reads characters from stream and stores them into the
buffer pointed.
fseek()
Sets the file position indicator for the stream pointed by stream
getc(), fgetc(),
getchar()
putc(), fputc(),
putchar()
fopen()
fclose()
fflush()
fseek()
fgetc()
getc()
fgets()
gets()
getchar()
putchar()
fputc()
putc()
Standard I/O
The fopen function opens a specified file.
Syntax:
FILE *fopen(const char *filename, const char *restrict type);
Filename:it opens the file named by the filename parameter in
specified mode.
Eg :
FILE *fp;
fp=fopen(m.txt,r);
Mode
Description
r+
Open a file for writing. If the file already exists its content will be
wiped out. Otherwise afresh file is created for writing.
w+
Open a file for writing and reading. If the file already exists its
content will be wiped out. Otherwise afresh file is created for
writing.
a+
fclose:
An open stream is closed by calling fclose.
Syntax: int fclose(FILE *fp);
All three return: next character if OK, EOF on end of file or error.
The function getchar is equivalent to getc(stdin). getc can be
implemented as a macro, whereas fgetc cannot be implemented as a
macro.
#include<stdio.h>
main()
{
char c;
FILE *fp1,*fp2;
fp1=fopen(f1.txt,r);
fp2=fopen(f2.txt,w);
while((c=fgetc(fp1))!=EOF)
fputc(c,fp2);
fclose(fp1);
fclose(fp2);
}
o/p:
cat f1.txt
hello2014
cat f2.txt
hello2014
*/
*/
Eg:
#include<stdio.h>
main()
{
int c;
while((c=getchar())!=EOF)
putchar(toupperc);
}
Eg2:
#include<stdio.h>
main()
{
int c;
c=getchar();
putchar(c);
}
o/p:
$./a.out
hello2014
HELLO2014
o/p:
$/a.out
hello
h
Line-at-a-Time I/O:
fgets:
gets:
#include<stdio.h>
main()
{
char c[100];
FILE *fp;
fp=fopen(m.txt,r);
if((fgets(c,5,fp)!=NULL)
puts(c);
fclose(c);
}
o/p:
$./a.out
$ cat m.txt
hello2014
HELL\O-nullchar
*/
fflush:
The C library function fflush,flushes the output buffer of a stream.
Syntax: int fflush(FILE *stream)
stream -- This is the pointer to a FILE object that specifies a
buffered stream.
Any buffered output data is flushed before the file is closed. Any
input data that may be buffered is discarded. If the standard I/O
library had automatically allocated a buffer for the stream, that
buffer is released.
This function returns zero value on success. If an error occurs, EOF
is returned and the error indicator is set(i.e. feof).
Eg: fflush(fp);
fflush(stdin);
fflush(stdout);
Fseek :
It is equivalent to lseek system call
Sets the file pointer position at the given offset from the whence
location in the file.
Syntax : int fseek(FILE *stream,long int offset,int whence);
o/p:
main()
{
FILE *fp;
fp=fopen(m.txt,r+);
puts(hello,fp);
fseek(fp,0,SEEK_END);
puts(2014,fp);
fcolse(fp);
}
vi m.txt
//existing file
hello2014
*/
DIRECTORIES MAINTAINANCE
1.
2.
3.
4.
5.
6.
7.
8.
link()
The UNIX system file structure allows more than one named
reference to a given file, a feature called "aliasing". all names of the
file refer to the same data.
Link() systemcall refers to hard link.
Syntax: int link(const char *filename1,const char *filename2);
or
int link(original_name, alias_name)
Filename1=original_name
Filename2=alias_name;
If the original file is deleted,the alias file can still be used to show the
content of the original file.
link() will fail and no link will be created if any of the following
conditions holds:
a path name component does not exist.
original_name does not exist.
alias_name does exist.
original_name is a directory and you are not the superuser.
a link is attempted across file systems.
The destination directory for alias_name is not writable.
The destination directory is on a mounted read-only file system.
Eg:
o/p:
#include <stdio.h>
main()
{
link(oldfile", newfile);
}
$cat oldfile
hello world
$cat newfile
helloworld
symlink()
Symlink() systemcall refers to softlink or symbolic link.
Syntax: int symlink(const char *filename1,const char *filename2);
or
int symlink(original_name, alias_name)
Filename1=original_name
Filename2=alias_name
If the original file is deleted,the alias file will becomes useless,that is,it
will lose its content too.
Eg:
o/p:
#include <stdio.h>
main()
{
symlink(oldfile", newfile);
}
$cat oldfile
hello world
$cat newfile
helloworld
unlink()
The opposite of the link() system call is the unlink() system call.
Syntax: int unlink(const char *filename1);
unlink() fails if any of the following conditions holds:
A path name component does not exist.
Chmod()
To change the file and directory permissions for three types of system
users-user,group,others.
Syntax: int chmod(const char * filename,int mode);
filename : name of file,absolute or relative path
mode : file access permissions
Eg:
o/p:
#include<stdio.h>
main()
{
chmod(dir1,0777);
}
$ls l dir1
-rwxrwxrwx.
Chown()
To change the owner or group of given files
Syntax: int chown (const char *filename,uid_t UID,gid_t GID);
filename : name of file,absolute or relative path
UID : represents the ID of the owner ie., user
Gid : represents the ID of the group
Eg:
#include<stdio.h>
main()
{
chown(m.txt,102,12);
}
o/p:
$ls l dir1
-rwxrwxrwx 1
102
12
File permissions
links
owner
group
46
bytes
filename
o/p:
#include<stdio.h>
main()
{
mkdir(dirname,0777);
}
./a.out
$ls
dirname
Returns 0 on success
Returns 1 on failure
Syntax: int mkdir(const char *filename,mode_t);
Eg:
o/p:
#include<stdio.h>
main()
{
rmdir(dirname);
}
./a.out
$ls
Eg:
o/p:
#include<stdio.h>
main()
{
chdir(/home/usract/dirname);
}
$./a.out
/home/usract/dirname
Opendir()
Closedir()
Readdir()
Telldir()
Seekdir()
Rewinddir()
opendir()
The opendir function opens a directory and establishes a directory stream.
#include<dirent.h>
Syntax:DIR *opendir(const char *path);/* directory path name*/
DIR data structure is defined in the <direct.h> or <sys/dir.h>
It returns the DIR pointer or NULL on error
closedir()
The closedir() close the directory.
#include<dirent.h>
Syntax: int closedir(DIR *dirp)
readdir()
It reads the next directory record from a directory file referenced by the
dirp(directory pointer) argument.
#include<dirent.h>
Syntax:Struct dirent *readdir(DIR *dirp); * DIR pointer from opendir*
Returns structure or NULL on EOF or error
struct dirent
{
u-long d_ino;
/* i-node number for the dir entry */
u_short d_reclen;
/* length of this record */
u_short d_namelen ;
/* length of the string in d_name */
char d_name[MAXNAMLEN+1] ;
/* directory name */
};
telldir()
It returns the current location in directory stream.
#include<direct.h>
Syntax: Off_t telldir(DIR *dirp); /* DIR pointer from opendir*/
returns the current location in the directory stream or -1 if an error
occurs;
seekdir()
Set the position of the next readdir() call in the directory stream.
Seekdir function sets the directory entry pointer in the directory
stream given by dirp.
The value of loc used to set the position.
Seekdir() should be used with an offset returned by telldir().
#include<sys/types.h>
#include<direct.h>
Syntax:Void seekdir(DIR *dirp, long int loc);
rewinddir()
This function resets a file pointer associated with a dir_fdesc, so that if
readdir is called again ,it will scan the directory from the beginning.
#include<direct.h>
Syntax: void rewinddir(DIR* dir_fdesc)
/*opendir(),readdir(),telldir(),seekdir(),rewinddir(),closedir()*/
#include<stdio.h>
#include<dirent.h>
main()
{
dir *mydir;
struct dirent *files;
mydir=opendir(/home/usract/syscall);
while(files=readdir(mydir))
printf(\n the current position is %ld\n,telldir(mydir));
seekdir(mydir,1024);
printf(\n the current position is %ld\n,telldir(mydir));
rewinddir(mydir);
printf(\n the current position is %ld\n,telldir(mydir));
closedir(mydir);
}
o/p:
List the files in /home/usract/syscall
the current position is 24454545454
the current position is 1024