Article No: 4
System call provides an interface between process and operating system. An application program can’t communicate directly with operating system, so there is a need of system call. User program receives operating system services through the set of system calls.
As an example of how system calls are used, consider writing a simple program to read data from one file and to copy them to another file.
The first input that program will need are the names of the two files: the input file and output file. These names can be specified in many ways, depending on the operating-system design. One approach is for the program to ask the user for the names of the two files. In an interactive system, this approach will require a sequence of system calls, first to write a prompting message on the screen, and then to read from the keyboard the characters that definite the two files. On mouse-based window-and-menu systems, a menu of file names is usually displayed in a window. The user can then use the mouse to select the source name, and similar a window can be opened for the destination name to be specified.
Once the two file names are obtained, the program must open the input file and create the output file. Each of these operations requires another system call and may encounter possible error conditions. When the program tries to open the input file, it may find that no file of that name exists or that the file protected against access. In these cases, the program should print a message on the screen (another sequences of system calls), and then terminate abnormally (another system call). If the input file exists, then we must create a new output file. We may find an output file with the same name. This situation may cause the program to abort (a system call), or we may delete the existing file (another system call) and create a new one (another system call). In an interactive system another option is to ask the user (a sequence of system calls to output the prompting message and to read the response from the keyboard) whether replace the existing file or to abort the program.
Now that both files are set up, we enter a loop that reads from the input file (a system call) and writes to the output file (another system call). Each read and write must return status information regarding various possible error conditions. On input, the program may find that the end of the file has been reached, or that a hardware failure occurred in the read (such as a parity error). On output, various errors may occur, depending on the output device (such as no more disk space, physical end of tape, printer out of paper).
Finally after the entire file is copied, the program may close both files (another system call), write a message to the screen (more system calls), and finally terminate normally (the final system call). As we can see, even simple programs may make heavy use of the operating system.
Types of System Calls
1. Process control
2. File management
3. Device management
4. Information maintenance
5. Communication.
Process Control
End, abort
Running program needs to be able to stop its execution either normally (end) or abnormally (abort). If a system call is made to terminate the currently running program abnormally or if the programs runs into a problem and causes trap, a dump of memory is sometimes taken and an error message is generated. The dump is written to the disk and may be examined by a debugger to determine the cause of the problem. Under normal or abnormal circumstances, the operating system must transfer control to the command interpreter. The command interpreter then reads the next command. In an interactive system, the command interpreter simply continues with the next command; it is assumed that the user will issue an appropriate command.
Load, execute
A process or job executing one program may want to load and execute another program. This feature allows the command interpreter to execute a program as directed by, for example, a user command, the click of a mouse, or a batch command. An interesting question is where to return control when the loaded program terminates.
Create process, terminate process
When one program calls other program it has to create a new process. For example, to print the data on the paper we will require to create a process to do that job. When printing is finished, finally operating system will terminate process.
Get process attributes, set process attributes
If we create a new process, or perhaps even a set of processes, we should be able to control its execution. This control requires the ability to determine and reset the attributes of a process, including the job’s priority, its maximum allowable execution time, and so on.
Wait for a time
Having created new jobs or processes, we may need to wait for them to finish their execution.
Wait event, signal event
It may be possible that after completing execution of one process second process can execute, so second process has to wait till the completion of first process. When the first process finishes its execution, it will send signal to second process to execute.
Allocate and free memory
When a new process is created operating system will allocate memory to it and when it terminates it will free the memory.
Call | Description |
pid = fork() | Create a child process identical to the parent |
pid = waitpid(pid, &statloc) | Wait for a child to terminate |
s = execve(name, argv) | Replaces a process’ core image |
exit(status) | Terminate process execution and return status |
FIGURE 1.1 Various types of system calls for Process Control
File Management
Create file, delete file
If any information is to be stored than we require a file to store that information, during these, operating system will create file and when the file is no longer needed it will delete the file.
Open, close
Once file is created there will be need to read the data so operating system will make call of open. When reading of the data is completed the call will be of close.
read, write, reposition
Once file is open the data can be read through read call, if there is any need to edit data than there will be call of write and eventually the call will be of reposition (rewind or skip to the end of the file).
Get file attributes, set file attributes
We may need these same sets of operations for directories if we have a directory structure for organizing files in the file system. In addition, for either files or directories, we need to be able to determine the values of various attributes, and perhaps to reset them if necessary. File attributes include the file name, a file type, protection code.
Call | Description |
fd = open(file, how…) | Open a file for reading, writing or both |
s = close(fd) | Close an open file |
n = read(fd, buffer, nbytes) | Read data from a file into a buffer |
n = write(fd, buffer, nbytes) | Write data from a buffer into a file |
position = iseek(fd, buffer, nbytes) | Move the file pointer |
s = stat(name, &buf) | Get a file’s status information |
FIGURE 1.2 Various types of system calls for File Management
Device Management
Request device, release device
For example, if there is a process (a + b) than there will be need to enter two variable values through keyboard so the call will be of request device. After entering the values the device will be released.
Read, write, reposition
Once the device has been requested (and allocated), we can read, write and (possibly) reposition the device, just as we can do with ordinary files.
Get device attributes, set device attributes.
Logically attach or detach devices.
Information Maintenance
Get time or date, set time or date
If there is need to get an idea about the current time and date of system and also if we want to set time or date than there will be this call.
Get system data, set system data
Data like operating system version, available memory …etc, this information can get from this call.
Get process file, or device attributes.
Set process file, or device attributes.
Communication
Create and delete communication
If two processes want to communicate with each other (parent and child process) the connection will be established and after completing the communication the connection will be deleted.
Send, receive message
After establishing the connection two processes can send and receive messages by this call.
Transfer status information
If there is any error in establishing communication or if both processes had completed their communication then there will be this call.
Get process Id, Host Id
With the help of process Id we can know which processes are communicating and with the help of Host Id we can know which computers are communicating.
Resource Used:
1. Operating System Concepts (By: Silberschatz, Galvin, Gagne)
Compiled By: Chaudhary Amit V.
Comments
Post a Comment