What is Open API in UNIX?
Last Updated :
04 Dec, 2020
There is a set of generic APIs in UNIX which is used to manipulate files. One of these APIs is the open API. The open API is used to create new files and also to establish a connection between a process and a file. After a file is created, any process can call the open function and it gets a file descriptor to refer to the file, which contains the inode information. The prototype of the open function is given below:
#include <sys/types.h>
#include <fcntl.h>
int open(const char *path_name, int access_mode, mode_t permission);
- If successful, the open function returns a non-negative integer representing the open file descriptor.
- If unsuccessful, the open function returns -1
Use of the arguments in the function prototype:
First Argument: path_name
This specifies the pathname of the file to be created or opened. This may be a relative pathname or an absolute pathname. If the pathname is a symbolic link, the open function will resolve the link refers to a non-symbolic link file to which the link refers.
Second Argument: access_mode
It is an integer value that specifies how the file is to be accessed by the calling process. This value should be one of the manifested constants described below as defined in the <fcntl.h> header:
Access mode flag |
Use
|
O_RDONLY |
Opens the file for read-only |
O_WRONLY |
Opens the file for write-only |
O_RDWR |
Opens the file for read and write |
These access node flags can be bitwise-ORed with the access modifier flags given below to change the access mechanism of the file:
Access modifier flag |
Use
|
O_APPEND |
Appends data to the end of the file
(Applicable only to Regular files)
|
O_CREAT |
Creates file if it does not exist
(Applicable only to Regular files)
|
O_EXCL |
It causes open function to fail if the named file exists already.
Also, it can be used with the O_CREAT flag only. (Applicable only to Regular files)
|
O_NONBLOCK |
Specifies that any subsequent read or write on the file should be nonblocking
(Applicable only to FIFO and device files)
|
O_NOCTTY |
Specifies not to use named terminal device file as the calling process control terminal
(Applicable only to terminal device files)
|
O_TRUNC |
Discards the file content and sets the file size to 0 bytes if the file exists
(Applicable only to Regular files)
|
Note:
- If a file is to be opened for read-only, then the file should already exist and also, no other modifier flags can be used with it.
- If a file is opened for read-write or write-only, then any modifier flags can be specified.
- If the named file does not exist and the O_CREAT file is not specified, then the open function will abort with a failure return status.
Third Argument: permission
This argument is used only when a new file is being created. This is required only if the O_CREAT flag is set in the access_mode argument. It specifies the access permission o the file for its owner, group member, and others. This argument is defined as mode_t and its value should be constructed based on the manifested constants defined in the <sys/stat.h> header. The symbolic names for file permission are given in the table below:
SYMBOL MEANING
S_IRUSR read by owner
S_IWUSR write by owner
S_IXUSR execute by owner
S_IRWXU read, write and execute by owner
S_IRGRP read by group
S_IWGRP write by group
S_IXGRP execute by group
S_IRWXG read, write and execute by group
S_IROTH read by others
S_IWOTH write by others
S_IXOTH execute by others
S_IRWXO read, write and execute by others
Example 1:
int fdesc= open(“abc/xyz/geeksforgeeks”,O_RDWR | O_APPEND, 0);
This example opens an already existing file called /abc/xyz/geeksforgeeks for read and write in append mode
The access mode flag O_RDWR is bitwise ORed “|” with access modifier flag O_APPEND
Example 2:
Let us see the use of open API in a C program which demonstrates interprocess communication between a reader and writer process. Do not worry about the functionality or procedure of the program as it will have other concepts also. Concentrate only on the use of open API. Firstly, create two C program files using the nano editor.
$nano write.c
After entering this command, the nano editor will pop up.

Enter this program write.c into the nano editor, then save it and exit from the editor
In this program, in line number 13 (fd=open(myfifo, O_WRONLY);), the open API is used to open a file in the WRITE ONLY MODE (O_WRONLY). In the next line the text, “GeeksforGeeks” is being written in the file using another API called write API. After this, the file descriptor fd is closed. As a result, the file which was opened has the text “GeeksforGeeks”.
Similarly, create a read.c file by entering the command:
$nano read.c

