Presentation is loading. Please wait.

Presentation is loading. Please wait.

File Structure Related system calls

Similar presentations


Presentation on theme: "File Structure Related system calls"— Presentation transcript:

1 File Structure Related system calls
The file structure related system calls available in the UNIX system. let you create, open, and close files, read and write files, randomly access files, alias and remove files, get information about files, check the accessibility of files, change protections, owner, and group of files, and control devices.

2 These operations either use a character string that defines the absolute or relative path name of a file, or a small integer called a file descriptor that identifies the I/O channel. The kernel presents and accepts data from the channel as a process reads and writes that channel.

3 File Descriptors Unix processes name I/O and IPC objects by integers known as file descriptors. A file descriptor is a small integer that is used to identify a file that has been opened for I/O. Each process is allowed a fixed number of file descriptors. Many programs associate the file descriptors 0, 1 & 2 with the standard input, standard output and standard error of a process. (i.e.) -File descriptor 0 is standard input. -File descriptor 1 is standard output. -File descriptor 2 is standard error.

4 CONSTANT DESCRIPTION O_RDONLY -Open for Reading only O_WRONLY -Open for Writing only O_RDWR -Open for Reading and Writing O_APPEND -Append to the end of file on each write O_CREAT -Create the file if it doesn’t exist O_EXCL -Generate an error if O_CREAT function is specified and the file exists O_TRUNC -If file exists, successfully opened for R or W, truncate its length to 0

5 Initial Permissions: When we create a file using the O_CREAT flag with open, we must use the three parameter form. Filename, mode, the third parameter, is made from a bitwise OR of the flags defined in the header file sys/stat.h , These are: S_IRUSR Read permission, owner. S_IWUSR Write permission, owner. S_IXUSR Execute permission, owner. S_IRGRP Read permission, group. S_IWGRP Write permission, group. S_IXGRP Execute permission, group. S_IROTH Read permission, others. S_IWOTH Write permission, others. S_IXOTH Execute permission, others

6 open Function A file is opened or created by calling the open function.

7 creat Function A new file can also be created by calling the creat function.

8 Close function An open file is closed by calling the close function.

9 How to use create():

10 Lseek function All open files have a "file pointer" associated with them to record the current position for the next file operation When the file is opened, the file pointer points to the beginning of the file. After reading/write m bytes, the file pointer moves m bytes forward . The lseek system call sets the read/write pointer of a file descriptor, fd, i.e. you can use it to set where in the file the next read or write will occur. We can set the pointer to an absolute location in the file or to a position relative to the current position or the end of file.

11

12 The 'whence' variable of lseek specifies how the seek is to be done
from the beginning of the file from the current value of the pointer, or from the end of the file The return value is the offset of the pointer after the lseek The offset parameter is used to specify the position and the whence parameter specifies how the offset is used. whence can be one of the following: SEEK_SET   offset is an absolute position SEEK_CUR   offset is relative to the current position SEEK_END   offset is relative to the end of the file lseek returns the offset measured in bytes from the beginning of the file that the file pointer is set to, or -1 on failure. The type off_t, used for the offset in seek operations, is an implementation-dependent type defined in sys/types.h.

13 How to work lseek()

14 Read function Data is read from an open file with the read function.

15 To read "size" bytes from the file specified by "fd“ into the memory location pointed to by "buf".
It returns many bytes were actually read (why?) 0 : at end of the file < size : fewer bytes are read to the buffer (why?) == size : read the specified # of bytes Things to be careful about buf needs to point to a valid memory location with length not smaller than the specified size Otherwise, what could happen? fd should be a valid file descriptor returned from open() to perform read operation

16 write Function Data is written to an open file with the write function.

17 int write(int fd, char *buf, int size) writes the bytes stored in buf to the file specified by fd
It returns the number of bytes actually written, which is usually “size” unless there is an error Things to be careful about buf needs to be at least as long as specified by “size”The file needs to be opened for write operations.

18 how to use open(), lseek(),read()and write():

19

20 dup and dup2 Functions An existing file descriptor is duplicated by either of the following functions.

21 The new file descriptor returned by dup is guaranteed to be the lowest-numbered available filedescriptor. With dup2, we specify the value of the new descriptor with the filedes2 argument. If filedes2 is already open, it is first closed. If filedes equals filedes2, then dup2 returns filedes2 without closing it.

22

23 Another way to duplicate a descriptor is with the fcntl function
Another way to duplicate a descriptor is with the fcntl function. Indeed, the call dup(filedes); is equivalent to fcntl(filedes, F_DUPFD, 0); Similarly, the call dup2(filedes, filedes2); close(filedes2); fcntl(filedes, F_DUPFD, filedes2);

24

25

26 sync, fsync, and fdatasync Functions
Traditional implementations of the UNIX System have a buffer cache or page cache in the kernel through which most disk I/O passes. When we write data to a file, the data is normally copied by the kernel into one of its buffers and queued for writing to disk at some later time. This is called delayed write.

27 To ensure consistency of the file system on disk with the contents of the buffer cache, the sync, fsync, and fdatasync functions are provided.

28 fcntl Function The fcntl function can change the properties of a file that is already open.

