0% found this document useful (0 votes)
15 views41 pages

LC 1

This document discusses an operating systems programming course that teaches students how to properly use system calls in C to manage files, directories, drivers, processes, permissions, pipes and signals on Linux. The course objectives are to use main system calls to enable program-OS communication, write programs that handle system objects, optimize resource use, and make programs interact. Students will write C programs that use features like dynamic data structures, pointers, and binary operators. The document also provides an overview of operating systems, their functions, and concepts like memory management, processors, devices, files, security and performance control. It discusses process management, scheduling algorithms, POSIX standards, system calls versus library functions, and the life cycle of a process.
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)
15 views41 pages

LC 1

This document discusses an operating systems programming course that teaches students how to properly use system calls in C to manage files, directories, drivers, processes, permissions, pipes and signals on Linux. The course objectives are to use main system calls to enable program-OS communication, write programs that handle system objects, optimize resource use, and make programs interact. Students will write C programs that use features like dynamic data structures, pointers, and binary operators. The document also provides an overview of operating systems, their functions, and concepts like memory management, processors, devices, files, security and performance control. It discusses process management, scheduling algorithms, POSIX standards, system calls versus library functions, and the life cycle of a process.
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/ 41

System

Programming
Teachers: Nicolas Magaud, Konul Aliyeva
CM01: Introduction / OS Programming
Synopsis

This course presents the main concepts of


operating systems from a programming point of
view. It aims at making the students use properly
system calls provided in C for files and directories,
drivers, processes, permissions, pipes and signals.
It focuses on building portable applications by
using the POSIX norm. Practical works consist in
writing C programs in a Linux environment.
Objectives


use the main system calls properly, thus
enabling communication between a program
and the operating system;

write programs handling system objects such
as processes, files, directories, pipes and
signals;

optimize the use of system resources;

make programs interact with one another.
Requirements


write programs using C features (such as
dynamic data structures, pointers, binary
operators, etc.);


translate algorithms into actual C programs;


read and write shell script
Operating System

An Operating System (OS) is an interface


between a computer user and computer
hardware. An operating system is a software
which performs all the basic tasks like file
management, memory management, process
management, handling input and output, and
controlling peripheral devices such as disk drives
and printers.
Important functions of an OS

Memory Management

Processor Management

Device Management

File Management

Security

Control over system performance

Error detecting aids

Coordination between other software and users
Memory Management


Keeps tracks of primary memory, i.e., what part
of it are in use by whom, what part are not in
use.

In multiprogramming, the OS decides which
process will get memory when and how much.

Allocates the memory when a process requests
it to do so.

De-allocates the memory when a process no
longer needs it or has been terminated.
Processor Management


Keeps tracks of processor and status of
process. The program responsible for this task
is known as traffic controller.

Allocates the processor (CPU) to a process.

De-allocates processor when a process is no
longer required.
Device Management


Keeps tracks of all devices. Program
responsible for this task is known as the I/O
controller.

Decides which process gets the device when
and for how much time.

Allocates the device in the efficient way.

De-allocates devices.
File Management


Keeps track of information, location, uses,
status etc. The collective facilities are often
known as file system.

Decides who gets the resources.

Allocates the resources.

De-allocates the resources.
Other Important Activities


Security − By means of password and similar other techniques, it
prevents unauthorized access to programs and data.

Control over system performance − Recording delays between
request for a service and response from the system.

Job accounting − Keeping track of time and resources used by
various jobs and users.

Error detecting aids − Production of dumps, traces, error messages,
and other debugging and error detecting aids.

Coordination between other softwares and users − Coordination and
assignment of compilers, interpreters, assemblers and other software
to the various users of the computer systems.
Operating System Services


Program execution

I/O operations

File System manipulation

Communication

Error Detection

Resource Allocation

Protection
Program execution


Loads a program into memory.

Executes the program.

Handles program's execution.

Provides a mechanism for process
synchronization.

Provides a mechanism for process
communication.

Provides a mechanism for deadlock handling.
I/O Operation


I/O operation means read or write operation
with any file or any specific I/O device.

Operating system provides the access to the
required I/O device when required.
File system manipulation


Program needs to read a file or write a file.

The operating system gives the permission to the program for
operation on file.

Permission varies from read-only, read-write, denied and so on.

Operating System provides an interface to the user to
create/delete files.

Operating System provides an interface to the user to
create/delete directories.

