Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit I

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 67

UNIT-I

REAL TIME SYSTEMS


OPEARTING SYSTEMS

 An operating system (OS) is system software


that manages computer hardware, software
resources, and provides common services for
computer programs
Introduction

Operating systems have some essential elements such as a


 process scheduler

 Task Management

 memory management

 File Management

 File systems multitasking

 Process synchronization

 interrupt event handling

 Timers and clocks

 Inter-task communication, I/O, and memory management


RTOS
 A real-time operating system is an operating system
intended to serve real-time applications that process
data as it comes in, typically without buffer delays.
Processing time requirements are measured in tenths of
seconds or shorter increments of time
 A RTOS has an advanced algorithm for scheduling.
Scheduler flexibility enables a wider
 Key factors in a real-time OS are minimal
interrupt latency and minimal thread switching latency
 a real-time OS is valued more for how quickly or how
predictably it can respond than for the amount of work
it can perform in a given period of time
RTOS USES
 Airlines reservation system.
 Air traffic control system.
 Systems that provide immediate updating.
 Used in any system that provides up to date and
minute information on stock prices.
 Defense application systems like RADAR.
 Networked Multimedia Systems.
 Command Control Systems
RTOS NAMES
 eCos
 LynxOS
 QNX
 RTLinux
 Symbian OS
 VxWorks
 Windows CE
 MontaVista Linux
Unix
 Unix is a family of multitasking, multiuser computer
operating systems that derive from the original
AT&T Unix, whose development started in the 1970s
at the Bell Labs research center by Ken Thompson,
Dennis Ritchie, and others
Flavors of Unix
 AIX : AIX is commercial version of unix product
IBM.
 BSD : Berkeley Software Distribution

 Sun Solaries

 Mac OS

HP-UX : HP-UX was the first Unix to use access


control lists for file access permissions
Linux
 Linux is a family of open-source Unix-like
operating systems based on the Linux kernel, an
operating system kernel first released on September
17, 1991, by Linus Torvalds. Linux is typically
packaged in a Linux distribution
Flavors of Linux
 Debian. ...
 Gentoo. ...
 Ubuntu. ...
 Linux Mint. ...
 Red Hat Enterprise Linux. ...
 CentOS. ...
 Fedora. ...
 Kali Linux.
Modern Unix Systems
 Unix Systems follows Designing principles and
features called modern unix systems
 Ex:BSD(Berkeley Software Distribution)

Features:
1.Creation of child process—fork()
2.Termination of a process
3.Waiting for termination of a child process
Structure of Modern Unix Systems

Common Facilities
1.STREAMS
2.Blak device switch
3.Virtual memory frame work
4.Execution switch
5.Vnode Vfs interface
6.Schedular framework
Structure of Modern Unix Systems
Features of Unix
 Ability to support multi user and multitasking
 Portability
 Excellent network environment
 Adaptability and simplicity
 Better security
 Flexible file systems
History of Linux
 the Unix operating system was conceived and implemented by
Ken Thompson and Dennis Ritchie (both of AT&T Bell Laboratories) in
1969 and first released in 1970
 In 1977, the Berkeley Software Distribution (BSD) was developed by the
Computer Systems Research Group (CSRG) from UC Berkeley, based on
the 6th edition of Unix from AT&T
 In 1983, Richard Stallman started the GNU project with the goal of
creating a free UNIX-like operating system. As part of this work, he wrote
the GNU General Public License (GPL). By the early 1990s, there was
almost enough available software to create a full operating system
 In 1991, while studying computer science at University of Helsinki, Linus
Torvalds began a project that later became the Linux kernel. He wrote the
program specifically for the hardware he was using and independent of an
operating system because he wanted to use the functions of his new PC
with an 80386 processor
Features of Linux

 Following are some of the important features of Linux Operating System.


 Portable − Portability means software can works on different types of hardware in
same way. Linux kernel and application programs supports their installation on any
kind of hardware platform.
 Open Source − Linux source code is freely available and it is community based
development project. Multiple teams work in collaboration to enhance the
capability of Linux operating system and it is continuously evolving.
 Multi-User − Linux is a multiuser system means multiple users can access system
resources like memory/ ram/ application programs at same time.
 Multiprogramming − Linux is a multiprogramming system means multiple
applications can run at same time.
 Hierarchical File System − Linux provides a standard file structure in which
