0% found this document useful (0 votes)
3 views22 pages

Chapter 9

Chapter 9 discusses system services that allow application programs to interact with the operating system for operations like console input/output and file management. It details how system calls function, including the use of registers for arguments and the process of opening, reading, and writing files. The chapter also provides examples of console output and input, as well as file operations, illustrating the necessary system service calls and their arguments.

Uploaded by

Phan Cong Thanh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views22 pages

Chapter 9

Chapter 9 discusses system services that allow application programs to interact with the operating system for operations like console input/output and file management. It details how system calls function, including the use of registers for arguments and the process of opening, reading, and writing files. The chapter also provides examples of console output and input, as well as file operations, illustrating the necessary system service calls and their arguments.

Uploaded by

Phan Cong Thanh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

02/03/2019

Chapter 9

SYSTEM SERVICES

Introduction

 There are many operations that an application program


must use the operating system to perform
 Such operations include console output, keyboard input,
file services (open,read, write, close, etc.), obtaining the
time or date, requesting memory allocation, and many
others.
 Accessing system services is how the application requests
that the operating system perform some specific operation
(on behalf of the process)
 More specifically, the system call is the interface between
an executing process and the operating system

1
02/03/2019

Calling System Services

 A system service call is logically similar to calling a


function, where the function code is located within the
operating system
 The function may require privileges to operate which is
why control must be transferred to the operating system.
 When calling system services, arguments are placed in
the standard argument registers.
 System services do not typically use stack-based
arguments. This limits the arguments of a system
services to six (6)
 To call a system service, the first step is to determine
which system service is desired

 The general process is that the system service call code is


placed in the rax register.
 The call code is a number that has been assigned for the
specific system service being requested.
 These are assigned as part of the operating system and
cannot be changed by application programs
 If any are needed, the arguments for system services are
placed in the rdi, rsi, rdx, r10, r8, and r9 registers (in that
order).
 The following table shows the argument locations
which are consistent with the standard calling convention.

2
02/03/2019

 Each system call will


use a different number
of arguments (from
none up to 6).However,
the system service call
code is always required
 After the call code and
any arguments are set,
the syscall instruction is
executed

System Calls

 Each x86-64 system call has a unique ID number


 Examples:

3
02/03/2019

Newline Character
 In the context of output, a newline means move the
cursor to the start of the next line.
 The many languages, including C, it is often noted as
“\n” as part of a string
 Nothing is displayed for the newline, but the cursor is
moved to the start of the next line.
 In Unix/Linux systems, the linefeed, abbreviated LF with
an ASCII value of 10 (or 0x0A), is used as the newline
character.
 In Windows systems, the newline is carriage
return, abbreviated as CR with an ASCII value 13 (or
0x0D) followed by the LF.

Console Output
 The system service to output characters to the console is
the system write (SYS_write).
 Like a high-level language characters are written to
standard out (STDOUT) which is the console
 The STDOUT is the default file descriptor for the console
 The arguments for the write system service are as follows:

4
02/03/2019

 Assuming the following declarations:

 For example to output “Hello World” (it’s traditional) to


the console, the system write (SYS_write) would be
used. The code would be as follows:

Example, Console Output

 This example is a complete program to output


some strings to the console.

5
02/03/2019

6
02/03/2019

7
02/03/2019

Console Input
 The system service to read characters from the
console is the system read (SYS_read).
 Like a high-level language, for the console,
characters are read from standard input (STDIN).
 We will need to declare an appropriate amount of
space to store the characters being read
 If we request 10 characters to read and the user types
more than 10, the additional characters will be lost
 If the user types less than 10 characters, for example
5 characters, all five characters will be read plus the
newline (LF) for a total of six characters.

 The arguments for the read system service are as


follows:

 Assuming the following declarations:

8
02/03/2019

Example

 Read a single character from the keyboard, the


system read (SYS_read) would be used. The code
would be as follows:

Example, Console Input


 Read a line of 50 characters from the keyboard, and then
echo the input back to the console to verify that the input
was read correctly
 Since space for the newline (LF) along with a final NULL termination is
included, an input array allowing 52 bytes would be required.

9
02/03/2019

10
02/03/2019

11
02/03/2019

12
02/03/2019

File Open Operations

 In order to perform file operations such as read and write,


the file must first be opened.
 There are two file open operations, open and open/create.
 After the file is opened, in order to perform file read or
write operations the operating system needs detailed
information about the file, including the complete status
and current read/write location.
 If the file open operation fails, an error code will be
returned.
 If the file open operation succeeds, a file descriptor is
returned.

 The complete set of information about an open


file is stored in an operating system data structure
named File Control Block (FCB)
 File Open
 The file open requires that the file exist in order to be
opened. If the file does not exist, it is an error
 The file open operation also requires the parameter
flag to specify the access mode. The access mode
must include one of the following
 Read-Only Access → O_RDONLY
 Write-Only Access → O_WRONLY
 Read/Write Access → O_RDWR

13
02/03/2019

 The arguments for the file open system service


are as follows:

 Assuming the following declarations:

 After the system call, the rax register will contain the
return value.
 If the file open operation fails, rax will contain a negative
value (i.e., < 0).
 If the file open operation succeeds, rax contains the file
descriptor
 File Open/Create
 A file open/create operation will create a file
 If the file does not exist, a new file will be created
 If the file already exists, it will be erased and a new file created.
 Since the file is being created, the access mode must include the
file permissions that will be set when the file is created.

14
02/03/2019

 The arguments for the file open/create system service are


as follows:

 Assuming the following declarations:

 File Read
 A file must be opened with the appropriate file access flags
before it can be read.
 The arguments for the file read system service are as follows:

 Assuming the following declarations:

 If the file read operation does not succeed, a negative value is


returned in the rax register. If the file read operation succeeds,
the number of characters actually read is returned.

15
02/03/2019

 File Write
 The arguments for the file write system service are as
follows:

 Assuming the following declarations:

 If the file write operation does not succeed, a negative


value is returned in the rax register. If the file write
operation does succeed, the number of characters
actually written is returned.

Example, File Write

 Program writes a short message to a file


 The file created contains a simple message, a URL in
this example.
 The file name and message to be written to the
file are hard-coded

16
02/03/2019

17
02/03/2019

18
02/03/2019

19
02/03/2019

20
02/03/2019

21
02/03/2019

Example, File Read

22

You might also like