Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

OS Basics

• An operating system is a program that manages the computer hardware. It also


provides a basis for application programs and acts as an intermediary between
the computer user and the computer hardware.
• Operating System (or OS) primarily provides services for running applications on a
computer system.
• A computer system can be divided roughly into four components: the hardware,
the operating system, the application programs, and the users.
Components of Operating System
• Process management
• A process is only one instant of a program in execution. There are many processes can be
running the same program. The major activities of an operating system in regard to process
management are
• Creation and deletion of user and system processes.
• Suspension and resumption of processes.
• A mechanism for process synchronization.
• A mechanism for process communication.
• A mechanism for deadlock handling.
• Main Memory management
• Main-memory provides storage that can be access directly by the CPU. That is to say for a
program to be executed, it must in the main memory. The major activities of an operating in
regard to memory-management are:
• Keep track of which part of memory are currently being used and by whom.
• Decide which processes are loaded into memory when memory space becomes available.
• Allocate and deallocate memory space as needed.
Components of Operating System
• Similarly few other services
• I/O management
• File management
• Network management
• Etc…
Multiprocessing

• Multiple applications running simultaneously or concurrently are called as multiprocessing.

• Benefit of multiprocessing is execution of one application is not dependent on other application


execution. Linux, windows, android and almost all general purpose OS are multiprocessing
operating systems, whereas DOS is single processing system.

• Multiprocessing can be achieved in two ways:

• Hardware multiprocessing: simply known as multiprocessing

• Software multiprocessing: simply known as multiprogramming

• In hardware multiprocessing systems, two or more processors are in close communication and
each CPU executes separate application. For example, dual core processors, quad core processors.
They share computer bus and sometimes clock, memory and peripherals.
• In software multiprocessing, all applications have to share single CPU. If one
process is waiting for any instance to happen then CPU is made engage with
other process. This is generally achieved based on time slice.
SHELL
• Shell is a user program or its environment provided for user
interaction.
• Shell is a command language interpreter that executes
commands read from the standard input device (keyboard) or
from a file.
• Shell is not part of system kernel, but uses the system kernel
to execute programs, create files etc
• When you executed command, e.g. ./a.out , then a process is
created, but who created this process?
• Your command is interpreted by the shell and passed to
kernel ,so that kernel can execute the process. The parent of
a.out process is shell.
• To determine which shell you are using you can give command,
• $ echo $SHELL
/bin/bash
• Bash is most common shell used nowadays. It is freeware shell.
• Commands in Shell are of 2 categories
• Internal commands(part of shell)
• External commands(executables)
• Shell is parent of the new job executed on shell.
• Process receives various properties from its parent.
• Command line argument and environment is inherited from parent.
Process ID and Parent Process ID

• Each process has a process ID (PID), a positive integer that uniquely identifies the process
on the system.
• pid_t getpid(void); //Always successfully returns process ID of caller
• The pid_t data type used for the return value of getpid() is an integer type specified for
the purpose of storing process IDs. With the exception of a few system processes such as
init (process ID 1), there is no fixed relationship between a program and the process ID of
the process that is created to run that program.
• The Linux kernel limits process IDs to being less than or equal to 32,767 defined by
PID_MAX.
• When a new process is created, it is assigned the next sequentially available process ID.
Each time the limit of 32,767 is reached, the kernel resets its process ID counter so that
process IDs are assigned starting from low integer values (not less than 300 because
those are reserved for system process and daemons). In Linux the PID_MAX is available
in the file /proc/sys/kernel/pid_max.
• Each process has a parent—the process that created it. A process can find out the process ID of its
parent using the getppid() system call.
• #include <unistd.h>
• pid_t getppid(void); //Always successfully returns process ID of parent of caller
• In effect, the parent process ID attribute of each process represents the tree-like relationship of
all processes on the system. The parent of each process has its own parent, and so on, going all
the way back to process 1, init, the ancestor of all processes. The parent of any process can be
found by looking at the PPID field.
• You can use pstree command to see the running process as tree.
#include<stdio.h>
#include<unistd.h>
int main()
{
printf("pid %d \n", getpid()); //prints PID of the process
printf("ppid %d \n", getppid()); //prints PPID of the process
return 0;
}
States of a Process
• In software multiprocessing when one process is
getting executed in CPU, other processes remain in the
memory in some state, for eg, ready, wait, suspend,
delayed etc. The process being executed by CPU is said
to be in running state.
• The state of a process is defined in part by the current
activity of that process. Each process may be in one of
the following states,
• Running state: process executed by CPU
• Ready state: process ready for execution but not being
executed by CPU
• Delayed state: process intentionally goes to delay (use
of sleep())
• Wait state or Blocked state: process waiting for an
external event to finish (use of scanf())
• Suspend state: process is suspended by some signal
• Consider following code executing as a process.
#include<stdio.h>
main(){ int i;
printf("Hi...\n");
scanf("%d",&i);
printf("i=%d\n",i); }
• When you execute the program as ./a.out, process is created. It goes into ready state.
• From ready state it goes to running state, CPU starts executing it and you get Hi printed on screen.
• Then scanf() function need input from keyboard, so process enters I/O wait state.
• When user enters value, I/O request completes and process goes again to ready state. Meanwhile
CPU is engaged in execution another process.
• From ready state this process goes to running state and prints i value.
• Here process execution completes and it terminates.
• Note that certain rules apply here.

• Processes entering the system must initially go into the ready state.

• A process can only enter the running state from the ready state.

• A process can normally only leave the system from the running state, although a process in the
ready or blocked state may be aborted by the system (in the event of an error, for example), or by
the user.

• Process can be in suspended or delayed state.

• When a process is suspended, it essentially becomes dormant until resumed by the system or by
a user.
• You can predict the probable states of process executing following code,
#include<stdio.h>
main()
{
int i;
printf("Hi...\n");
sleep (10);
printf("Bye...\n");
}
• The state diagram of above process can be,

You might also like