Operating system provides the set of system calls that define the interface between the user programs and the operating system.
The system calls available in the interface vary from OS to OS (Operating System to Operating System).
Operating Systems (OSs) have system calls for reading files.
Unix has a read system call with the following three parameters.
As you know that the actual mechanics of issuing a system call are highly machine dependent and therefore it must be expressed in assembly language code.
A procedure library is provided to make system calls form C programs and also from other languages as well.
Making a system call is a special kind of making a procedure call.
Procedure calls don't enter the kernel whereas system calls do.
System call has the following three parameters.
Here is a sample of a system call from C program.
count = read(fd, buffer, nbytes);
The system call return the number of bytes actually read in count.
In case, if the system call can't be carried out, either due to an invalid parameter or a disk error, count is set to -1, and the error number is put in a global variable, errno.
Now, let's take a look at some of the major POSIX system calls. Here the return code s is -1 in case if an error has occurred.
The return code is given as follows.
(1) Create a child process identical to the parent:
pid = fork()
(2) Wait for a child to terminate:
pid = waitpid(pid, &statloc, options)
(3) Replace a process core image:
s = execve(name, argv, environp)
(4) Terminate the process execution and return status:
(1) Open a file for reading, writing, or for both:
fd = open(file, how, ...)
(2) Close an open file:
s = close(fd)
(3) Read the data from a file into a buffer:
n = read(fd, buffer, nbytes)
(4) Write the data from a buffer into a file:
n = write(fd, buffer, nbytes)
(5) Move the file pointer:
position = lseek(fd, offset, whence)
(6) Get a file's status information:
s = stat(name, &buf)
(1) Create a new directory:
s = mkdir(name, mode)
(2) Remove an empty directory:
s = rmdir(name)
(3) Create a new entry, name2, pointing to name1:
s = link(name1, name2)
(4) Remove a directory entry:
s = unlink(name)
(5) Mount a file system:
s = mount(special, name, flag)
(6) Unmount a file system:
s = umount(special)
(1) Change the working directory:
s = chdir(dirname)
(2) Change a file's protection bits:
s = chmod(name, mode)
(3) Send a signal to a process:
s = kill(pid, signal)
(4) Get the elapsed time since January 1, 1970:
seconds = time(&seconds)
So far we have discussed or focused primarily on Unix. Now let's take a look at all the system calls for Windows Win32 API.
Here is a table describes the system calls for Windows Win32 API. Here, the Win32 API calls, roughly correspond to the Unix calls as discussed or given above.
|fork||CreateProcess||Create a new process|
|waitpid||WaitForSingleObject||Can wait for a process to exit|
|execve||CreateProcess = fork + execve|
|open||CreateFile||Create a file or open an existing file|
|close||CloseHandle||Close a file|
|read||ReadFile||Read data from a file|
|write||WriteFile||Write data to a file|
|lseek||SetFilePointer||Move the file pointer|
|stat||GetFileAttributesEx||Get various file attributes|
|mkdir||CreateDirectory||Create a new directory|
|rmdir||RemoveDirectory||Remove an empty directory|
|unlink||DeleteFile||Destroy an existing file|
|time||GetLocalTime||Get the current time|
|chdir||SetCurrentDirectory||Change the current working directory|
|link||Not supported by Win32|
|mount||Not supported by Win32|
|umount||Not supported by Win32|
|chmod||Not supported by Win32|
|kill||Not supported by Win32|