You are on page 1of 37

OS Tutorial

BITS Pilani
Pilani Campus
BITS Pilani
Pilani Campus

S. H. Islam
hafizul@pilani.bits-pilani.ac.in

Room No. 6120 K (NAB)


UNIX File System

3
BITS Pilani, Pilani Campus
creat() system call
Prototype for the creat() system call
int creat(file_name, mode)
char *file_name;
int mode;
• Where file_name is pointer to a null terminated
character string that names the file.
• mode defines the file's access permissions.

4
BITS Pilani, Pilani Campus
creat() system call
• If the file named by file_name does not exist,
the UNIX system creates it with the specified
mode permissions.
• If the file does exist, its contents are discarded
and the mode value is ignored.
• The permissions of the existing file are
retained.

5
BITS Pilani, Pilani Campus
mode
Mode argument as defined in
/usr/include/sys/stat.h:

 #define S_IRWXU 0000700 /* -rwx------ */


 #define S_IREAD 0000400 /* read permission, owner */
 #define S_IWRITE 0000200 /* write permission, owner */
 #define S_IEXEC 0000100 /* execute/search permission,
owner */
 #define S_IRGRP 0000040 /* read permission, group */
 #define S_IROTH 0000004 /* read permission, other */
6
BITS Pilani, Pilani Campus
open()
open a file for reading, writing, or reading and writing
• Prototype
#include <fcntl.h>
int open(file_name, option_flags [ , mode])
char *file_name;
int option_flags, mode;

• where file_name is a pointer to the character string that


