Escolar Documentos
Profissional Documentos
Cultura Documentos
: 01
Date:
BASICS OF UNIX COMMANDS
AIM :
To study the basic unix commands and their usage
Directory Commands
Command
pwd
ls
cd
mkdir
Description
Shows the name and location of the directory where you are
currently working.
Gives you a short list of the files in the directory where you
are currently working.
Moves you to another directory.
Creates a new subdirectory inside of the directory where
you are currently working
Description
Creates a file
Display contents of FILE, page by page.
Display a file.
Append FILE1 and FILE2 creating new file NEW.
Append FILE1 at the end of FILE2.
Sort FILE, putting sorted version into NEWFILE.
Display lines of FILE(S) which contain ITEM.
Count characters, words and lines in FILE(S).
Show differences between two versions of a file
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.
file
chmod
mv
rm
Eg:
rm n*
This command removes all files beginning with n.
Type rm -i followed by a filename if you would like to be
prompted before the file is actually removed.
Eg:
rm -i newfile
rm -i n*
By using this option, you have a chance to verify the removal
of each file. The -i option is very handy when removing a
number of files using the wildcard character *.
find
Nl filename
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. 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.
echo [-n] text
-n
text
cal
who
man
clear
write
tty
Filter commands
Grep
Prints all lines of the file except those that contain the pattern.
What the "-n" does is tell grep to print out the line number as well
as the line itself.
egrep
sort
uniq
in reverse
-r -r file.txt Sorts
Sort
- Would
sort theorder
file, file.txt in reverse order
Report or filter out repeated lines in a file.
uniq [-c | -d | -u ] [ -f fields ] [ -s char ] [-n] [+m] [input_file [
output_file ] ]
-c
Precede each output line with a count of the
number Of times the line occurred in the input.
Suppress the writing of lines that are not
-d
repeated In the input.
Suppress the writing of lines that are
-u
repeated In the input.
Ignore the first fields fields on each input line
-f fields
when doing comparisons, where fields is a
positive Decimal integer.
Ignore the first chars characters when
s char
doing comparisons,
Equivalent to -f fields with fields set to n.
-n
Equivalent to -s chars with chars set to m.
+m
input_file A path name of the input file. If input_file is
specified,
if the
input_file
-, the
A path
name oforthe
output
file. Ifisoutput_file
is
output_file not
not specified, the standard output will be used.
The results are unspecified if the file named
by output_file is the file named by
input_file.
-r
head
tail
cut
paste
tr
EX:
Echo "12345678 9247" | tr 123456789 computer hope - 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]'`
redirection
tee
Exercise:
1. Create the following directory structure in UNIX
CSE
PG
UG
Create files under CSE with extensions like *.txt, *.c etc.
Display all files with extension txt.
Rename all files with extension txt to dat.
Copy the files with extension C to UG directory
Delete all the files with extension C from CSE directory
Display all files with 2 character filename and extension .txt
Create a file with list of subject names and Display the first three and
last three Subjects stored in the file.
9. List the contents of the file from the fourth subject from the end of the file and
also list the contents of the file from the seventh subject from the beginning of
the file
10. Create a file named INDIA which contains the following data
India is my country
All Indians are my Brothers and Sister
I love my country
a) Use the filter command to search for the pattern my
b) Display all the lines that do not contain the above pattern
c) Count the total number of lines containing the pattern my
d) Display all the lines which contains the pattern country
2.
3.
4.
5.
6.
7.
8.
11. Create a file with 15 lines of data. Using filter commands select top 10 lines, Last 8 lines
and display them in two different files. Add line numbers to the files.
12. Create a file containing 10 lines of information. Count the number of lines, number of
words and number of character.
13. Redirect the contents of ls p to a file called LISTING. Give the commands to Redirect the
output of a long listing of files and append it to the file LISTING
14. Change the modes of those files which begin with the letter s in such a way that the
owner has read and execute permission, the group has read and write permission and
others only read permission(use octaldecimal representation)
15. Create a file called MARK which contains the sample data
as follows:
S001 Raja dbms 78
S002 Usha os 96
a) Display the contents of the file sorted according to the marks in the descending order
b) Display the names of the students in the alphabetic order ignoring the cases
c) Display the list of students who have scored marks between 50 and 80
d) Display the list of students and their registration numbers
e) Sort the files according to the third field and dump it to the file called SCODE
16. Create a file called EMP as given below:
E001:malar:mktg:5000
E002:balan:acct:7000
a) Sort the file on the employees department and display the name of the employee and
the department.
b) List the employees who earn between 4000 and 6000
c) Sort the file on the employee name in the reverse order and extract their codes and their
names
d) Display the contents of the file without redundancy in sorted order
17. Display the permission of the group for the files whose names begin with p
18. Give the command to extract logins of the people whose login name starts with CSE
19. Display the entire text of the file in uppercase
20. Give the command to extract the links, the file owner and the file name only in
the current directory.
RESULT:
The various Unix operating system commands are studied and their usage are
understood.
Department of Information Technology
Exp. No. : 02
Date:
SHELL PROGRAMMING
AIM:
To write shell scripts in Unix operating system using various programming constructs
Introduction:
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
Creation and execution of shell scripts using command line editor:
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
Assigning values to variable:
Variable=value
Displaying values of variables:
$ echo value of n is $n
Operators:
Arithmetic Operators provided by the shell are +,- * and /
Logical operators
-a and
-o or
! not
Relational operators
-eq : check fro equality of integers
-ne : check for inequality
Department of Information Technology
10
-gt
-lt
-ge
-le
-f
-d
-r
-w
-x
b) if..then..elif..else ..fi
2. fordodone
3. while..do..done
4. untildo..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
Department of Information Technology
11
2) for construct
Used to perform same set of operations on a list of values
for variable in value1 value2 value3
do
Commands
done
Ex.
for k in 1 2 3 4 5 do
echo the number is $k
echo the square of the number is `expr $k \* $k`
done
3) while construct
Repeatedly executing group of commands as long as the condition is true.
while condition
do
Commandlist
done
Ex.to print 3 numbers
a=1
while [$a -le 3]
do
echo $a
$a=`expr $a+1`
done
o/p. 1 2 3
4)
until construct
Repeatedly executing group of commands until a condition is true.
until condition
do
Commandlist
done
Ex.to print 3 numbers
a=1
until [$a -le 3]
do
echo $a
$a=`expr $a+1`
done
o/p. 1 2 3
5)
case construct:
case value in
choice1) commands;;
choice2)commands;;
.
Department of Information Technology
12
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
Exercise:
1. Greatest among three numbers
Algorithm:
Step 1. Read 3 numbers n1,n2 and n3
Step 2. Check if n1 is greater than n2
check n1 is greater than n3 also announce n1 as greatest
else announce n3 as greatest
else, check whether n2 is greater than n3
if yes, announce n2 as greatest
else announce n3 as greatest
2. Factorial of a given number
Algorithm:
Step 1. Read n
Step 2. Initialize fact to 1 and i to n
Step 3. Repeat the following until i>0
Assign fact * i to fact
Decrement i by 1
3. Sum of Odd numbers upto n
Algorithm:
Step 1. Read n
Step 2. Initialize x=1 and sum=0
Step 3. Repeat the following until x < n
Assign sum + x to sum
Increment x by 2
4.Generation of Fibonacci numbers
Algorithm:
Step 1. Read n
Step 2. Initialize p=-1, q=1 and I=1
Step 3. Repeat the following until I < n
Assign p + q to r
Assign q to p
Assign r to q
Increment I by 1
Department of Information Technology
13
RESULT:
Thus programs in Unix shell script are developed using various language
constructs and execueted successfully.
14
Exp. No. : 03
Date:
IMPLEMENTATION OF CPU SCHEDULING ALGORITHMS
Non-Pre-emptive Scheduling:
15
16
Step 3: Read the process name, its CPU burst from the user and store it in the structure
Proc_stru
Step 4: Get from the user CPU Time slice
Step 5: Allot the process to CPU for given time slice
Step 6: Switch the CPU to next process when time slice over
Step 7: Display the currently allotted processs detail
Step 8: Repeat the step 5, 6 till all the process are serviced completely.
Step 9: Stop
RESULT:
Thus the for CPU scheduling algorthims are implemented in C language and
executed successfully.
Department of Information Technology
17
Exp. No. : 04
Date:
IMPLEMENTATION OF FILE ALLOCATION STRATEGIES
4.a. Sequential File Allocation:
Aim:
To implement Sequential file allocation in C.
Algorithm:
Step 1: Start
Step 2: Declare the starting block no. and the length of the file.
Step 3: Get the Starting block no. and length of the file from the user. Allocate files
sequentially until end of the file.
Step 4: Display the fragments of the file.
Step 5: Stop
4.b. Indexed File Allocation:
Aim:
To implement Indexed file allocation technique in C.
Algorithm:
Step 1: Start
Step 2: Declare the index block no. and total no.of files in a block
Step 3: Get the index block no. and total no.of files in a block from the user.
Step 4: Allocate files based on the index block no.
Step 5: Arrange the files based on indexes which are created for each fragment of the
file such that each and every similar indexed file is maintained by the primary
index to provide the flow to file fragments.
Step 6: Stop
18
Result:
Thus the different file allocation strategies are implemented and executed successfully.
19
Exp. No. : 05
Date:
IMPLEMENTATION OF SEMAPHORES
Aim:
To implement the Producer and Consumer Problem using semaphores
Algorithm:
Step 1: Start
Step 2: Initialize the semaphore variable S
Step 3: In the producer function,
3a) While s ==1 do nothing
3b) Produce the value
3c) Assign s=1
3d) Return
Step 4: In the Consumer function
4a) While s==0 do nothing
4b) Display the consumed value
4c) Assign s=0
4d) Return
Step 5: Create threads for producer and consumer function to make it run concurrently
Step 6: Stop
Result:
Thus the implementation of Producer and Consumer problem is implemented and executed
successfully.
20
Exp. No. : 06
Date:
IMPLEMENTATION OF FILE ORGANIZATION TECHNIQUES
6.a. Implementation of Single level directory
Aim:
To implement Single level directory structure in C
Algorithm:
Step 1: Start
Step 2: Declare the number, names and size of the directories and file names.
Step 3: Get the values for the declared variables.
Step 4: Display the files that are available in the directories.
Step 5: Stop.
21
Result:
Thus the implementation of different file organization techniques are implemented
and executed successfully.
Department of Information Technology
22
Exp. No. : 07
Date:
IMPLEMENTATION OF BANKERS ALGORITHM
Aim:
To detect and prevent deadlock using Bankers algorithm
Algorithm:
Safety algorithm
Step 1: Start
Step 2: Initialize a temporary vector W (Work) to equal the available vector A.
Step 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.
Step 4: If such a row is found, mark this process as finished, and add all
its resources to W Vector i,
W = W + Ci
Step 5: 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.
Resource request algorithm
Step 1: If
Step 2: If Request i < = Available, go to step 3. Otherwise, Pi must wait, since resources
are not available
Step 3: Modify the state ( the system pretend to have allocated the requested
resources to process Pi )
Available = Available - Requesti
Allocationi = Allocationi + Requesti
Needi = Needi - Requesti
Step 4: If the resulting state is safe, the transaction is completed and process Pi is
allocated its resources. If new state is unsafe, then Pi must wait for Requesti and
the old state is restored.
Step 5: Stop
Result:
Thus the C program for Bankers algorithm is implemented and executed successfully.
23
Exp. No. : 08
Date:
IMPLEMENTATION OF DEADLOCK DETECTION
Aim:
To write a C program for implementing the deadlock detection algorithm
Algorithm:
Algorithm I: Simply detects the existence of a Cycle
Step 1: Start at any vertex finds all its immediate neighbors.
Step 2: From each of these find all immediate neighbors, etc.
Step 3: Until a vertex repeats (there is a cycle) or one cannot continue (there is no cycle).
Step 4: Stop.
Result:
Thus the program for Deadlock detection algorithm is implemented and executed
successfully.
24
Exp. No. : 09
Date:
IMPLEMENTATION OF PAGE REPLACEMENT ALGORITHMS
9. a. FIFO page replacement algorithm
Aim:
To write a C program for implementing FIFO page replacement algorithm
Algorithm:
Step 1: Start the process
Step 2: Declare the size with respect to page length
Step 3: Check the need of replacement from the page to memory
Step 4: Check the need of replacement from old page to new page in memory
Step 5: Forma queue to hold all pages
Step 6: Insert the page require memory into the queue
Step 7: Check for bad replacement and page fault
Step 8: Get the number of processes to be inserted
Step 9: Display the values
Step 10: Stop the process
25
26
Result:
Thus the programs for Page Replacement algorithms are implemented and executed
successfully.
Department of Information Technology
27
Exp. No. : 10
Date:
INTER PROCESS COMMUNICATION
Aim:
To develop a client-server application program that uses shared memory using IPC
Algorithm:
Client :
Step 1: Define the key to be 5600
Step 2: Attach the client to the shared memory created by the server.
Step 3: Read the content from the shared memory.
Step 4: Display the content on the screen.
Step 5: Stop
Server:
Step 1: Define shared memory size of 30 bytes
Step 2: Define the key to be 5600
Step 3: Create a shared memory using shmget () system calls and gets the shared
memory id in variable shmid.
Step 4: Attach the shared memory to server data space
Step 5: Get the content to be placed in the shared memory from the user of the server.
Step 6: Write the content in the shared memory, which will read out by the client.
Step 7: Stop
Result:
Thus the programs for Inter Process Communication (IPC) is implemented and executed
successfully.
Department of Information Technology
28
Exp. No. : 11
Date:
IMPLEMENTATION OF PAGING
Aim:
To implement memory allocation with pages
Algorithm:
Step 1: Start
Step 2: Declare the structure P_table with variables for page no and frame no
Step 3: Display the status of physical memory
Step 4: Get the number of pages needed for a process
Step 5: Get the contents of the pages
Step 6: Display the contents of logical memory
Step 7: If the physical memory is available then allot the pages of the process
Step 8: Update the physical memory status
Step 9: Update the page table status
Step 10: Display the page table after allocation
Step 11: Display the physical memory after allocation
Step 12: Stop
Result:
Thus the C program for Paging concept is implemented and executed successfully.
Department of Information Technology
29
Exp. No. : 12
Date:
IMPLEMENTATION OF THREADING AND SYNCHRONIZATION
Aim:
To implement Banking system involving Concurrency using Thread
Algorithm:
Step 1: Create a Bank Database
Step 2: Create functions for adding a new user, depositing the amt, withdrawing the amt
& viewing the customer
Step 3: Create a thread for each functionality
Step 4: Implement the concurrency control among the threads
Step 5: Function add new user
a) Get the user details namely, Acno, Name & Bank Balance
b) Write the user details to the bank database
Step 6: Function Deposit
a) Get the Acno & Amt
b) Update the Bank balance for the given Acno
Step 7: Function Withdraw
a) Get the Acno & Amt
b) Update the Bank balance for the given Acno
Step 8: Function View
a) Get the Acno
b) Display the account details
Result:
Thus the C program for concurrency and synchronization are implemented and executed
successfully.
Department of Information Technology
30
Result:
Thus the C program for Deadlock Detection for multiple resources is implemented and
executed successfully.
Department of Information Technology
31
Exp. No. : 14
Date:
DINING PHILOSOPHER PROBLEM
Aim:
To write a C program to implement the concept of the Dining Philosopher
problem
Algorithm:
Step 1: Get the total number of philosopher
Step 2: Get the number of philosopher who are hungry
Step 3: If all are hungry then deadlock occurs
Step 4: Get the philosopher position
Step 5: Display the menu
Step 6: Two can eat at a time
Step 7: One can eat at a time
Step 8: If choice is one, then
a)get the philosopher states of eating position
b)display the means of those who are waiting.
Result:
Thus the C program to implement the Dining philosopher problem was created, executed
successfully and the output was verified
Department of Information Technology
32