Programming interface to the services provided by the OS Typically written in a high-level language (C or C++)
Mostly accessed by programs via a high-level Application Programming Interface (API) rather than direct system call use
Three most common APIs are Win32 API for Windows, POSIX API for POSIX-based systems (including virtually all versions of UNIX, Linux, and Mac OS X), and Java API for the Java virtual machine (JVM)
Why would an application programmer prefer programming according to an API rather than invoking actual system calls?
Program portability
Actual system calls can often be more detailed and difficult to work with than the API.
A strong correlation between a function in the API and its associated system call within the kernel.
Typically, a number associated with each system call System-call interface maintains a table indexed according to these numbers
The system call interface invokes the intended system call in OS kernel and returns the status of the system call and any return values The caller needs to know nothing about how the system call is implemented
Most details of OS interface hidden from programmer by API
Managed by run-time support library (set of functions built into libraries included with compiler)
This system call creates a new empty file with a system call. It is available in the fcntl.h library, which is a file handling library for Unix and Linux.
The return type for this function is an integer. If file creation is successful, it returns a non-negative integer. If the creation of the file fails, it returns -1.
The following shows the syntax. int creat(char *filename, mode_t mode);
The first parameter in the creat function is the name of a file.
The second parameter, mode, deals with the permissions of the file. The permission modes are different from normal Linux file system permissions. There are various modes available for this flag, but the following are the most common modes.
O_RDONLY: If you set this flag mode to the creat function, the file has read-only permission.
O_WRONLY: This mode gives write permissions.
O_RDWR: This mode gives both read and write permissions.
O_EXCL: This flag mode prevents the creation of a file if it already exists.
O_APPEND: This mode appends the content to existing file data without overriding it.
O_CREAT: This flag mode creates a file if it does not exist.
If you want to use multiple modes at the same time, you can use the bitwise OR operator.
Example: create.c
The open system call function opens a file and can perform read and write operations based on the mode set to the function. An open system call can also create a file.
If the specified filename is not available, then it automatically creates a new file with the given name. The return type of this function is an integer.
If the file opens successfully, it returns a positive integer value; otherwise, it returns -1.
The following shows the syntax.
int
open(const
char *filepath, int flags,
...);
The first parameter deals with the absolute path of a file that you want to open.
The flags that are passed as a second argument are O_RDONLY, O_ WRONLY, O_RDWR, and so forth.
Example: open.c
This system call closes the file descriptor that was created to open, create, or read the contents in a file. The return type of this function is an integer.
If the file descriptor is closed successfully, it returns 0; otherwise, it returns -1.
The following shows the syntax.
int close(int file_descriptor);
file_descriptor is an integer value that identifies the open file in a process.
Example: close.c
This function system call reads the content of a file that was indicated by a file descriptor. The return type of this function is an integer. It returns -1 if an error occurs or when any signal interrupt occurs during a read operation. A successful read of a file returns the number of bytes read during the operation.
The following shows the syntax.
size_t read (int file_descriptor, void* buffer, size_t size);
file_descriptor is a unique integer value that identifies the open file in a process.
The buffer
argument reads the file data.
size
is the third argument indicates the size of the buffer that you want to read from the file.
Example:read.c
This function writes content to a given file descriptor.
The return type of this file is an integer. It returns -1 for an error or if any signal interrupt is raised; otherwise, it returns the number of bytes that are returned to a file.
The following shows the syntax.
size_t write (int file_descriptor, void* buffer, size_t size);
This function syntax is the same as the read function. However, the key difference is that it writes the content in a file using the buffer. The read function reads the content from a file using a buffer.
Example: write.c
int mkdir(const char *path, mode_t mode);
path is the first argument that describes the path and the new directory name to create in the system.
mode represents the permissions to give to a new directory.
The deletion of a directory is done with the rmdir function, which is available in the sys/stat.h library. The return type of this function is an integer.
It returns 0 on the successful deletion of a directory; it returns -1 if a failure.
The following shows the syntax.
int rmdir(const char *pathname);
char getcwd(char *buffer, size_t buffersize);
buffer is the first argument; it describes the char array that stores the buffer content.
buffersize is the second argument; it is the length of the buffer.
There is a chdir system call that changes directory in your operating system. It is available in the unistd.h library.
The return type of this function is an integer. It returns 0 on the successful change in a directory; it returns -1 for a failure.
The following shows the syntax.
int chdir(const char *path);
DIR *opendir(const char *path);
struct dirent *readdir(DIR* directorypointer);
The directorypointer
argument should contain the directory stream pointer, which is a return value of the opendir function.
The internal structure of dirent is as follows.
struct dirent{
ino_t d_ino; // inode number
off_t
d_off; // offset to the next dirent unsigned short d_reclen; // length of this record unsigned char d_type; // type of file;
char d_name[256];
//
filename
};
int closedir(DIR *directorypointer);
Adapted from “Operating System Concepts”, 10th Edition; Abraham Silberschatz, Peter B. Galvin,Greg Gagne; 2018