Operating System provides an interface to create the backup of
file system.
Communication


Two processes often require data to be
transferred between them

Both the processes can be on one computer or
on different computers, but are connected
through a computer network.

Communication may be implemented by two
methods, either by Shared Memory or by
Message Passing.
Error handling


The OS constantly checks for possible errors.

The OS takes an appropriate action to ensure
correct and consistent computing.
Resource Management


The OS manages all kinds of resources using
schedulers.

CPU scheduling algorithms are used for better
utilization of CPU.
Protection


The OS ensures that all access to system
resources is controlled.

The OS ensures that external I/O devices are
protected from invalid access attempts.

The OS provides authentication features for
each user by means of passwords.
Operating System – Processes
A process is defined as an entity which
represents the basic unit of work to be
implemented in the system.
Process Life Cycle
Operating System Scheduling Algorithms


First-Come, First-Served (FCFS) Scheduling

Shortest-Job-Next (SJN) Scheduling

Priority Scheduling

Shortest Remaining Time

Round Robin(RR) Scheduling

Multiple-Level Queues Scheduling

Etc.
POSIX Standard-Portable Operating System Interface

POSIX is a set of standard operating system


interfaces based on the Unix operating system.
Long time ago, UNIX had a lot of different
implementations. This was leading to problems
about portability. One application developed for
one Unix variant like HP-UX did not work for
other Unix variant like AIX.
POSIX versions

POSIX.1 or C API
This standard extend ANSI C standard and add some
concepts, operations and libraries like mkdir ,
dirname , fork , kill , networking and Linux system
calls.

POSIX.2 or CLI Utilities
This standard defines commands like cd , ls , echo , …

POSIX.3 or Shell Language
This substandard defined some shell language and
programming basics like a=b echo $a etc.

POSIX.4 or Environment Variables
Environment variables provide a way to influence the
behaviour of software on the system. Printenv prints
the names and values of all currently defined env
variables.

POSIX.5 or Program Exit Status

POSIX.6 or Regular Expression

POSIX.7 or Directory Structure

POSIX.8 or File Names

POSIX.9 or Command Line API Utility Conventions
System Calls and Library Functions

The functions which are a part of standard C


library are known as Library functions. For
example the standard string manipulation
functions like strcmp(), strlen() etc are all library
functions.
The functions which change the execution
mode of the program from user mode to kernel
mode are known as system calls. These calls
are required in case some services are required
by the program from kernel.
Types of Library Functions


Functions which do not call any system call.
The string manipulation functions like strlen()
etc fall under this category.

Functions that make a system call.
The fopen() function which is a standard library
function but internally uses the open() sytem
call.
Interaction Between Components
fopen() vs open()

Why do we have two functions for the same


operation i.e. opening a file?
fopen() is a library function which provides
buffered I/O services for opening a file while
open() is a system call that provides non-
buffered I/O services.
Buffered I/O services refer to the technique of
temporarily storing the results of an I/O
operation in user-space before transmitting it to
the kernel (in the case of writes) or before
providing it to your process (in the case of
reads).
By so buffering the data, you can minimize the
number of system calls and can block-align I/O
operations, which may improve the
performance of your application.
If there are functions exist which do the same
job (library functions and system calls), which
one a user should use?
If a library function corresponding to a system
call exists, then applications should use the
library function because :

Library functions are portable which means an
application using standard library functions will
run on all systems. While on the other hand an
application relying on the corresponding system
call may not run on every system as system call
interface may vary from system to system.

Sometimes the corresponding library function
makes the load to system call lesser resulting in
non-frequent switches from user mode to kernel
mode. For example if there is an application
that reads data from file very frequently, then
using fread() instead of read() would provide
buffered I/O which means that not every call to
fread() would result in a call to system call
read().
Some other differences

A library function is linked to the user program
and executes in user space while a system call
is not linked to a user program and executes in
kernel space.

A library function execution time is counted in
user level time while a system call execution
time is counted as a part of system time.

Library functions can be debugged easily using
a debugger while System calls cannot be
debugged as they are executed by the kernel.
Command Line

Most applications can be launched using a


command , e.g. firefox & instead of clicking on
an icon.
Options, (as well as file names) can be added
to the command line : gcc -o a.out my_file.c
These arguments can be retrieved inside a C
program using argc/argv mechanism using the
formal parameters argc and argv:

You might also like