Professional Documents
Culture Documents
Lab Manual
Lab Manual
Lab Manual
Objective:
Basic Commands:
Directory Commands
Command Description
pwd Shows the name and location of the directory where you are
curretly working.
ls Gives you a short list of the files in the directory where you
are currently working.
cd Moves you to another directory.
mkdir Creates a new subdirectory inside of the directory where
you are currently working
Eg:
cp sourcefile destfile
To copy a file to a different directory specify the directory
instead of filename.
Eg:
cp newfile testdir
To copy a file to a different directory and create a new file name, you need to
specify a directory/a new file name.
Eg:
cp newfile testdir/newerfile
cp newfile ../newerfile
The .. represents one directory up in the hierarchy.
Use the table above to define the settings for the three "users." In the command, the first number
refers to your permissions, the second refers to group, and the third refers to general users.
Typing the command: chmod 751 filename
gives you full access, the group read and execute, and all others execute only permission
.
mv Type mv followed by the current name of a file and the new
name of the file.
Eg:
mv oldfile newfile
Type mv followed by the name of a file and the new directory where you'd like to place the file
Eg:
mv newfile testdir
This moves the file named newfile to an existing directory named testdir. Be certain you‟re
specifying a directory name or the mv command alters the name of the file instead of moving it.
Eg:
rm n* This command removes all files beginning with n.
Type rm -i followed by a filename if you‟d like to be
prompted before the file is actually removed.
Eg:
rm -i newfile
rm -i n*
Note: The -print option will print out the path of any file
that is found with that name. In general -print will print out
the path of any file that meets the find criteria.
date print or set the system date and time to set date and time
date -s "11/20/2003 12:48:00" - Set the date to the date and time shown.
DATE: 02/08/01
TIME:16:44:55
bc Calculator
bc [-c] [-l] [file]
-c Compile only. The output is dc commands that are sent to
the standard output.
-l Define the math functions and initialize scale to 20,
instead of the default zero.
file Name of the file that contains the bc commands to be
calculated this is not a necessary command.
Within the cal.txt file you could have a simple statement such as:
/* Add the value 1+2 /*
1+2
quit
bc cal.txt
When running the above command you will receive the results of the cal.txt
file. Which in this case would be 3.
echo Echo's to the screen what you type after echo. Echo is useful for producing
diagnostics in command files, for sending known data into a pipe, and for
displaying the contents of environment variables.
Filter commands
Prints all lines of file that contain the pattern, regardless of case.
grep -i pattern file For example, grep -i cg data prints any line of data that contains
"cg," "Cg," "cG," or "CG." The -i option ignores the case of the
string when searching.
grep -v pattern file Prints all lines of the file except those that contain the pattern.
Name=`who am i | cut -f1 -d' '` - set name to current login name.
paste Merge corresponding or subsequent lines of files.
The below example would take the input from ls and paste that
input into four columns.
ls | paste - - - -
tr Translate characters.
tr [-c] [-d] [-s] [string1] [string2]
-c Complement the set of characters specified by
string1.
-d Delete all occurrences of input characters that are
specified by string1.
-s Replace instances of repeated characters with a
single character.
string1 First string or character to be changed.
string2 Second string or character to change the string1.
EX:
Echo "12345678 9247" | tr 123456789 computerh - this
example takes an echo response of '12345678 9247' and pipes it
through the tr replacing the appropriate numbers with the letters.
In this example it would return computer hope.
Shell Commands
command Description
Pipe symbol (|). Pipes are a UNIX feature which allows you to connect several
commands together in one line and pass data from one to the next
much like a chain.
pipe connects one commands output to the next commands input
directs UNIX to connect stdout from the first command to the stdin
of the second command
line_count=`wc -l $filename | cut -c1-8`
the wc -l command counts the number of lines in the filename
contained in the variable $filename. This text string is then piped to
the cut command which snips off the first 8 characters and passes
them on to stdout, hence setting the variable line_count.
upper_case=`echo $lower_case | tr '[a-z]' '[A-Z]'`
Exercises:
On the spot problems will be given during the lab sessions to make the students to analyze the problem and
find out the suitable method to solve the given problem.
EX.NO.2 PROGRAMS USING THE FOLLOWING SYSTEM CALLS OF UNIX
OPERATING SYSTEM FORK, EXEC, GETPID, EXIT, WAIT, CLOSE, STAT,
OPENDIR, READDIR
Objective:
To implement programs using the following system calls of UNIX fork, exec, getpid, exit, wait, close, stat, opendir,
readdir.
Commands:
fork:
System call fork() is used to create processes. It takes no arguments and returns a process ID. The purpose
of fork() is to create a new process, which becomes the child process of the caller. After a new child process is
created, both processes will execute the next instruction following the fork() system call. Therefore, we have to
distinguish the parent from the child. This can be done by testing the returned value of fork():
If fork() returns a negative value, the creation of a child process was unsuccessful.
fork() returns a positive value, the process ID of the child process, to the parent. The returned process ID is of type
pid_t defined in sys/types.h. Normally, the process ID is an integer. Moreover, a process can use function getpid()
to retrieve the process ID assigned to this process.
After the system call to fork(), a simple test can tell which process is the child.
Note: Unix will make an exact copy of the parent's address space and give it to the child. The parent and child
processes have separate address spaces.
exec:
exec is a BOURNE and POSIX shell command that replaces the current shell process with the command specified
after exec. This command does not create a new PID. For example, if you were to run exec <command>, the shell
would be replaced by that command. When that command is exited, the shell will exit.
getpid:
exit
The exit command terminates a script, just as in a C program. It can also return a value, which is available to the
script's parent process.
wait:
wait(): on success, returns the process ID of the terminated child; on error, -1 is returned.
waitpid(): on success, returns the process ID of the child whose state has changed; on error, -1 is returned; if
WNOHANG was specified and no child(ren) specified by pid has yet changed state, then 0 is returned.
waitid(): returns 0 on success or if WNOHANG was specified and no child(ren) specified by id has yet changed
state; on error, -1 is returned.
Each of these calls sets errno to an appropriate value in the case of an error.
close:
STAT:
Opendir:
opendir() function opens a directory stream corresponding to the directory name, and returns a pointer to the
directory stream. The stream is positioned at the first entry in the directory
fdopendir() function is like opendir(), but returns a directory stream for the directory referred to by
the open file descriptor fd. After a successful call to fdopendir(), fd is used internally by the implementation, and
should not otherwise be used by the application.
opendir() and fdopendir() functions return a pointer to the directory stream. On error, NULL is returned,
and errno is set appropriately.
readdir:
readdir - read directory entry
Exercises:
On the spot problems will be given during the lab sessions to make the students to analyze the problem and
find out the suitable method to solve the given problem.
EX.NO.3 C PROGRAMS TO SIMULATE UNIX COMMANDS LIKE CP, LS, GREP.
Objective:
Commands:
cp Type cp followed by the name of an existing file and the name of the new file.
Eg:
cp sourcefile destfile
To copy a file to a different directory specify the directory
instead of filename.
Eg:
cp newfile testdir
To copy a file to a different directory and create a new file name, you need to
specify a directory/a new file name.
Eg:
cp newfile testdir/newerfile
cp newfile ../newerfile
The .. represents one directory up in the hierarchy.
ls Gives you a short list of the files in the directory where you
are currently working.
Grep Finds text within a file.
Eg:
grep "unix" *.htm
search all .htm files in the current directory for any reference of
unix and give results similar to the below example text
Prints all lines of file that contain the pattern, regardless of case.
grep -i pattern file For example, grep -i cg data prints any line of data that contains
"cg," "Cg," "cG," or "CG." The -i option ignores the case of the
string when searching.
grep -v pattern file Prints all lines of the file except those that contain the pattern.
Type cd into the PuTTY terminal, followed by the directory address that holds the C or C++ source code
you wish to compile.
Type ls if you are using PuTTY to log into a Unix-based machine, or dir if you are using PuTTY to log into
a Windows machine. This lists the files in the current directory. Ensure the source code file you wish to
compile is present.
Type gcc -o MyExecutable MySourceCode where "MyExecutable" is the name you want your compiled
executable to bear and "MySourceCode" is the name of the source code you want to compile. It will have
the C or CPP file extension, depending on whether it's a C language source file or a C++ language source
file.
Exercises:
On the spot problems will be given during the lab sessions to make the students to analyze the problem and
find out the suitable method to solve the given problem.
Objective:
To implement Shell Programming.
Concepts Involved:
Shell programming is a group of commands grouped together under single filename. The shell interprets
the input, takes appropriate action, and finally displays the output. Shell scripts are dynamically
interpreted, not compiled.
Types of shell:
Bourne shell sh
C shell csh
Korne Shell ksh
1. creation
$ cat > greet
echo “please enter your name:”
read name
echo “hi! Welcome to this session $name”
Ctrl + D
2. Execution
$ sh greet
please enter your name: jaya
hi! Welcome to this session jaya”
area
a1
account
a_count
Operators:
Logical operators
-a and
-o or
! not
Relational operators
-eq : check fro equality of integers
-ne : check for inequality
!= not equal to
Logical operators
-a and
-o or
-! Not
Read command
Used to read the value of the shell variable from a user.
Comment statement
# this is a text program.
3.while..do..done
4.until…do..done
5.case …esac
1) if construct
useful for executing a set of commands based on the condition being true and alternate set of
commands to be executed if the condition is false. Ex. if (grep India countri.dat)
then
echo “pattern found”
else
echo “pattern not found”
fi
2) for construct
Commands
done
Ex. for k in 1 2 3 4 5
do
echo “the number is $k”
3)while construct
Commandlist
done
done
o/p. 1 2 3
5) case construct:
case value in
choice1) commands;;
choice2)commands;;
….
esac
Ex. $echo “enter a value”
read myval
case “$myval” in
1) echo zero;;
2) echo one;;
3) echo two;;
4) echo three;;
*) echo “invalid argument”;;
esac
Exercises:
On the spot problems will be given during the lab sessions to make the students to analyze the problem and
find out the suitable method to solve the given problem
Objective:
CPU scheduler will decide which process should be given the CPU for its execution. For this
it use different algorithm to choose among the process. One among that algorithm is FCFS algorithm.
In this algorithm the process which arrives first is given the CPU after finishing its request only it will allow CPU
to execute other process.
ALGORITHM:
CPU scheduler will decide which process should be given the CPU for its execution .For this it use different
algorithm to choose among the process. One among that algorithm is SJF algorithm. In this algorithm the process
which has less service time given the CPU after finishing its request only it will allow CPU to execute next other
process.
ALGORITHM:
ROUND ROBIN :
CPU scheduler will decide which process should be given the CPU for its execution .For this it use different
algorithm to choose among the process .one among that algorithm is Round robin algorithm.
In this algorithm we are assigning some time slice .The process is allocated according to the time slice ,if the
process service time is less than the time slice then process itself will release the CPU voluntarily .The scheduler
will then proceed to the next process in the ready queue .If the CPU burst of the currently running process is longer
than time quantum ,the timer will go off and will cause an interrupt to the operating system .A context switch will
be executed and the process will be put at the tail of the ready queue.
ALGORITHM:
PRIORITY SCHEDULING
CPU scheduler will decide which process should be given the CPU for its execution. For this it use different
algorithm to choose among the process. One among that algorithm is Priority Scheduling algorithm. In this
algorithm the process which has highest priority executed first. After finishing its request only it will allow CPU to
execute next process which has next priority level.
ALGORITHM:
Exercises:
On the spot problems will be given during the lab sessions to make the students to analyze the problem and
find out the suitable method to solve the given problem
EX.NO.6 SEMAPHORES
Objective:
Concepts Involved:
Definition: Semaphore
Principle
• Two or more processes can cooperate by means of simple signals, such that processes is forced to stop at a
specified place unit it has received a specific signal, For signaling special variables used called semaphore.
• To transmit a signal by semaphores, a process is to execute the primitive signal(s). To receive a signal by
semaphores, a process executes the primitives wait(s).
• If the corresponding signal has not yet been transmitted; the process is suspended until the transmission takes
place.
Operations of Semaphore:
Disadvantages:
• We must keep the value of S=1
• Busy waiting which consumes many CPU cycles.
Wait : decrement the value of its argument S as soon as it would become non-negative.
Pseudocode
wait(S) {
while (S <= 0)
// no-op( No Operation)
s--;
}
Signal : increment the value of its argument, S as an individual operation.
Singnal(s){
S++;}
Note:
• Modifications to the integer value of the semaphore in the wait and signal operations must be executed indivisibly.
That is, when one process modifies the semaphore value, no other process can simultaneously modify that same
semaphore value.
• A semaphore is a programming concept that is frequently used to solve multi-threading problems.
Producer–Consumer problem
Producer–Consumer problem (also known as the bounded-buffer problem) is a classic example of a multi-process
synchronization problem. The problem describes two processes, the producer and the consumer, who share a
common, fixed-size buffer used as a queue.
ALGORITHM
ii) If there is empty space in the buffer check the mutex value for enter into the critical
section.
iii) If the mutex value is 0, allow the producer to add value in the temporary variable to the
buffer.
ii) If there is any item in the buffer check for mutex value, if the mutex==0, remove item
from buffer
iii) Signal the mutex value and reduce the empty value by 1.
Objective:
Inter process communication (IPC) is a mechanism which allows processes to communicate each other and
synchronize their actions. The communication between these processes can be seen as a method of co-operation
between them. Processes can communicate with each other using these two ways:
Shared Memory
Message passing
Shared Memory:
Algorithm:
Server:
1. Define shared memory size of 30 bytes
2. Define the key to be 5600
3. Create a shared memory using shmget () system calls and gets the shared memory id in variable shmid.
4. Attach the shared memory to server data space
5. Get the content to be placed in the shared memory from the user of the server.
6. Write the content in the shared memory, which will read out by the client.
7. stop
Client :
1. Define the key to be 5600
2. Attach the client to the shared memory created by the server.
3. Read the content from the shared memory.
4. Display the content on the screen.
5. stop
Objective:
Concepts Involved:
Deadlock Characteristics
.
Mutual Exclusion.
Hold and Wait.
No preemption.
Circular wait.
Deadlock Avoidance: Deadlock can be avoided by eliminating any of the above four condition
Banker’s Algorithm
Bankers’s Algorithm is resource allocation and deadlock avoidance algorithm which test all the request made by
processes for resources, it check for safe state, if after granting request system remains in the safe state it allows the
request and if there is no safe state it don’t allow the request made by the process.
Inputs to Banker’s Algorithm
1. Max need of resources by each process.
2. Currently allocated resources by each process.
3. Max free available resources in the system.
Request will only be granted under below condition.
1. If request made by process is less than equal to max need to that process.
2. If request made by process is less than equal to freely availbale resource in the system.
Example
Total resources in system:
ABCD
6576
Available system resources are:
ABCD
3112
Processes (currently allocated resources):
ABCD
P1 1 2 2 1
P2 1 0 3 3
P3 1 2 1 0
Processes (maximum resources):
ABCD
P1 3 3 2 2
P2 1 2 3 4
P3 1 3 5 0
ABCD
P1 2 1 0 1
P2 0 2 0 1
P3 0 1 4 0
Algorithm:
Safety algorithm
1. Start
2. Initialize a temporary vector W (Work) to equal the available vector A.
3. Find an index i (row i) such that
Need i < = W
If no such row exists, the system will deadlock, since no process can run to completion.
4. If such a row is found, mark this process as finished, and add all its resources to W
Vector i,
W = W + Ci
Go to step 2, until either all processes are marked terminated (in this case initial state is safe), or until a
deadlock occurs, in which the state is not safe.
Objective:
Concepts Involved:
Sample Output
Objective:
Procedure:
Objective:
Concepts Involved:
First Fit
The partition is allocated which is first sufficient from the top of Main Memory.
Implementation:
1- Input memory blocks with size and processes with size.
2- Initialize all memory blocks as free.
3- Start by picking each process and check if it can
be assigned to current block.
4- If size-of-process <= size-of-block if yes then
assign and check for next process.
5- If not then keep checking the further blocks.
Example:
Input : blockSize[] = {100, 500, 200, 300, 600};
processSize[] = {212, 417, 112, 426};
Output:
Process No. Process Size Block no.
1 212 2
2 417 5
3 112 2
4 426 Not Allocated
Best fit
It allocates the process to a partition which is the smallest sufficient partition among the free available partitions
Implementation:
1- Input memory blocks and processes with sizes.
2- Initialize all memory blocks as free.
3- Start by picking each process and find the
minimum block size that can be assigned to
current process i.e., find min(bockSize[1],
blockSize[2],.....blockSize[n]) >
processSize[current], if found then assign
it to the current process.
5- If not then leave that process and keep checking
the further processes.
Example:
Input : blockSize[] = {100, 500, 200, 300, 600};
processSize[] = {212, 417, 112, 426};
Output:
Process No. Process Size Block no.
1 212 4
2 417 2
3 112 3
4 426 5
Worst Fit
It allocates a process to the partition which is largest sufficient among the freely available partitions available in the
main memory. If a large process comes at a later stage, then memory will not have space to accommodate it.
Implementation:
1- Input memory blocks and processes with sizes.
2- Initialize all memory blocks as free.
3- Start by picking each process and find the
maximum block size that can be assigned to
current process i.e., find max(bockSize[1],
blockSize[2],.....blockSize[n]) >
processSize[current], if found then assign
it to the current process.
5- If not then leave that process and keep checking
the further processes.
Example:
Input : blockSize[] = {100, 500, 200, 300, 600};
processSize[] = {212, 417, 112, 426};
Output:
Process No. Process Size Block no.
1 212 5
2 417 2
3 112 5
4 426 Not Allocated
EX.NO.12 PAGING TECHNIQUE OF MEMORY MANAGEMENT
Objective:
Concepts Involved:
Paging
It is a memory management scheme that eliminates the need for contiguous allocation of physical memory. This
scheme permits the physical address space of a process to be non – contiguous.
The mapping from virtual to physical address is done by the memory management unit (MMU) which is a hardware
device and this mapping is known as paging technique.
The Physical Address Space is conceptually divided into a number of fixed-size blocks, called frames.
The Logical address Space is also spitted into fixed-size blocks, called pages.
Page Size = Frame Size
Example:
Physical Address = 12 bits, then Physical Address Space = 4 K words
Logical Address = 13 bits, then Logical Address Space = 8 K words
Page size = frame size = 1 K words (assumption)
Implementation:
Step2 : Read the base address, page size, number of pages and memory unit.
Step3 : If the memory limit is less than the base address display the memory limit is less than limit.
Step4 : Create the page table with the number of pages and page address.
Step6 : If the page number and displacement value is valid, add the displacement value with the address
corresponding to the page number and display the result.
Step7 : Display the page is not found or displacement should be less than page size.
Sample Input 1:
Program for paging
100
150
Sample Output 1:
1 150 200
Sample Input 2:
100
Sample Output 2:
Invalid page.
EX.NO.13 PAGE REPLACEMENT ALGORITHMS A) FIFO B) LRU C) LFU
Objective:
Concepts Involved:
These Algorithms are needed to decide which page needed to be replaced when new page comes in. Whenever a
new page is referred and not present in memory, page fault occurs and Operating System replaces one of the
existing pages with newly needed page
This is the simplest page replacement algorithm. In this algorithm, operating system keeps track of all pages in the
memory in a queue, oldest page is in the front of the queue. When a page needs to be replaced page in the front of
the queue is selected for removal.
FIFO Implementation
Least Recently Used (LRU)–In this algorithm page will be replaced which is least recently used.
LRU Implementation
In LFU we check the old page as well as the frequency of that page and if the frequency of the page is larger than
the old page we cannot remove it and if all the old pages are having same frequency then take last i.e FIFO method
for that and remove that page.
LFU Implementation
Objective:
Concepts Involved:
Algorithm:
1. Start
2. Declare the number, names and size of the directories and file names.
3. Get the values for the declared variables.
4. Display the files that are available in the directories.
5. Stop.
Output:
enter number of directorios:2
enter names of directories: abc xyz
enter size of directories:3 4
enter the file names :a b c x y z l
directory size filenames
*********************************
abc 3 a
b
c
xyz 4 x
y
z
l
Algorithm:
1. Start
2. Declare the number, names and size of the directories and subdirectories and file names.
3. Get the values for the declared variables.
4. Display the files that are available in the directories and subdirectories.
5. Stop.
Output:
Algorithm:
1. Start
2. Declare the number, names and size of the directories and subdirectories and file names.
3. Get the values for the declared variables.
4. Display the files that are available in the directories and subdirectories.
5. Stop.
Algorithm:
1. Start
2. Collect set of nodes 1, 2, …, n
3. Get the value of an edge (i,j) whenever i < j
4. Calculate N-choose-2 = n (n-1)/2 edges, but no cycles.
5. Stop.
EX.NO.15 FILE ALLOCATION STRATEGIES A) SEQUENTIAL B) INDEXED C) LINKED
Objective:
Concepts Involved:
Output:
Enter no. of Files ::2
Enter file 1 name ::x.c
Enter file1 size(in kb)::4
Enter Starting block of 1::100
Enter blocksize of File1(in bytes)::512
Enter file 2 name ::y.c
Enter file2 size(in kb)::2
Enter Starting block of 2::500
Enter blocksize of File2(in bytes)::256
Enter the Filename ::y.c
Fname Start Nblocks Blocks
---------------------------------------------
y.c 500 8 500->501->502->503->504->505->506->507->
---------------------------------------------
Do U want to continue ::(Y:n) n
Output:
Enter no. of Files :2
Enter file 1 name ::x.c
Enter file1 size(in kb)::1
Enter blocksize of File1(in bytes)::512
Enter file 2 name ::y.c
Enter file2 size(in kb)::1
Enter blocksize of File2(in bytes)::512
Enter blocks for file1
Enter the 1block ::1000
Enter the 2block ::1001
Enter blocks for file2
Enter the 1block ::2000
Enter the 2block ::2001
Enter the Filename ::x.c
Fname Fsize Bsize Nblocks Blocks
---------------------------------------------
x.c 1 512 2 1000->1001->
---------------------------------------------