system files/ user files are arranged.
 Shell − Linux provides a special interpreter program which can be used to execute
commands of the operating system. It can be used to do various types of operations,
call application programs. etc.
 Security − Linux provides user security using authentication features like password
The UNIX File System
• A simplified UNIX directory/file system:
/

bin dev lib usr


tmp
... ...
User

User3
User1 User2
• /Bin: contains executable files for most of the unix
commands.
• /Dev: contain files that control various input & output
devices.
• /Lib: contains all the library functions in binary form.
• /Usr: contains several directories each associated with
a particular user.
• /Tmp: contain the temporary files created by unix or
by any user.
• /Etc: contains configuration files of the system.
Architecture

 Hardware layer − Hardware


consists of all peripheral devices
(RAM/ HDD/ CPU etc).
 Kernel − It is the core component of
Operating System, interacts directly
with hardware, provides low level
services to upper layer components.
 Shell − An interface to kernel, hiding
complexity of kernel's functions
from users. The shell takes
commands from the user and
executes kernel's functions.
 Utilities − Utility programs that
provide the user most of the
functionalities of an operating
systems.
Components of Linux Kernel
 Memory management –Memory in secure manner
 Process management –it allows and controls the
process
 Inter-process communication (IPC)
 File systems
 Networking –access to networking protocols and
hardware
 Device control and device drivers—details and
hardware and other devices information
Overview of Commands
 Ls—list the files and directorys
 Cd—change a directory
 Mkdir—make a directory
 Rmdir—remove directory
 Cp—copy files into directory
 Rm—remove or delete files
 Mv—move or rename files
 More---look at file
 Lpr—send file to print
 Man---online manual
SED COMMAND
1.Line Number Command
‘=‘ it prints the current line number to standard output
2.Modify Commands
a)Append command—”a”,line after line
b)Insert command-----”i”,insert a line before every line
c)Change Command---replace every line matches with the
pattran
d)Delete Command---delete command in 2ways
a)”d”—delete entire pattern space
b)”D”---delete only first line of the pattern space
Input out put commands
 Next command(n)—read the next line from the input file

 Append next command(N)—clear the pattern space

before inputting the next line


 PrinList command—p—copies the current content total

pattern space
P—the print first line command prints only
 list command(I)

 It coverts the unprintable characters to their

corresponding octal code


File Commands
Read file command(r)—reads a file and places it
content in the output before moving to the next
command
Write file command(w)—line to a file name at the
point in the editing process
Branch Commands
Branch(b)---address ,command,attributes
Branch on substituttion(t)
File I/O
 Open()
 Create()
 Read()
 Write()
 Close()
 Lseek()
General API System calls
 open()---a process to open a file
 Read()---read a file
 Write()----write a file
 Lseek()---allow random access to a file
 Close()—close a file
 Chmod()—change the file access permissions
 Chown()—change UID/GID of a file
 Utime()---changes the last use and modification time
 Link()—create a hard link of a file
 Unlink()—delete hard link of a file
 Umask()---set default file creation mask
System calls
To use the services in the OS Unix offers some special
functions known as system calls.

The system call is a task which performs very basic


functions that requires communication with
CUP(common unix printing system), memory and
other devices.

The system calls are functions used in the kernel itself.


To the programmer, the system call appears as a
normal C function call.
File descriptors
 Each UNIX process has 20 file descriptors at it disposal,
numbered 0 through 19.
 The first three are already opened when the process
begins
 0: The standard input
 1: The standard output
 2: The standard error output
 When the parent process forks a process, the child
process inherits the file descriptors of the parent
open function
A file is opened or created by calling the open function.
#include <sys/types.h>(structure of data return by fun)
#include <sys/stat.h> >(structure of data return by fun)
#include <fcntl.h> (refer to file control –open,read,close)
int open(const char *pathname, int flags);
int open(const char *pathname, int flags, mode_t mode);
Returns: file descriptor if OK, -1 on error.
The pathname is the name of the file to open or create.
This function has a multitude of options, which are specified by
the oflag argument.
• This argument is formed by ORing together one or more of the
following constants from the <fcntl.h> header

O_RDONLY open for reading only.


O_WRONLY open for writing only.
O_RDWR open for reading and writing.

• Most implementations define O_RDONLY as 0, O_WRONLY


