Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
8 views

Unit 7 (Unix Command)

The document outlines various assignments for a Computer Science & Engineering course at Walchand Institute of Technology, focusing on Unix commands, system calls, scheduling algorithms, and deadlock avoidance. It includes practical exercises on commands like fork(), exec(), and scheduling algorithms such as FCFS, SJF, round robin, and priority scheduling. Additionally, it discusses the implementation of mutual exclusion and the Banker's algorithm for deadlock avoidance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Unit 7 (Unix Command)

The document outlines various assignments for a Computer Science & Engineering course at Walchand Institute of Technology, focusing on Unix commands, system calls, scheduling algorithms, and deadlock avoidance. It includes practical exercises on commands like fork(), exec(), and scheduling algorithms such as FCFS, SJF, round robin, and priority scheduling. Additionally, it discusses the implementation of mutual exclusion and the Banker's algorithm for deadlock avoidance.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Subject : Operating System Concepts


Class: TE CSE (Shift) Year 2018-19
Lab Handouts
Assignment 1 : Study of Unix commands

List of Unix commands:

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.

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

In this way students have to study and execute all the listed commands.

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Assignment 2:

Program using system calls: fork (), exec(), suspend() , abort()

1. fork() System Call :

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 :

integer variable = fork();

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

2. exec System Call :

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

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

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.

The exec Family of Functions

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;

printf("I am EXEC.c called by execvp() ");


printf("\n");

return 0;
}

Now,create an executable file of EXEC.c using command


gcc EXEC.c -o EXEC

//execDemo.c

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

#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;
}

gcc execDemo.c -o execDemo

After running the executable file of execDemo.c by using command ./excDemo, we get the following
output:
I AM EXEC.c called by execv()

3. abort() System Call


abort - generate an abnormal process abort

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.

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Return Value: The abort() function does not return.

Errors: No errors are defined.


4. 3. sigsuspend() System Call

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().

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Assignment 3

Implementation of FCFS scheduling algorithm

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.

How to compute below times in Round Robin using a program?

1. Completion Time: Time at which process completes its execution.


2. Waiting Time(W.T): Time Difference between turn around time and burst time.
Waiting Time = Time when Process is taken for Completion- Arrival Time
3. Turn Around Time: Total time require by the process for its complete execution
Turn Around Time = Waiting time + Burst Time
Consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in the
same order, with Arrival Time 0, and given Burst Time, let's find the average waiting time
using the FCFS scheduling algorithm.

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

For the above given proccesses, first P1 will be provided with the CPU resources,

Hence, waiting time for P1 will be 0


P1 requires 21 ms for completion, hence waiting time for P2 will be 21 ms
Similarly, waiting time for process P3 will be execution time of P1 + execution time for P2,
which will be (21 + 3) ms = 24 ms.
For process P4 it will be = 30 ms.

Completion Time: Time at which process completes its execution.

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

Average Waiting Time = Addition of all Waiting Time/ No. Of Processes

Average Turn Around Time = addition of all T.A.T / No. Of Processes

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Assignment 4

Implementation of SJF (non preemptive)

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.

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Assignment 5

Implementation of round robin (RR).

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.

Round Robin is the preemptive process scheduling algorithm.

Each process is provided a fix time to execute, it is called a quantum.

Once a process is executed for a given time period, it is preempted and other process
executes for a given time period.

Context switching is used to save states of preempted processes.

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Assignment 6

Implementation of priority scheduling algorithm

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.

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Assignment 7

Implementation of Mutual Exclusion 1st / 2nd / 3rd algorithm.


Algorithm 1:

Algorithm 2
Initially: p2_inside=false, p1_inside=false

Algorithm 3
Initially: p2_wants_to_enter, p1_wants_to_enter;

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Assignment 8
Implementation of Mutual Exclusion using semaphore (wait & signal)

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Assignment 9

Implement Bankers Algorithm for Deadlock Avoidance

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.

Following Data structures are used to implement the Banker’s Algorithm:


Let ‘n’ be the number of processes in the system and ‘m’ be the number of resources types.
Available :

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

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

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

2) Find an i such that both


a) Finish[i] = false
b) Needi <= Work if no such i exists goto step (4)

3) Work = Work + Allocation


Finish[i] = true
goto step (2)

4) if finish [i] = true for all i


then the system is in a safe state

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:

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

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:

Question 1: What will be the content of the Need matrix?

Need [i, j] = Max [i, j] – Allocation [i, j]


So, the content of Need Matrix is:

Question2. Is the system in safe state? If Yes, then what is the safe sequence?

Applying the Safety algorithm on the given system,

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Assignment 10

Implementation of RAG or WFG method for deadlock detection for single instance of resources

Concept:

Deadlock Detection

Single Instance of Each Resource Type

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Assignment 11

Simulation of page replacement strategies (FIFO, LRU , Optimal)

Concept:

Paging

Paging is a memory-management scheme which allows the physical address of a process to


be non-contiguous.
The concept of paging is used to remove the problem of fragmentation. Here we are able to
allocate physical memory to the process in a non-contiguous manner wherever memory is
available.
Paging avoids external fragmentation and the need for compaction.
Paging is done by breaking the physical memory into fixed size blocks called frames and
breaking the logical memory into blocks of same size called pages.
When a process is being executed, the corresponding pages are fetched and loaded into the
available memory frames.
First In Firtst Out page replacement:
In FIFO page replacement, when a page is needed to be replaced, we select the oldest page.

Page hit: If the file is already present, then it is a Page Hit (indicated by circles in the diagram)

Page Miss: If an entry is not found, then it is a Page miss


No. of page hit=4
No. of page miss=9
LRU page replacement:

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

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.

No. of page hit=4


No. of page miss=9
Optimal Page replacement:

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.

No. of page hit=6


No. of page miss=7

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

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.

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

Assignment 13

Study of I/O System

Write a note on

1. I/O Hardware : Draw PC bus structure ,Explain Polling , Interrupts and DMA

2. Character and Block devices

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept


Walchand Institute of Technology, Solapur Computer Science & Engineering Department

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/

Mrs. Komal R. Pardeshi Assistant Professor CSE Dept

You might also like