Operating System
(CSN204)
Unit-1
Syllabus
Unit 1: Introduction to Operating System.
(8 L)
• Introduction:
• Components of a computer System,
• Operating system: User view & System view,
• Evolution of operating system,
• Single Processor & Multiprocessor systems,
• Real Time System,
• Distributed Systems,
• Multimedia Systems,
• Handheld Systems.
• Operating System Structure:
• Operating System Services,
• User Operating System Interfaces: Command- Line and GUI,
• System Calls.
2
Introduction to Operating Systems
• An Operating system is the resource manager i.e. it can manage the resource of a
computer system internally.
• The resources are processor, memory, files, and I/O devices. In simple terms, an
operating system is the interface between the user and the machine.
3
Introduction to Operating Systems
• Operating System functions-
• Process management which involves putting the tasks into order and
pairing them into manageable size before they go to the CPU.
• Memory management which coordinates data to and from RAM (random-
access memory) and determines the necessity for virtual memory.
• Device management which provides interface between connected devices.
• Storage management which directs permanent data storage.
• Accounting keep track of all the jobs running in foreground or background.
• Error handling keep track of all the errors generated by the system and
inform to the user for proper action.
4
Introduction to Operating Systems
EXAPMLE OF OPERATING SYSTEM
1. Microsoft Disk operating system (MS-DOS)
2. Windows ( 98, 2000, XP, vista, Window 7/8/10)
3. Linux
4. Unix
5
Linux Commands
1. List files and directories – ls , ls –l
2. Check present working directory – pwd
3. Create directory – mkdir <name of directory) e.g mkdir dit
4. Change directory – cd dit
5. Create file – cat>name of file , e.g. cat>test
6. Display file – cat test
7. Copy file – cp file1 file2 , e.g cp test test1
8. Remove file – rm file
9. Compile c program file
gcc file.c
10. Execute file - ./a.out
6
Introduction to Operating Systems
Classification of Operating System
Based on number of task handled Based on user interface
concurrently Based on number of users
Single User: Operates a Command Line: Use the
Single Task: One task is user at a given time
operated at a any given time commands to activate any
Multi User: More than programmes e.g. MS-DOS
Multi-Task: More than one one user can operate a
task is executed Graphical User Interface:
given computer at any The user interacts with the
simultaneously time computer using icons and
menus and select them
using pointer
7
Introduction to Operating Systems
Objective of Operating System
• Convenience: Convenient to use.
• Efficiency: Use resources efficiently.
8
Two Views of Operating System
Two Views of Operating System
Operating System View:
User’s View: As a resource allocator also.
It refers to the interface being used. A computer system consists of many resources
The operating system is designed mostly for like - hardware and software - that must be
ease of use, with some attention paid to managed efficiently.
performance, and none paid to resource Acts as the manager of the resources, decides
utilization. between conflicting requests, controls
execution of programs etc.
9
Evolution of Operating Systems
Stages include:
Time
Sharing
Multiprogramm
Systems
ed Batch
Simple Systems
Batch
Serial Systems
Processin
g
10
Modes of Operation
User Mode Kernel Mode
• user program executes in user • monitor executes in kernel
mode mode
• certain areas of memory are • privileged instructions may be
protected from user access executed
• certain instructions may not • protected areas of memory
be executed may be accessed
11
Transition from User to Kernel
Mode
Example-
Access a file and write something into the file.
Uni-programming
• The processor spends a certain amount of time executing, until it reaches an
I/O instruction; it must then wait until that I/O instruction concludes before
proceeding.
13
Multiprogramming
• There must be enough memory to hold the OS (resident monitor) and one user program
• When one job needs to wait for I/O, the processor can switch to the other job, which is
likely not waiting for I/O
14
Multiprogramming
• Multiprogramming
• also known as multitasking
• memory is expanded to hold three, four,
or more programs and switch among all
of them
15
Time-Sharing Systems
• Can be used to handle multiple interactive jobs
• Processor time is shared among multiple users
• Multiple users simultaneously access the system through terminals, with the OS
interleaving the execution of each user program in a short burst or quantum of
computation
16
Batch
Multiprogramming
vs. Time Sharing
17
Multiprocessor System
• have two or more central processing units (CPUs) share full access to a
common RAM.
• Objective is to boost the system’s execution speed, with other objectives being
fault tolerance and application matching.
• Two types of multiprocessors are:
• shared memory multiprocessor - all the CPUs shares the common
memory
• distributed memory multiprocessor - every CPU has its own private
memory
• Benefits:
• Enhanced performance.
• Multiple applications.
• Multi-tasking inside an application.
• High throughput and responsiveness.
18
• Hardware sharing among CPUs.
Distributed Operating Systems
• Various autonomous interconnected computers
communicate each other using a shared communication
network.
• Independent systems possess their own memory unit and
CPU.
• also known as loosely coupled systems
• These systems processors may differ in sizes and
functions.
• The major benefit
• it is always possible that one user can access the files or
software which are not actually present on his system but on
some other system connected within this network i.e., remote
access is enabled within the devices connected in that network.
19
Distributed Operating Systems
• Advantages of Distributed Operating System:
• Failure of one will not affect the other network communication, as all systems are independent
from each other
• Since resources are being shared, computation is highly fast and durable
• Load on host computer reduces
• These systems are easily scalable as many systems can be easily added to the network
• Disadvantages of Distributed Operating System:
• Failure of the main network will stop the entire communication
• To establish distributed systems the language which are used are not well defined yet
• These types of systems are not readily available as they are very expensive.
• Examples of Distributed Operating System are- LOCUS etc.
20
Real Time System
• Used in environments where a large number of events, mostly external to the computer system, must
be accepted and processed within certain deadlines.
• Like in industrial control, telephone switching equipment, flight control, and real time simulations.
• The real time operating systems can be of 2 types –
• Hard Real Time operating system:
• guarantee that critical tasks be completed within a range of time.
• For example, a robot is hired to weld a car body, if robot welds too early or too late, the car
cannot be sold, so it is a hard real time system that require to complete car welding by robot
hardly on the time.
• Soft real time operating system:
• provides some relaxation in time limit.
• For example – Multimedia systems, digital audio system etc.
21
Multimedia System
• In multimedia applications, a lot of data manipulation (e.g. A/D, D/A and format conversion) is required
and this involves a lot of data transfer, which consumes many resources.
• The integration of discrete and continuous multimedia data demands additional services from many
operating system components.
• The major aspect in this context is real-time processing of continuous media data.
• Issues concerned:
• Process management: a brief presentation of traditional real-time scheduling algorithms.
• File systems: outlines disk access algorithms, data placement and structuring
• Interprocess communication and synchronization.
22
Handheld System
• It is a small portable devices that can be carried along and are capable of performing normal
operations.
• They are usually battery powered.
• Examples include Personal Digital Assistants (PDAs), mobile phones, palm-top computers,
pocket-PCs etc.
• Weights and sizes have certain limitations as a result they are equipped with small memories,
slow processors and small display screens, etc.
• The physical memory capacity is very less (512 KB to 128 MB) hence the operating systems
of these devices must manage the memory efficiently.
• As the processors are slower due to battery problem, the operating system should not burden.
23
A View of Operating System Services
24
System Calls
• It is a way to perform the task of user mode from kernel mode.
• It gives you access the functionalities of kernel mode of OS.
• It is the programmatic way in which a computer program requests a service from the kernel of
the operating system it is executed on.
• A computer program makes a system call when it makes a request to the operating system’s
kernel.
• It provides an interface between a process and operating system to allow user-level processes to
request services of the operating system.
• System calls are the only entry points into the kernel system.
• All programs needing resources must use system calls.
• Services Provided by System Calls :
• Process creation and management (example –fork(), getpid)
• Main memory management (example - read(), write())
• File Access, Directory and File system management (example - open(), read(), write())
• Device handling(I/O) (example – read, write)
25
System Calls
• read() - The use of read() system call is to read from a file descriptor.
Syntax -
ssize_t read(int fd, const void *buf, size_t count);
Default descriptor value -0 (read from keyboard)
Example-
#include<unistd.h> // required with Linux OS
int main()
{
char buff[20];
read(0,buff,10); //read 10 bytes from standard input device(keyboard), store in buffer (buff)
buff[10]=‘\0’;
printf(“%s”, buff);
}
26
System Calls
• write() - The use of write() system call is to write to a file descriptor.
Syntax -
ssize_t read(int fd, const void *buf, size_t count);
Default descriptor value -1 (write to monitor)
Example-
#include<unistd.h>
int main()
{
write(1,"hello\n",6);
//1 is the file descriptor, "hello\n" is the data, 6 is the count of characters in data
}
27
System Calls
• open() – Used to open or create a file and get a file descriptor for reading and writing.
Syntax –
Int filedescriptor=open(“filename”, modeflag)
Modes-
Flags Description
O_RDONLY Opens the file in read-only mode.
O_WRONLY Opens the file in write-only mode.
O_RDWR Opens the file in read and write mode.
Create a file if it doesn’t exist.
O_CREAT
Opens the file and places the cursor at the end of the
O_ APPEND contents.
28
System Calls
Example
#include <fcntl.h> // Required with windows OS
int main()
{
// if file is not created, then file test.txt is created.
int fd = open(“test.txt", O_RDONLY | O_CREAT);
printf("fd = %d\n", fd);
if (fd == -1)
printf(“Error”);
else
printf(“Success”);
return 0;
}
29
System Calls
Write a program using open() system call to read 10 characters from a file test.txt and
write on monitor.
#include<stdio.h>
#include<unistd.h>
#include <stdlib.h>
int main()
{
int fd;
char b[50];
fd=open(“test1.txt", O_RDONLY);
read(fd,b,10);
write(1,b,10);
}
30
System Calls
Write a program using open() system call to write 10 chars into a file. Create file if not
created.
#include<stdio.h>
#include<unistd.h>
#include <stdlib.h>
int main()
{
int fd;
Char buff[]=“I am testing”;
fd=open(“test.txt",O_WRONLY|O_CREAT, 0642);
write(fd,buff,10);
}
31
System Calls
Write a program using open() system call to create a file “test1.txt”. Write some text
into the file and read 10 chars from start.
#include<stdio.h>
#include<unistd.h>
#include <stdlib.h>
int main()
{
int n,fd;
char a[]=“I am new to system calls”;
char b[50]’;
fd=open(“test1.txt", O_CREAT|O_RDWR);
write(fd,a,sizeof(a));
lseek(fd,0,SEEK_SET);
read(fd,b,10);
printf(“%s”,b);
} 32
Example of System Calls
• System call sequence to copy the contents of one file to another file
System Calls
• Fork() - The Fork system call is used for creating a new process in Linux, and Unix systems, which is called
the child process, which runs concurrently with the process that makes the fork() call (parent process). After a
new child process is created, both processes will execute the next instruction following the fork() system call.
Return value – 0 (child), +ive value (parent), -ive (fork unsuccessful)
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
// make two process which run same
// program after this instruction
pid_t p = fork();
if(p<0){
perror("fork fail");
exit(1);
}
printf("Hello world!, process_id(pid) = %d \n",getpid());
return 0;
}
System Calls
• Calculate how many times test will be printed
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
fork();
fork();
fork();
printf(“test\n");
return 0;
}
2^n
System Calls
• Predict Output
void forkexample()
{
pid_t p;
p = fork();
printf(“hello hi !”);
if ( p == 0)
printf("Hello from Child!\n");
else
printf("Hello from Parent!\n");
}
int main()
{
forkexample();
return 0;
}
System Calls
• Predict Output
void forkexample()
{
int x = 1;
pid_t p = fork();
if (p == 0)
printf("Child has x = %d\n", ++x);
else
printf("Parent has x = %d\n", --x);
}
int main()
{
forkexample();
return 0;
}
Process States
38