Unix File System Calls
Shashank Gupta
BITS Pilani Assistant Professor
Department of Computer Science and Information Systems
Pilani Campus
BITS Pilani, Pilani Campus
Points to be Covered
User Process
Kernel Process
System Calls
Hierarchy of System Calls
Creat
Open
Close
Read
Write
Lseek
2
BITS Pilani, Pilani Campus
Problem 1
• If a process makes a request to access 9000 byte offset, then
find out which index block contains this byte information.
• 9000 / 1024 = 8 , 9000 mod 1024 = 808
• That means 9000 – 808 = 8192 byte offset is last offset in 8th
index block
• Since 808 < 1024 , therefore byte offset 9000 is surely present
in 9th index block with block no. 367 and in 808th byte
BITS Pilani, Pilani Campus
If a process makes a request to access 350,000
byte offset, then find out which index block
contains this byte information.
• First 10 direct blocks can access 10240 bytes or 10K bytes.
• 350,000 > 10240, therefore lets look into single indirect block.
• Single indirect can address 256 blocks, each block contains 1024
bytes, so 256 * 1024= 262144 bytes can be accessed = 256KB
• So 10 K + 256 K = 266 K = 272,384 bytes
• Still 350,000 > 272,384 , Go ahead and search in double indirect.
• 350,000 – 272,384 = 77,616 byte no in double indirect.
• First entry in double indirect block points to first single indirect
block which holds 256 block nos, each block contains 1024 bytes,
and hence points to 256K byte addresses i.e 262,144 byte address.
• Since 77,616 < 262,144, 350,000 will be there in one of the blocks
pointed to by single indirect.
• 77616/1024 = 75, 77616 mod 1024 = 816
• It comes out to be 75th block and 816th byte entry.
BITS Pilani, Pilani Campus
Modes of Operating System
5
BITS Pilani, Pilani Campus
File System Calls
System It is a programmatic way in which a computer
program requests a service from the kernel of
Calls the operating system.
It provides an interface between a process and
operating system to allow user-level processes
to request services of the operating system.
System calls are the only entry points into the
kernel system.
6
BITS Pilani, Pilani Campus
File System Calls
7
BITS Pilani, Pilani Campus
Lower level File System Algorithms
Name of Operation
Algorithm
namei Parses the path name one component at a time and returns the inode of the input path
name.
iget Allocates the in-core copy of the inode if it exist and locks it. The inode is returned with
reference count 1 greater than previous.
iput Releases the inode by decrementing the reference count. Unlocks the inode to provide
access to other system calls. Stores back if in-core copy is different from disk copy.
bmap Converts a file byte offset into a physical disk block.
ialloc Allocates the inode for a new file from the free list of inodes
ifree If reference count becomes 0, the inode is released and added to the free list of inodes
alloc Allocates the disk inode
free Releases the disk inode
8
BITS Pilani, Pilani Campus
creat() system call
Syntax is
fd = creat(pathname,mode)
int creat(const char *path, mode_t mode);
BITS Pilani, Pilani Campus
creat() system call
# include <stdio.h>
int main ()
{
int fd1, fd2;
printf(“/nThis would create two files”);
fd1= creat(“txt1.txt”, 0777);
fd2= creat(“txt2.txt”, 0777);
}
BITS Pilani, Pilani Campus
open system call
Syntax is
fd = open(pathname,flags,modes);
int open(const char *path, int flags, mode_t mode);
int open(const char *path, int flags);
where,
pathname is the file name
flags indicate type of file (reading/writing)
modes gives the file permission (if file is created)
Returns an integer called file descriptor
Rest of the system calls make use of this file
descriptor.
BITS Pilani, Pilani Campus
Different flags values
BITS Pilani, Pilani Campus
Example on open()
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd1, fd2;
fd1 = open(“txt1.txt”, O_RDONLY | O_CREAT, 0777);
fd 2= open(“txt2.txt”, O_RDONLY | O_CREAT, 0777);
}
BITS Pilani, Pilani Campus
Example on open()
Shashank@localhost myopen]$ ./exam1 t2.txt
Error opening file
[Shashank@localhost myopen]$ ./exam1 t1.txt
file opened successfully
fd1=3
int main(int argc, char *argv[])
{
int fd1;
fd1 = open(argv[1],O_RDONLY);
if(fd1 == -1){
printf("Error opening file \n");
exit(0);
}
printf("file opened successfully\n");
printf("fd1=%d\n",fd1);
}
BITS Pilani, Pilani Campus
Close ()
• int close( int fd); /*file descriptor */
• /* Returns 0 on success and -1 on error */
• It makes the file descriptor available for re-use.
• It does not flush any kernel buffers or perform
any other clean-up task.
BITS Pilani, Pilani Campus
Close ()
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
int main()
{
int fd1, ret;
fd1 = open(“txt1.txt”, O_RDONLY | O_CREAT, 0777);
ret = close(fd1);
printf(“\n Result:%d”, ret);
}
BITS Pilani, Pilani Campus
read() system call
size_t read(int fd, void *buf, size_t nbytes);
size_t write(int fd, void *buf, size_t nbytes);
fd – file descriptor
buf – buffer to hold data after read
nbytes – number of bytes to be read
Both functions return number of bytes read/written
and returns -1 on error.
BITS Pilani, Pilani Campus
read() system call
int main (int argc, char *argv[])
{
char buff[10];
int ret;
ret = read(0, buff, 10);
printf(“%s\n”,buff);
return 0;
}
BITS Pilani, Pilani Campus
write system call
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
char buffer[12] = “I LOVE BITS”
int fd1, ret;
fd1 = creat(“txt1.txt”, 0777);
ret = write(fd1, buffer, sizeof(buffer));
return 0;
}
BITS Pilani, Pilani Campus
If a process makes a request to access 350,000
byte offset, then find out which index block
contains this byte information.
• First 10 direct blocks can access 10240 bytes or 10K bytes.
• 350,000 > 10240, therefore lets look into single indirect block.
• Single indirect can address 256 blocks, each block contains 1024
bytes, so 256 * 1024= 262144 bytes can be accessed = 256KB
• So 10 K + 256 K = 266 K = 272,384 bytes
• Still 350,000 > 272,384 , Go ahead and search in double indirect.
• 350,000 – 272,384 = 77,616 byte no in double indirect.
• First entry in double indirect block points to first single indirect
block which holds 256 block nos, each block contains 1024 bytes,
and hence points to 256K byte addresses i.e 262,144 byte address.
• Since 77,616 < 262,144, 350,000 will be there in one of the blocks
pointed to by single indirect.
• 77616/1024 = 75, 77616 mod 1024 = 816
• It comes out to be 75th block and 816th byte entry.
BITS Pilani, Pilani Campus
read & write system calls
int main()
{char buffer[128];
int nread;
nread = read(0,buffer,128); // reading data from STD_INPUT fd =0
printf("nread = %d\n",nread);
if(nread == -1)
write(2,"A read Error has occurred\n",26); //writing error to STD_ERR fd = 2
if((write(1,buffer,nread)) != nread) //writing data to STD_OUTPUT fd = 1
write(2,"write error has occurred\n",25);
exit(0);}
BITS Pilani, Pilani Campus
lseek() system call
It sets the read/write pointer of a file descriptor
which it can use for next read/write.
off_t lseek(int fd, off_t offset, int reference);
offset is used to specify the position
reference is used by the offset
SEEK_SET – offset is absolute position
SEEK_CUR – offset is relative to the current
position
SEEK_END – offset is relative to the end of
the file
BITS Pilani, Pilani Campus
lseek() system call
#include <unistd.h> Testfile.txt
This is a text file that will be used to demonstrate
#include <fcntl.h>
the use of lseek.
#include <sys/types.h>
int main() Output: This is a text file
{ int file; text file that will
if((file=open("testfile.txt",O_RDONLY)) < 0)
return -1;
char buffer[19];
if(read(file,buffer,19) != 19) return -1;
printf("%s\n",buffer);
if(lseek(file,10,SEEK_SET) < 0) return -1;
if(read(file,buffer,19) != 19) return -1;
printf("%s\n",buffer);
return 0;}
BITS Pilani, Pilani Campus
int main(int argc,char * argv[])
{
int fd1,sekval,nread;
lseek()
char c,buf[20];
[shashank@localhost myopen]$ ./a.out t1.txt
fd1 = open(argv[1],O_RDONLY);
i
if(fd1 == -1)
n
{ [shashank@localhost myopen]$
printf("Error opening file\n");
exit(0);
}
if((nread=read(fd1,&c,1))==1) t1.txt
introduction to computing systems from bits and
{ gates to C and beyond Birla institute of technology
printf("%c\n",c); and science, pilani (rajasthan)
sekval = lseek(fd1,10,SEEK_CUR);
}
read(fd1,&c,1);
printf("%c\n",c);
close(fd1); }
BITS Pilani, Pilani Campus
dup() system call
The dup() system call creates a copy of the file descriptor
oldfd, using the lowest-numbered unused file descriptor
for the new descriptor.
Syntax: int dup(int oldfd);
Return Value: On success, these system calls return the
new file descriptor. On error, -1 is returned.
BITS Pilani, Pilani Campus
dup() system call
#include<stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main()
{
int file_desc = open("dup.txt", O_WRONLY | O_APPEND);
if(file_desc < 0)
printf("Error opening the file\n");
int copy_desc = dup(file_desc);
write(copy_desc,"This will be output to the file named dup.txt\n", 46);
write(file_desc,"This will also be output to the file named dup.txt\n", 51);
return 0;
}
BITS Pilani, Pilani Campus
dup2() system call
The dup2() system call performs the same task as dup(),
but instead of using the lowest-numbered unused file
descriptor, it uses the file descriptor number specified in
newfd. If the file descriptor newfd was previously open, it
is silently closed before being reused.
Syntax: int dup2(int oldfd, int newfd);
Return Value: On success, these system calls return the
new file descriptor. On error, -1 is returned.
BITS Pilani, Pilani Campus
dup2() system call
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>
int main()
{
int file_desc = open("tricky.txt",O_WRONLY | O_APPEND);
dup2(file_desc, 1) ;
printf("I will be printed in the file tricky.txt\n");
return 0;
}
BITS Pilani, Pilani Campus
dup() and dup2() system call
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
int fd1, fd2;
fd1 = open(“txt1.txt”, O_RDONLY | O_CREAT, 777);
close(2);
dup(fd1);
}
BITS Pilani, Pilani Campus
dup() and dup2() system call
#include<stdlib.h>
#include<unistd.h>
#include<fcntl.h>
int main()
{
int fd1, fd2, fd3;
fd1 = open(“txt1.txt”, O_RDONLY | O_CREAT, 777); //3
fd2 = open(“txt2.txt”, O_RDONLY | O_CREAT, 777); //4
//close (fd2);
fd3=dup2(fd1, fd2);
}
BITS Pilani, Pilani Campus
chmod
• The chmod command is used to change the access mode of a file.
• Syntax: chmod [reference][operator][mode] filename
• The references are used to distinguish the users to whom the
permissions apply i.e. they are list of letters that specifies whom to
give permissions.
• Reference Class
• u owner
• g group
• o others
• a all
31
BITS Pilani, Pilani Campus
chmod
The operator is used to specify how the modes of a file should be adjusted. The
following operators are accepted:
Operator Description
+ Adds the specified modes to the specified classes.
- Removes the specified modes from the specified classes.
= The modes specified are to be made the exact modes for the specified
classes.
32
BITS Pilani, Pilani Campus
chmod
• The modes indicate which permissions are to be granted or
removed from the specified classes. There are three basic
modes which correspond to the basic permissions:
Mode Description
r Permission to read the file.
w Permission to write (or delete) the file.
x Permission to execute the file, or, in the case of a directory,
search it.
33
BITS Pilani, Pilani Campus
chmod
- rw- rw- r-- shashank shashank text1.c
- rw- rw- r-- shashank shashank text2.c
d rwx rwx r-x shashank shashank BITS
- rw- rw- r-- shashank shashank semaphore.c
- rwx r-x r-x shashank shashank xyz.c
BEFORE: - rw- rw- r-- shashank shashank text1.c
COMMAND: chmod u=r text1.c
AFTER: - r-- rw- r-- shashank shashank text1.c
34
BITS Pilani, Pilani Campus
chmod
Let’s restrict the permission such that the user cannot search the directory
BITS.
- r-- rw- r-- shashank shashank text1.c
- rw- rw- r-- shashank shashank text2.c
d rwx rwx r-x shashank shashank BITS
- rw- rw- r-- shashank shashank semaphore.c
- rwx r-x r-x shashank shashank xyz.c
BEFORE: drwxrwxr-x shashank shashank BITS
COMMAND: chmod u=rw BITS
AFTER: drw-rwxr-x shashank shashank BITS
35
BITS Pilani, Pilani Campus
chown
• The chown command changes the user and/or group
ownership of for given file..
• Syntax
– chown owner-user file
– chown owner-user:owner-group file
– chown owner-user:owner-group directory
36
BITS Pilani, Pilani Campus
chown
First, list permissions for demo.txt, enter:
ls -l demo.txt
Sample outputs: -rw-r--r-- 1 root root 0 Aug 31 05:48 demo.txt
In this example change file ownership to vivek user and list the permissions, run:
• chown vivek demo.txt
ls -l demo.txt
Sample outputs:
• -rw-r--r-- 1 vivek root 0 Aug 31 05:48 demo.txt
37
BITS Pilani, Pilani Campus
chown
Now, the owner is set to vivek followed by a colon and a group
onwership is also set to vivek group, run:
chown vivek:vivek demo.txt
ls -l demo.txt
Sample outputs:-rw-r--r-- 1 vivek vivek 0 Aug 31 05:48 demo.txt
38
BITS Pilani, Pilani Campus
chown
Here, we have changed only the group of file. To do so, the colon and following
GROUP-name ftp are given, but the owner is omitted, only the group of the files is
changed:
chown :ftp demo.txt
ls -l demo.txt
Sample outputs:
-rw-r--r-- 1 vivek ftp 0 Aug 31 05:48 demo.txt
39
BITS Pilani, Pilani Campus
link() system call
link - make a new name for a file
int link(const char *oldpath, const char *newpath)
link() creates a new link (also known as a hard link) to an existing file.
If newpath exists it will not be overwritten. This new name may be used
exactly as the old one for any operation; both names refer to the same file
(and so have the same permissions and ownership) and it is impossible to tell
which name was the `original’.
Return Value: On success, zero is returned. On error, -1 is returned.
40
BITS Pilani, Pilani Campus
unlink() system call
unlink - delete a name and possibly the file it refers to
Syntax: int unlink(const char *pathname);
Return Value: On success, zero is returned. On error, -1 is
returned, and errno is set appropriately.
41
BITS Pilani, Pilani Campus
Problem 1
Assume that the text file is already created. You need to open the
file using O_RDWR flag only and print “hi All” from the
program without use any printf or cout function.
42
BITS Pilani, Pilani Campus