Enter this program read.c into the nano editor, then save it and exit from the editor
In this program, in line number 12 (fd=open(myfifo, O_RDONLY);), the open API is used to open the same file in which text “GeeksforGeeks” was written. The API opens it in the READ ONLY MODE (O_RDONLY). In the next line the read API is used to read the data from the file and the data (“GeeksforGeeks”) is displayed. After this, the file descriptor fd is closed.
Let us see the terminal screen on Ubuntu:

First Terminal Screen
We have already discussed the first two nano commands. The third command (cc write.c) compiles the write.c program and ./a.out runs the write.c program. After the execution of the program, the text “GeeksforGeeks” has been written into the file and a line is displayed saying that “Writer process started and now open one more terminal for running reader process”.
Now, open one more terminal screen of Ubuntu and execute the commands as given in the picture below:

Second Terminal Screen
The first command (cc read.c) compiles the read.c program and ./a.out runs the read.c program. After the execution, you can see that the data from the file is being displayed.
This was possible because the file was opened in the read-only mode and the data was read by the read API and got displayed.
Similar Reads
What Is Linux
The Linux opeÂrating system is a collection of open-source software programs designed to function similarly to Unix systeÂms. Linus Torvalds, a Finnish software engineeÂr, initially launched the Linux kerneÂl, which serves as the core component, on SeptembeÂr 17, 1991. This kernel acts as a vital
12 min read
What is Linux Operating System
The Linux Operating System is a type of operating system that is similar to Unix, and it is built upon the Linux Kernel. The Linux Kernel is like the brain of the operating system because it manages how the computer interacts with its hardware and resources. It makes sure everything works smoothly a
13 min read
How to Open a File in Linuxâ
In Linux, a file is a fundamental unit of storage, representing everything from documents and images to system logs and program data. Unlike traditional operating systems, Linux treats almost everythingâfiles, directories, devices, and processesâas a file. Whether you're accessing a simple text docu
6 min read
Creating Unix Sockets
Sockets are a means to allow communication between two different processes over the same or different machines/networks. To be more precise, it's a way to talk to other computers and exchange data. In Unix, every I/O action is done by writing or reading a file descriptor. Sockets are the end point o
8 min read
Ways to Find Out List of All Open Ports in Linux
In this guide, we'll explore how to identify the comprehensive list of open ports in Linux, crucial endpoints for communication within computer networks. Ports, serving as gateways for network communication, are represented by 16-bit numbers ranging from 0 to 65535. These ports play a pivotal role i
4 min read
What is the alternative to YAML and JSON?
YAML (YAML Ain't Markup Language) and JSON (JavaScript Object Notation) have been enduring choices for data serialization, valued for their simplicity and readability. Despite their prominence, the world of data serialization formats is diverse, providing alternatives tailored to specific requiremen
5 min read
What is a Linux Server and Why use it
A Linux server is a computer running the Linux operating system designed to serve various functions, such as hosting websites, managing databases, and handling network services. In this article, we'll explore what Linux servers are and delve into the reasons why they are widely used in enterprise en
9 min read
xdg-open command in Linux with Examples
xdg-open command in the Linux system is used to open a file or URL in the userâs preferred application. The URL will be opened in the userâs preferred web browser if a URL is provided. The file will be opened in the preferred application for files of that type if a file is provided. xdg-open support
2 min read
Introduction to UNIX System
UNIX is an innovative or groundbreaking operating system which was developed in the 1970s by Ken Thompson, Dennis Ritchie, and many others at AT&T Laboratories. It is like a backbone for many modern operating systems like Ubuntu, Solaris, Kali Linux, Arch Linux, and also POSIX. Originally, It wa
8 min read
What is API Authentication? Definition and Working
APIs are the backbone of contemporary applications, facilitating effortless communication between various services and platforms. But in the absence of security, APIs are exposed to unauthorized access, data breaches, and cyber-attacks. This is where API authentication steps inâallowing only authori
9 min read