The open system call opens a file and returns a file descriptor. It takes a file path, access flags, and optional mode as arguments. The library function open() wraps the system call. It returns the file descriptor on success or -1 on error and sets errno. The program then uses the descriptor for read, write, or other file operations.
1 of 24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
More Related Content
Services and system calls
1. Introduction to Systemcalls
• Objectives
•
•
•
•
•
Understanding system calls
Systemcalls and library functions
Interfacing functions between user spaceand kernelspace
Typesof systemcalls
• File management
• Processmanagement
• Device management
• Information andmaintenance
• Processcommunication Programs
implementing systemcalls
2. Operating systemservices
•
•
•
An Operating system provides an environment for the executionof
programs.
It provides set of services to programs and to users of those programs.
One set of services provides functions that are helpful to the user
– User interface
– Program execution
– I/O operations
– File systemmanipulations
– Communications
– Error detection
• Another set of services exists not for helping the user but rather for
ensuring the efficient operation ofthe system itself.
–
–
–
Resourceallocation
Accounting
Protection andsecurity
3. User related services
• Userinterface
–
–
Almost all OShave auser interface(UI)
Thesecan Command line interface (CLI),Batch (in which commandsare entered in files)
and Graphical user interface (GUI)
• Programexecution
– The system must be able to load the program in memory and execute.
• I/O operation
–
–
–
Arunning program may require I/O, which may involve afile and an I/O device.
For efficiency and protection purpose , users cannot control I/O devicesdirectly.
OSmust provides means to doI/O
• File systemmanipulations
– Programs needsto read and write filesand directories
– Alsocreate, delete, search and list files
– Permission management to allow or deny accesstofiles and directories
4. •
•
Communication
– Communication between processexecuting on samecomputer or
between processesthat are executing on different computersystems
tied together by computernetworks
– Communication canbe implemented via “shared memory” orthrough
“message passing” in which casepackets of information moves
between processesby the OS.
Error detection
– OSneeds to be constantly aware of possible errors. Errorsmay occur
• In CPUor memory hardware (suchasmemory error or power
failure),
• In I/O devices (lack of paper in printer or connection failure on
network)
• In Userprograms (arithmetic over flow or an attempt toaccessan
illegal memory location)
5. Systemrelated services
•
•
Resourceallocation
– When there are multiple usersrunning at the sametime,resources
must be allocated to eachofthem.
– Many different types of resources are managed by the OSsuchas
• Main memory
• File storage
• CPUcycles
CPUSchedulingRoutines
Accounting
– Keeping track of usersactivity and resource allocation andutilization
– Usagestatistics may be valuable tool for researchers who wishto
reconfigure the system to improve computingservices
6. Systemrelated services
• Protection and security
– When several processesexecute concurrently, it not be possible for
one process to interfere with the others or with the operating system
itself.
– Protection involves ensuring that all accessto system resourcesis
controlled
– Security of the system from theoutsiders is also important
– Suchsecurity starts with requiring eachuser to authenticate himself
or herself to the system, usually by meansof password to gain access
to the systemresources.
7. Systemcalls
•
•
•
•
•
•
All operating systemsprovides somemechanism to request servicesfrom
the kernel.
Thisservice request points are called “systemcalls”
In Linux system, there is an interface layer between the user spaceand
kernel space.Thislayer consists of library made up of functions that
communicate between normal user applications and theKernel.
Thisimplementation of Clibrary is called libc/glibc. Thisprovides wrapper
functions for the systemcalls.
Systemcalls are implemented using software interrupt or trap.
System call transfer control to the operating system. It sets the system call
number, the Carguments into the general registers and then executes
somemachine instruction that generates asoftware interrupt in the
kernel.
8. Example on how system calls are used
•
•
•
•
•
Writing a simple program to read
data from one file and copy them
to anotherfile
Programinputs
– Name of two files
Program opens the input fileand
create output file and openit
Eachof these operationsrequire
another systemcalls
Now that both files are setup,
enter into the loop that read
from input file (systemcall)and
writes into the output file
(another systemcall)
11. Library functions
•
•
•
•
•
Library functions are the general purpose functions defined in Section 3of
“Linux/Unix programmersManual”
$ man <section_no> <command_name>
Thesefunctions are not entry points into the kernel although they may
invoke one or more of thekernel functions.
For example, ‘printf’ may invoke the ‘write’ system call to perform the
output .
‘strcpy’ and ‘atoi’ doesn’t invoke the kernel service at all. They are library
functions
12. Clibrary interface
•
•
•
Thestandard Clibrary provides a
portion of the system call
interface for many versions of
UNIXand Linux
For example, the ‘printf’
statement. TheClibrary
interprets this call and invokes
the necessarysystem call(s) in
the operating system in this
instance , the write() systemcall
TheClibrary takes the value
returned by write() and passesit
back to the userprogram.
13. UserSpace
system call and library function
• Examples of systems
calls
• Read() / write()
• Fork()/ exec()
• Example of library
functions
• Date/time
• Strcpy/atoi/printf
Application code
Clibrary functions
Systemcalls
14. Header Files
•
•
TheApplication
programming interface(API)
of the Cstandard library is
declared in anumber of
header files. Eachheader
files contains one or more
function declarations, data
types definitions and
macros.
Header files arelocated
at /usr/include
Name Description
<errno.h>
Testingerror code reported bylibrary
functions.
<stdarg.h>
For accessingvarying numberof
arguments passedto functions
<stdatomic.h>
For atomic operations on datashared
between threads
<stddef.h> Defines several types andmacros
<stdio.h> Defines core input/output APIS
<stdlib.h>
Defines memory allocation/process
control
<string.h> Defines string manipulationsAPIs.
<threads.h> Threadhandling APIs
<time.h> Date/Time handling APIs
16. Systemcall categories
• Systemcalls can be roughly
grouped into five major
categories:
– Processmanagement
– File management
– Device management
– Information/Maintenance
– Communication
17. • Process management
– create processthe process
– Terminate process- end and abort
– Loadthe process
– Execute the process
– get/set processattributes
– wait fortime, wait event, signal event
– Allocate and free memory
21. Systemcall categories..contd…
Communication: Used for intercrosses Communication
Create ,delete communication connection
Send receive messages
Help operating system to transfer status information
Attach or detach remote devices
22. Systemcall categories..contd…
Protection: controlling Access to resources provided by computer
system
Set permissions and get permissions: manipulate permission of
resources
Allow user or deny any user to access specific resource.
23. Open function
Openandpossiblycreate a file or device
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int open(const char *pathname, intflags);
int open(const char *pathname, int flags, mode_t mode);
int creat(const char *pathname, mode_tmode);
DESCRIPTION
Given apathname for afile, open() returns afile descriptor, asmall, nonnegative
integer for use in subsequent system calls (read, write, lseek, fcntl, etc.).
Flags: Oneof the accessmodes: O_RDONLY,O_WRONLY,or O_RDWR. These
request opening the file read-only, write-only, or read/write, respectively.
RETURNVALUE
open() and creat() return the new file descriptor, or -1 if an error occurred (in
which case,errno is setappropriately).