
Intro to System Calls Dr. Chokchai (Box) Leangsuksun Louisiana Tech University 1 Outline • Introduction to System Call • Anatomy of system calls • Some useful system calls – Process control" –! File management" –! Information maintenance" –! Communications" Louisiana Tech University 2 System Calls" •! Programming interface to the services provided by the OS" •! Typically written and used in a high-level language (C or C ++)" •! Mostly accessed by programs via a high-level Application Program 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)" •! # " "(Note that the system-call names used throughout this text are generic)" Louisiana Tech University 3 Linux/Unix System Calls" •! Linux/UNIX has about 60 system calls " •! The most calls are written in C. " •! can be accessed from C programs. " •! There are similar system programs that provide similar system call features/services " •! Basic I/0" •! Process control (creation, termination, execution)" •! File operations and permission " •! System status " Louisiana Tech University 4 Example of System Calls" •! System call sequence to copy the contents of one file to another file" Louisiana Tech University 5 System Call Implementation" •! Typically, a number associated with each system call" –! System-call interface maintains a table indexed according to these numbers" •! The system call interface invokes intended system call in OS kernel and returns status of the system call and any return values" •! The caller need know nothing about how the system call is implemented" –! Just needs to obey API and understand what OS will do as a result call" –! 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)" Louisiana Tech University 6 API – System Call – OS Relationship" Standard C Library Example" •! C program invoking printf() library call, which calls write() system call" Louisiana Tech University 8 System Call Parameter Passing" •! Often, more information is required than simply identity of desired system call" –! Exact type and amount of information vary according to OS and call" •! Three general methods used to pass parameters to the OS" 1.! Simplest: pass the parameters in registers! •! In some cases, may be more parameters than registers" 2.! Parameters stored in a block, or table, in memory, and address of block passed as a parameter in a register " •! This approach taken by Linux and Solaris" 3.! Parameters placed, or pushed, onto the stack by the program and popped off the stack by the operating system" –! Block and stack methods do not limit the number or length of parameters being passed" Louisiana Tech University 9 Parameter Passing via Table" Types of System Calls" •! Process control" •! File management" •! Device management" •! Information maintenance" •! Communications" " Louisiana Tech University 11 Linux System Calls •! System calls are low level functions the operating system makes available to applications via a defined API (Application Programming Interface) •! System calls represent the interface the kernel presents to user applications. •! In Linux all low-level I/O is done by reading and writing file handles, regardless of what particular peripheral device is being accessed—a tape, a socket, even your terminal, they are all files. •! Low level I/O is performed by making system calls. Louisiana Tech University 12 Anatomy of a Linux System Call •! A System Call is an explicit request to the kernel made via a software interrupt. •! The interrupt call ‘0x80’ call to a system call handler (sometimes called the “call gate”). •! The system call handler in turns calls the system call interrupt service routine (ISR). •! To perform Linux system calls we have to do following: –! Put the system call number in EAX register. –! Set up the arguments to the system call in EBX,ECX, etc. –! call the relevant interrupt (for Linux, 80h). –! The result is usually returned in EAX. Louisiana Tech University 13 Sample of how a System Call is made read (fd, buffer, nbytes)! Louisiana Tech University 14 POSIX APIs vs. System Calls • An application programmer interface is a function definition that specifies how to obtain a given service. • A system call is an explicit request to the kernel made via a software interrupt. Louisiana Tech University 1515 From a Wrapper Routine to a System Call • Unix systems include several libraries of functions that provide APIs to programmers. • Some of the APIs defined by the libc standard C library refer to wrapper routines (routines whose only purpose is to issue a system call). • Usually, each system call has a corresponding wrapper routine, which defines the API that application programs should employ. Louisiana Tech University 1616 APIs and System Calls • An API does not necessarily correspond to a specific system call. – First of all, the API could offer its services directly in User Mode. (For something abstract such as math functions, there may be no reason to make system calls.) – Second, a single API function could make several system calls. – Moreover, several API functions could make the same system call, but wrap extra functionality around it. Louisiana Tech University 1717 Example of Different APIs Issuing the Same System Call • In Linux, the malloc( ) , calloc( ) , and free( ) APIs are implemented in the libc library. • The code in this library keeps track of the allocation and deallocation requests and uses the brk( ) system call to enlarge or shrink the process heap. – P.S.: See the section "Managing the Heap" in Chapter 9. Louisiana Tech University 1818 The Return Value of a Wrapper Routine • Most wrapper routines return an integer value, whose meaning depends on the corresponding system call. • A return value of -1 usually indicates that the kernel was unable to satisfy the process request. • A failure in the system call handler may be caused by – invalid parameters – a lack of available resources – hardware problems, and so on. • The specific error code is contained in the errno variable, which is defined in the libc library. Louisiana Tech University 1919 Sample of System Calls Louisiana Tech University 2020 Process Management Louisiana Tech University 21 File Management Louisiana Tech University 22 Directory Management Louisiana Tech University 23 Miscellaneous Tasks Louisiana Tech University 24 System Calls (5) Some Win32 API calls Louisiana Tech University 25 Process Management Louisiana Tech University 2626 Process Concept" •! An operating system executes a variety of programs:" –! Batch system – jobs" –! Time-shared systems – user programs or tasks" •! Textbook uses the terms job and process almost interchangeably" •! Process – a program in execution; process execution must progress in sequential fashion" •! A process includes:" –! program" –! program counter " –! stack" –! data section •! In Linux, how do you see process info and what can you see?" Louisiana Tech University 27 Process State" •! As a process executes, it changes state" –! new: The process is being created" –! running: Instructions are being executed" –! waiting: The process is waiting for some event to occur" –! ready: The process is waiting to be assigned to a process" –! terminated: The process has finished execution" Louisiana Tech University 28 Diagram of Process State" Louisiana Tech University 29 Process Creation •! Parent process create children processes, which, in turn create other processes, forming a tree of processes" •! Resource sharing" –! Parent and children share all resources" –! Children share subset of parent’s resources" –! Parent and child share no resources" •! Execution" –! Parent and children execute concurrently" –! Parent waits until children terminate" •! fork() system call creates new process" " Louisiana Tech University 3030 Linux Process Louisiana Tech University 3131 PROGRAMS AND PROCESSES fork(): Creating a Process Include File(s) <sys/ Manual Section 2 types.h> <unistd.h> Summary pid_t fork ( void ); Return Success Failure Sets errno 0 in child, child process ID in the parent -1 Yes 32 PROGRAMS AND PROCESSES fork(): Creating a Process § The fork system call does not take an argument. § If the fork system call fails, it returns a −1 and sets the value in errno to indicate one of the error conditions. 33 PROGRAMS AND PROCESSES fork(): errono # Constant perror Message Explanation 11 EAGAIN Resource temporarily The operating system was unable to allocate unavailable sufficient memory to copy the parent's page table information and allocate a task structure for the child. 12 ENOMEM Cannot allocate Insufficient swap space available to generate memory another process. 34 Example of a fork system call" #include <stdio.h>! #include <stdlib.h>! int main()! {! pid_t pid;! !/* fork another process */! !pid = fork();! !if (pid < 0) { /* error occurred */! ! !fprintf(stderr, "Fork Failed");! ! !exit(-1);! !}! !else if (pid == 0) { /* child process */! ! !execlp("/bin/ls", "ls", NULL);! !}! !else { /* parent process */! ! !/* parent will wait for the child to complete */! ! !wait (NULL);! ! !printf ("Child Complete");! ! !exit(0);! !}! }! Louisiana Tech University 35 Show sample of execlp & fork Louisiana Tech University 3636 PROGRAMS AND PROCESSES Managing Failures Include File(s) <stdio.h> Manual Section 3 Summary void perror (const char
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages131 Page
-
File Size-