Unit 7 (Unix Command)
Unit 7 (Unix Command)
ls , who, whoami, date, wc, grep, cat, cd, vi,echo, man, cal, rm, mv, mkdir, cp, rmdir, gcc, history,
xcalc, pipe, tty
The information about this command can be found using man (manual pages) command on linux
terminal.
e.g to find information about coomand ls , type man ls on terminal as shown below.
It will display information about command ls
after quit (pree q) from manual pages we can directly write ls on terminal which
display result as shown below.
In this way students have to study and execute all the listed commands.
Assignment 2:
Fork system call use for creates a new process, which is called child process, which runs
concurrently with process (which process called system call fork) and this process is called parent
process. After a new child process created, both processes will execute the next instruction
following the fork() system call. A child process use same pc(program counter), same CPU
registers, same open files which use in parent process.
It take no parameters and return integer value. Below are different values returned by fork().
Syntax :
fork() creates a new process by duplicating the calling process, The new process, referred to as
child, is an exact duplicate of the calling process, referred to as parent, except for the following :
1. The child has its own unique process ID, and this PID does not match the ID of any existing
process group.
2. The child’s parent process ID is the same as the parent’s process ID.
3. The child does not inherit its parent’s memory locks and semaphore adjustments.
4. The child does not inherit outstanding asynchronous I/O operations from its parent nor does
it inherit any asynchronous I/O contexts from its parent.
Return value of fork()
On success, the PID of the child process is returned in the parent, and 0 is returned in the
child. On failure, -1 is returned in the parent, no child process is created, and errno is set
appropriately.
Please refer :
https://www.geeksforgeeks.org/fork-system-call/
http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html
The exec() family of functions replaces the current process image with a new process image. It
loads the program into the current process space and runs it from the entry point. It can be used to
run a C program by using another C program. It comes under the header file unistd.h. There are
many members in the exec family which are shown below with examples.
There is a family of exec() functions, all of which have slightly different characteristics:
int execl ( const char *path, const char *arg, ... );
int execlp( const char *file, const char *arg, ... );
int execle( const char *path, const char *arg, ..., char *const envp[] );
int execv ( const char *path, char *const argv[] );
int execvp( const char *file, char *const argv[] );
int execve( const char *file, char *const argv[], char *const envp[] );
execvp : Using this command, the created child process does not have to run the same
program as the parent process does. The exec type system calls allow a process to run any
program files, which include a binary executable or a shell script . Syntax:
int execvp (const char *file, char *const argv[]);
file: points to the file name associated with the file being executed.
argv: is a null terminated array of character pointers.
Let us see a small example to show how to use execvp() function in C. We will have two .C
files , EXEC.c and execDemo.c and we will replace the execDemo.c with EXEC.c by
calling execvp() function in execDemo.c .
//EXEC.c
#include<stdio.h>
#include<unistd.h>
int main()
{
int i;
return 0;
}
//execDemo.c
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main()
{
//A null terminated array of character
//pointers
char *args[]={"./EXEC",NULL};
execvp(args[0],args);
/*All statements are ignored after execvp() call as this whole process(execDemo.c) is replaced by
another process (EXEC.c)*/
printf("Ending-----");
return 0;
}
After running the executable file of execDemo.c by using command ./excDemo, we get the following
output:
I AM EXEC.c called by execv()
Syntax
#include <stdlib.h>
void abort(void);
Description
The abort() function causes abnormal process termination to occur, unless the signal SIGABRT is
being caught and the signal handler does not return. The abnormal termination processing includes
at least the effect of fclose() on all open streams, and message catalogue descriptors, and the default
actions defined for SIGABRT. The SIGABRT signal is sent to the calling process as if by means of
raise() with the argument SIGABRT.
The status made available to wait() or waitpid() by abort() will be that of a process terminated by
the SIGABRT signal. The abort() function will override blocking or ignoring the SIGABRT signal.
sigsuspend() temporarily replaces the signal mask of the calling process with the mask given by
mask and then suspends the process until delivery of a signal whose action is to invoke a signal
handler or to terminate a process.
If the signal terminates the process, then sigsuspend() does not return. If the signal is caught, then
sigsuspend() returns after the signal handler returns, and the signal mask is restored to the state
before the call to sigsuspend().
Assignment 3
Concept:
First in, first out (FIFO), also known as first come, first served (FCFS), is the simplest scheduling
algorithm. FCFS simply queues processes in the order that they arrive in the ready queue.
In this, the process that comes first will be executed first and next process starts only after the
previous gets fully executed.
Given n processes with their burst times, the task is to find average waiting time and average turn
around time using FCFS scheduling algorithm.
Here we are considering that arrival time for all processes is 0.
For the above given proccesses, first P1 will be provided with the CPU resources,
Turn Around Time: Time Difference between completion time and arrival time.
Turn Around Time = Completion Time – Arrival Time
Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Turn Around Time – Burst Time
Assignment 4
Concept: Shortest job first (SJF) or shortest job next, is a scheduling policy that selects the
waiting process with the smallest execution time to execute next. SJN is a non-preemptive
algorithm.
Shortest Job first has the advantage of having minimum average waiting time among all
scheduling algorithms.
It is a Greedy Algorithm.
It may cause starvation if shorter processes keep coming. This problem can be solved using
the concept of aging.
It is practically infeasible as Operating System may not know burst time and therefore may
not sort them. While it is not possible to predict execution time, several methods can be used
to estimate the execution time for a job, such as a weighted average of previous execution
times. SJF can be used in specialized environments where accurate estimates of running time
are available.
Algorithm:
1- Sort all the processes in increasing order according to burst time.
2- Then simply, apply FCFS.
Assignment 5
Concept:
Round robin scheduling is an algorithm mainly used by operating systems and applications that
serve multiple clients that request to use resources. It handles all requests in a circular first-in-first-
out (FIFO) order and eschews priority so that all processes/applications may be able to use the same
resources in the same amount of time and also have the same amount of waiting time each cycle;
hence it is also considered as cyclic executive.
It is one of the oldest, simplest, fairest and most widely used scheduling algorithms of all time,
partly because it is very easy to implement as there are no complicated timings or priorities to
consider, only a FIFO system and a fixed time constraint for each usage of the resource. This also
solves the problem of starvation, a problem in which a process is not able to use resources for a long
time because it always gets preempted by other processes thought to be more important.
Once a process is executed for a given time period, it is preempted and other process
executes for a given time period.
Assignment 6
Concept:
Priority scheduling is a method of scheduling processes based on priority. In this method, the
scheduler chooses the tasks to work as per the priority, which is different from other types of
scheduling, for example, a simple round robin.
Priority scheduling involves priority assignment to every process, and processes with higher
priorities are carried out first, whereas tasks with equal priorities are carried out on a first-come-
first-served (FCFS) or round robin basis. An example of a general-priority-scheduling algorithm is
the shortest-job-first (SJF) algorithm.
Non-preemptive algorithm:
It is one of the most common scheduling algorithms in batch systems. Each process is assigned a
priority. Process with the highest priority is to be executed first and so on.
Processes with the same priority are executed on first come first served basis. Priority can be
decided based on memory requirements, time requirements or any other resource requirement.
Algorithm :
First input the processes with their burst time and priority.
Sort the processes, burst time and priority according to the priority.
Now simply apply FCFS algorithm.
Assignment 7
Algorithm 2
Initially: p2_inside=false, p1_inside=false
Algorithm 3
Initially: p2_wants_to_enter, p1_wants_to_enter;
Assignment 8
Implementation of Mutual Exclusion using semaphore (wait & signal)
Assignment 9
Concept:
The banker’s algorithm is a resource allocation and deadlock avoidance algorithm that tests for
safety by simulating the allocation for predetermined maximum possible amounts of all resources,
then makes an “s-state” check to test for possible activities, before deciding whether allocation
should be allowed to continue.
It is a 1-d array of size ‘m’ indicating the number of available resources of each type.
Available[ j ] = k means there are ‘k’ instances of resource type Rj
Max :
It is a 2-d array of size ‘n*m’ that defines the maximum demand of each process in a
system.
Max[ i, j ] = k means process Pi may request at most ‘k’ instances of resource type Rj.
Allocation :
It is a 2-d array of size ‘n*m’ that defines the number of resources of each type currently
allocated to each process.
Allocation[ i, j ] = k means process Pi is currently allocated ‘k’ instances of resource type Rj
Need :
It is a 2-d array of size ‘n*m’ that indicates the remaining resource need of each process.
Need [ i, j ] = k means process Pi currently allocated ‘k’ instances of resource type Rj
Need [ i, j ] = Max [ i, j ] – Allocation [ i, j ]
Allocationi specifies the resources currently allocated to process Pi and Needi specifies the
additional resources that process Pi may still request to complete its task.
Banker’s algorithm consist of Safety algorithm and Resource request algorithm
Safety Algorithm
The algorithm for finding out whether or not a system is in a safe state can be described as follows:
1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively.
Initialize: Work = Available
Finish[i] = false; for i=1, 2, 3, 4….n
Resource-Request Algorithm
Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi wants k instances
of resource type Rj. When a request for resources is made by process Pi, the following actions are
taken:
1) If Requesti <= Needi
Goto step (2) ; otherwise, raise an error condition, since the process has exceeded its maximum
claim.
2) If Requesti <= Available Goto step (3); otherwise, Pi must wait, since the resources are not
available.
3) Have the system pretend to have allocated the requested resources to process Pi by modifying the
state as
follows:
Available = Available – Requesti
i = Allocationi + Requesti
Allocation
Needi = Needi– Requesti
Example:
Considering a system with five processes P0 through P4 and three resources types A, B, C.
Resource type A has 10 instances, B has 5 instances and type C has 7 instances. Suppose at
time t0 following snapshot of the system has been taken:
Question2. Is the system in safe state? If Yes, then what is the safe sequence?
Assignment 10
Implementation of RAG or WFG method for deadlock detection for single instance of resources
Concept:
Deadlock Detection
Assignment 11
Concept:
Paging
Page hit: If the file is already present, then it is a Page Hit (indicated by circles in the diagram)
This method uses the recent past as an approximation of near future. We replace the page which has
not been referenced for a long time in the past.
Here, when a page replacement is needed, it looks ahead in the input queue for the page frame
which will be referenced only after a long time. The page with the longest reference is swapped.
Assignment 12
Simulation of Memory Allocation Strategies (First Fit, Best Fit, Worst Fit)
Concept:
Memory allocation is the process of assigning blocks of memory on request. Typically the allocator
receives memory from the operating system in a small number of large blocks that it must divide up
to satisfy the requests for smaller blocks. It must also make any returned blocks available for reuse.
Techniques
First Fit
Best Fit
Worst Fit
In the first fit approach is to allocate the first free partition or hole large enough which can
accommodate the process. It finishes after finding the first suitable free partition.
Assignment 13
Write a note on
1. I/O Hardware : Draw PC bus structure ,Explain Polling , Interrupts and DMA
References
1. https://www.geeksforgeeks.org/fork-system-call/
2. http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html
3. https://www.geeksforgeeks.org/program-fcfs-scheduling-set-1/
4. https://www.geeksforgeeks.org/program-shortest-job-first-sjf-scheduling-set-1-non-
preemptive/
5. https://www.tutorialspoint.com/operating_system/os_process_scheduling_algorithms.htm
6. https://www.geeksforgeeks.org/operating-system-bankers-algorithm/
http://www.ques10.com/p/3773/what-is-paging-explain-lru-fifo-opt-page-replace-1/