Linux Programming and Data Mining Lab Manual
Linux Programming and Data Mining Lab Manual
in
JNTU World
JN
TU
or
ld
LAB MANUAL
www.alljntuworld.in
JNTU World
Program Outcomes
PO4
PO5
PO6
PO7
PO8
PO9
TU
PO10
ld
PO3
or
PO2
PO1
PO11
JN
PO12
PSO1
PSO2
PSO3
Professional Skills: The ability to research, understand and implement computer programs in the
areas related to algorithms, system software, multimedia, web design, big data analytics, and
networking for efficient analysis and design of computer-based systems of varying complexity.
Problem-Solving Skills: The ability to apply standard practices and strategies in software project
development using open-ended programming environments to deliver a quality product for business
success.
Successful Career and Entrepreneurship: The ability to employ modern computer languages,
environments, and platforms in creating innovative career paths, to be an entrepreneur, and a zest for
higher studies.
www.alljntuworld.in
JNTU World
S. No.
List of Experiments
Page No.
LINUX PROGRAMMING
1
TU
or
ld
a) Write a shell script that accepts a file name, starting and ending line numbers as
arguments and displays all the lines between the given line numbers.
b) *Illustrate by writing script that will print, message Hello World, in Bold and
Blink effect, and in different colors like red, brown etc using echo commands?
a) Write a shell script that deletes all lines containing a specified word in one or
more files supplied as arguments to it.
b) *Illustrate by writing script using for loop to print the following patterns?
i) *
ii) 1
**
22
***
333
****
4444
*****
55555
a) Write a shell script that displays a list of all the files in the current directory to
which the user has read, write and execute permissions.
b) * Illustrate to redirect the standard input (stdin) and the standard output (stdout)
of a process, so that scanf () reads from the pipe and printf () writes into the
pipe?
a) Write a shell script that receives any number of file names as arguments checks
if every argument supplied is a file or a directory and reports accordingly.
Whenever the argument is a file, the number of lines on it is also reported.
b) *Illustrate by writing c program where process forks to a child, and create a
child process by using forks and suddenly terminates itself?
Write a shell script that accepts a list of file names as its arguments, counts and
reports the occurrence of each word that is present in the first argument file on other
argument files
Write a shell script to list all of the directory files in a directory.
Write a shell script to find factorial of a given integer.
Write an awk script to count the number of lines in a file that do not contain vowels.
Write an awk script to find the number of characters, words and lines in a file.
Write a c program that makes a copy of a file using standard I/O and system calls
Implement in C the following UNIX commands using System calls
a) cat
b) ls
c) mv
Write a program that takes one or more file/directory names as command line input
and reports the following information on the file.
a) File type
b) Number of links
c) Time of last access
d) Read Write and Execute permissions
Write a C program to emulate the UNIX ls l command.
Write a C program to list for every file in a directory, its inode number and file
name.
Write a C program that demonstrates redirection of standard output to a file.Ex: ls >
f1.
JN
6
7
8
9
10
11
12
13
14
15
11
14
15
16
17
18
19
20
23
24
25
26
www.alljntuworld.in
21
22
23
24
25
26
27
28
DATA MINING
List all the categorical (or nominal) attributes and the real-valued attributes
separately.
What attributes do you think might be crucial in making the credit assessment?
Come up with some simple rules in plain English using your selected attributes.
*What attributes do you think might be crucial in making the bank assessment?
One type of model that you can create is a Decision Tree -train a Decision Tree
using the complete dataset as the training data. Report the model obtained after
training.
Suppose you use your above model trained on the complete dataset, and classify
credit good/bad for each of the examples in the dataset. What % of examples can
you classify correctly? (This is also called testing on the training set) Why do you
think you cannot get 100 % training accuracy?
*Find out the correctly classified instances, root mean squared error, kappa
statistics, and mean absolute error for weather data set?
Is testing on the training set as you did above a good idea? Why or Why not?
One approach for solving the problem encountered in the previous question is using
cross-validation? Describe what is cross -validation briefly. Train a Decision Tree
again using cross - validation and report your results. Does your accuracy
increase/decrease? Why?
TU
2
3
4
JN
6
7
8
28
29
30
33
34
ld
20
Page No.
27
or
17
18
19
List of Experiments
Write a C program to create a child process and allow the parent to display parent
and the child to display child on the screen.
Write a C program to create a Zombie process.
Write a C program that illustrates how an orphan is created.
Write a C program that illustrates how to execute two commands concurrently with
a command pipe. Ex: - ls l | sort
Write C programs that illustrate communication between two unrelated processes
using named pipe.
Write a C program to create a message queue with read and write permissions to
write 3 messages to it with different priority numbers.
Write a C program that receives the messages (from the above message queue as
specified in (21)) and displays them.
Write a C program to allow cooperating processes to lock a resource for exclusive
use, using
a) Semaphores
b) flock or lockf system calls.
Write a C program that illustrates suspending and resuming processes using signals
Write a C program that implements a producer-consumer system with two
processes.
Write client and server programs (using c) for interaction between server and client
processes using Unix Domain sockets. (Using Semaphores).
Write client and server programs (using c) for interaction between server and client
processes using Internet Domain sockets.
Write a C program that illustrates two processes communicating using shared
memory.
S. No.
16
JNTU World
36
38
39
40
41
45
48
52
53
56
57
60
64
68
69
www.alljntuworld.in
12
13
14
TU
15
JN
16
17
74
75
ld
11
Page No.
72
or
10
List of Experiments
Check to see if the data shows a bias against "foreign workers" (attribute 20), or
"personal
-status" (attribute 9). One way to do this (perhaps rather simple minded) is to
remove these attributes from the dataset and see if the decision tree created in those
cases is significantly different from the full dataset case which you have already
done. To remove an attribute you can use the preprocess tab in Weka's GUI
Explorer. Did removing these attributes have any significant effect? Discuss.
*Load the weather.arff dataset in Weka and run the ID3 classification algorithm.
What problem do you have and what is the solution?
Another question might be, do you really need to input so many attributes to get
good results? Maybe only a few would do. For example, you could try just having
attributes 2, 3, 5, 7, 10, 17 (and 21, the class attribute (naturally)). Try out some
combinations. (You had removed two attributes in problem 7. Remember to reload
the arff data file to get all the attributes initially before you start selecting the ones
you want.)
Sometimes, the cost of rejecting an applicant who actually has a good credit (case
1) might be higher than accepting an applicant who has bad credit (case 2). Instead
of counting the misclassifications equally in both cases, give a higher cost to the
first case (say cost 5) and lower cost to the second case. You can do this by using a
cost matrix in Weka. Train your Decision Tree again and report the Decision Tree
and cross -validation results. Are they significantly different from results obtained
in problem 6 (using equal cost)?
Do you think it is a good idea to prefer simple decision trees instead of having long
complex decision trees? How does the complexity of a Decision Tree relate to the
bias of the model?
*Run the J48 and 1Bk classifiers using-the cross-validation strategy with various
fold levels. Compare the accuracy results. Holdout strategy with three percentage
levels. Compare the accuracy results.
You can make your Decision Trees simpler by pruning the nodes. one approach is
to use Reduced Error Pruning -Explain this idea briefly. Try reduced error pruning
for training your Decision Trees using cross -validation (you can do this in Weka)
and report the Decision Tree you obtain? Also, report your accuracy using the
pruned model. Does your accuracy increase?
(Extra Credit): How can you convert a Decision Trees into "if then -else rules".
Make up your own small Decision Tree consisting of 2 - 3 levels and convert it into
a set of rules. There also exist different classifiers that output the model in the form
of rules -one such classifier in Weka is rules. PART, train this model and report the
set of rules obtained. Sometimes just one attribute can be good enough in making
the decision, yes, just one! Can you predict what attribute that might be in this
dataset? OneR classifier uses a single attribute to make decisions (it chooses the
attribute based on minimum error). Report the rule obtained by training a one R
classifier. Rank the performance of j48, PART and oneR.
*Run J48 and Nave Bayes classifiers on the following datasets and determine the
accuracy:
1.vehicle.arff
2.kr-vs-kp.arff
3.glass.arff
4.wave-form-5000.arff
On which datasets does the Nave Bayes perform better?
S. No.
9
JNTU World
77
79
80
82
84
88
www.alljntuworld.in
JNTU World
Program
Outcomes
Attained
Experiment
Program
Specific
Outcomes
Attained
LINUX PROGRAMMING
PO1, PO2
TU
or
c) Write a shell script that accepts a file name, starting and ending line
numbers as arguments and displays all the lines between the given
line numbers.
d) *Illustrate by writing script that will print, message Hello World, in
Bold and Blink effect, and in different colors like red, brown etc
using echo commands?
c) Write a shell script that deletes all lines containing a specified word
in one or more files supplied as arguments to it.
d) *Illustrate by writing script using for loop to print the following
patterns?
i) *
ii) 1
**
22
***
333
****
4444
*****
55555
c) Write a shell script that displays a list of all the files in the current
directory to which the user has read, write and execute permissions.
d) * Illustrate to redirect the standard input (stdin) and the standard
output (stdout) of a process, so that scanf () reads from the pipe and
printf () writes into the pipe?
c) Write a shell script that receives any number of file names as
arguments checks if every argument supplied is a file or a directory
and reports accordingly. Whenever the argument is a file, the
number of lines on it is also reported.
d) *Illustrate by writing c program where process forks to a child, and
create a child process by using forks and suddenly terminates itself?
Write a shell script that accepts a list of file names as its arguments,
counts and reports the occurrence of each word that is present in the first
argument file on other argument files.
Write a shell script to list all of the directory files in a directory.
Write a shell script to find factorial of a given integer.
Write an awk script to count the number of lines in a file that do not
contain vowels.
Write an awk script to find the number of characters, words and lines in
a file.
Write a c program that makes a copy of a file using standard I/O and
system calls
ld
JN
6
7
8
9
10
PSO1
PO1
PSO1
PO1, PO2
PSO1
PO1
PSO1
PO1
PSO1
PO1
PO1
PSO1
PSO1
PO1
PSO1
PO1
PSO1
PO1
PSO1
www.alljntuworld.in
JNTU World
Exp.
No.
15
16
17
18
19
20
21
22
TU
23
24
25
26
JN
27
28
18
19
PO1, PO2
PSO1
PO1, PO2
PSO1
PO1
PSO1
PO1
PSO1
PO1
PSO1
PO1
PSO1
ld
13
14
Program
Specific
Outcomes
Attained
or
12
11
Experiment
Program
Outcomes
Attained
PO1
PO1
PSO1
PSO1
PO1
PSO1
PO1
PSO1
PO1
PSO1
PO1
PSO1
PO1, PO2
PSO1
PO1, PO2
PSO1
PO1, PO2,
PO4
PSO1,
PSO2
PO1, PO2,
PO3, PO4
PSO1,
PSO2
PO1, PO2,
PO3, PO4
PSO1,
PSO2
PO1, PO2,
PO3, PO4
PSO1,
PSO2
PO1, PO2
PSO1
PO1, PO2
PSO1,
PSO2
DATA MINING
List all the categorical (or nominal) attributes and the real-valued
attributes separately.
What attributes do you think might be crucial in making the credit
assessment? Come up with some simple rules in plain English using
your selected attributes.
www.alljntuworld.in
JNTU World
Exp.
No.
23
24
25
26
27
TU
28
JN
29
30
31
PO1, PO2,
PO12
PSO1,
PSO2
PO1, PO2,
PO5
PSO1
PO1, PO2
PSO1,
PSO2
ld
22
Program
Specific
Outcomes
Attained
PO1, PO2,
PO5
PO1, PO2,
PO5, PO12
or
21
20
Experiment
Program
Outcomes
Attained
PSO1
PSO1
PO1, PO2,
PO5
PSO1
PO1, PO2,
PO4, PO5
PSO1
PO1, PO2,
PO5
PSO1,
PSO2
PO1, PO2,
PO4, PO12
PSO1
PO1, PO2,
PO5, PO12
PSO1,
PSO2
PO1, PO2,
PO12
PSO1,
PSO2
PO1, PO2,
PO4, PO5
PSO1,
PSO2
www.alljntuworld.in
JNTU World
Exp.
No.
32
You can make your Decision Trees simpler by pruning the nodes. one
approach is to use Reduced Error Pruning -Explain this idea briefly. Try
reduced error pruning for training your Decision Trees using cross validation (you can do this in Weka) and report the Decision Tree you
obtain? Also, report your accuracy using the pruned model. Does your
accuracy increase?
(Extra Credit): How can you convert a Decision Trees into "if then else rules". Make up your own small Decision Tree consisting of 2 - 3
levels and convert it into a set of rules. There also exist different
classifiers that output the model in the form of rules -one such classifier
in Weka is rules. PART, train this model and report the set of rules
obtained. Sometimes just one attribute can be good enough in making
the decision, yes, just one! Can you predict what attribute that might be
in this dataset? OneR classifier uses a single attribute to make decisions
(it chooses the attribute based on minimum error). Report the rule
obtained by training a one R classifier. Rank the performance of j48,
PART and oneR.
*Run J48 and Nave Bayes classifiers on the following datasets and
determine the accuracy:
1.vehicle.arff
2.kr-vs-kp.arff
3.glass.arff
4.wave-form-5000.arff
On which datasets does the Nave Bayes perform better?
Program
Specific
Outcomes
Attained
PO1, PO2,
PO4, PO5,
PO12
PSO1,
PSO2
ld
33
Experiment
Program
Outcomes
Attained
or
34
PO1, PO2,
PO4, PO5,
PO12
JN
TU
PO1, PO2,
PO4, PO5,
PO12
PSO1,
PSO2
PSO1,
PSO2
www.alljntuworld.in
JNTU World
ld
The Linux programming laboratory course covers major methods of Inter Process Communication
(IPC), which is the basis of all client / server applications under Linux, Linux Utilities, working
with the Bourne again shell (bash), files, process and signals. There will be extensive programming
exercises in shell scripts. It also emphasizes various concepts in multithreaded programming and
socket programming.
OUTCOMES:
or
Data mining tools allow predicting future trends and behaviors, allowing businesses to make
proactive, knowledge-driven decisions. The data mining laboratory course is designed to exercise
the data mining techniques such as classification, clustering, pattern mining etc with varied datasets
and dynamic parameters. Weka data mining tool is used for the purpose of acquainting the students
with the basic environment of the data mining tools.
Upon the completion of Linux Programming and Data Mining practical course, the student will be able
to:
2.
3.
4.
Design and implement advanced features such as threads, IPC, pipes, FIFOs.
5.
TU
1.
6.
7.
Implement and manage client server technology using TCP and UDP.
8.
Learn to build a data warehouse and query it using open source tools.
9.
Learn to execute data mining tasks using a data mining toolkit (such as WEKA) and
visualize the results.
10. Demonstrate the working of algorithms for data mining tasks such association rule mining,
JN
JNTU World
or
ld
www.alljntuworld.in
JN
TU
LINUX PROGRAMMING
www.alljntuworld.in
JNTU World
EXPERIMENT 1
OBJECTIVE
a) Write a shell script that accepts a file name, starting and ending line numbers as arguments and
displays all the lines between the given line numbers.
b) *Illustrate by writing script that will print, message Hello World, in Bold and Blink effect, and
in different colors like red, brown etc using echo commands?
1.2
RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
1.3
PROGRAM LOGIC
1. Read a filename, starting and ending line numbers as arguments
2. Find difference between starting and ending line numbers
3. Test given filename exists or not
4. If exists display between lines to output stream else display file not exists
1.4.a
DESCRIPTION / PROCEDURE
echo " Enter the file name
read fname
echo enter starting line number
read sl
echo enter ending line number
read el
d=`expr $el - $sl`
if [ -f $fname ]
then
echo "the lines between $sl and $el of given file are"
head -$el $fname | tail -$d
else
echo "file doesnt exist"
fi
INPUT:
sh prog1.sh
enter the file name
file1
enter starting line number
15
enter ending line number
20
OUTPUT:
# It displays 15 to 20 between lines
JN
TU
or
ld
1.1
1.4.b
DESCRIPTION / PROCEDURE
# clear the screen
clear
echo -e "\033[1m Hello World"
# print bold effect
echo -e "\033[5m Blink"
# blink effect
echo -e "\033[0m Hello World"
# print back to normal
echo -e "\033[31m Hello World"
#print in Red color
5
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
ld
PRE-LAB QUESTIONS
1. Define shell script? What is the difference between shell and kernel.
2. Name few file handling commands present in unix.
1.6
LAB ASSIGNMENT
1. Write a shell script to count number of words present in a file without using commands.
2. Write a menu driven shell script to execute a command as 1.for `ls` ,2 for grep and 3 for cat.
1.7
POST-LAB QUESTIONS
1. What is the purpose of case statement?
2. What the difference between break and exit statement?
JN
TU
or
1.5
6
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 2
OBJECTIVE
a) Write a shell script that deletes all lines containing a specified word in one or more files
supplied as arguments to it.
b) *Illustrate by writing script using for loop to print the following patterns?
2.2
RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
2.3
PROGRAM LOGIC
Read file name from command line arguments and display lines inverse of specified word.
2.4.a
DESCRIPTION / PROCEDURE
if [ $# -ne 0 ]
then
echo enter the word
read word
for fname in $*
do
if [ -f $fname ]
then
echo the given input filename is:$fname
grep -v "$word" $fname
else
echo its not a file
fi
done
else
echo "enter atleast one argument as input"
fi
TU
or
ld
2.1
INPUT:
sh prog2.sh 3.sh
JN
OUTPUT:
The given input filename is : 3.sh
It displays all the lines other than pattern matching
2.4.b.i
DESCRIPTION / PROCEDURE
# do the following for loop
echo "Stars"
# outer loop
for (( i=1; i<=5; i++ ))
do
#inner loop
for (( j=1; j<=i; j++ ))
do
echo -n " *"
7
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
done
echo ""
done
#
or
ld
PRE-LAB QUESTIONS
1. What are positional parameter and name any two.
2. Write down the syntax of `if` statement.
2.6
LAB ASSIGNMENT
1.Read two string str1 and str2 and check
i) Compare two strings
ii) Palindrome or not
2.7
POST-LAB QUESTIONS
1. What is the purpose of the variable $? What are the various output it has?
JN
TU
2.5
8
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 3
OBJECTIVE
a) Write a shell script that displays a list of all the files in the current directory to which the user has read,
write and execute permissions.
b) Illustrate to redirect the standard input (stdin) and the standard output (stdout) of a process, so that
scanf () reads from the pipe and printf () writes into the pipe?
3.2
RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
3.3
PROGRAM LOGIC
Read a list of files from current directory and display the file names to output stream whose files has read,
write, execute permissions.
ld
3.1
or
JN
TU
www.alljntuworld.in
JNTU World
}}}
PRE-LAB QUESTIONS
1. What is the difference between $* and $@.
2. How to read a variable ,assign ,and access it
3.6
LAB ASSIGNMENT
1. Read a file name from command line and check its a file or not.
2. Read a file name from command line and check if it read and write permission or not.
3.7
POST-LAB QUESTIONS
1. How to check if file is existing, it has read, write and execution permission.
JN
TU
or
ld
3.5
10
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 4
OBJECTIVE
a) Write a shell script that receives any number of file names as arguments checks if every argument
supplied is a file or a directory and reports accordingly. Whenever the argument is a file, the number
of lines on it is also reported.
b) *Illustrate by writing c program where process forks to a child, and create a child process by using
forks and suddenly terminates itself?
4.2
RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
4.3
PROGRAM LOGIC
Read a list of files from current directory and display the file names to output along with number of lines
of each file
or
ld
4.1
TU
INPUT:
sh prog4.sh
OUTPUT:
enter the name
file 3.sh
number of lines 9
JN
www.alljntuworld.in
JNTU World
return 1;
TU
or
ld
}
if (childpid == 0) {
printf("Child 1: I inherited my parent's pid as %d.\n", mypid);
mypid = getpid();
printf("Child 1: getppid() tells my parent is %d. My own pid instead is %d.\n", getppid(), mypid);
/* forks another child */
childpid = fork();
if ( childpid == -1 ) {
perror("Cannot proceed. fork() error");
return 1;
}
if (childpid == 0) {
/* this is the child of the first child, thus "Child 2" */
printf("Child 2: I hinerited my parent's PID as %d.\n", mypid);
mypid = getpid();
printf("Child 2: getppid() tells my parent is %d. My own pid instead is %d.\n", getppid(),
mypid);
childpid = fork();
if ( childpid == -1 ) {
perror("Cannot proceed. fork() error");
return 1;
}
if (childpid == 0) {
/* "Child 3" sleeps 30 seconds then terminates 12, hopefully before its parent "Child 2" */
printf("Child 3: I hinerited my parent's PID as %d.\n", mypid);
mypid = getpid();
printf("Child 3: getppid() tells my parent is %d. My own pid instead is %d.\n", getppid(),
mypid);
sleep(30);
return 12;
} else /* the parent "Child 2" suddendly returns 15 */ return 15;
} else {
/* this is still "Child 1", which waits for its child to exit */
while ( waitpid(childpid, &status, WNOHANG) == 0 ) sleep(1);
if ( WIFEXITED(status) ) printf("Child1: Child 2 exited with exit status %d.\n",
WEXITSTATUS(status));
else printf("Child 1: child has not terminated correctly.\n");
}
JN
} else {
/* then we're the parent process, "Parent" */
printf("Parent: fork() went ok. My child's PID is %d\n", childpid);
/* wait for the child to terminate and report about that */
wait(&status);
if ( WIFEXITED(status) ) printf("Parent: child has exited with status %d.\n",
WEXITSTATUS(status));
else printf("Parent: child has not terminated normally.\n");
}
return 0;
4.5
PRE-LAB QUESTIONS
1. How to write arithmetic multiplication operator in shell.
2. Write down the syntax for nested if statement.
12
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
LAB ASSIGNMENT
1. Write a shell script to count number of txt,c and shell programs present in current directory.
2. Write a shell script to count number of only files present in current directory.
4.7
POST-LAB QUESTIONS
1. What is means by relation operator , name any three relation operators present in shell.
2. What is meant by logic operator, and Explain about each operator.
JN
TU
or
ld
4.6
13
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 5
OBJECTIVE
Write a shell script that accepts a list of file names as its arguments, counts and reports the occurrence
of each word that is present in the first argument file on other argument files.
5.2
RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
5.3
PROGRAM LOGIC
Read list of file names and counts and report the occurrence of each word that is present in the first
argument file on other argument files using comm and grep commands.
5.4
DESCRIPTION / PROCEDURE
ld
5.1
INPUT:
sh prog5.sh
or
TU
5.6
LAB ASSIGNMENT
1. Write a script to reverse a given string.
2. Write a script to copy list of file into specified directory.
JN
5.5
5.7
POST-LAB QUESTIONS
1. What is difference between grep, egrep and fgrep commands
2. How to copy and paste lines in a vi-editor
3. What are different types of shell and how to move from one shell to other shell
14
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 6
OBJECTIVE
Write a shell script to list all of the directory files in a directory.
6.2
RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
6.3
PROGRAM LOGIC
1. Read a directory
2. Test given directory is directory file and exist using test options -d
3. If its directory and exist display all the sub directories and files to output stream
4. else display its not directory or not exists.
6.4
DESCRIPTION / PROCEDURE
echo Enter dir name
read dir
if [ -d $dir ]
then
echo Files in $dir are
ls $dir
else
echo Dir does not exist
fi
or
TU
OUTPUT:
Files in prasanna are
3.sh
4.sh
pp2.txt
INPUT:
sh Lp6.sh
Enter dir name
Presanna
ld
6.1
PRE-LAB QUESTIONS
1. Name few test commands present in unix.
2. Write down the syntax case statement
6.6
LAB ASSIGNMENT
1. Write a shell script to count number of words present in a file without using commands.
2. Write a menu driven shell script to execute a command as 1.for `ls` ,2 for grep and 3 for cat.
JN
6.5
6.7
POST-LAB QUESTIONS
1. What is the purpose of shift statement.
2. What the difference is between break and exit statement.
15
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 7
OBJECTIVE
Write a shell script to find factorial of a given number.
7.2
RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
7.3
PROFRAM LOGIC
1. Read a filename
2. take a variable i for count and execute expression in for loop ,increment the variable till loop ends, 3.
Display the factorial of given number to output stream.
7.4
DESCRIPTION / PROCEDURE
or
TU
echo Factorial
echo Enter number:
read n
fact=1
i=1
for((i=1;i<=n;i++))
do
fact=`expr $fact \* $i`
done
echo Factorial of $n is $fact
INPUT:
sh p7.sh
Factorial
Enter number:5
OUTPUT:
Factorial of 5 is 120
ld
7.1
PRE-LAB QUESTIONS
1. What is the use of while loop.
2. How you do command substitution in shell script.
3. How do you access command line arguments from within a shell script?
7.6
LAB ASSIGNMENT
1. Write a shell script for menu driven to execute a command like 1 for ls,2 for ls l, etc..
2. Write a shell script to find the sum of digits.
7.7
POST-LAB QUESTIONS
1. Illustrate the difference between while and for loop.
2. Which operator is used to check string is Null .
3. What is the name of the variable which counts number of arguments passed?
JN
7.5
16
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 8
OBJECTIVE
Write an awk script to count the number of lines in a file that do not contain vowels.
8.2
RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
8.3
PROGRAM LOGIC
1. Initialize total=0 in begin part
2. Using if command check each line to count the number of lines in a file that do not contain vowels in
body part.
3. Display total lines to output stream that do not contain vowels in end part
8.4
DESCRIPTION / PROCEDURE
BEGIN{ print Displaying number of lines in a file that do not contain vowels
total=0}
{if($0!~/[aeiouAEIOU]/)
total=total + 1}
END{print "The total lines in a file that do not contain vowels:",total}
or
ld
8.1
INPUT:
awk prog8.awk lp1.sh
Displaying number of lines in a file that do not contain vowels
OUTPUT:
The total lines in a file that do not contain vowels:1
PRE-LAB QUESTIONS
1. Define awk? What are the features of awk script?
2. What is the syntax for awk script?
8.6
LAB ASSIGNMENT
1. Write a awk script to display between lines eg.2 to 8 lines to output stream.
2. Write a awk script to find sum of total salary of all employees of given file.
TU
8.5
POST-LAB QUESTIONS
1. Illustrate difference between awk and sed.
2. Write a awk command to search given pattern in file if found display to output entire line.
JN
8.7
17
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 9
OBJECTIVE
Write an awk script to find the number of characters, words and lines in a file.
9.2
RESOURCE/REQUIREMENTS
Linux operating system ,vi-editor, shell-interpreter
9.3
PROGRAM LOGIC
Write awk script to find the number of characters, words and lines in a file
9.4
DESCRIPTION / PROCEDURE
BEGIN{ print Displaying number of characters, words and lines in a file}
{word=words + NF}
{len = length($0)}
{charcount=charcount + len}
END{print The total number of characters, words and lines in a file is:
print("Words:\t",words)
print("Lines:\t",NR)
print("Chars:\t",len) }
INPUT:
awk prog9.awk lp5.sh
or
ld
9.1
PRE-LAB QUESTIONS
1. How input file is given to awk script?
2. What is the use of next, getline and exit control actions in awk
TU
9.5
OUTPUT:
The total number of characters, words and lines in a file is:
Words:12
Lines:3
Chars:39
LAB ASSIGNMENT
1. Write a awk script to convert lower case characters to upper case of given file.
9.7
POST-LAB QUESTIONS
1. Explain about associative arrays in awk.
JN
9.6
18
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 10
OBJECTIVE
Write a C program that makes a copy of a file using Systems calls
10.2
RESOURCE/REQUIREMENTS
Linux operating system, vi editor, shell interpreter
10.3
PROGRAM LOGIC
1. Read source filename, destination filename
2. Open given source filename
3. Read the content from file and write to destination file
4. Repeat step 3 till end of file reaches
5. close open files
10.4
DESCRIPTION / PROCEDURE
#include<stdio.h>
#include<unistd.h>
#include<sys/types.h>
#include<string.h>
void main() {
char src[10], dest[10], buff;
int fd,fd1;
printf("enter the source file name \n");
scanf("%s\n",src);
fd=open("src",O_RDONLY);
printf("enter the destination file name\n"
scanf("%s\n",dest);
fd1=open("dest",O_WRONLY|O_CREAT|O_TRUNC|S_IRUSR|S_IWUSR);
while(read(fd,&buff,1));
wirte(fd1,&buff,1);
printf("The copy of a file is successed");
close(fd);
close(fd1);
}
TU
or
ld
10.1
JN
INPUT:
cc prog10.c
./a.out
entr the source file name:
file1
enter the destination file name:
file2
OUTPUT:
The copy of a file is successes
10.5
PRE-LAB QUESTIONS
1. What is meant by file descriptor and user file descriptor starts from which number
10.6
LAB ASSIGNMENT
1. Write a c-program to count number lines in a file.
10.7
POST-LAB QUESTIONS
1. What are the file descriptors values of keyword, monitor, error.
2. What is the use of lseek() function
19
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 11
OBJECTIVE
Write a C Program to Implement the Unix command cat using system calls.
11.A.2
RESOURCE/REQUIREMENTS
Linux operating system, vi editor, shell interpreter
11. A.3
PROGRAM LOGIC
1. Open file which is input given by command line arguments
2. Read content from opened file
3. Display content to output stream
4. Repeat step 2 and step 3 till end of file reach.
11. A.4
DESCRIPTION / PROCEDURE
#include<fcntl.h>
#include<sys/stat.h>
#define BUFSIZE 1
int main(int argc, char **argv)
{
int fd1;
int n;
char buf;
fd1=open(argv[1],O_RDONLY);
printf("Displaying content of file\n");
while((n=read(fd1,&buf,1))>0)
{
printf("%c",buf);
/*
or
write(1,&buf,1); */
}
return (0);
}
TU
or
ld
11.A.1
INPUT:
cc prog11a.c unit1
OUTPUT:
Displays content of file
PRE-LAB QUESTIONS
1. What is the difference between open() and fopen()?
JN
11. A.5
11. A.6
LAB ASSIGNMENT
1. Write a c-program to count number words in a file.
11. A.7
POST-LAB QUESTIONS
1. What is the difference between read(), write() and scanf, printf respectively
11.B.1
OBJECTIVE
Write a C Program to Implement the Unix command ls using system calls
11. B.2
RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
20
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
PROGRAM LOGIC
1. Open a directory of given directory name
2. Scan directory and Read file and display filename to output stream
3. Repeat step 2 till eof directory reach.
11. B.4
DESCRIPTION / PROCEDURE
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <stdio.h>
#define FALSE 0
#define TRUE 1
extern int alphasort();
char pathname[MAXPATHLEN];
main() {
int count,i;
struct dirent **files;
int file_select();
if (getcwd(pathname) == NULL )
{ printf("Error getting pathn");
exit(0);
}
printf("Current Working Directory = %sn",pathname);
count = scandir(pathname, &files, file_select, alphasort);
if (count <= 0)
{ printf("No files in this directoryn");
exit(0);
}
printf("Number of files = %d \n",count);
for (i=1;i<count+1;++i)
printf("%s \n",files[i-1]->d_name);
}
int file_select(struct direct *entry)
{
if ((strcmp(entry->d_name, ".") == 0) ||(strcmp(entry->d_name, "..") == 0))
return (FALSE);
else
return (TRUE);
}
TU
or
ld
11. B.3
JN
INPUT:
Cc prog11c.c
OUTPUT:
Current Working Directory =/home/prasanna
Number of files:2
Lp1.sh
lp2.sh
11. B.5
PRE-LAB QUESTIONS
1. What is the difference between lseek() and seekdir();
11. B.6
LAB ASSIGNMENT
1. Write a c-program to reverse a file using lseek();
21
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
POST-LAB QUESTIONS
1. What is the difference between system call and library functions
11.C.1
OBJECTIVE
Write a C Program to Implement the Unix command mv using system calls.
11. C.2
RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
11. C.3
PROGRAM LOGIC
1. Open two files. Read and write mode respectively.
2. Using rename function move file content to another file
3. Remove source file using unlink() function.
11. C.4
DESCRIPTION / PROCEDURE
#include<fcntl.h>
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
int main(int argc, char **argv)
{
int fd1,fd2;
int n,count=0;
fd1=open(argv[1],O_RDONLY);
fd2=creat(argv[2],S_IWUSR);
rename(fd1,fd2);
unlink(argv[1]);
return (0);
}
or
ld
11. B.7
INPUT:
cc mv.c file1 file2
TU
OUTPUT:
# creates file2 and copies the content of file1 to file2 and removes file1
PRE-LAB QUESTIONS
1. What is the difference between cp and mv commands?
11. C.6
LAB ASSIGNMENT
1. Write a C program to move file content to another file without using functions rename and
unlink.
2. Write a C program to copy content from source to destination file
11. C.7
POST-LAB QUESTIONS
1. What is difference between file descriptor and file pointer.
JN
11. C.5
22
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 12
OBJECTIVE
Write a C program that takes one or more file or directory names as command line input and reports
the following information on the file.
1. file type
2. number of links
3. read, write and execute permissions
4. time of last access
(Note: use /fstat system calls)
12.2
RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
12.3
PROGRAM LOGIC
1. Open a file using fopen() function
2. Read a file and display a file properties to output stream.
12.4
DESCRIPTION / PROCEDURE
#include<stdio.h>
main()
{
FILE *stream;
int buffer_character;
stream=fopen(test,r);
if(stream==(FILE*)0)
{
fprintf(stderr,Error opening file(printed to standard error)\n);
fclose(stream);
exit(1);
}}
if(fclose(stream))==EOF)
{
fprintf(stderr,Error closing stream.(printed to standard error)\n);
exit(1);
}
return();
}
TU
or
ld
12.1
PRE-LAB QUESTIONS
1. What is the difference between stat( ), fstat() and lstat() functions
JN
12.5
12.6
LAB ASSIGNMENT
1.Write a C program to count number of words, lines and characters using system calls
12.7
POST-LAB QUESTIONS
1.List properties of files and different types of files in Linux
23
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 13
13.1 OBJECTIVE
Write a C program to emulate the Unix ls l command.
13.2 RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
ld
TU
or
JN
24
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 14
14.1 OBJECTIVE
Write a C program to list for every file in a directory, its inode number and file name
14.2 RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
TU
or
ld
INPUT:
cc inode.c o inode
./inode
JN
OUTPUT:
FILE NAME
..
File2.c
.
File1.c
INODE NUMBER
4195164
4195164
4195164
25
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 15
15.1 OBJECTIVE
Write a C program that demonstrates redirection of standard output to a file. Ex: ls >f1.
/* freopen example: redirecting stdout */
ld
15.2 RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
INPUT:
cc 15.c o file1
./file1
or
OUTPUT:
This sentence is redirected to a file which is given at output
TU
JN
26
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 16
OBJECTIVE
Write a C program to create a child process and allow the parent to display parent and the child
to display child on the screen.
16.2
RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
16.3
PROGRAM LOGIC
1. Create child process using fork() function
2. If pid is not equal to zero execute parent block
3. else if pid is equal to zero execute child block
4. Display process id using getpid() and parent process id getppid() functions.
16.4
DESCRIPTION / PROCEDURE
#include <stdio.h>
main()
{
int pid;
printf("I'm the original process with PID %d and PPID %d.\n",getpid(),getppid());
pid=fork(); /* Duplicate. Child and parent continue from here.*/
if (pid!=0) {/* pid is non-zero, so I must be the parent */
printf("I'm the parent process with PID %d and PPID %d.\n",getpid(),getppid());
printf("My child's PID is %d.\n", pid);
}
else { /* pid is zero, so I must be the child. */
printf("I'm the child process with PID %d and PPID %d.\n",getpid(),getppid());
}
printf("PID %d terminates.\n",pid); /* Both processes execute this */
}
or
ld
16.1
TU
INPUT:
$cc fork.c
./a.out
JN
OUTPUT:
I'm the original process with PID 13292 and PPID 13273.
I'm the parent process with PID 13292 and PPID 13273.
My child's PID is 13293.
I'm the child process with PID 13293 and PPID 13292.
PID 13293 terminates. ... child terminates.
PID 13292 terminates. ... parent terminates.
16.5
PRE-LAB QUESTIONS
1. What are process identifiers in linux programming.
2. What is process, how you create new process?
16.6
LAB ASSIGNMENT
1. Write a program to find sum of odd numbers of parent process and sum of even numbers by
child process.
16.7
POST-LAB QUESTIONS
1. Illustrate difference between fork() and vfork() functions.
2. What are different process ids in Linux programming?
27
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 17
OBJECTIVE
Write a C program to create a Zombie process.
17.2
RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
17.3
PROGRAM LOGIC
1. Create a new child process using fork()
2. If pid is not equal to zero execute parent process and block the process 1000 seconds
3. If pid is equal to zero executer child process and terminate immediately
4. To see already terminated process i.e zombie process open new command prompt and type
ps -a it shows status z means that process is zombie process.
17.4
DESCRIPTION / PROCEDURE
#include <stdio.h>
main(){
int pid;
pid=fork(); /* Duplicate */
if (pid!=0) /* Branch based on return value from fork() */
{
while (1) /* never terminate, and never execute a wait() */
sleep(1000);
}
else
{
exit(42); /* Exit with a silly number */
}}
or
ld
17.1
INPUT:
$ cc prog17.c
./a.out& ... execute the program in the background.
[1] 13545
JN
TU
OUTPUT:
$ ps
PID TT STAT TIME COMMAND
13535 p2 s 0:00 -ksh(ksh) ...the shell
13545 p2 s 0:00 aombie.exe...the parent process
13536 p2 z 0:00 <defunct> ...the zombie child process
13537 p2 R 0:00 ps
$ kill 13545
... kill the parent process.
[1] Terminated zombie.exe
$ ps
... notice the zombie is gone now.
PID TT STAT TIME COMMAND
13535 p2 s 0:00 -csh(csh)
13548 p2 R 0:00 ps
17.5
PRE-LAB QUESTIONS
1. Define Zombie process.
2. How to know the status of process using commands.
17.6
LAB ASSIGNMENT
1. Write a C-program to create a child process to convert lower to upper case letters.
17.7
POST-LAB QUESTIONS
1. Explain different ways to terminate the process?
28
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 18
OBJECTIVE
Write a C program that illustrates how an orphan is created.
18.2
RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
18.3
PROGRAM LOGIC
1. Create a new child process using fork()
2. If pid is not equal to zero execute parent process block of code and terminates
3. If pid is equal to zero executer child process and block process for 5 seconds in mean time
parent process terminates.
4. To see child process is became orphan process open new command prompt and type
ps -a it shows status O means that process is orphan process.
18.4
DESCRIPTION / PROCEDURE
#include <stdio.h>
main()
{
int pid;
printf("I'm the original process with PID %d and PPID %d.\n",
getpid(),getppid());
pid=fork(); /* Duplicate. Child and parent continue from here.*/
if (pid!=0) /* Branch based on return value from fork() */
{ /* pid is non-zero, so I must be the parent */
printf("I'm the parent process with PID %d and PPID %d.\n",
getpid(),getppid());
printf("My child's PID is %d.\n", pid);
}
else { /* pid is zero, so I must be the child. */
sleep(5); /*Make sure that the parent terminates first. */
printf("I'm the child process with PID %d and PPID %d.\n",
getpid(),getppid());
}
printf("PID %d terminates.\n",pid); /* Both processes execute this */}
TU
or
ld
18.1
INPUT:
$cc prog18.c
./a.out
JN
OUTPUT:
I'm the original process with PID 13364 and PPID 13346.
I'm the parent process with PID 13364 and PPID 13346.
PID 13364 terminates.
I'm the child process with PID 13365 and PPID 1. ...orphaned!
PID 13365 terminates. ... child terminates.
18.5
PRE-LAB QUESTIONS
1. Illustrate difference between zombie process and orphan process.
18.6
LAB ASSIGNMENT
1. Write a C program to find given number is Armstrong number or not by parent process.
18.7
POST-LAB QUESTIONS
1. What is the child parent process id if child process status is orphan process?
29
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 19
19.1 OBJECTIVE
Write a C program that illustrates how to execute two commands concurrently with a command
pipe. Eg. ls-l|sort.
19.2 RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
JN
TU
or
ld
30
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
or
JN
TU
}
if(S_IRUSR & b.st_mode)
printf(" r");
else
printf(" -");
if(S_IWUSR & b.st_mode)
printf(" w");
else
printf(" -");
if(S_IXUSR & b.st_mode)
printf(" x");
else
printf(" -");
if(S_IRGRP & b.st_mode)
printf(" r");
else
printf(" -");
if(S_IWGRP & b.st_mode)
printf(" w");
else
printf(" -");
if(S_IXGRP & b.st_mode)
printf(" x");
else
printf(" -");
if(S_IROTH & b.st_mode)
printf(" r");
else
printf(" -");
if(S_IWOTH & b.st_mode)
printf(" w");
else
printf(" -");
if(S_IXOTH & b.st_mode)
printf(" x");
else
printf(" -");
printf("%3d ",b.st_nlink);
printf("%4d ",b.st_uid);
printf("%4d ",b.st_gid);
printf("%6d ",b.st_size);
printf("%9ld",b.st_ctime);
printf(" %s\n",p->d_name);
}
}
ld
break;
case S_IFIFO: printf(" p");
break;
INPUT:
vi 19.c
cc 19.c
./a.out
31
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
ld
OUTPUT:
- r w - r w - r - - 1 500 500 1506 1380610351 19.c
- r w - r w - r - - 1 500 500 0 1380523478 2
- r w - r w - r - - 1 500 500 0 1380523478 3
- r w x r w x r - x 1 500 500 6038 1380610357 a.out
- r w - r w - r - - 1 500 500 0 1380523478 1
- r w - r w - r - - 1 500 500 421 1380524812 12.c
- r w - r w - r - - 1 500 500 0 1380523478 4
d r w x - - - - - - 15 500 500 4096 1380609957 ..
d r w x r w x r - x 2 500 500 4096 1380610357 .
- r w - r w - r - - 1 500 500 347 1380523684 13.c
JN
TU
or
32
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 20
20.1 OBJECTIVE
Write a C program in which a parent writes a message to a pipe and the child reads the message.
20.2 RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
TU
or
ld
INPUT:
cc prog20.c
./a.out
JN
OUTPUT:
CHILD: Writing to the pipe
CHILD:Exiting
PARENT:reading from the pipe
PARENT:Received Data is : Hello World, I am child
www.alljntuworld.in
JNTU World
EXPERIMENT 21
21.1 OBJECTIVE
Write a C program (sender.c) to create a message queue with read and write permissions to write 3
messages to it with different priority numbers.
21.2 RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
ld
JN
TU
or
34
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
OUTPUT:
Enter the message to send: hi
Enter the message to send: hello, how are you
Enter the message to send: bye
21.5 PRE-LAB QUESTIONS
1. What is the purpose of msgget(),msgsnd(),msgrcv().
2. What is structure of message queue.
JN
TU
or
ld
35
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 22
22.1 OBJECTIVE
Write a C program (receiver.c) that receives the messages (from the above message queue as
specified in (22)) and displays them.
22.2 RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
JN
TU
or
ld
INPUT:
cc prog22.c
./a.out
36
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
OUTPUT:
Message received from sender is: hi
Message received from sender is: hello, how are you
Message received from sender is: bye
22.5 PRE-LAB QUESTIONS
1. How many message queues can create in linux programming and what are limitations for
message queues?
ld
JN
TU
or
37
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 23
23.1 OBJECTIVE
Write a C program to allow cooperating processes to lock a resource for exclusive use, using
a) Semaphores
b) flock or lockf system calls.
JN
TU
or
ld
23.2 RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
www.alljntuworld.in
JNTU World
EXPERIMENT 24
24.1 OBJECTIVE
Write a C program that illustrates suspending and resuming processes using signals.
24.2 RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
or
#include<sys/types.h>
#include<signal.h>
//suspend the process(same as hitting crtl+z)
kill(pid,SIGSTOP);
ld
JN
TU
39
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 25
25.1 OBJECTIVE
Write a C program that implements a producer-consumer system with two processes. (using
Semaphores).
25.2 RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
25.3 PROGRAM LOGIC
or
ld
JN
TU
JNTU World
or
semop(sem_set_id,&sem_op,1);
printf(producer:%d\n,i);
fflush(stdout);
}
break;
default:
for(i=0;i<num_loops;i++)
{
printf(consumer:%d\n,i);
fflush(stdout);
sem_op.sem_num=0;
sem_op.sem_op=1;
sem_op.sem_flg=0;
semop(sem_set_id,&sem_op,1);
if(rand()>3*(rano_max14));
{
delay.tv_sec=0;
delay.tv_nsec=10;
nanosleep(&delay,null);
}
}
break;
}
return 0;
}
ld
www.alljntuworld.in
TU
JN
41
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 26
26.1 OBJECTIVE
Write client and server programs (using c) for interaction between server and client processes
using Unix Domain sockets.
26.2 RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
or
ld
TU
close(connection_fd);
return 0;
}
JN
int main(void)
{
struct sockaddr_un address;
int socket_fd, connection_fd;
socklen_t address_length;
pid_t child;
www.alljntuworld.in
JNTU World
unlink("./demo_socket");
/* start with a clean address structure */
memset(&address, 0, sizeof(struct sockaddr_un));
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
or
if(listen(socket_fd, 5) != 0)
{
printf("listen() failed\n");
return 1;
}
ld
if(bind(socket_fd,
(struct sockaddr *) &address,
sizeof(struct sockaddr_un)) != 0)
{
printf("bind() failed\n");
return 1;
}
while((connection_fd = accept(socket_fd,
(struct sockaddr *) &address,
&address_length)) > -1)
{
child = fork();
if(child == 0)
{
/* now inside newly created connection handling process */
return connection_handler(connection_fd);
}
TU
close(socket_fd);
unlink("./demo_socket");
return 0;
}
JN
Client.c
#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
int main(void)
{
struct sockaddr_un address;
int socket_fd, nbytes;
char buffer[256];
43
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
ld
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");
or
if(connect(socket_fd,
(struct sockaddr *) &address,
sizeof(struct sockaddr_un)) != 0)
{
printf("connect() failed\n");
return 1;
}
TU
1. Write a program to design a TCP client server application which takes IP address, Port number and
string to be echoed as command line inputs in client application and implements echo service.
JN
1. Explain about IPV6 socket address structure and compare it with IPV4 and unix socket address structures.
44
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 27
27.1 OBJECTIVE
Write client and server programs (using c) for interaction between server and client processes
using Internet Domain sockets.
27.2 RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
TU
Server.c
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <time.h>
or
ld
JN
char sendBuff[1025];
time_t ticks;
www.alljntuworld.in
JNTU World
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL);
ticks = time(NULL);
snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks));
write(connfd, sendBuff, strlen(sendBuff));
close(connfd);
sleep(1);
ld
}
}
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
or
Client.c
TU
if(argc != 2)
{
printf("\n Usage: %s <ip of server> \n",argv[0]);
return 1;
}
JN
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
46
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
ld
return 0;
}
if(n < 0)
{
printf("\n Read error \n");
}
or
TU
JN
47
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT 28
28.1 OBJECTIVE
Implement shared memory form of IPC
28.2 RESOURCE/REQUIREMENTS
Linux operating system, vi editor, c-compiler
ld
JN
TU
or
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define SHMSZ 27
main()
{ char c;
int shmid;
key_t key;
char *shm, *s;
key = 5678;
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0)
{
perror("shmget");
exit(1); }
if ((shm = shmat(shmid, NULL, 0)) == (char *) -1)
{
perror("shmat");
exit(1); }
s = shm;
for (c = 'a'; c <= 'z'; c++)
*s++ = c;
*s = NULL;
while (*shm Linux operating system, vi editor, c-compiler= '*')
sleep(1);
exit(0);
}
shm_client.c
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#define SHMSZ 27
main()
{
int shmid;
key_t key;
char *shm, *s;
key = 5678;
48
www.alljntuworld.in
JNTU World
exit(0);
}
or
ld
*shm = '*';
JN
TU
49
Downloaded From JNTU World (http://www.alljntuworld.in)
JNTU World
or
ld
www.alljntuworld.in
JN
TU
50
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
ld
To do the assignment, you first and foremost need some knowledge about the world of credit . You can
acquire such knowledge in a number of ways.
1. Knowledge Engineering. Find a loan officer who is willing to talk. Interview her and try to
represent her knowledge in the form of production rules.
or
2. Books. Find some training manuals for loan officers or perhaps a suitable textbook on finance.
Translate this knowledge from text form to production rule form.
3. Common sense. Imagine yourself as a loan officer and make up reasonable rules which can be
used to judge the credit worthiness of a loan applicant.
4. Case histories. Find records of actual cases where competent loan officers correctly judged when
not to, approve a loan application.
Actual historical credit data is not always easy to come by because of confidentiality rules. Here is one
such dataset (original) Excel spreadsheet version of the German credit data (download from web).
In spite of the fact that the data is German, you should probably make use of it for this assignment,
(Unless you really can consult a real loan officer !)
A few notes on the German dataset :
DM stands for Deutsche Mark, the unit of currency, worth about 90 cents Canadian (but looks
and acts like a quarter).
TU
Owns_telephone. German phone rates are much higher than in Canada so fewer people own
telephones.
Foreign_worker. There are millions of these in Germany (many from Turkey). It is very hard to
get German citizenship if you were not born of German parents.
There are 20 attributes used in judging a loan applicant. The goal is the classify the applicant into
one of two categories, good or bad.
JN
51
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-1
1.1 OBJECTIVE:
List all the categorical (or nominal) attributes and the real-valued attributes separately.
1.2 RESOURCES:
Weka mining tool
ld
1.3 PROCEDURE:
1) Open the Weka GUI Chooser.
2) Select EXPLORER present in Applications.
3) Select Preprocess Tab.
4) Go to OPEN file and browse the file that is already stored in the system bank.csv.
JN
TU
1.4 OUTPUT:
or
5) Clicking on any attribute in the left panel will show the basic statistics on that selected attribute.
52
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-2
2.1 OBJECTIVE:
Which attributes do you think might be crucial in making the credit assessment? Come up with some
simple rules in plain English using your selected attributes.
2.2 RESOURCES:
Weka mining tool
or
ld
2.3 THEORY:
Association rule mining is defined as: Let be a set of n binary attributes called items. Let be a set of
transactions called the database. Each transaction in D has a unique transaction ID and contains a subset
of the items in I. A rule is defined as an implication of the form X=>Y where X,Y C I and X Y= . The
sets of items (for short itemsets) X and Y are called antecedent (left hand side or LHS) and consequent
(right hand side or RHS) of the rule respectively.
To illustrate the concepts, we use a small example from the supermarket domain.
The set of items is I = {milk,bread,butter,beer} and a small database containing the items (1 codes
presence and 0 absence of an item in a transaction) is shown in the table to the right. An example rule for
the supermarket could be meaning that if milk and bread is bought, customers also buy butter.
Note: this example is extremely small. In practical applications, a rule needs a support of several hundred
transactions before it can be considered statistically significant, and datasets often contain thousands or
millions of transactions.
TU
To select interesting rules from the set of all possible rules, constraints on various measures of
significance and interest can be used. The best known constraints are minimum thresholds on support and
confidence. The support supp(X) of an itemset X is defined as the proportion of transactions in the data set
which contain the itemset. In the example database, the itemset {milk, bread} has a support of 2 / 5 = 0.4
since it occurs in 40% of all transactions (2 out of 5 transactions).
The confidence of a rule is defined. For example, the rule has a confidence of 0.2 / 0.4 = 0.5 in the
database, which means that for 50% of the transactions containing milk and bread the rule is correct.
Confidence can be interpreted as an estimate of the probability P(Y | X), the probability of finding the
RHS of the rule in transactions under the condition that these transactions also contain the LHS .
JN
ALGORITHM:
Association rule mining is to find out association rules that satisfy the predefined minimum support and
confidence from a given database. The problem is usually decomposed into two sub problems. One is to
find those itemsets whose occurrences exceed a predefined threshold in the database; those itemsets are
called frequent or large itemsets. The second problem is to generate association rules from those large
itemsets with the constraints of minimal confidence.
Suppose one of the large itemsets is Lk, Lk = {I1, I2, , Ik}, association rules with this itemsets are
generated in the following way: the first rule is {I1, I2, , Ik1} and {Ik}, by checking the confidence
this rule can be determined as interesting or not. Then other rule are generated by deleting the last items in
the antecedent and inserting it to the consequent, further the confidences of the new rules are checked to
determine the interestingness of them. Those processes iterated until the antecedent becomes empty.
53
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
Since the second subproblem is quite straight forward, most of the researches focus on the first
subproblem. The Apriori algorithm finds the frequent sets L In Database D.
Find frequent set Lk 1.
Join Step.
o Ck is generated by joining Lk 1with itself
Prune Step.
ld
or
Apriori Pseudocode
Apriori (T,)
while L(k1)
C(k)<Generate( Lk 1)
for transactions t T
C(t)Subset(Ck,t)
K<2
TU
JN
return L(k) k
2.4 PROCEDURE:
www.alljntuworld.in
JNTU World
JN
TU
or
ld
2.5 OUTPUT:
55
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-3
3.1 OBJECTIVE:
*What attributes do you think might be crucial in making the bank assessment?
3.2 RESOURCES:
ld
or
4) Go to OPEN file and browse the file that is already stored in the system bank.csv.
5) Clicking on any attribute in the left panel will show the basic statistics on that selected attribute.
JN
TU
3.4 OUTPUT:
56
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-4
4.1 OBJECTIVE:
One type of model that you can create is a decision tree. Train a decision tree using the complete dataset
as the training data. Report the model obtained after training.
4.2 RESOURCES:
Weka mining tool
ld
4.3 THEORY:
or
Classification is a data mining function that assigns items in a collection to target categories or classes.
The goal of classification is to accurately predict the target class for each case in the data. For example, a
classification model could be used to identify loan applicants as low, medium, or high credit risks. A
classification task begins with a data set in which the class assignments are known. For example, a
classification model that predicts credit risk could be developed based on observed data for many loan
applicants over a period of time.
In addition to the historical credit rating, the data might track employment history, home ownership or
rental, years of residence, number and type of investments, and so on. Credit rating would be the target,
the other attributes would be the predictors, and the data for each customer would constitute a case.
TU
Classifications are discrete and do not imply order. Continuous, floating point values would indicate a
numerical, rather than a categorical, target. A predictive model with a numerical target uses a regression
algorithm, not a classification algorithm. The simplest type of classification problem is binary
classification. In binary classification, the target attribute has only two possible values: for example, high
credit rating or low credit rating. Multiclass targets have more than two values: for example, low,
medium, high, or unknown credit rating. In the model build (training) process, a classification algorithm
finds relationships between the values of the predictors and the values of the target. Different
classification algorithms use different techniques for finding relationships. These relationships are
summarized in a model, which can then be applied to a different data set in which the class assignments
are unknown.
JN
Classification models are tested by comparing the predicted values to known target values in a set of test
data. The historical data for a classification project is typically divided into two data sets: one for building
the model; the other for testing the model. Scoring a classification model results in class assignments and
probabilities for each case. For example, a model that classifies customers as low, medium, or high value
would also predict the probability of each classification for each customer. Classification has many
applications in customer segmentation, business modeling, marketing, credit analysis, and biomedical and
drug response modeling.
Different Classification Algorithms: Oracle Data Mining provides the following algorithms for
classification:
Decision Tree - Decision trees automatically generate rules, which are conditional statements that
reveal the logic used to build the tree.
Naive Bayes - Naive Bayes uses Bayes' Theorem, a formula that calculates a probability by counting
the frequency of values and combinations of values in the historical data.
57
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
4.4 PROCEDURE:
1) Open Weka GUI Chooser.
2) Select EXPLORER present in Applications.
3) Select Preprocess Tab.
4) Go to OPEN file and browse the file that is already stored in the system bank.csv.
5) Go to Classify tab.
clicking the button choose and select tree j48
7) Select Test options Use training set
8) if need select attribute.
9) Click Start.
10) Now we can see the output details in the Classifier output.
JN
TU
4.5 OUTPUT:
or
11) Right click on the result list and select visualize treeoption .
ld
6) Here the c4.5 algorithm has been chosen which is entitled as j48 in Java and can be selected by
58
Downloaded From JNTU World (http://www.alljntuworld.in)
JNTU World
or
ld
www.alljntuworld.in
JN
TU
59
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-5
5.1 OBJECTIVE:
Suppose you use your above model trained on the complete dataset, and classify credit good/bad for each
of the examples in the dataset. What % of examples can you classify correctly? (This is also called
testing on the training set) Why do you think you cannot get 100 % training accuracy?
5.2 RESOURCES:
ld
or
Naive Bayes classifier assumes that the presence (or absence) of a particular feature of a class is unrelated
to the presence (or absence) of any other feature. For example, a fruit may be considered to be an apple if
it is red, round, and about 4" in diameter. Even though these features depend on the existence of the other
features, a naive Bayes classifier considers all of these properties to independently contribute to the
probability that this fruit is an apple.
An advantage of the naive Bayes classifier is that it requires a small amount of training data to estimate
the parameters (means and variances of the variables) necessary for classification. Because independent
variables are assumed, only the variances of the variables for each class need to be determined and not the
entire covariance matrix The naive Bayes probabilistic model :
The probability model for a classifier is a conditional model
TU
P(C|F1 .................Fn) over a dependent class variable C with a small number of outcomes or classes,
conditional on several feature variables F1 through Fn. The problem is that if the number of features n is
large or when a feature can take on a large number of values, then basing such a model on probability
tables is infeasible. We therefore reformulate the model to make it more tractable.
Using Bayes theorem, we write
P(C|F1...............Fn)=[{p(C)p(F1..................Fn|C)}/p(F1,........Fn)]
JN
In practice we are only interested in the numerator of that fraction, since the denominator does not depend
on C and the values of the features Fi are given, so that the denominator is effectively constant. The
numerator is equivalent to the joint probability model p(C,F1........Fn) which can be rewritten as follows,
using repeated applications of the definition of conditional probability:
p(C,F1........Fn) =p(C) p(F1............Fn|C) =p(C)p(F1|C) p(F2.........Fn|C,F1,F2)
=p(C)p(F1|C) p(F2|C,F1)p(F3.........Fn|C,F1,F2)
= p(C)p(F1|C) p(F2|C,F1)p(F3.........Fn|C,F1,F2)......p(Fn|C,F1,F2,F3.........Fn1)
Now the "naive" conditional independence assumptions come into play: assume that each feature Fi is
conditionally independent of every other feature Fj for ji .
60
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
This means that p(Fi|C,Fj)=p(Fi|C) and so the joint model can be expressed as
p(C,F1,.......Fn)=p(C)p(F1|C)p(F2|C)...........= p(C) p(Fi|C)
This means that under the above independence assumptions, the conditional distribution over the class
variable C can be expressed like this:
p(C|F1..........Fn)= p(C) p(Fi|C) Z
where Z is a scaling factor dependent only on F1.........Fn, i.e., a constant if the values of the feature
variables are known.
or
ld
Models of this form are much more manageable, since they factor into a so called class prior p(C) and
independent probability distributions p(Fi|C). If there are k classes and if a model for each p(Fi|C=c) can
be expressed in terms of r parameters, then the corresponding naive Bayes model has (k 1) + n r k
parameters. In practice, often k = 2 (binary classification) and r = 1 (Bernoulli variables as features) are
common, and so the total number of parameters of the naive Bayes model is 2n + 1, where n is the
number of binary features used for prediction
TU
JN
www.alljntuworld.in
JNTU World
k=1
P(X/Ci) = P(x1/Ci) * P(x2/Ci) ** P(xn/ Ci)
5.4 PROCEDURE:
1) Given the Bank database for mining.
2) Use the Weka GUI Chooser.
3) Select EXPLORER present in Applications.
ld
or
46
Kappa statistic
0.845
92.3333 %
7.6667 %
0.1389
0.2636
27.9979 %
TU
52.9137 %
600
JN
FP Rate
Precision
Recall
F-Measure
ROC Area
Class
0.894
0.052
0.935
0.894
0.914
0.936
YES
0.948
0.106
0.914
0.948
0.931
0.936
NO
0.923
0.936
0.923
Weighted Avg.
0.081
0.924
0.923
62
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
245
29
17
309
JN
TU
or
ld
<-- classified as
a = YES , b = NO
63
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-6
6.1 OBJECTIVE:
*Find out the correctly classified instances, root mean squared error, kappa statistics, and mean absolute
error for weather data set?
6.2 RESOURCES:
ld
or
Naive Bayes classifier assumes that the presence (or absence) of a particular feature of a class is unrelated
to the presence (or absence) of any other feature. For example, a fruit may be considered to be an apple if
it is red, round, and about 4" in diameter. Even though these features depend on the existence of the other
features, a naive Bayes classifier considers all of these properties to independently contribute to the
probability that this fruit is an apple.
An advantage of the naive Bayes classifier is that it requires a small amount of training data to estimate
the parameters (means and variances of the variables) necessary for classification. Because independent
variables are assumed, only the variances of the variables for each class need to be determined and not the
entire covariance matrix The naive Bayes probabilistic model :
The probability model for a classifier is a conditional model
P(C|F1 .................Fn) over a dependent class variable C with a small number of outcomes or classes,
conditional on several feature variables F1 through Fn. The problem is that if the number of features n is
large or when a feature can take on a large number of values, then basing such a model on probability
tables is infeasible. We therefore reformulate the model to make it more tractable.
TU
P(C|F1...............Fn)=[{p(C)p(F1..................Fn|C)}/p(F1,........Fn)]
JN
In practice we are only interested in the numerator of that fraction, since the denominator does not depend
on C and the values of the features Fi are given, so that the denominator is effectively constant. The
numerator is equivalent to the joint probability model p(C,F1........Fn) which can be rewritten as follows,
using repeated applications of the definition of conditional probability:
p(C,F1........Fn) =p(C) p(F1............Fn|C) =p(C)p(F1|C) p(F2.........Fn|C,F1,F2)
=p(C)p(F1|C) p(F2|C,F1)p(F3.........Fn|C,F1,F2)
= p(C)p(F1|C) p(F2|C,F1)p(F3.........Fn|C,F1,F2)......p(Fn|C,F1,F2,F3.........Fn1)
Now the "naive" conditional independence assumptions come into play: assume that each feature Fi is
conditionally independent of every other feature Fj for ji .
64
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
This means that p(Fi|C,Fj)=p(Fi|C) and so the joint model can be expressed as
p(C,F1,.......Fn)=p(C)p(F1|C)p(F2|C)...........= p(C) p(Fi|C)
This means that under the above independence assumptions, the conditional distribution over the class
variable C can be expressed like this:
p(C|F1..........Fn)= p(C) p(Fi|C) Z
where Z is a scaling factor dependent only on F1.........Fn, i.e., a constant if the values of the feature
variables are known.
or
ld
Models of this form are much more manageable, since they factor into a so called class prior p(C) and
independent probability distributions p(Fi|C). If there are k classes and if a model for each p(Fi|C=c) can
be expressed in terms of r parameters, then the corresponding naive Bayes model has (k 1) + n r k
parameters. In practice, often k = 2 (binary classification) and r = 1 (Bernoulli variables as features) are
common, and so the total number of parameters of the naive Bayes model is 2n + 1, where n is the
number of binary features used for prediction
TU
JN
www.alljntuworld.in
JNTU World
k=1
P(X/Ci) = P(x1/Ci) * P(x2/Ci) ** P(xn/ Ci)
6.4 PROCEDURE:
1) Given the Bank database for mining.
2) Use the Weka GUI Chooser.
3) Select EXPLORER present in Applications.
ld
or
46
Kappa statistic
0.845
92.3333 %
7.6667 %
0.1389
0.2636
27.9979 %
TU
52.9137 %
600
JN
FP Rate
Precision
Recall
F-Measure
ROC Area
Class
0.894
0.052
0.935
0.894
0.914
0.936
YES
0.948
0.106
0.914
0.948
0.931
0.936
NO
0.923
0.936
0.923
Weighted Avg.
0.081
0.924
0.923
66
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
245
29
17
309
JN
TU
or
ld
<-- classified as
a = YES , b = NO
67
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-7
7.1 OBJECTIVE:
Is testing on the training set as you did above a good idea? Why or Why not?
7.2 RESOURCES:
7.3 PROCEDURE:
1) In Test options, select the Supplied test set radio button
ld
Click Set
3)
Choose the file which contains records that were not in the training set we used to create the model.
4)
5)
Click Start(WEKA will run this test data set through the model we already created. )
Compare the output results with that of the 4th experiment
7.4 OUTPUT:
or
2)
This can be experienced by the different problem solutions while doing practice.
The important numbers to focus on here are the numbers next to the "Correctly Classified Instances"
(92.3 percent) and the "Incorrectly Classified Instances" (7.6 percent). Other important numbers are in the
"ROC Area" column, in the first row (the 0.936); Finally, in the "Confusion Matrix," it shows the number
of false positives and false negatives. The false positives are 29, and the false negatives are 17 in this
matrix.
TU
Based on our accuracy rate of 92.3 percent, we say that upon initial analysis, this is a good model.
One final step to validating our classification tree, which is to run our test set through the model and
ensure that accuracy of the model
JN
Comparing the "Correctly Classified Instances" from this test set with the "Correctly Classified Instances"
from the training set, we see the accuracy of the model, which indicates that the model will not break
down with unknown data, or when future data is applied to it.
68
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-8
8.1 OBJECTIVE:
One approach for solving the problem encountered in the previous question is using cross-validation?
Describe what is cross -validation briefly. Train a Decision Tree again using cross - validation and report
your results. Does your accuracy increase/decrease? Why?
ld
8.2 RESOURCES:
Weka mining tool
8.3 THEORY:
or
Decision tree learning, used in data mining and machine learning, uses a decision tree as a predictive
model which maps observations about an item to conclusions about the item's target value In these tree
structures, leaves represent classifications and branches represent conjunctions of features that lead to
those classifications. In decision analysis, a decision tree can be used to visually and explicitly represent
decisions and decision making. In data mining, a decision tree describes data but not decisions; rather the
resulting classification tree can be an input for decision making. This page deals with decision trees in
data mining.
Decision tree learning is a common method used in data mining. The goal is to create a model that
predicts the value of a target variable based on several input variables. Each interior node corresponds to
one of the input variables; there are edges to children for each of the possible values of that input variable.
Each leaf represents a value of the target variable given the values of the input variables represented by
the path from the root to the leaf.
TU
A tree can be "learned" by splitting the source set into subsets based on an attribute value test. This
process is repeated on each derived subset in a recursive manner called recursive partitioning. The
recursion is completed when the subset at a node all has the same value of the target variable, or when
splitting no longer adds value to the predictions. In data mining, trees can be described also as the
combination of mathematical and computational techniques to aid the description, categorization and
generalization of a given set of data.
Data comes in records of the form:
JN
The dependent variable, Y, is the target variable that we are trying to understand, classify or generalize.
The vector x is comprised of the input variables, x1, x2, x3 etc., that are used for that task.
8.4 PROCEDURE:
www.alljntuworld.in
JNTU World
6) Go to Classify tab.
7) Choose Classifier Tree
8) Select J48
9) Select Test options Cross-validation.
10) Set Folds Ex:10
11) if need select attribute.
12) now Start weka.
13)now we can see the output details in the Classifier output.
15) check whether the accuracy increased or decreased?
JN
TU
or
8.5 OUTPUT:
ld
539
89.8333 %
61
10.1667 %
0.7942
0.167
0.305
70
www.alljntuworld.in
JNTU World
33.6511 %
61.2344 %
600
FP Rate
Precision
Recall
F-Measure
0.861
0.071
0.911
0.861
0.886
0.883
YES
0.929
0.139
0.889
0.929
0.909
0.883
NO
0.898
0.898
0.883
JN
TU
23 303 | b = NO
0.899
0.108
Class
or
Weighted Avg.
0.898
ROC Area
ld
TP Rate
71
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-9
9.1 OBJECTIVE
ld
Check to see if the data shows a bias against "foreign workers" (attribute 20), or "personal -status"
(attribute 9). One way to do this (perhaps rather simple minded) is to remove these attributes from the
dataset and see if the decision tree created in those cases is significantly different from the full dataset
case which you have already done. To remove an attribute you can use the preprocess tab in Weka's GUI
Explorer. Did removing these attributes have any significant effect? Discuss.
9.2 RESOURCES:
Weka mining tool
or
9.3 PROCEDURE:
5) Go to OPEN file and browse the file that is already stored in the system bank.csv.
6) In the "Filter" panel, click on the "Choose" button. This will show a popup window with list available
filters.
7) Select weka.filters.unsupervised.attribute.Remove
8) Next, click on text box immediately to the right of the "Choose" button
9) In the resulting dialog box enter the index of the attribute to be filtered out (Make sure that the "invert
Selection" option is set to false )
TU
10) Then click "OK" . Now, in the filter box you will see "Remove -R 1"
11) Click the "Apply" button to apply this filter to the data. This will remove the "id" attribute and create
a new working relation
12) To save the new working relation as an ARFF file, click on save button in the top panel.
13) Go to OPEN file and browse the file that is newly saved (attribute deleted file)
14) Go to Classify tab.
JN
20) Now we can see the output details in the Classifier output.
21) Right click on the result list and select visualize tree option .
22) Compare the output results with that of the 4th experiment
www.alljntuworld.in
JNTU World
JN
TU
or
ld
9.4 OUTPUT:
73
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-10
10.1 OBJECTIVE:
*Load the weather.arff dataset in Weka and run the ID3 classification algorithm. What problem do you
have and what is the solution?
10.2 RESOURCES:
10.3 PROCEDURE:
1) In Test options, select the Supplied test set radio button
2) click Set
ld
3) Choose the file which contains records that were not in the training set we used to create the model.
or
4) Click Start (WEKA will run this test data set through the model we already created. )
5) Compare the output results with that of the 4th experiment
10.4 OUTPUT:
This can be experienced by the different problem solutions while doing practice.
The important numbers to focus on here are the numbers next to the "Correctly Classified Instances"
(92.3 percent) and the "Incorrectly Classified Instances" (7.6 percent). Other important numbers are in the
"ROC Area" column, in the first row (the 0.936); finally, in the "Confusion Matrix," it shows the number
of false positives and false negatives. The false positives are 29, and the false negatives are 17 in this
matrix.
TU
Based on our accuracy rate of 92.3 percent, we say that upon initial analysis, this is a good model.
One final step to validating our classification tree, which is to run our test set through the model and
ensure that accuracy of the model
JN
Comparing the "Correctly Classified Instances" from this test set with the "Correctly Classified Instances"
from the training set, we see the accuracy of the model, which indicates that the model will not break
down with unknown data, or when future data is applied to it.
74
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-11
11.1 OBJECTIVE:
ld
Another question might be, do you really need to input so many attributes to get good results? Maybe
only a few would do. For example, you could try just having attributes 2, 3, 5, 7, 10, 17 (and 21, the class
attribute (naturally)). Try out some combinations. (You had removed two attributes in problem 7.
Remember to reload the arff data file to get all the attributes initially before you start selecting the ones
you want).
11.2 RESOURCES:
11.3 PROCEDURE:
1) Given the Bank database for mining.
2) Use the Weka GUI Chooser.
or
5) Go to OPEN file and browse the file that is already stored in the system bank.csv.
6) Select some of the attributes from attributes list which are to be removed. With this step only
the attributes necessary for classification are left in the attributes panel.
7) The go to Classify tab.
8) Choose Classifier Tree
TU
9) Select j48
10) Select Test options Use training set
11) If need select attribute.
12) Now start Weka.
13) Now we can see the output details in the Classifier output.
14) Right click on the result list and select visualize tree option.
15) Compare the output results with that of the 4th experiment.
JN
75
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
JN
TU
or
ld
11.4 OUTPUT:
76
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-12
12.1 OBJECTIVE:
ld
Sometimes, the cost of rejecting an applicant who actually has a good credit (case 1) might be higher than
accepting an applicant who has bad credit (case 2). Instead of counting the misclassifications equally in
both cases, give a higher cost to the first case (say cost 5) and lower cost to the second case. You can do
this by using a cost matrix in Weka. Train your Decision Tree again and report the Decision Tree and
cross -validation results. Are they significantly different from results obtained in problem 6 (using equal
cost)?
12.2 RESOURCES:
Weka mining tool
or
12.3 PROCEDURE:
6) Go to Classify tab.
7) Choose Classifier Tree
8) Select j48
5) Go to OPEN file and browse the file that is already stored in the system bank.csv.
TU
12) Set the matrix values and click on resize. Then close the window.
13) Click Ok
JN
20) Now we can see the output details in the Classifier output.
21) Compare results of 15th and 20th steps.
77
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
JN
TU
or
ld
12.4 OUTPUT:
78
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-13
13.1 OBJECTIVE:
Do you think it is a good idea to prefer simple decision trees instead of having long complex decision
trees? How does the complexity of a Decision Tree relate to the bias of the model?
13.2 RESOURCES:
Weka mining tool
ld
13.3 PROCEDURE:
JN
TU
or
This will be based on the attribute set, and the requirement of relationship among attribute we want to
study. This can be viewed based on the database and user requirement.
79
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-14
14.1 OBJECTIVE:
*Run the J48 and 1Bk classifiers using-the cross-validation strategy with various fold levels. Compare the
accuracy results. Hold out strategy with three percentage levels. Compare the accuracy results.
14.2 RESOURCES:
14.3 THEORY:
Reduced-error pruning
Each node of the (over-fit) tree is examined for pruning
ld
Assigning the pruned node the most common classification of the training instances attached to
that node
or
A node is pruned (removed) only if the resulting pruned tree performs no worse than the original over
the validation set
Always select a node whose removal most increases the DT accuracy over the validation set
Stop when further pruning decreases the DT accuracy over the validation set
IF (Children=yes) (income=>30000)
THEN (car=Yes)
TU
14.4 PROCEDURE:
5) Go to OPEN file and browse the file that is already stored in the system bank.csv.
JN
www.alljntuworld.in
JNTU World
17) Right click on the result list and select visualize tree option.
JN
TU
or
ld
14.5 OUTPUT:
81
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-15
15.1 OBJECTIVE:
You can make your Decision Trees simpler by pruning the nodes. one approach is to use Reduced Error
Pruning -Explain this idea briefly. Try reduced error pruning for training your Decision Trees using cross
-validation (you can do this in Weka) and report the Decision Tree you obtain? Also, report your accuracy
using the pruned model. Does your accuracy increase?
or
ld
15.2 RESOURCES:
Assigning the pruned node the most common classification of the training instances attached to that
node
Always select a node whose removal most increases the DT accuracy over the validation set
Stop when further pruning decreases the DT accuracy over the validation set
IF (Children=yes) (income=>30000)
TU
THEN (car=Yes)
15.4 PROCEDURE:
JN
5) Go to OPEN file and browse the file that is already stored in the system bank.csv.
6) Select some of the attributes from attributes list
7) Go to Classify tab.
8) Choose Classifier Tree
9) Select NBTree i.e., Navie Bayesian tree.
10) Select Test options Use training set
11) Right click on the text box besides choose button, select show properties
12) Now change unprone mode false to true.
13) Change the reduced error pruning % as needed.
82
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
JN
TU
or
ld
15.5 OUTPUT:
83
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-16
16.1 OBJECTIVE:
ld
(Extra Credit): How can you convert a Decision Trees into "if then -else rules". Make up your own small
Decision Tree consisting of 2 - 3 levels and convert it into a set of rules. There also exist different
classifiers that output the model in the form of rules -one such classifier in Weka is rules. PART, train this
model and report the set of rules obtained. Sometimes just one attribute can be good enough in making
the decision, yes, just one! Can you predict what attribute that might be in this dataset? OneR classifier
uses a single attribute to make decisions (it chooses the attribute based on minimum error). Report the
rule obtained by training a one R classifier. Rank the performance of j48, PART and OneR.
16.2 RESOURCES:
Weka mining tool.
or
16.3 PROCEDURE:
TU
JN
www.alljntuworld.in
JNTU World
ld
5) Go to OPEN file and browse the file that is already stored in the system bank.csv.
6) Select some of the attributes from attributes list
7) Go to Classify tab.
9) Select PART.
10) Select Test options Use training set
11) if need select attribute.
12) Now start weka.
or
13) Now we can see the output details in the Classifier output.
Attribute relevance with respect to the class relevant attribute (science)
JN
TU
12.4 OUTPUT:
85
Downloaded From JNTU World (http://www.alljntuworld.in)
JNTU World
JN
TU
One R
J48
or
ld
www.alljntuworld.in
86
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
JN
TU
or
ld
PART
87
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
EXPERIMENT-17
17.1 OBJECTIVE:
ld
*Run J48 and Nave Bayes classifiers on the following datasets and determine the accuracy:
1.vehicle.arff
2.kr-vs-kp.arff
3.glass.arff
4.wave-form-5000.arff
On which datasets does the Nave Bayes perform better?
17.2 RESOURCES:
Weka mining tool.
or
17.3 PROCEDURE:
5) Go to OPEN file and browse the file that is already stored in the system bank.csv.
7) Go to Classify tab.
TU
JN
www.alljntuworld.in
JNTU World
ld
or
13) Now we can see the output details in the Classifier output.
17.4 OUTPUT:
JN
TU
J48
89
Downloaded From JNTU World (http://www.alljntuworld.in)
www.alljntuworld.in
JNTU World
JN
TU
PART
or
ld
One R
90
Downloaded From JNTU World (http://www.alljntuworld.in)