You are on page 1of 24

STAT , DUP , DUP2 , MOUNT , UNMOUNT

Group-9
Harsh Shah Narendra Kumawat
Sujit Shinde Vrund Shah
Shefali Dsouza
A system call is the programmatic way in which a computer
program requests a service from the kernel of the operating system it
is executed on. ...System calls provide an essential interface between a
process and the operating system.
Harsh Shah
• Stat is a system call in UNIX to check the status of a file such as to check when the file was accessed.

• Simple term Stat get the file status.

• The stat() system call actually returns file attributes.

• The file attributes of an inode are basically returned by Stat() function.

• Inode contains all the data that is required for the stat() system call.
Syntex :
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *path, struct stat *buf);

• The return type of the function in int, if the function is executed successfully, 0 is returned if there are
any errors, -1 will be returned.

• Here const char *path specifies the name of the file. If the path of file is a symbolic link then you
need to specify the link instead of file name.

• Then in the function we have a stat structure in which the data or information about the file is stored
which uses a pointer named buf, which is passed in as a parameter and filled in during the
execution of the call and readable by the user after the call.
Syntax Of Stat Function :
struct stat
{
mode_t st_mode;
ino_t st_ino;
dev_t st_dev;
dev_t st_rdev;
nlink_t st_nlink;
uid_t st_uid;
gid_t st_gid;
off_t st_size;
struct timspec st_atim;
struct timspec st_mtim;
struct timspec st_ctim;
blksize_t st_blksize;
blkcnt_t st_blocks;
};
Description :

st_dev : It is the ID of device in which we have our file residing currently.

st_rdev : This field describes that a particular file represents a particular device.

st_ino : It is the inode number or the serial number of the file. As it is an index number so it should
be unique for all files

st_size : st_size is the size of the file in bytes.

st_atime : It is the last time or the recent time at which the file was accessed.

st_ctime : It is the recent time at which the status or the permissions of the file was changed.

st_mtime : It is the recent time at which the file was modified.

st_blksize : This field gives the preferred block size for I/O file system which may vary from file to
file.

st_blocks : This field tells the total number of blocks in multiples of 512 bytes.
st_nlink : This field tells the total number of hard links.

st_uid : This field indicates the user ID.

st_gid : This field indicates the group ID.

st_mode : It indicates the permissions on the file, tells the modes on a file. Following are the flags that
should be defined for st_mode field
Program :
#include<stdio.h>

#include<sys/stat.h>

int main()
{
struct stat file; //pointer to stat struct

stat("stat.c", &file); //stat system call

Printf("st_mode = %o", sfile.st_mode); //accessing st_mode (data member of stat struct)

return 0;
}
Narendra Kumawat
• The dup() system call creates a copy of the file descriptor “oldfd”.

• It uses the lowest-numbered unused file descriptor for the new descriptor.

• After creating the new file descriptor, both of the file descriptors can be used interchangeably.

• They both refer to the same open file description and thus share file offset and file status flags.

Syntax : int dup(int oldfd);

OldFd : Old File descriptor whose copy is to be created.


Program :

int main()
{

int oldfd = open("dup.txt", O_WRONLY);

int newfd = dup(oldfd);

write(oldfd, "Hello World");

write(newfd, "Hello World");

}
Sujit Shinde
• 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.

• The steps of closing and reusing the file descriptor newfd are performed atomically.

Syntax : int dup2(int oldfd, int newfd);

Oldfd : Old file descriptor.

Newfd : New file descriptor which is used by dup2() to create copy.


Important Points :

• Include the header file unistd.h for using dup() and dup2() system call.

• If the descriptor newfd was previously open, it is silently closed before being reused.

• If oldfd is not a valid file descriptor, then the call fails, and newfd is not closed.

• If oldfd is a valid file descriptor, and newfd has the same value as oldfd, then dup2() does
nothing, and returns newfd.
Program :
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
#include<fcntl.h>

int main()
{
int file_desc = open(“simple.txt“,O_WRONLY | O_APPEND);

dup2(file_desc, 1) ;

printf("I will be printed in the file simple.txt\n");

return 0;
}
Vrund Shah
• Mounting is a process by which the operating system makes file & directories on a strorage device.
Available for user to access via the computers file system.

• In UNIX directories are represented by a tree structure, and hence mounting would mean attaching
them to the branches.

• This means the file system found on one device can be attached to the tree. The location in the system
where the file is attached is called as a mount point.

Example :

Mount –t type device dir.

• This will attach or mount the file system found on device of type type to the directory dir.
Shefali Dsouza
• The umount command detaches the specified file system(s) from the file hierarchy.

• A file system is specified by giving the directory where it has been mounted.

• Giving the special device on which the file system lives may also work, but is an obsolete method,
mainly because it will fail in case this device was mounted on more than one directory.

Syntax :
I. umount [-hV]

II. umount -a [-dflnrv] [-t vfstype] [-O options]

III. umount [-dflnrv] {dir|device}...


Options :
a, --all : All of the file systems described in /etc/mtab are unmounted.

-A, --all-targets : Unmount all mountpoints in the current namespace for the specified filesystem.

-d, --detach-loop : When the unmounted device was a loop device, also free this loop device.

--fake : Causes everything to be done except for the actual system call; this 'fakes' unmounting the
filesystem.

-f, --force : Force unmount (in case of an unreachable NFS system)

-i, --internal-only : Do not call the /sbin/umount.<filesystem> helper even if it exists. By default
/sbin/umount.<filesystem> helper is called if one exists.

-n, --no-mtab : Unmount without writing in /etc/mtab.

-l, --lazy : Lazy unmount.


O, --test-opts options,list : Indicate that the actions should only be taken on file systems with the
specified options in /etc/fstab. More than one option type may be specified
in a comma separated list. Each option can be prefixed with no to specify
options for which no action should be taken.

-R, --recursive : Recursively unmount each directory specified.

-r, --read-only : In case unmounting fails, try to remount read-only.

-t, --types vfstype,ext2,ext3 : Indicate that the actions should only be taken on file systems of the
specified type.

-v, --verbose : Verbose mode.

-h, --help : Print help message and exit.

-V, --version : Print version and exit.


Thank You

You might also like