as 1, and O_RDWR as 2, for compatibility with older
programs.
The following constants are optional:

O_APPEND Append to the end of file on


each write.
O_CREAT Create the file if it doesn't exist. This option requires
a third argument to the open function, the mode, which specifies
the access permission bits of the new file.

O_EXCL Generate an error if O_CREAT is also specified and


the file already exists
O_TRUNC If the file exists and if it is successfully opened for
either write-only or readwrite, truncate its length to 0.
O_NOCTTY If the pathname refers to a terminal device, do not allocate the
device as the controlling terminal for this process.

O_NONBLOCK If the pathname refers to a FIFO, a block special file, or a


character special file, this option sets the non blocking mode for both the
opening of the file and subsequent I/O.
#include<stdio.h>
#include<fcntl.h>
#include<errno.h>
extern int errno;
int main()
{
// if file does not have in directory
// then file foo.txt is created.
int fd = open("foo.txt", O_RDONLY | O_CREAT);

printf("fd = %d/n", fd);

if (fd ==-1)
{
// print which type of error have in a code
printf("Error Number % d\n", errno);

// print program detail "Success or failure"


perror("Program");
}
return 0;ANS: fd = 3
}
create() Function
A new file can also be created by calling the create function.
Syntax:
#include <fcntl.h>
int creat( const char *pathname, mode_t mode);
Returns: file descriptor opened for write-only if OK, -1 on error.
This function is equivalent to
open (pathname, O_WRONLY | O_CREAT | O_TRUNC, mode);
close Function
An open file is closed by calling the close function.
#include <unistd.h>
int close(int filedes);
Returns: 0 if OK,-1 on error.