29 The fcntl function is used for five different purposes.
1. Duplicate an existing descriptor (cmd = F_DUPFD) 2. Get/set file descriptor flags (cmd = F_GETFD or F_SETFD) 3. Get/set file status flags (cmd = F_GETFL or F_SETFL) 4. Get/set asynchronous I/O ownership (cmd = F_GETOWN or F_SETOWN) 5.Get/set record locks (cmd = F_GETLK, F_SETLK, or F_SETLKW)

30 stat, fstat, and lstat Functions
There are three stat functions available.

31 The fstat system call returns status information about the file associated with an open file descriptor. The information is written to a structure,buf, the address of which is passed as a parameter. The related functions stat and lstat return status information for a named file. They produce the same results, except when the file is a symbolic link. lstat returns information about the link itself, while stat returns information about the file that the link refers to.

32 struct stat { mode_t st_mode; /. file type & mode (permissions)
struct stat { mode_t st_mode; /* file type & mode (permissions) */ ino_t st_ino; /* i-node number (serial number) */ dev_t st_dev; /* device number (file system) */ dev_t st_rdev; /* device number for special files */ nlink_t st_nlink; /* number of links */ uid_t st_uid; /* user ID of owner */ gid_t st_gid; /* group ID of owner */ off_t st_size; /* size in bytes, for regular files */ time_t st_atime; /* time of last access */ time_t st_mtime; /* time of last modification */ time_t st_ctime; /* time of last file status change */ blksize_t st_blksize; /* best I/O block size */ blkcnt_t st_blocks; /* number of disk blocks allocated */ };

33 access Function When we open a file, the kernel performs its access tests based on the effective user and group IDs. There are times when a process wants to test accessibility based on the real user and group IDs.

34

35 umask function There are nine permission bits associated with every file, we can describe the file mode creation mask that is associated with every process. The umask function sets the file mode creation mask for the process and returns the previous value.

36 #include <sys/stat.h>
mode_t umask(mode_t cmask); Returns: previous file mode creation mask st_mode mask Meaning S_IRUSR user-read S_IWUSR user-write S_IXUSR user-execute S_IRGRP group-read S_IWGRP group-write S_IXGRP group-execute S_IROTH other-read

37 chmod and fchmod Functions
These two functions allow us to change the file access permissions for an existing file.

38 Stick bit(S_ISVTX) the sticky bit is an access-right flag that can be assigned to files and directories on Unix systems When set, it instructed the operating system to retain the text segment of the program in swap space after the process exited. This speeds up subsequent executions by allowing the kernel to make a single operation of moving the program from swap to real memory. Thus, frequently-used programs like editors would load noticeably faster.

39 SYMBOLIC LINKS A symbolic link is an indirect pointer to a file, unlike hard links. No file system limitations on a symbolic link. Any user can create a symbolic link, unlike hard links where superuser can only create them. Symbolic links were introduced with 4.2BSD and subsequently supported by SVR4.

40 symlink and readlink Functions

41 File Times Three time fields are maintained for each file. Their purpose is summarized in below table.

42 utime Function The access time and the modification time of a file can be changed with the utime function.

43 The structure used by this function is
struct utimbuf { time_t actime; /* access time */ time_t modtime; /* modification time */ } The two time values in the structure are calendar times, which count seconds. The operation of this function, and the privileges required to execute it, depend on whether the times argument is NULL.

44 DIRECTORY HANDLING SYSTEM CALLS
mkdir and rmdir Functions Directories are created with the mkdir function and deleted with the rmdir function.

45 A common mistake is to specify the same mode as for a file: read and write permissions only.
But for a directory, we normally want at least one of the execute bits enabled, to allow access to filenames within the directory. The user ID and group ID of the new directory are established according to the rules .

46 An empty directory is deleted with the rmdir function.
Recall that an empty directory is one that contains entries only for dot and dot-dot.

47 Reading Directories Directories can be read by anyone who has access permission to read the directory. But only the kernel can write to a directory, to preserve file system . The actual format of a directory. depends on the UNIX System implementation and the design of the file system.

48

49 The dirent structure defined in the file <dirent
The dirent structure defined in the file <dirent.h> is implementation dependent. Implementations define the structure to contain at least the following two members: struct dirent { ino_t d_ino; /* i-node number */ char d_name[NAME_MAX + 1]; /* null-terminated filename */ }

50 chdir, fchdir, and getcwd Functions
Every process has a current working directory When a user logs in to a UNIX system, the current working directory normally starts at the directory specified by the sixth fieldin the /etc/passwd filethe user's home directory. The current working directory is an attribute of a process; the home directory is an attribute of a login name. We can change the current working directory of the calling process by calling the chdir or fchdir functions.

51

52 DEVICE SPECIAL FILES The two fields st_dev and st_rdev are often confused. Every file system is known by its major and minor device numbers, which are encoded in the primitive system data type dev_t. The major number identifies the device driver and sometimes encodes which peripheral board to communicate with; the minor number identifies the specific subdevice.

53 We can usually access the major and minor device numbers through two macros defined
by most implementations: major and minor The st_dev value for every filename on a system is the device number of the file system containing that filename and its corresponding i-node. •Only character special files and block special files have an st_rdev value. This value contains the device number for the actual device.


Download ppt "File Structure Related system calls"

Similar presentations


Ads by Google