open() - Unix, Linux System Call

Unix - Home
Unix - Getting Started
Unix - File Management
Unix - Directories
Unix - File Permission
Unix - Environment
Unix - Basic Utilities
Unix - Pipes & Filters
Unix - Processes
Unix - Communication
Unix - The vi Editor
Unix - What is Shell?
Unix - Using Variables
Unix - Special Variables
Unix - Using Arrays
Unix - Basic Operators
Unix - Decision Making
Unix - Shell Loops
Unix - Loop Control
Unix - Shell Substitutions
Unix - Quoting Mechanisms
Unix - IO Redirections
Unix - Shell Functions
Unix - Manpage Help
Unix - Regular Expressions
Unix - File System Basics
Unix - User Administration
Unix - System Performance
Unix - System Logging
Unix - Signals and Traps
Unix - Useful Commands
Unix - Quick Guide
Unix - Builtin Functions
Unix - System Calls
Unix - Commands List
Unix Useful Resources
Computer Glossary
Who is Who
Copyright © 2014 by tutorialspoint
#include <sys/types.h> 
#include <sys/stat.h> 
#include <fcntl.h> 

int open(const char *pathname, int flags); 
int open(const char *pathname, int flags, mode_t mode); 
int creat(const char *pathname, mode_t mode); 


int open(const char *pathname, int flags); 
int open(const char *pathname, int flags, mode_t mode); 
int creat(const char *pathname, mode_t mode); 


The new file descriptor is set to remain open across an
execve(2)
(i.e., the
FD_CLOEXEC file descriptor flag described in
fcntl(2)
is initially disabled).
The file offset is set to the beginning of the file (see
lseek(2)).


A call to
open() creates a new
open file description, an entry in the system-wide table of open files.
This entry records the file offset and the file status flags
(modifiable via the
fcntl() F_SETFL operation).
A file descriptor is a reference to one of these entries;
this reference is unaffected if
pathname is subsequently removed or modified to refer to a different file.
The new open file description is initially not shared
with any other process,
but sharing may arise via
fork(2).


The parameter
flags must include one of the following
access modes: O_RDONLY, O_WRONLY, or O_RDWR. These request opening the file read-only, write-only, or read/write,
respectively.


In addition, zero or more file creation flags and file status flags
can be
bitwise-or’d in
flags. The
file creation flags are
O_CREAT, O_EXCL, O_NOCTTY, and O_TRUNC. The
file status flags are all of the remaining flags listed below.
The distinction between these two groups of flags is that
the file status flags can be retrieved and (in some cases)
modified using
fcntl(2).
The full list of file creation flags and file status flags is as follows:


A semantically similar (but deprecated) interface for block devices
is described in
raw(8).




The argument
mode specifies the permissions to use in case a new file is created. It is
modified by the process’s
umask in the usual way: the permissions of the created file are
(mode & ~umask). Note that this mode only applies to future accesses of the
newly created file; the
open() call that creates a read-only file may well return a read/write
file descriptor.


creat() is equivalent to
open() with
flags equal to
O_CREAT|O_WRONLY|O_TRUNC. 

On NFS file systems with UID mapping enabled, open() may
return a file descriptor but e.g. read(2) requests are denied
with EACCES.
This is because the client performs open() by checking the
permissions, but UID mapping is performed by the server upon
read and write requests.


If the file is newly created, its st_atime, st_ctime, st_mtime fields
(respectively, time of last access, time of last status change, and
time of last modification; see
stat(2))
are set
to the current time, and so are the st_ctime and st_mtime fields of the
parent directory.
Otherwise, if the file is modified because of the O_TRUNC flag,
its st_ctime and st_mtime fields are set to the current time.


The (undefined) effect of
O_RDONLY | O_TRUNC varies among implementations. On many systems the file is actually
truncated.


The
O_DIRECT flag was introduced in SGI IRIX, where it has alignment restrictions
similar to those of Linux 2.4.  IRIX has also a fcntl(2) call to
query appropriate alignments, and sizes.   FreeBSD 4.x introduced
a flag of same name, but without alignment restrictions.
Support was added under Linux in kernel version 2.4.10.
Older Linux kernels simply ignore this flag.
One may have to define the
_GNU_SOURCE macro to get its definition.


Currently, it is not possible to enable signal-driven
I/O by specifying
O_ASYNC when calling
open(); use
fcntl(2)
to enable this flag.


POSIX provides for three different variants of synchronised I/O,
corresponding to the flags O_SYNC, O_DSYNC and
O_RSYNC.  Currently (2.1.130) these are all synonymous under Linux.

close (2)
close (2)
dup (2)
dup (2)
fcntl (2)
fcntl (2)
link (2)
link (2)
lseek (2)
lseek (2)
mknod (2)
mknod (2)
mount (2)
mount (2)
mmap (2)
mmap (2)
openat (2)
openat (2)
path_resolution (2)
path_resolution (2)
read (2)
read (2)
socket (2)
socket (2)
stat (2)
stat (2)
umask (2)
umask (2)
unlink (2)
unlink (2)
write (2)
write (2)

Advertisements


    129 Lectures 
     23 hours 

 Eduonix Learning Solutions

    5 Lectures 
     4.5 hours 

 Frahaan Hussain

    35 Lectures 
     2 hours 

 Pradeep D

    41 Lectures 
     2.5 hours 

 Musab Zayadneh

    46 Lectures 
     4 hours 

 GUHARAJANM

    6 Lectures 
     4 hours 

 Uplatz
 Print
 Add Notes
 Bookmark this page