names the file.
• option_flags represent the type of channel
• mode defines the file's access permissions if the file is
being created.
7
BITS Pilani, Pilani Campus
open()
• If the open() system call succeeds, it returns a small
non-negative integer called a file descriptor that is used
in subsequent I/O operations on that file.
• If open ( ) fails, it returns –1 (#include <errno.h>).

Value Meaning Code Meaning


0 Standard Input EBADF Bad file descriptor
1 Standard Output EACCES Permission denied
2 Standard Error EBUSY Device or resource busy
8
BITS Pilani, Pilani Campus
open()
• The printf( ) library function always sends its
output using file descriptor 1,
• scanf( ) always reads its input using file
descriptor 0, which is the display screen.

9
BITS Pilani, Pilani Campus
option_flags for open()
The allowable option_flags as defined in
"/usr/include/fcntl.h"

• #define O_RDONLY /* Open the file for reading only */

• #define O_WRONLY /* Open the file for writing only */

• #define O_RDWR /* Open the file for both reading and writing*/
• #define O_TRUNC /* Truncate file size to zero if it already exists
*/
• #define O_CREAT /*Create the file if it doesn't already exist */

10
BITS Pilani, Pilani Campus
close()
To close a file, use the close() system call.
int close(file_descriptor)
int file_descriptor;
• file_descriptor identifies a currently open
channel.
• close() fails if file_descriptor does not identify a
currently open channel.
• If successful, close( ) returns zero, otherwise it
returns –1.
11
BITS Pilani, Pilani Campus
read()
int read (int fd, char *buff, int count)
• read() copies count bytes from the file
referenced by the file descriptor fd into the
buffer buff.
• The bytes are read from the current file
position, which is then updated accordingly.

12
BITS Pilani, Pilani Campus
read()
• read() copies as many bytes from the file as it
can, up to the number specified by count, and
returns the number of bytes actually copied.
• If a read() is attempted after the last byte has
already been read, it returns 0, which indicates
end-of-file.
• If successful, read() returns the number of
bytes that it read; otherwise, it returns –1.

13
BITS Pilani, Pilani Campus
write()

int write (int fd, char *buff, int count)


• write() copies count bytes from a buffer buff
to the file referenced by the file descriptor fd.
• The bytes are written at the current file
position, which is then updated accordingly.

14
BITS Pilani, Pilani Campus
write()
• If the O_APPEND flag was set for fd, the file
position is set to the end of the file before
each write.
• write() copies as many bytes from the buffer
as it can, up to the number specified by count,
and returns the number of bytes actually
copied.
• If the returned value is not count, then the
disk probably filled up and no space was left.

15
BITS Pilani, Pilani Campus
#include <stdio.h>
#include <sys/types.h> /*defines types used by sys/stat.h*/
#include <sys/stat.h> /* defines S_IREAD & S_IWRITE*/
int main()
{
int fd;
fd = creat("datafile.txt", S_IREAD | S_IWRITE);
if (fd == -1)
printf("Error in opening datafile.txt\n");
else
{
printf("datafile.txt opened for read/write access\n");
printf("datafile.txt is currently empty\n");
}
close(fd); Output
exit (0); datafile.txt opened for read/write access
} datafile.txt is currently empty
16
BITS Pilani, Pilani Campus
File pointer
• Both read() and write() will change the file
pointer.
• The pointer will be incremented by exactly the
number of bytes read or written.

17
BITS Pilani, Pilani Campus
lseek()
long lseek (int fd, long offset, int where)
• lseek() allows you to change a descriptor’s
current file position.
• fd is the file descriptor.
• offset is a long integer, and where describes
how offset should be interpreted.

18
BITS Pilani, Pilani Campus
lseek()
• If successful, lseek( ) returns the current file
position; otherwise, it returns –1.
• The three possible values of where are defined
in “/usr/include/sys/file.h”, and have the
following meaning:

Value Meaning
SEEK_SET offset is relative to the start of the file.
SEEK_CUR offset is relative to the current file position.
SEEK_END offset is relative to the end of the file.
19
BITS Pilani, Pilani Campus
lseek()
where Meaning
0 beginning of the file.
1 Current position.
2 End of the file.

call Meaning
lseek(fd,0,0) places the current position at the first byte
lseek(fd, 0, 2) places the current position at EOF.
lseek(fd, -10, 1) Backs up the current position by 10 bytes

20
BITS Pilani, Pilani Campus
lseek()
• Random access
– Jump to any byte in a file
• Move to byte #16
– newpos = lseek(fd, 16, SEEK_SET);

• Move forward 4 bytes


– newpos = lseek(fd, 4, SEEK_CUR);

• Move to 8 bytes from the end


– newpos = lseek(fd, -8, SEEK_END);

21
BITS Pilani, Pilani Campus
lseek - SEEK_SET (10)
File (stream of bytes)
Original Position 0 #
1 !
2 /
3 b
4 i
5 n
6 /
7 s
8 h
9 \n
lseek(fd, 10, SEEK_SET) 10
11

22
BITS Pilani, Pilani Campus
lseek - SEEK_CUR (-5)
File (stream of bytes)
0 #
1 !
2 /
3 b
4 i
lseek(fd, -5, SEEK_CUR) 5 n
6 /
7 s
8 h
9 \n
Original Position 10
11

23
BITS Pilani, Pilani Campus
lseek - SEEK_CUR(3)
File (stream of bytes)
0 #
1 !
2 /
3 b
4 i
Original Position 5 n
6 /
7 s
lseek( fd, 3, SEEK_CUR ) 8 h
9 \n
10
11

24
BITS Pilani, Pilani Campus
lseek - SEEK_END (-3)
File (stream of bytes)
0 #
1 !
2 /
3 b
4 i
Original Position 5 n
6 /
7 s
8 h
9 \n

lseek(fd, -3, SEEK_END) 98 (


99 0
100 )

25
BITS Pilani, Pilani Campus
Read – File Pointer
File (stream of bytes)
Original Position #
0
1 !
2 /
3 b
4 i
5 n
6 /
7 s
8 h
9 \n
read(fd, buffer, 10) 10
11

buffer: #!/bin/sh

26
BITS Pilani, Pilani Campus
Write – File Pointer
buffer: #!/bin/csh

File (stream of bytes)


Original Position 0 #
1 !
2 /
3 b
4 i
5 n
6 /
7 c
8 s
9 h
10 \n
write(fd, buffer, 11) 11

27
BITS Pilani, Pilani Campus
#include <fcntl.h> /* defines options flags */
#include <sys/types.h> /* defines types used by sys/stat.h */
#include <sys/stat.h> /* defines S_IREAD & S_IWRITE */
char message[] = "Hello world";
void main()
{
int fd;
char buffer[80];
fd = open("datafil3.txt", O_RDWR | O_CREAT | S_IREAD | S_IWRITE);
if (fd != -1)
{
printf(“\n datafil3.txt opened for read/write access\n");
write(fd, message, sizeof(message));
lseek(fd, 10, SEEK_SET);
if (read(fd, buffer, sizeof(message)) == sizeof(message))
printf(“\n\"%s\" was written to datafile3.txt\n", buffer);
else
printf(“\n*** error reading datafile3.txt ***\n");
close (fd);
} • datafil3.txt opened for read/write access
else • "Hello world" was written to datafile3.txt
printf(“\n*** datafile3.txt already exists ***\n");
} 28
BITS Pilani, Pilani Campus
dup()
• The dup() system call duplicates an open file
descriptor and returns the new file descriptor.
• The prototype:
int dup(file_descriptor)
int file_descriptor;
• file_descriptor is the file descriptor
describing the original I/O channel returned
by creat(), open() system calls.

29
BITS Pilani, Pilani Campus
#include <stdio.h> #include <fcntl.h> #include <sys/types.h>
#include <sys/stat.h>
void main()
{
int fd, fd1;
fd = open("dup.txt", O_WRONLY | O_CREAT, S_IREAD |S_IWRITE );
printf("\n\n original fd=%d\n\n", fd);
if (fd == -1) original fd=3
{ Contents of dup.txt
printf(“\n\n ERROR\n\n"); fd after dup()=2
exit (1); Hello, world!
}
close(1); /* close standard output */
fd1=dup(fd); /* fd will be duplicated */
printf("\n\n fd after dup()=%d\n\n", fd1);
close(fd); /* close the extra slot */
printf("Hello, world!\n"); /* should go to file dup.txt */
exit (0); /* exit() will close the files */
30
} BITS Pilani, Pilani Campus
Directory System Calls
 getcwd()
 mkdir()
 chdir()
 rmdir()
 opendir()
 closedir()
 readdir()

31
BITS Pilani, Pilani Campus
getcwd()
 # include<unistd.h>
 char *getcwd(char *buf, size_t bufsize);
 char *buf /* Returned pathname */
 size_t bufsize /* sizeof buf */
 It returns current working directory on
success and NULL on error
 bufsize should the maximum size of path.

32
BITS Pilani, Pilani Campus
getcwd()
 To get the current working directory in UNIX,
we use the “pwd” command.
 The system call behind the “pwd” command is
the getcwd() system call.
 The getcwd() function copies an absolute
pathname of the current working directory to
the array pointed to by buf, which is of
length size.

33
BITS Pilani, Pilani Campus
getcwd()
#include <stdio.h> #include <stdlib.h>#include <unistd.h>#include <limits.h>
#define PATH_MAX 255
int main (void)
{
char dirname[PATH_MAX+1]; /* To be passed to getcwd system call. */
/* Use getcwd to get the name of the current working directory. */
if (getcwd(dirname, PATH_MAX) == NULL)
{
fprintf(stderr, "Could not obtain current working directory.\n");
exit(1);
}
else
{
printf("Current working directory: %s\n", dirname);
}
return 0;
} Current working directory: /home/hafi786/Desktop/test
BITS Pilani, Pilani Campus
chdir()
• # include<unistd.h>
int chdir(const char *path)
• On success, zero is returned.
• On error, -1 is returned, and errno is set
appropriately.
• This system call changes the current working
directory to that specified in “path”.

35
BITS Pilani, Pilani Campus
chdir()
• ERRORS: Depending on the file system, other errors
can be returned. The more general errors for chdir()
are listed below:

Error code Description


EIO An I/O error occurred.
ENAMETOOLONG path is too long.
ENOENT The file does not exist.

36
BITS Pilani, Pilani Campus
#include <stdio.h> #include <unistd.h> #include <stdlib.h>
#include <errno.h>
const char * const path = "/home/hafi786/Download/test1";
int main ()
{
printf ("\n\n Changing directory to < %s >\n\n", path);
if (chdir (path) == -1)
{
printf ("\n\nchdir failed - %s\n\n", strerror (errno));
}
else
{
printf ("\n chdir done !!!\n");
printf ("\n\n directory content of %s\n\n", path);
system ("ls -l");
}
return 0;
} 37
BITS Pilani, Pilani Campus

You might also like