Tuesday 5 January 2021

What is Open API in UNIX?

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.

LPI Tutorial and Material, LPI Exam Prep, LPI Learning, LPI Certification, LPI Career
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

LPI Tutorial and Material, LPI Exam Prep, LPI Learning, LPI Certification, LPI Career
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:

LPI Tutorial and Material, LPI Exam Prep, LPI Learning, LPI Certification, LPI Career
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:

LPI Tutorial and Material, LPI Exam Prep, LPI Learning, LPI Certification, LPI Career
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.

Source: geeksforgeeks.org

Related Posts

0 comments:

Post a Comment