Return value:
-1-------error
0-----success
Int filedes(fd)—2 properties
1.Freeup all the file desc
2.De allocate all the resources for file table entry
#include<stdio.h>
#include <fcntl.h>
int main()
{
int fd1 = open("foo.txt", O_RDONLY);
if (fd1 < 0)
{
perror("c1");
exit(1);
}
printf("opened the fd = % d\n", fd1);

// Using close system Call


if (close(fd1) < 0)
{
perror("c1");
exit(1);
}
printf("closed the fd.\n");} opened the fd = 3 closed the fd.
Lseek() function
An open file's offset can be set explicitly by calling
lseek.
#include <unistd.h>
off_t lseek(int filedes, off_t offset, int whence);
Returns: new file offset if OK, -1 on error.
Return value:
-1------error
File offset----success
off_t lseek---unistd.h
1fd----file descrptor
2.offset---reference location
The interpretation of the offset depends on the value of the
whence argument.
If whence is:
• SEEK_SET--the file's offset is set to offset bytes from the
beginning of the file.
• SEEK_CUR--the file's offset is set to its current value plus the
offset. The offset can be positive or negative.
• SEEK_END--the file's offset is set to the size of the file plus
the offset. The offset can be positive or negative.
Eg:
lseek(fd, 10, SEEK_CUR)
It moves the pointer forwarded by 10 characters from its
current position.
Eg:
lseek(fd, -10, SEEK_END)
It sets pointer 10 characters before EOF.
lseek returns position of pointer in bytes from the
beginning of the file.The return value can be used to
determine the file size.
Size= lseek(fd, 0,SEEK_END)
Read()) Function
• Data is read from an open file with the read function.
#include <unistd.h>
ssize_t read( int filedes, void *buf, size_t nbytes);
Returns: number of bytes read, 0 if end of file, -1 on
error
• read attempts to read nbyte characters from the file
descriptor filedes into the buffer buf.buf represents the
pointer to the generic buffer. nbyte is the size of the
buffer.
#include<stdio.h>
#include <fcntl.h>
int main()
{
int fd, sz;
char *c = (char *) calloc(100, sizeof(char));

fd = open("foo.txt", O_RDONLY);
if (fd < 0) { perror("r1"); exit(1); }

sz = read(fd, c, 10);
printf("called read(% d, c, 10). returned th
" %d bytes were read.\n", fd, sz);
c[sz] = '\0';
printf("Those bytes are as follows: % s\n", c
}
Write() function
• Data is written to an open file with the write function.
#include <unistd.h>
ssize_t write(int filedes, const void *buf, size_t nbytes);
Returns: number of bytes written if OK,-1 on error
• write writes nbyte no. of bytes from the generic buffer
buf to the file descriptor filedes.
Process Control
The process control provided by the unix system
and includes
1.Creation of a process
2.Process execution
3.Process termination
Every process have a unique id(always unique)
and a non negative number
 Unique ID
 Reused
 Process ID
 Prsocess ID-0 is the scheduler process it is known
as swaper (scheduling)
 Process ID-1is the INIT process and it is invoked
by the kernel at end of the bootstrap procedure
 Process ID-2 is the pagedaemon, responsible for
paging of the virtual memory system..
Process Identifiers
Process Control System calls

 fork()
 vfork()
 Exit()
 Exec()
 Wait()
 Waitpid()
fork() system call
 To create a new process, you must use the fork()
system call.

#include<unistd>
Pid_t fork(void);

Return Values:
0—in child ,the process Id of child
1---Error
 The process is created by fork is called child
process.
 The function is called once but return Twice.

 Both Parent and Child process continue

Execution .
 fork() system call have no arguments

 It Returns process ID of the Child Process

 Different address space


Conditions
 fork()>0----parent process successfully
exe
 fork()<0----error

 fork()=0---child process successfully exe


main()
{
fork();
fork();
printf(“hello”);
}
fork () output
 The output print twice
 For child

4356 7 89
Pid num glob var

 For parent
4357 6 88
Pid num glob var
vfork() system call
 It is like a fork() system call,

 But the Sematics of two functions Differ


 Syntax: pid vfork(void);
Return value:
0---Child Process single parent
PID—process Identification number return by parent
-1----error
• Same address space for parent and child

• Parent process suspended , child process executed then after

resumes parent process


 vfork() function is intended to create a new process
like fork() without copying the address space of the
parent
 Parent and child have same address space
 Parent process suspended,
 child process executed, if terminate(exit()) resumes
the parent process
Vfork ()output
 The output
For parent
7654 6 88
Pid num glob var
For child
7654 7 89
Pid num glob var
exit() system call
 The exit() system call ends a process and returns a value to it
parent.

 The prototype for the exit() system call is:


void exit(status)
int status;

 where status is an integer between 0 and 255. This number is


returned to the parent via wait() as the exit status of the process.
 By convention, when a process exits with a status of zero that
means it didn't encounter any problems; when a process exit
with a non-zero status that means it did have problems.
1.Normal Termination
a)Return from main return 0;
b)Calling Exit() exit/remove resources user
c)Calling _Exit() or _exit()—kernel—relese/remove
d)Return of the last thread P—t1—t2—t3ter
2.Abnormal Termination
a)Calling abort abort();
b)Receipt of a signal ESC
c)Response of the last thread connection P—t1—t2—t3
exec() system calls
 exec() it create the new process/replace the process
to another process with same process ID, but content
is different
 When that file is made into a process, that
process replaces the process that executed the exec
system call -- a new process is not created.
 Unlike the other system calls and subroutines, a
successful exec system call does not return. Instead,
control is given to the executable binary file named
as the first argument
exec() system calls
 Letters added to the end of exec indicate the type of
arguments:
 l argn is specified as a list of arguments.
 v argv is specified as a vector (array of character
pointers).
 e environment is specified as an array of character
pointers.
 p user's PATH is searched for command, and
command can be a shell program
wait() system call
 You can control the execution of child processes by
calling wait() in the parent.
 wait() forces the parent to suspend execution until
the child is finished.
 wait() returns the process ID of a child process that
finished.
 If the child finishes before the parent gets around to
calling wait(), then when wait() is called by the
parent, it will return immediately with the child's
process ID.
wait() system call
 The prototype for the wait() system call is:
#include<sys/wait.h>
pid_t wait(int statloc);
Statloc---process infn(location)-terminatation status

 “status” is a pointer to an integer where the UNIX


system stores the value returned by the child process.
wait() returns the process ID of the process that ended.
Waitpid()
 Waitpid(): Waiting for a Specific Process
pid_t waitpid(pid_t pid, int &status,int options)
 1.suspends current process until specific process
terminates
 various options (that we won’t talk about)

pid_tpid---
1. pid===-1 wait for only child
2.pid>0 wait for child process ID equal to pid
3.pid==0 wait for any child process GID is equal to the
signal to calling process

You might also like