COM222 INTRODUCTION TO OPERATING SYSTEMS LESSON 2. SYSTEM CALLS AND SYSTEM PROGRAMS Unit introduction • This unit aims at understanding what system calls and interrupts are. It explains basics of operating systems system calls and introduces the aspect of interrupts and interrupts handling. Learning outcomes: • By the end of this unit the student should be able to: • Define what a system call is • Explain the difference between system calls and system programs • Explain the concept of interrupts and interrupts handling System Calls • System calls provide an interface between the process and the operating system. • System calls allow user-level processes to request some services from the operating system, which process itself is not allowed to do. • In handling the trap, the operating system will enter in the kernel mode, where it has access to privileged instructions, and can perform the desired service on the behalf of user-level process. It is because of the critical nature of operations that the operating system itself does them every time they are needed. For example, for I/O a process involves a system call telling the operating system to read or write particular area and this request is satisfied by the operating system. System Calls for Process Management • The examples of system calls for process management are FORK, WAITPID and EXEC. • FORK is a system call for process management that helps to create a new process. • It creates an exact duplicate of the original process, including all the file descriptors, registers and everything. After the FORK, the original process and the copy (the parent and child) go their separate ways. WAITPID system call • This is one of the system calls for process management. To wait for the child to finish executing a command, the parent executes a WAITPID system call, which just waits until the child terminates any child if more than one exists. • WAITPID can wait for a specific child or for any old child by setting the first parameter to -1. When WATPID completes, the address pointed to by the second parameter will be set to the child's exit status. EXEC System call • This is another system call for process management. The Exec System call is used by the child process to execute the user command. When a command is typed, a new process is generated. When this is done it causes its entire core image to be replaced by the file named in its first parameter. • In the most general case, EXEC has three parameters: the name of the file to be executed, a pointer to the argument array, and a pointer to the environment array (in a program). System Calls for signaling • Although most forms of inter-process communication are planned, situations exist in which unexpected communication is needed. For example, if a user accidently tells a text editor to list the entire contents of a very long file, and then realizes the error, some way is needed to interrupt the editor. • When a signal is sent to a process that has not announced its willingness to accept that signal, the process is instantly killed. To avoid this fate, a process can use the SIGACTION system call to announce that it is prepared to accept some signal type, and to provide the address of the signal handling procedure and-a place to store the address of the current one. • Signals are handled by signal handlers that may run for as long as they want to and perform any system calls they want to. • In practice, signal handlers are usually fairly short. When the signal handling procedure is done, it calls SIGRETURN to continue where it left off before the signal. The SIGACTION call replaces the older SIGNAL call, which is now provided as a library procedure, however, for backward compatibility. System Calls for File Management • Many system calls relate to the file system. In this section we take a look at calls that operate on individual files. • To create a new file, the CREAT call is used. • CREAT not only creates a new file but also opens it for writing, regardless of the file's mode. • The CREAT call is obsolete, as OPEN can now create new files, but it has been included for backward compatibility. System Calls for Directory Management • The first two calls are MKDIR and RMDIR. • The MKDIR is used to create an empty directory. The RMDIR is used to remove an empty directory. • The next call is LINK. Its purpose is to allow the same file to appear under two or more names, often in different directories. A typical use is to allow several members of the same programming team to share a common file, with each of them having the file appear in his own directory, possibly under different names. • Sharing a file is not the same as giving every team member a private copy, because having a shared file means that changes that any member of the team makes are instantly visible to the other members-there is only one file. Interrupts • Interrupts are an unpleasant fact of life. They should be hidden away, deep in the bowels of the operating system, so that as little of the system as possible knows about them. The best way to hide them is to have every process starting an I/O operation has completed and the interrupt occurs. Interrupt Handling • When an event occurs, the process is put on the ready queue. When an event occurs, the system receives an interrupt. An interrupt is a signal to the CPU. It is called an interrupt. It is called an interrupt, because the CPU stops whatever it is doing and looks to see what caused the interrupt. • The interrupt come from a hardware device (e.g. a timer, disk drive or network card) stating that some action is complete and data is available that the CPU needs to use. It could arise from software – e.g. a run-time error being trapped in a process. • When an interrupt occurs, the CPU jumps to a special interrupt service routine to deal with the interrupt. This can involve rescheduling processes or moving processes between states. For example, a mouse click will generate an interrupt. This might make our process runnable again. When servicing this interrupt the CPU will therefore move the process Blocked state to Ready state. • After executing the interrupt service routine, the CPU may resume the original process or it may reschedule a new process. The decision depends on the event and the scheduling policies. References: • • • • Beaumont C, (2001): “Introducing operating systems” edge hill, 1st Edition Operating Systems, McGraw-Hill Book Company, New York By S. E. Madnick and J. J. Donovan Millenkovi C.M (2002). "Operating System; Concepts & Design", Megraw Hill. Tanenbaum A.S (1999). "Operating Systems Design and Implementations", Prentice Hall NJ.