www.alljntuworld.in JNTU World LINUX PROGRAMMING AND DATA MINING JN TU W or ld LAB MANUAL Downloaded From JNTU World (http://www.alljntuworld.in) www.alljntuworld.in JNTU World COMPUTER SCIENCE AND ENGINEERING Program Outcomes PO4 PO5 PO6 PO7 PO8 PO9 TU PO10 ld PO3 or PO2 Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals, and an engineering specialization to the solution of complex engineering problems. Problem analysis: Identify, formulate, review research literature, and analyze complex engineering problems reaching substantiated conclusions using first principles of mathematics, natural sciences, and engineering sciences. Design/development of solutions: Design solutions for complex engineering problems and design system components or processes that meet the specified needs with appropriate consideration for the public health and safety, and the cultural, societal, and environmental considerations. Conduct investigations of complex problems: Use research-based knowledge and research methods including design of experiments, analysis and interpretation of data, and synthesis of the information to provide valid conclusions. Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern engineering and IT tools including prediction and modeling to complex engineering activities with an understanding of the limitations. The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the professional engineering practice. Environment and sustainability: Understand the impact of the professional engineering solutions in societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable development. Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the engineering practice. Individual and team work: Function effectively as an individual, and as a member or leader in diverse teams, and in multidisciplinary settings. Communication: Communicate effectively on complex engineering activities with the engineering community and with society at large, such as, being able to comprehend and write effective reports and design documentation, make effective presentations, and give and receive clear instructions. Project management and finance: Demonstrate knowledge and understanding of the engineering and management principles and apply these to one’s own work, as a member and leader in a team, to manage projects and in multidisciplinary environments. Life-long learning: Recognize the need for, and have the preparation and ability to engage in independent and life-long learning in the broadest context of technological change. W PO1 PO11 JN PO12 PSO1 PSO2 PSO3 Program Specific Outcomes 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. Downloaded From JNTU World (http://www.alljntuworld.in) www.alljntuworld.in JNTU World LINUX PROGRAMMING AND DATA MINING LAB SYLLABUS S. No. List of Experiments Page No. LINUX PROGRAMMING 1 5 7 4 TU 5 W 3 or ld 2 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 Downloaded From JNTU World (http://www.alljntuworld.in) 9 11 14 15 16 17 18 19 20 23 24 25 26 Write client and server programs (using c) for interaction between server and client processes using Unix Domain sockets. 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. Write a C program that illustrates how to execute two commands concurrently with a command pipe. and classify credit good/bad for each of the examples in the dataset.alljntuworld. 16 JNTU World Downloaded From JNTU World (http://www.in) 36 38 39 40 41 45 48 52 53 56 57 60 64 68 69 . (Using Semaphores). 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. No. Write a C program to create a message queue with read and write permissions to write 3 messages to it with different priority numbers.in 21 22 23 24 25 26 27 28 1 DATA MINING List all the categorical (or nominal) attributes and the real-valued attributes separately. root mean squared error. Write a C program that receives the messages (from the above message queue as specified in (21)) and displays them. Write a C program that illustrates two processes communicating using shared memory.ls –l | sort Write C programs that illustrate communication between two unrelated processes using named pipe. Write a C program to create a Zombie process. using a) Semaphores b) flock or lockf system calls. W S.www. *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. Does your accuracy increase/decrease? Why? TU 2 3 4 JN 5 6 7 8 28 29 30 33 34 ld 20 Page No. 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 a C program that illustrates how an orphan is created.alljntuworld. kappa statistics. Suppose you use your above model trained on the complete dataset. 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. Write client and server programs (using c) for interaction between server and client processes using Internet Domain sockets. 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. Report the model obtained after training. Ex: . Write a C program to allow cooperating processes to lock a resource for exclusive use. Train a Decision Tree again using cross .validation and report your results. www.alljntuworld.3 levels and convert it into a set of rules.) Sometimes.wave-form-5000. 5.arff 2.arff 3. 10. 7.arff 4. or "personal -status" (attribute 9).arff On which datasets does the Naïve Bayes perform better? W S. one approach is to use Reduced Error Pruning -Explain this idea briefly. 72 or 10 List of Experiments Check to see if the data shows a bias against "foreign workers" (attribute 20). Train your Decision Tree again and report the Decision Tree and cross -validation results. train this model and report the set of rules obtained. Report the rule obtained by training a one R classifier. Rank the performance of j48. 17 (and 21. No. You can do this by using a cost matrix in Weka. yes. 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. You can make your Decision Trees simpler by pruning the nodes. There also exist different classifiers that output the model in the form of rules -one such classifier in Weka is rules. *Load the „weather. PART.in) 77 79 80 82 84 88 .kr-vs-kp.arff‟ dataset in Weka and run the ID3 classification algorithm.in 12 13 14 TU 15 JN 16 17 74 75 ld 11 Page No. Compare the accuracy results. (You had removed two attributes in problem 7. Did removing these attributes have any significant effect? Discuss.alljntuworld. you could try just having attributes 2. Instead of counting the misclassifications equally in both cases. To remove an attribute you can use the preprocess tab in Weka's GUI Explorer. give a higher cost to the first case (say cost 5) and lower cost to the second case. Holdout strategy with three percentage levels. the class attribute (naturally)). 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. 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. *Run J48 and Naïve Bayes classifiers on the following datasets and determine the accuracy: 1. Remember to reload the arff data file to get all the attributes initially before you start selecting the ones you want.glass.vehicle. Try out some combinations. PART and oneR. For example. Sometimes just one attribute can be good enough in making the decision. report your accuracy using the pruned model. Compare the accuracy results. 3. 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. 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). Make up your own small Decision Tree consisting of 2 . 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). Does your accuracy increase? (Extra Credit): How can you convert a Decision Trees into "if –then -else rules". 9 JNTU World *Content beyond the university prescribed syllabi Downloaded From JNTU World (http://www. in) PSO1 PO1 PSO1 PO1.www. counts and reports the occurrence of each word that is present in the first argument file on other argument files. PO2 PSO1 PO1 PSO1 PO1 PSO1 PO1 PO1 PSO1 PSO1 PO1 PSO1 PO1 PSO1 PO1 PSO1 . d) * Illustrate to redirect the standard input (stdin) and the standard output (stdout) of a process. d) *Illustrate by writing script that will print. message “Hello World.alljntuworld. 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. Write a c program that makes a copy of a file using standard I/O and system calls ld 1 JN 5 6 7 8 9 10 Downloaded From JNTU World (http://www. d) *Illustrate by writing c program where process forks to a child. write and execute permissions. 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.in JNTU World ATTAINMENT OF PROGRAM OUTCOMES & PROGRAM SPECIFIC OUTCOMES Exp.alljntuworld. starting and ending line numbers as arguments and displays all the lines between the given line numbers. words and lines in a file. Write a shell script to find factorial of a given integer. No. 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. 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. and in different colors like red. PO2 3 TU 4 W or 2 c) Write a shell script that accepts a file name. in Bold and Blink effect. Write an awk script to find the number of characters. Write an awk script to count the number of lines in a file that do not contain vowels. the number of lines on it is also reported. Write a shell script to list all of the directory files in a directory. Program Outcomes Attained Experiment Program Specific Outcomes Attained LINUX PROGRAMMING PO1. Whenever the argument is a file. its inode number and file name.in JNTU World Exp. Write a C program that illustrates how an orphan is created. Ex: . PSO2 PO1. PO2 PSO1 PO1. Write client and server programs (using c) for interaction between server and client processes using Unix Domain sockets.alljntuworld. 15 16 17 18 19 20 21 22 TU 23 24 25 26 JN 27 28 18 19 PO1. PO4 PSO1. Write a C program that illustrates two processes communicating using shared memory.www. Downloaded From JNTU World (http://www. Write a C program that illustrates how to execute two commands concurrently with a command pipe. PO2 PSO1. PO3. 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.ls –l | sort Write C programs that illustrate communication between two unrelated processes using named pipe.Ex: ls > f1. PO2 PSO1 PO1 PSO1 PO1 PSO1 PO1 PSO1 PO1 PSO1 ld 13 14 Program Specific Outcomes Attained or 12 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. (Using Semaphores). PO3. PO3. using a) Semaphores b) flock or lockf system calls. Write a C program to list for every file in a directory. Write a C program that demonstrates redirection of standard output to a file. PO2 PSO1 PO1. 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. No. Write a C program to allow cooperating processes to lock a resource for exclusive use. Write a C program that receives the messages (from the above message queue as specified in (21)) and displays them. PO2 PSO1 PO1. PSO2 PO1. PO4 PSO1. PO2 PSO1 PO1. 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. Write client and server programs (using c) for interaction between server and client processes using Internet Domain sockets. PO4 PSO1. PO2. PO2. W 11 Experiment Program Outcomes Attained PO1 PO1 PSO1 PSO1 PO1 PSO1 PO1 PSO1 PO1 PSO1 PO1 PSO1 PO1. PSO2 DATA MINING List all the categorical (or nominal) attributes and the real-valued attributes separately.in) . PO4 PSO1. PO2. 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. PSO2 PO1. PSO2 PO1. Write a C program to create a message queue with read and write permissions to write 3 messages to it with different priority numbers.alljntuworld. PO2. Write a C program to create a Zombie process. PO5 PO1. PO2. PSO2 PO1. PO4. 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. PSO2 ld 22 Program Specific Outcomes Attained PO1. and classify credit good/bad for each of the examples in the dataset.alljntuworld. kappa statistics. PSO2 PO1. *Load the „weather. PO2. 17 (and 21. For example. PO2. 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). Suppose you use your above model trained on the complete dataset. Try out some combinations. No. Train your Decision Tree again and report the Decision Tree and cross validation results. you could try just having attributes 2.) Sometimes. 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. PO4. Remember to reload the arff data file to get all the attributes initially before you start selecting the ones you want. PSO2 PO1. Holdout strategy with three percentage levels.www. 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. do you really need to input so many attributes to get good results? Maybe only a few would do. Does your accuracy increase/decrease? Why? Check to see if the data shows a bias against "foreign workers" (attribute 20). PO5. PO2. Compare the accuracy results. PO12 PSO1 PO1. PO2 PSO1. PO4. PO2. PSO2 PO1. PO2. PO12 PSO1. root mean squared error. PO12 or 21 *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. PO5 PSO1. 3. the class attribute (naturally)). 10. 5. PO5.in) PSO1 PSO1 PO1.in JNTU World Exp. PO5 PSO1 PO1. 23 24 25 26 27 TU 28 JN 29 30 31 PO1. PO2. PO5 PSO1. PO5 PSO1 PO1. (You had removed two attributes in problem 7. Report the model obtained after training. PSO2 . To remove an attribute you can use the preprocess tab in Weka's GUI Explorer. give a higher cost to the first case (say cost 5) and lower cost to the second case. PO2. What problem do you have and what is the solution? Another question might be. PO5 PSO1 PO1. PO2. PO2. 7. W 20 Experiment Program Outcomes Attained Downloaded From JNTU World (http://www. Compare the accuracy results. Train a Decision Tree again using cross . PO12 PSO1. Instead of counting the misclassifications equally in both cases.alljntuworld. You can do this by using a cost matrix in Weka. or "personal -status" (attribute 9). Did removing these attributes have any significant effect? Discuss. PO2.arff‟ dataset in Weka and run the ID3 classification algorithm.validation and report your results. PO12 PSO1. 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. PO12 PSO1. Make up your own small Decision Tree consisting of 2 . Rank the performance of j48. PO4. PART and oneR. PO4.alljntuworld. 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). PO4. PO2. PSO2 PSO1. PO12 JN TU *Content beyond the university prescribed syllabi Downloaded From JNTU World (http://www.arff 4. report your accuracy using the pruned model.wave-form-5000. yes.alljntuworld.in JNTU World Exp.kr-vs-kp. PO12 PSO1. train this model and report the set of rules obtained. PO5. one approach is to use Reduced Error Pruning -Explain this idea briefly. There also exist different classifiers that output the model in the form of rules -one such classifier in Weka is rules. PART. PO5. PO2.glass. Does your accuracy increase? (Extra Credit): How can you convert a Decision Trees into "if –then else rules". *Run J48 and Naïve Bayes classifiers on the following datasets and determine the accuracy: 1. PO2.3 levels and convert it into a set of rules.arff 3.arff 2.vehicle. 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.www. PSO2 ld 33 Experiment Program Outcomes Attained or W 34 PO1. PSO2 . PO5. Report the rule obtained by training a one R classifier.arff On which datasets does the Naïve Bayes perform better? Program Specific Outcomes Attained PO1. Sometimes just one attribute can be good enough in making the decision. No.in) PO1. 32 You can make your Decision Trees simpler by pruning the nodes. files. W Upon the completion of Linux Programming and Data Mining practical course.alljntuworld. 10. FIFOs. 7. Understand synchronized programs using shared memory. Demonstrate Inter process Communication techniques.www. Demonstrate client server technology using socket programming. pattern mining etc with varied datasets and dynamic parameters. OUTCOMES: or Data mining tools allow predicting future trends and behaviors. 2. TU 1. IPC. working with the Bourne again shell (bash). allowing businesses to make proactive. clustering.in) . the student will be able to: Understand and implement basic system functionalities of Linux operating system. 3. The data mining laboratory course is designed to exercise the data mining techniques such as classification. There will be extensive programming exercises in shell scripts.alljntuworld. Demonstrate the working of algorithms for data mining tasks such association rule mining. Write shell scripts to automate different tasks. Learn to execute data mining tasks using a data mining toolkit (such as WEKA) and visualize the results. 6. pipes. Learn to build a data warehouse and query it using open source tools. Implement and manage client server technology using TCP and UDP. Weka data mining tool is used for the purpose of acquainting the students with the basic environment of the data mining tools.in JNTU World LINUX PROGRAMMING AND DATA MINING LABORATORY OBJECTIVE: ld The Linux programming laboratory course covers major methods of Inter Process Communication (IPC). 4. 5. Design and implement advanced features such as threads. knowledge-driven decisions. process and signals. 9. It also emphasizes various concepts in multithreaded programming and socket programming. Downloaded From JNTU World (http://www. JN classification. which is the basis of all client / server applications under Linux. clustering and regression. Linux Utilities. 8. alljntuworld.alljntuworld.JNTU World W or ld www.in) .in JN TU LINUX PROGRAMMING Downloaded From JNTU World (http://www. 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. and in different colors like red. Find difference between starting and ending line numbers 3. shell-interpreter 1. b) *Illustrate by writing script that will print.vi-editor. message “Hello World.4. in Bold and Blink effect.www.in) . If exists display between lines to output stream else display file not exists 1. brown etc using echo commands? 1.2 RESOURCE/REQUIREMENTS Linux operating system .alljntuworld. starting and ending line numbers as arguments and displays all the lines between the given line numbers. Read a filename.3 PROGRAM LOGIC 1.1 1.in JNTU World EXPERIMENT 1 OBJECTIVE a) Write a shell script that accepts a file name.4.alljntuworld. Test given filename exists or not 4.$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. starting and ending line numbers as arguments 2.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 W or ld 1.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 . 2 for grep and 3 for cat. 1. 1.7 POST-LAB QUESTIONS 1.www.in) . Name few file handling commands present in unix. 2. Write a shell script to count number of words present in a file without using commands.in JNTU World ld echo -e "\033[32m Hello World" # Green color echo -e "\033[33m Hello World" # See remains on screen echo -e "\033[34m Hello World" echo -e "\033[35m Hello World" echo -e "\033[36m Hello World" echo -e -n "\033[0m " # print back to normal echo -e "\033[41m Hello World" echo -e "\033[42m Hello World" echo -e "\033[43m Hello World" echo -e "\033[44m Hello World" echo -e "\033[45m Hello World" echo -e "\033[46m Hello World" echo -e "\033[0m Hello World" # Print back to normal PRE-LAB QUESTIONS 1.6 LAB ASSIGNMENT 1.alljntuworld. What is the purpose of case statement? 2.5 6 Downloaded From JNTU World (http://www.alljntuworld. 2. Write a menu driven shell script to execute a command as 1. Define shell script? What is the difference between shell and kernel. What the difference between break and exit statement? JN TU W or 1.for `ls` . vi-editor.sh 3.1 INPUT: sh prog2. b) *Illustrate by writing script using for loop to print the following patterns? 2.alljntuworld. j<=i.2 RESOURCE/REQUIREMENTS Linux operating system .b.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 W or ld 2. 2.alljntuworld. shell-interpreter 2.4.i DESCRIPTION / PROCEDURE # do the following for loop echo "Stars" # outer loop for (( i=1. i<=5.sh enter the word echo JN OUTPUT: The given input filename is : 3.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.in) .4. j++ )) do echo -n " *" 7 Downloaded From JNTU World (http://www. i++ )) do #inner loop for (( j=1.www.3 PROGRAM LOGIC Read file name from command line arguments and display lines inverse of specified word.sh It displays all the lines other than pattern matching 2. in) . Write down the syntax of `if` statement.ii DESCRIPTION / PROCEDURE # do the following for loop PRE-LAB QUESTIONS 1.Read two string str1 and str2 and check i) Compare two strings ii) Palindrome or not 2. What is the purpose of the variable $? What are the various output it has? JN TU W 2. 2.alljntuworld. What are positional parameter and name any two. i++ )) do #inner loop for (( j=1. j<=i. i<=5.5 8 Downloaded From JNTU World (http://www. j++ )) do echo -n "$i" done echo "" done # ld 2.b.7 POST-LAB QUESTIONS 1.6 LAB ASSIGNMENT 1.alljntuworld.in JNTU World done echo "" done # or echo "Can you see the following:" # outer loop for (( i=1.www.4. 2. */ printf("n = %d\n".vi-editor. /* try stdout of this has not changed . */ n++.alljntuworld.www. i < 10. so that scanf () reads from the pipe and printf () writes into the pipe? 3.4. } } else {/* Parent process */close(0) . */ for (i=0.So we close fd[1].h> #include <unistd. shell-interpreter 3. /* Redirect the stdin of this process to the pipe*/ close(fd[1]). So this will be shown in the terminal. printf automatically writes into the pipe. /* Now that stdin has been redirected. ld 3. dup(fd[0]) .a DESCRIPTION / PROCEDURE echo "List of Files which have Read.int n=0. execute permissions. write and execute permissions. close(fd[0]).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. i++) {scanf("%d".alljntuworld.in) .h> #include <sys/ipc.h> main() { int fd[2]. Write and Execute Permissions in Current Directory" for file in * do if [ -r $file -a -w $file -a -x $file ] then echo $file fi done INPUT: sh prog3.sh OUTPUT: List of Files which have Read.1 W or 3. i < 10.txt JN TU 3.n).&n).3 PROGRAM LOGIC Read a list of files from current directory and display the file names to output stream whose files has read. dup(fd[1]) . i. /* try not read from the pipe in this example.b DESCRIPTION / PROCEDURE #include <stdio. pipe(fd). */ sleep(1). /* will not write into the pipe. */ for (i=0. 9 Downloaded From JNTU World (http://www.4. i++) {printf("%d\n".So close fd[0]. scanf automatically reads from the pipe. /* Now that stdout has been redirected. write.2 RESOURCE/REQUIREMENTS Linux operating system . b) Illustrate to redirect the standard input (stdin) and the standard output (stdout) of a process. if (fork() == 0) { /* create Child process */ close(1) . Write and Execute Permissions in Current Directory pp2.n). in) .assign .and access it 3. Read a file name from command line and check if it read and write permission or not.in JNTU World }}} PRE-LAB QUESTIONS 1.6 LAB ASSIGNMENT 1. How to read a variable . write and execution permission.7 POST-LAB QUESTIONS 1. Read a file name from command line and check it‟s a file or not.alljntuworld.alljntuworld. 3. How to check if file is existing. 2. JN TU W or ld 3.5 10 Downloaded From JNTU World (http://www. it has read. What is the difference between $* and $@. 2.www. shell-interpreter 4. mypid).vi-editor. mypid = getpid(). the number of lines on it is also reported.\n".h> /* for wait*() */ #include <sys/wait.2 RESOURCE/REQUIREMENTS Linux operating system .h> /* for fork() */ #include <sys/types.h> int main() { pid_t mypid. 11 Downloaded From JNTU World (http://www.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. printf("Hi. childpid = fork().www. My pid is %d.alljntuworld.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 W or 4.sh number of lines 9 JN 4.alljntuworld.sh OUTPUT: enter the name file 3. if ( childpid == -1 ) { perror("Cannot proceed. b) *Illustrate by writing c program where process forks to a child.h> #include <unistd. int status.1 TU INPUT: sh prog4.a DESCRIPTION / PROCEDURE echo enter the name for fname in * do if test -f $fname then echo "file" $fname echo "number of lines" `cat $fname | wc -l` else if test -d $fname then echo "dir" $fname fi fi done ld 4. I'm the parent process. fork() error").in) .4.4. childpid. Whenever the argument is a file. and create a child process by using forks and suddenly terminates itself? 4.b DESCRIPTION / PROCEDURE #include <stdio. How to write arithmetic multiplication operator in shell. } 4.\n"). mypid = getpid(). if ( childpid == -1 ) { perror("Cannot proceed. mypid = getpid(). } if (childpid == 0) { /* this is the child of the first child. /* forks another child */ childpid = fork(). return 1. &status. TU W or ld } if (childpid == 0) { printf("Child 1: I inherited my parent's pid as %d. else printf("Child 1: child has not terminated correctly. Write down the syntax for nested if statement. /* wait for the child to terminate and report about that */ wait(&status). My own pid instead is %d.alljntuworld. return 1. getppid(). if ( childpid == -1 ) { perror("Cannot proceed. hopefully before its parent "Child 2" */ printf("Child 3: I hinerited my parent's PID as %d.www. childpid = fork().\n". getppid(). mypid).in) . } else /* the parent "Child 2" suddendly returns 15 */ return 15.\n"). getppid(). thus "Child 2" */ printf("Child 2: I hinerited my parent's PID as %d. which waits for its child to exit */ while ( waitpid(childpid. WEXITSTATUS(status)). WEXITSTATUS(status)). childpid). fork() error"). if ( WIFEXITED(status) ) printf("Parent: child has exited with status %d.alljntuworld.\n". printf("Child 1: getppid() tells my parent is %d. mypid). mypid). My own pid instead is %d.\n".in JNTU World return 1. else printf("Parent: child has not terminated normally. mypid). if ( WIFEXITED(status) ) printf("Child1: Child 2 exited with exit status %d. } if (childpid == 0) { /* "Child 3" sleeps 30 seconds then terminates 12. sleep(30). "Parent" */ printf("Parent: fork() went ok. 12 Downloaded From JNTU World (http://www. } JN } else { /* then we're the parent process. return 12.\n". } else { /* this is still "Child 1". mypid). 2.\n". } return 0.5 PRE-LAB QUESTIONS 1.\n". fork() error"). My own pid instead is %d. mypid). printf("Child 2: getppid() tells my parent is %d. mypid = getpid(). My child's PID is %d\n". WNOHANG) == 0 ) sleep(1).\n".\n". printf("Child 3: getppid() tells my parent is %d. in JNTU World LAB ASSIGNMENT 1. 2.c and shell programs present in current directory. What is means by relation operator .in) . name any three relation operators present in shell. Write a shell script to count number of txt.www.alljntuworld. 2.alljntuworld.6 13 Downloaded From JNTU World (http://www. Write a shell script to count number of only files present in current directory. JN TU W or ld 4. and Explain about each operator. 4. What is meant by logic operator.7 POST-LAB QUESTIONS 1. sh or echo Enter file name: read file1 read file2 a=`comm -2 $file1 $file2` b=`grep -c $a $file2` echo Words contained in file one occurred in file two $b times grep -n $a $file2 TU Words contained in file one occured in file two 3 times 1:myfile contains 5:myfile 8:myfile PRE-LAB QUESTIONS 1. egrep and fgrep commands 2. JN 5.vi-editor. What are environmental variables. Write a script to copy list of file into specified directory.alljntuworld.in) . What are different types of shell and how to move from one shell to other shell 14 Downloaded From JNTU World (http://www. Write a script to reverse a given string.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.7 POST-LAB QUESTIONS 1. 3.www.in JNTU World EXPERIMENT 5 OBJECTIVE Write a shell script that accepts a list of file names as its arguments.1 Enter file name: f1 myfile OUTPUT: W INPUT: sh prog5. 5. 2.5 5. What is the significance of using single and double quotation in echo statement.4 DESCRIPTION / PROCEDURE ld 5.alljntuworld. counts and reports the occurrence of each word that is present in the first argument file on other argument files. 5. What is difference between comm and diff commands 2. How to copy and paste lines in a vi-editor 3.6 LAB ASSIGNMENT 1. shell-interpreter 5. What is difference between grep.2 RESOURCE/REQUIREMENTS Linux operating system . 5. for `ls` . If its directory and exist display all the sub directories and files to output stream 4.7 POST-LAB QUESTIONS 1.txt W INPUT: sh Lp6. 2.2 RESOURCE/REQUIREMENTS Linux operating system .vi-editor. JN 6.1 PRE-LAB QUESTIONS 1. 6.alljntuworld.2 for grep and 3 for cat.in) .3 PROGRAM LOGIC 1.5 6. 15 Downloaded From JNTU World (http://www.www. Write down the syntax case statement 6. What is the purpose of shift statement.sh Enter dir name Presanna ld 6. Write a menu driven shell script to execute a command as 1. 6. What the difference is between break and exit statement. else display its not directory or not exists. Name few test commands present in unix. 2.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. Write a shell script to count number of words present in a file without using commands.sh 4.6 LAB ASSIGNMENT 1.alljntuworld. Read a directory 2. Test given directory is directory file and exist using test options -d 3.in JNTU World EXPERIMENT 6 OBJECTIVE Write a shell script to list all of the directory files in a directory.sh pp2. 2. shell-interpreter 6. Write a shell script to find the sum of digits. 7. What is the name of the variable which counts number of arguments passed? JN 7.vi-editor. 2. 3. 2. Read a filename 2. 7. 7. 3. shell-interpreter 7..4 DESCRIPTION / PROCEDURE or TU W echo Factorial echo Enter number: read n fact=1 i=1 for((i=1.alljntuworld. 2.2 RESOURCE/REQUIREMENTS Linux operating system .in JNTU World EXPERIMENT 7 OBJECTIVE Write a shell script to find factorial of a given number. How do you access command line arguments from within a shell script? 7.www.alljntuworld. What is the use of while loop. Which operator is used to check string is Null . take a variable i for count and execute expression in for loop . etc.7 POST-LAB QUESTIONS 1.in) .i++)) do fact=`expr $fact \* $i` done echo Factorial of $n is $fact INPUT: sh p7.1 PRE-LAB QUESTIONS 1.i<=n.6 LAB ASSIGNMENT 1.5 16 Downloaded From JNTU World (http://www.3 PROFRAM LOGIC 1. 3. Illustrate the difference between while and for loop. Write a shell script for menu driven to execute a command like 1 for ls.increment the variable till loop ends. How you do command substitution in shell script.2 for ls –l.sh Factorial Enter number:5 OUTPUT: Factorial of 5 is 120 ld 7. Display the factorial of given number to output stream. total} or ld 8.2 RESOURCE/REQUIREMENTS Linux operating system . Illustrate difference between awk and sed. Define awk? What are the features of awk script? 2.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:".5 POST-LAB QUESTIONS 1.in JNTU World EXPERIMENT 8 OBJECTIVE Write an awk script to count the number of lines in a file that do not contain vowels.alljntuworld.2 to 8 lines to output stream. Initialize total=0 in begin part 2.www. JN 8. Write a awk script to display between lines eg. 3. 2. Using if command check each line to count the number of lines in a file that do not contain vowels in body part.in) .vi-editor. TU 8. 8.sh Displaying number of lines in a file that do not contain vowels W OUTPUT: The total lines in a file that do not contain vowels:1 PRE-LAB QUESTIONS 1.alljntuworld. Write a awk command to search given pattern in file if found display to output entire line.3 PROGRAM LOGIC 1. 2. What is the syntax for awk script? 8.7 17 Downloaded From JNTU World (http://www. shell-interpreter 8. Display total lines to output stream that do not contain vowels in end part 8. Write a awk script to find sum of total salary of all employees of given file.awk lp1.6 LAB ASSIGNMENT 1.1 INPUT: awk prog8. alljntuworld. Explain about associative arrays in awk. words and lines in a file is: Words:12 Lines:3 Chars:39 LAB ASSIGNMENT 1.awk lp5.words) print("Lines:\t".4 DESCRIPTION / PROCEDURE BEGIN{ print Displaying number of characters.vi-editor. 9. Write a awk script to convert lower case characters to upper case of given file.www. How input file is given to awk script? 2. words and lines in a file is: print("Words:\t". getline and exit control actions in awk TU 9.2 RESOURCE/REQUIREMENTS Linux operating system .in) . 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 9. shell-interpreter 9. words and lines in a file.5 W OUTPUT: The total number of characters. JN 9.NR) print("Chars:\t".3 PROGRAM LOGIC Write awk script to find the number of characters.7 POST-LAB QUESTIONS 1.in JNTU World EXPERIMENT 9 OBJECTIVE Write an awk script to find the number of characters.sh or ld 9.alljntuworld. What is the use of next. 9.len) } INPUT: awk prog9.1 PRE-LAB QUESTIONS 1.6 18 Downloaded From JNTU World (http://www. 6 LAB ASSIGNMENT 1.&buff.O_RDONLY). monitor. wirte(fd1.www.in JNTU World EXPERIMENT 10 OBJECTIVE Write a C program that makes a copy of a file using Systems calls 10. vi –editor. dest[10].out entr the source file name: file1 enter the destination file name: file2 OUTPUT: The copy of a file is successes 10. close(fd). shell interpreter 10. int fd.h> #include<sys/types. printf("The copy of a file is successed"). Open given source filename 3.2 RESOURCE/REQUIREMENTS Linux operating system. printf("enter the source file name \n").5 PRE-LAB QUESTIONS 1.1). } TU W or ld 10. fd=open("src". buff. error.in) . What are the file descriptors values of keyword.c . Write a c-program to count number lines in a file. scanf("%s\n". fd1=open("dest".3 PROGRAM LOGIC 1.4 DESCRIPTION / PROCEDURE #include<stdio.alljntuworld. Read the content from file and write to destination file 4.1 JN INPUT: cc prog10. 10.1)).alljntuworld. What is meant by file descriptor and user file descriptor starts from which number 10.7 POST-LAB QUESTIONS 1.h> #include<unistd.&buff.dest). destination filename 2. What is the use of lseek() function 19 Downloaded From JNTU World (http://www.h> void main() { char src[10]. 2.h> #include<string. printf("enter the destination file name\n" scanf("%s\n".O_WRONLY|O_CREAT|O_TRUNC|S_IRUSR|S_IWUSR). Repeat step 3 till end of file reaches 5. close(fd1). while(read(fd. close open files 10./a. Read source filename.fd1.src). buf). char buf. Open file which is input given by command line arguments 2.alljntuworld.1 OBJECTIVE Write a C Program to Implement the Unix command ls using system calls 11. write() and scanf.h> #define BUFSIZE 1 int main(int argc. char **argv) { int fd1. int n. vi –editor.5 11.1 INPUT: cc prog11a.alljntuworld. /* or write(1. 11.B.4 DESCRIPTION / PROCEDURE #include<fcntl. A. Read content from opened file 3. 11.in) . B. What is the difference between open() and fopen()? JN 11.h> #include<sys/stat. */ } return (0). 11.2 RESOURCE/REQUIREMENTS Linux operating system. while((n=read(fd1.6 LAB ASSIGNMENT 1. What is the difference between read().1))>0) { printf("%c". c-compiler 20 Downloaded From JNTU World (http://www.A.&buf.www. fd1=open(argv[1]. A. printf("Displaying content of file\n"). A.O_RDONLY). printf respectively 11.&buf.in JNTU World EXPERIMENT 11 OBJECTIVE Write a C Program to Implement the Unix command cat using system calls. Display content to output stream 4. shell interpreter 11. vi –editor.A.3 PROGRAM LOGIC 1.c unit1 OUTPUT: Displays content of file PRE-LAB QUESTIONS 1. Repeat step 2 and step 3 till end of file reach. } TU W or ld 11. A.2 RESOURCE/REQUIREMENTS Linux operating system.1). Write a c-program to count number words in a file. A.7 POST-LAB QUESTIONS 1. Repeat step 2 till eof directory reach.sh 11.i<count+1. int file_select(). exit(0). exit(0). B. else return (TRUE). } printf("Current Working Directory = %sn". B. B.in JNTU World PROGRAM LOGIC 1. alphasort).c OUTPUT: Current Working Directory =/home/prasanna Number of files:2 Lp1..alljntuworld.5 PRE-LAB QUESTIONS 1. } printf("Number of files = %d \n".sh lp2. struct dirent **files. char pathname[MAXPATHLEN]. ".pathname).4 DESCRIPTION / PROCEDURE #include <sys/types.++i) printf("%s \n". for (i=1. Write a c-program to reverse a file using lseek().3 JN INPUT: Cc prog11c.h> #include <sys/dir. 11.") == 0) ||(strcmp(entry->d_name.count). Scan directory and Read file and display filename to output stream 3.files[i-1]->d_name).www.alljntuworld. &files. } int file_select(struct direct *entry) { if ((strcmp(entry->d_name. if (count <= 0) { printf("No files in this directoryn"). B.6 LAB ASSIGNMENT 1.h> #define FALSE 0 #define TRUE 1 extern int alphasort(). Open a directory of given directory name 2. } TU W or ld 11.in) .h> #include <sys/param. ".") == 0)) return (FALSE). 21 Downloaded From JNTU World (http://www. if (getcwd(pathname) == NULL ) { printf("Error getting pathn"). main() { int count.i. What is the difference between lseek() and seekdir(). file_select.h> #include <stdio. count = scandir(pathname. 11. 1 OBJECTIVE Write a C Program to Implement the Unix command mv using system calls. What is difference between file descriptor and file pointer. c-compiler 11.in JNTU World POST-LAB QUESTIONS 1. B.h> #include<unistd. C. What is the difference between system call and library functions 11.h> int main(int argc. 11.fd2. 11.6 LAB ASSIGNMENT 1.c file1 file2 TU OUTPUT: # creates file2 and copies the content of file1 to file2 and removes file1 PRE-LAB QUESTIONS 1. Write a C program to move file content to another file without using functions rename and unlink. int n. Write a C program to copy content from source to destination file 11.5 22 Downloaded From JNTU World (http://www. fd1=open(argv[1]. Using rename function move file content to another file 3. C.O_RDONLY). C. rename(fd1.S_IWUSR). 2.4 DESCRIPTION / PROCEDURE #include<fcntl.h> #include<stdio. C. char **argv) { int fd1. C.fd2). C. Read and write mode respectively.C. return (0). unlink(argv[1]).7 INPUT: cc mv.in) .3 PROGRAM LOGIC 1.www.7 POST-LAB QUESTIONS 1.h> #include<sys/stat. vi –editor.alljntuworld.alljntuworld. fd2=creat(argv[2].count=0. } W or ld 11. Open two files. 2.2 RESOURCE/REQUIREMENTS Linux operating system. JN 11. Remove source file using unlink() function. What is the difference between cp and mv commands? 11. if(stream==(FILE*)0) { fprintf(stderr. } return().h> main() { FILE *stream.alljntuworld.”r”).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.4 DESCRIPTION / PROCEDURE #include<stdio.List properties of files and different types of files in Linux 23 Downloaded From JNTU World (http://www. exit(1). int buffer_character. }} if(fclose(stream))==EOF) { fprintf(stderr. fstat() and lstat() functions JN 12.5 12. lines and characters using system calls 12.1 PRE-LAB QUESTIONS 1. c-compiler 12.7 POST-LAB QUESTIONS 1. read.”Error closing stream. number of links 3. Read a file and display a file properties to output stream. } TU W or ld 12.”Error opening file(printed to standard error)\n”).3 PROGRAM LOGIC 1. file type 2.(printed to standard error)\n).2 RESOURCE/REQUIREMENTS Linux operating system.in) .6 LAB ASSIGNMENT 1. 1.alljntuworld. vi –editor. fclose(stream). What is the difference between stat( ). Open a file using fopen() function 2. exit(1).Write a C program to count number of words. write and execute permissions 4. 12. time of last access (Note: use /fstat system calls) 12. stream=fopen(“test”.www. 13. 5.www.6 LAB ASSIGNMENT 1.h> #include <sys/types.h> int main (void) { DIR *dp. ep->d_name).. vi –editor. c-compiler ld 13. Read the entry available in the directory. Write a C-program to simulate `nl` command. 2.h> #include <dirent.out OUTPUT: 2 3 a. if (dp != NULL) { while (ep = readdir (dp)) printf("%s\n". What is the use of -> operator? 24 Downloaded From JNTU World (http://www.3 PROGRAM LOGIC 1. name of the file or sub directory. 3. Open the directory using opendir() system call and report error if the directory is not available.c .in) . Repeat the step 6 and 7 until all the entries were read.7 POST-LAB QUESTIONS 1."). 13.in JNTU World EXPERIMENT 13 13. 13. . What is the purpose of 0_CREAT and O_SYNC. 13.h> #include <stdio. } else perror ("Couldn't open the directory"). dp = opendir (". struct dirent *ep. Display the directory entry ie. 4. 6.5 PRE-LAB QUESTIONS 1. Read the directory name form the user. } INPUT: cc 13. TU W or 13.1 OBJECTIVE Write a C program to emulate the Unix ls – l command./a. return 0.c JN 13. Declare and initialize required objects. closedir (dp).alljntuworld.alljntuworld.4 DESCRIPTION / PROCEDURE #include <stddef.out 1 4 ….2 RESOURCE/REQUIREMENTS Linux operating system. vi –editor. Scan entire directory and display its directory file name and inode number till eof of the directory INPUT: cc inode. Printf(“Enter the directory name:”). } } ld 14.in JNTU World EXPERIMENT 14 14. Read Directory name as input 2. While((p=readdir(dp))!=NULL) { Printf(“%s\t %/d\n”. Dp=opendir(dname).h> #include<sys/types. Scanf(“%s”. 25 Downloaded From JNTU World (http://www.dname).1 OBJECTIVE Write a C program to list for every file in a directory.5 PRE-LAB QUESTIONS 1. c-compiler TU W or 14..4 DESCRIPTION / PROCEDURE #include<stddef.st_ino).p->d_name./inode JN OUTPUT: FILE NAME …………. char dname[20]. Struct stat x.6 LAB ASSIGNMENT 1. Printf(“\n FILE NAME \t INODE NUMBER \n”). File1.h> #include<stdio.alljntuworld.2 RESOURCE/REQUIREMENTS Linux operating system.7 POST-LAB QUESTIONS 1.Write a C-program to display all files using system calls 14. Struct dirent *p. List all directory systems calls with example. 14.h> #include<dirent.c –o inode . 2. File2.3 PROGRAM LOGIC 1. its inode number and file name 14.x. What is the system call that change the directory. What flag is used to append the data /value in the file.c ….alljntuworld.www.h> main() { DIR *dp.c INODE NUMBER 4195164 4195164 4195164 14.in) . in) . printf ("This sentence is redirected to a file which is given at output.3 PROGRAM LOGIC 1."). Explain different streams in linux 2.c –o file1 . Ex: ls >f1.h> int main () { freopen ("myfile. Redirect of standard to out put to a new file ld 15.alljntuworld.alljntuworld.in JNTU World EXPERIMENT 15 15. return 0.What is the purpose of fopen().6 LAB ASSIGNMENT 1."w". Open file using freopen() function 2. vi –editor.5 PRE-LAB QUESTIONS 1./file1 or 15.txt".fread().www. c-compiler W INPUT: cc 15.stdout). Illustrate different types of files in linux 26 Downloaded From JNTU World (http://www. /* freopen example: redirecting stdout */ 15.Write a C-program to append content of file to another file JN 15.fwrite() functions 15. } OUTPUT: This sentence is redirected to a file which is given at output TU 15.1 OBJECTIVE Write a C program that demonstrates redirection of standard output to a file. fclose (stdout).2 RESOURCE/REQUIREMENTS Linux operating system.4 DESCRIPTION / PROCEDURE #include <stdio.7 POST-LAB QUESTIONS 1. . 2. } printf("PID %d terminates.in) . 16. so I must be the child. I'm the child process with PID 13293 and PPID 13292.\n"..5 PRE-LAB QUESTIONS 1. What is process.alljntuworld. 16. If pid is not equal to zero execute parent block 3. PID 13293 terminates.\n". /* Duplicate. I'm the parent process with PID 13292 and PPID 13273./a. PID 13292 terminates.*/ if (pid!=0) {/* pid is non-zero.4 DESCRIPTION / PROCEDURE #include <stdio. else if pid is equal to zero execute child block 4.getppid()).\n".getpid()..getppid()).. c-compiler 16. TU INPUT: $cc fork. } else { /* pid is zero. so I must be the parent */ printf("I'm the parent process with PID %d and PPID %d. run the program.2 RESOURCE/REQUIREMENTS Linux operating system. /* Both processes execute this */ } W or ld 16.www. 16. Child and parent continue from here. What are process identifiers in linux programming. Display process id using getpid() and parent process id getppid() functions.. */ printf("I'm the child process with PID %d and PPID %d. Illustrate difference between fork() and vfork() functions. My child's PID is 13293. What are different process ids in Linux programming? 27 Downloaded From JNTU World (http://www.out JN OUTPUT: I'm the original process with PID 13292 and PPID 13273. vi –editor.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. Create child process using fork() function 2.pid).alljntuworld.6 LAB ASSIGNMENT 1.getpid().h> main() { int pid. how you create new process? 16..1 .3 PROGRAM LOGIC 1. Write a program to find sum of odd numbers of parent process and sum of even numbers by child process.7 POST-LAB QUESTIONS 1. pid). printf("I'm the original process with PID %d and PPID %d. .getppid()). child terminates. parent terminates. printf("My child's PID is %d. 16.c . 2. pid=fork(). .\n".\n".getpid(). .alljntuworld. pid=fork(). 2. c-compiler 17. kill the parent process.6 LAB ASSIGNMENT 1. To see already terminated process i.4 DESCRIPTION / PROCEDURE #include <stdio. If pid is equal to zero executer child process and terminate immediately 4... Define Zombie process.. 17. 17. Write a C-program to create a child process to convert lower to upper case letters. } else { exit(42). /* Duplicate */ if (pid!=0) /* Branch based on return value from fork() */ { while (1) /* never terminate. execute the program in the background. notice the zombie is gone now.3 PROGRAM LOGIC 1..exe $ ps . How to know the status of process using commands.in JNTU World EXPERIMENT 17 OBJECTIVE Write a C program to create a Zombie process../a.www. and never execute a wait() */ sleep(1000).h> main(){ int pid..c . Explain different ways to terminate the process? 28 Downloaded From JNTU World (http://www. [1] Terminated zombie.in) . [1] 13545 JN TU OUTPUT: $ ps PID TT STAT TIME COMMAND 13535 p2 s 0:00 -ksh(ksh) . /* Exit with a silly number */ }} W or ld 17.the shell 13545 p2 s 0:00 aombie..e zombie process open new command prompt and type ps -a it shows status z means that process is zombie process.the parent process 13536 p2 z 0:00 <defunct> .. 17. Create a new child process using fork() 2.the zombie child process 13537 p2 R 0:00 ps $ kill 13545 . 17.2 RESOURCE/REQUIREMENTS Linux operating system.out& . PID TT STAT TIME COMMAND 13535 p2 s 0:00 -csh(csh) 13548 p2 R 0:00 ps 17. If pid is not equal to zero execute parent process and block the process 1000 seconds 3...alljntuworld.7 POST-LAB QUESTIONS 1.1 INPUT: $ cc prog17.5 PRE-LAB QUESTIONS 1.exe. vi –editor.. \n". } else { /* pid is zero. If pid is equal to zero executer child process and block process for 5 seconds in mean time parent process terminates.1 INPUT: $cc prog18. 4. so I must be the parent */ printf("I'm the parent process with PID %d and PPID %d. child terminates.www. /* Duplicate.in) .alljntuworld. /*Make sure that the parent terminates first. 18. Write a C program to find given number is Armstrong number or not by parent process. 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. run the program. I'm the child process with PID 13365 and PPID 1.. . vi –editor. getpid()..alljntuworld./a.\n".4 DESCRIPTION / PROCEDURE #include <stdio. getpid(). Illustrate difference between zombie process and orphan process. .3 PROGRAM LOGIC 1.orphaned! PID 13365 terminates.getppid())..out .. 18.\n". 18. If pid is not equal to zero execute parent process block of code and terminates 3. JN OUTPUT: I'm the original process with PID 13364 and PPID 13346. c-compiler 18.in JNTU World EXPERIMENT 18 OBJECTIVE Write a C program that illustrates how an orphan is created.*/ if (pid!=0) /* Branch based on return value from fork() */ { /* pid is non-zero.6 LAB ASSIGNMENT 1. printf("I'm the original process with PID %d and PPID %d. 18. pid).getppid()). getpid(). 18. What is the child parent process id if child process status is orphan process? 29 Downloaded From JNTU World (http://www.2 RESOURCE/REQUIREMENTS Linux operating system. Create a new child process using fork() 2. */ sleep(5).getppid()). */ printf("I'm the child process with PID %d and PPID %d.c .7 POST-LAB QUESTIONS 1.\n". Child and parent continue from here. } printf("PID %d terminates. so I must be the child. I'm the parent process with PID 13364 and PPID 13346. printf("My child's PID is %d..pid).h> main() { int pid. /* Both processes execute this */} TU W or ld 18..\n". pid=fork(). PID 13364 terminates.5 PRE-LAB QUESTIONS 1. Eg. ld 19. Repeat step 2 till eof directory reach. ls-l|sort. return. vi –editor. dp=opendir(". break. } while((p=readdir(dp))!=NULL) { if(stat(p->d_name.in JNTU World EXPERIMENT 19 19.h> #include<dirent. 4.&b)<0) { printf("\nStat failure \n").char *argv[]) { struct dirent *p.h> #include<stdlib. case S_IFLNK: printf(" l").h> main(int argc."). break. break. case S_IFSOCK: printf(" s"). exit(0). case S_IFBLK: printf(" b"). break. c-compiler JN TU W or 19. DIR *dp.h> #include<unistd.st_mode & S_IFMT ) { case S_IFREG: printf(" -"). break.alljntuworld.2 RESOURCE/REQUIREMENTS Linux operating system.4 DESCRIPTION / PROCEDURE #include<stdio.in) .3 PROGRAM LOGIC 1. case S_IFDIR: printf(" d").www. Using stat function test type of file.h> #include<sys/stat. } switch(b. case S_IFCHR: printf(" c"). 30 Downloaded From JNTU World (http://www. Scan directory and Read file and display filename to output stream 3. its permissions and file properties and display to output stream. Open a directory of given directory name 2.h> #include<fcntl. 19. struct stat b. if(dp==NULL) { printf("\nDirectory opening problem\n").1 OBJECTIVE Write a C program that illustrates how to execute two commands concurrently with a command pipe.alljntuworld. st_mode) printf(" w"). printf("%9ld". printf("%4d ".b.st_mode) printf(" x").p->d_name).st_ctime). else printf(" -"). else printf(" -").alljntuworld./a.st_mode) printf(" w"). INPUT: vi 19.st_gid). if(S_IWUSR & b.st_mode) printf(" r"). if(S_IXOTH & b.st_mode) printf(" r").st_uid). } } ld break. else printf(" -"). printf("%4d ". if(S_IRGRP & b. else printf(" -").alljntuworld.in JNTU World or JN TU W } if(S_IRUSR & b. else printf(" -"). if(S_IWGRP & b. if(S_IXUSR & b.c cc 19. else printf(" -"). if(S_IXGRP & b. case S_IFIFO: printf(" p"). printf(" %s\n".st_mode) printf(" w").st_mode) printf(" x"). if(S_IROTH & b.c . else printf(" -").out 31 Downloaded From JNTU World (http://www.b. if(S_IWOTH & b.b.st_mode) printf(" x").b. printf("%6d ". break.in) .www.st_size). printf("%3d ". else printf(" -").st_mode) printf(" r").st_nlink). else printf(" -").b. www.alljntuworld.in JNTU World 19.5 PRE-LAB QUESTIONS 1. What is the purpose of stat() functions. 2. Define file system? 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 19.6 LAB ASSIGNMENT 1. Write a C program to find number of sub directories in given directory. JN TU W or 19.7 POST-LAB QUESTIONS 1. Which system call is used to remove any type of file. 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 W or 20.4 DESCRIPTION / PROCEDURE #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include<unistd.h> main() { int pfds[2]; char buf[30]; if(pipe(pfds) == -1) { perror("Pipe is not created"); exit(1); } if(!fork()) { printf("CHILD: Writing to the pipe\n"); write(pfds[1],"Hello World, I am child",23); printf("CHILD:Exiting"); exit(0); } else { printf("PARENT:reading from the pipe\n"); read(pfds[0],buf,23); printf("PARENT:Received Data is :%s",buf); }} ld 20.3 PROGRAM LOGIC 1. Create pipe using pipe() function and new process using fork() function 2. If parent process executing write a message to pipe at one end 3. If child process executing read a message from pipe at other end which is written by parent 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 20.5 PRE-LAB QUESTIONS 1. What are the return values of pipe function? 20.6 LAB ASSIGNMENT 1. Implement two way communication using pipes. 20.7 POST-LAB QUESTIONS 1. What is the use of read and write system calls 2. How many processes will create if 4 fork functions executed? 3. Illustrate difference between pipes and named pipes? 33 Downloaded From JNTU World (http://www.alljntuworld.in) 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 21.3 PROGRAM LOGIC 1. Create process using msgget(), identify process using a key 2. Declare structure to store message and message type. 3. Using msgsnd() function write a message to message queue. JN TU W or 21.4 DESCRIPTION / PROCEDURE #include<sys/types.h> #include<sys/ipc.h> #include<sys/msg.h> #include<stdio.h> #include<string.h> #include<stdlib.h> main() { int qid,len,i; char s[15]; struct { long mtype; char mtext[15]; }message,buff; qid=msgget((key_t)10,IPC_CREAT|0666); if(qid==-1) { perror("message queue create failed"); exit(1); } for(i=1;i<=3;i++) { printf("Enter the message to send\n"); scanf("%s",s); strcpy(message.mtext,s); message.mtype=i; len=strlen(message.mtext); if(msgsnd(qid,&message,len+1,0)==-1) { perror("message failed\n"); exit(1); } } } INPUT: cc prog21.c 34 Downloaded From JNTU World (http://www.alljntuworld.in) 7 POST-LAB QUESTIONS 1. What is structure of message queue. 35 Downloaded From JNTU World (http://www.6 LAB ASSIGNMENT 1. 2.5 PRE-LAB QUESTIONS 1. fifos and message queues ld 21.alljntuworld.msgrcv(). Describe use of pipe.alljntuworld.www. JN TU W or 21.msgsnd(). What is the purpose of msgget(). Implement message queues like sender and receiver. where receiver can receive the message in un-order.in JNTU World OUTPUT: Enter the message to send: hi Enter the message to send: hello.in) . how are you Enter the message to send: bye 21. exit(0).h> #include<sys/msg.i.in) . char s[15].2 RESOURCE/REQUIREMENTS Linux operating system.h> main() { int qid. exit(0). ii) Provide socket address (port.15. if(qid==-1) { perror("message queue create failed").c) that receives the messages (from the above message queue as specified in (22)) and displays them.1 OBJECTIVE Write a C program (receiver.out 36 Downloaded From JNTU World (http://www. iv) If successful use accept() in server.len. } } ld 22.IPC_CREAT|0666)./a.&buff.www.listen() in client and server respectively. struct { long mtype.h> #include<string. char mtext[15].3 i) Connect client and server with socket() fuction.i. } for(i=1.in JNTU World EXPERIMENT 22 22.i++) { if(msgrcv(qid.mtext).4 DESCRIPTION / PROCEDURE #include<sys/types.alljntuworld. vi –editor.h> #include<stdio.alljntuworld. }buff. qid=msgget((key_t)10. } printf("Message received from sender is %s\n".h> #include<stdlib.h> #include<sys/ipc.0)==-1) { perror("message failed\n"). iii) Use connect() and bind().Ip-address).c .buff. v) Use fprintf() and read() for sending and receiving the data for communication between server and client.i<=3. vi) Close the connectrion INPUT: cc prog22. 22. c-compiler JN TU W or 22. www.7 POST-LAB QUESTIONS 1. Implement message queues like sender and receiver.5 PRE-LAB QUESTIONS 1.in JNTU World OUTPUT: Message received from sender is: hi Message received from sender is: hello.alljntuworld. JN TU W or 22. where sender sends number and receiver can receive the message and find square of received number.6 LAB ASSIGNMENT 1. Which system call is used to remove or delete a message queue 37 Downloaded From JNTU World (http://www. how are you Message received from sender is: bye 22. How many message queues can create in linux programming and what are limitations for message queues? ld 22.alljntuworld.in) . 5 PRE-LAB QUESTIONS 1.alljntuworld.h> #include<sys/sem.1 OBJECTIVE Write a C program to allow cooperating processes to lock a resource for exclusive use.h> #include<sys/ipc. } ld 23. 23.7 POST-LAB QUESTIONS 1. union semun arg.h> #include<error.0. exit(1).SETVAL.0666|IPC_CREAT))== -1) { perror("semget"): exit(1).h> int main(void) { key_t key. if(semctl(semid. } arg. Which system call is used to remove or delete a semaphore? 38 Downloaded From JNTU World (http://www. int semid. How to control the process in shared segment 23. Create semaphore and create key using ftok() function 2. Use semctl() to control the process. using a) Semaphores b) flock or lockf system calls.alljntuworld.1.h> #include<stdlib. JN TU W or 23.c".arg)== -1) { perror("smctl").3 PROGRAM LOGIC 1.6 LAB ASSIGNMENT 1.in) .2 RESOURCE/REQUIREMENTS Linux operating system."j"))== -1) { perror("ftok"). if((key==ftok("sem demo.www. } if(semid=semget(key. c-compiler 23. vi –editor. Write a program for shared memory form of IPC using producer consumer relation in such a way that consumer should read only after the producer has written some text to the shared memory.in JNTU World EXPERIMENT 23 23.4 DESCRIPTION / PROCEDURE #include<stdio.h> #include<sys/types. 23. } return 0. exit(1).val=1. 24.2 RESOURCE/REQUIREMENTS Linux operating system.alljntuworld.3 PROGRAM LOGIC 1.4 DESCRIPTION / PROCEDURE //continue the process kill(pid. Which signals cannot catch using signal handler functions.7 POST-LAB QUESTIONS 1. c-compiler 24.1 OBJECTIVE Write a C program that illustrates suspending and resuming processes using signals.in) .SIGCONT). Use kill() to suspend and resume process 24.alljntuworld.SIGSTOP).5 PRE-LAB QUESTIONS 1. Define signal? Explain how to handle signals. SIGFPE.h> //suspend the process(same as hitting crtl+z) kill(pid.in JNTU World EXPERIMENT 24 24.6 LAB ASSIGNMENT 1.www.h> #include<signal. vi –editor. or #include<sys/types. W 24. 39 Downloaded From JNTU World (http://www. SIGQUIT JN TU 24. Write a program to handle the signals like SIGINT . ld 24. child_pid=fork().char* argv[]) { int sem_set_id. sem_op.alljntuworld. 25.www.semaphore setid„%d‟\n ”. sem_set_id=semget(ipc_private.3 PROGRAM LOGIC or ld 1.h> #include<stdlib.h> #include<sys/types. struct sembuf sem_op.h> #include<sys/sem.in) .sem_num=0.sem_flg=0.2 RESOURCE/REQUIREMENTS Linux operating system. 40 Downloaded From JNTU World (http://www.i++) { sem_op. create two new processes 4. create semaphore using semget( ) system call 2.alljntuworld.i<num_loops. case 0: for(i=0. if(sem_set_id==-1) { perror(“main:semget”). (using Semaphores).0600). struct timespec delay.i. int rc. clrscr(). exit(1).sem_op=-1.in JNTU World EXPERIMENT 25 25.sem_val.h> #include<sys/ipc. int child_pid. } printf(“semaphore set created. sem_set_id).1 OBJECTIVE Write a C program that implements a producer-consumer system with two processes. c-compiler 25.2. vi –editor. switch(child_pid) { case -1: perror(“fork”).4 DESCRIPTION / PROCEDURE #include<stdio. sem_op. exit(1).h> #define num_loops 2 int main(int argc.h> #include<unistd. if successful it returns positive value 3. until first process produces second process cannot consume JN TU W 25. first process will produce 5. Define semaphores? What is their purpose? List and explain the APIs used to create and control the semaphores. { delay. fflush(stdout).tv_sec=0.i). sem_op. } } break. } break. printf(“producer:‟%d‟\n”.1). sem_op.&sem_op.1).in TU INPUT AND OUTPUT: semaphore set created semaphore set id „327690‟ producer: „0‟ consumer:‟0‟ producer:‟1‟ consumer:‟1‟ 25.sem_num=0.7 POST-LAB QUESTIONS 1.i<num_loops. JN 25.in) .alljntuworld. if(rand()>3*(rano_max14)). nanosleep(&delay. semop(sem_set_id. default: for(i=0.sem_op=1.i). fflush(stdout). Write a C program to remove semaphores using semaphore functions. sem_op.6 LAB ASSIGNMENT 1.tv_nsec=10.null). } ld www.5 PRE-LAB QUESTIONS 1.i++) { printf(“consumer:‟%d‟\n”.sem_flg=0. 25. Which system call is used to remove or delete a semaphores? 41 Downloaded From JNTU World (http://www. } return 0.&sem_op. delay.JNTU World or W semop(sem_set_id.alljntuworld. 3 PROGRAM LOGIC 1. Close socket file descriptor after completion of communication.in) . 256. "hello from the server"). int socket_fd. return 0. Create socket at client side and server side for communication. connection_fd.h> #include <string. vi –editor. } 42 Downloaded From JNTU World (http://www. 2. Client side execute connect function to establish connection between client and server 4.www. pid_t child. nbytes = read(connection_fd. char buffer[256]. close(connection_fd). socklen_t address_length. socket_fd = socket(PF_UNIX.4 DESCRIPTION / PROCEDURE #include <stdio.h> #include <unistd. 26.h> ld 26. buffer). TU printf("MESSAGE FROM CLIENT: %s\n". buffer[nbytes] = 0. 256).2 RESOURCE/REQUIREMENTS Linux operating system. if(socket_fd < 0) { printf("socket() failed\n"). write(connection_fd.alljntuworld. nbytes = snprintf(buffer. W int connection_handler(int connection_fd) { int nbytes. c-compiler or 26.h> #include <sys/un. Exchange information through unix domain sockets.alljntuworld.h> #include <sys/socket. buffer.1 OBJECTIVE Write client and server programs (using c) for interaction between server and client processes using Unix Domain sockets.h> #include <sys/types. } JN int main(void) { struct sockaddr_un address. SOCK_STREAM. buffer. Server side execute bind and accept function to accept client 3. 0). return 1. 5.in JNTU World EXPERIMENT 26 26. nbytes). alljntuworld. 5) != 0) { printf("listen() failed\n"). int socket_fd.in) . char buffer[256]. } JN Client. snprintf(address. } W while((connection_fd = accept(socket_fd. ". } TU /* still inside server process */ close(connection_fd). return 0.h> #include <sys/un./demo_socket").h> #include <unistd. unlink(".alljntuworld. (struct sockaddr *) &address. } ld if(bind(socket_fd. /* start with a clean address structure */ memset(&address. or if(listen(socket_fd./demo_socket").in JNTU World unlink(".h> #include <sys/socket. 0.sun_path. } close(socket_fd). nbytes. (struct sockaddr *) &address. &address_length)) > -1) { child = fork().www. 43 Downloaded From JNTU World (http://www. return 1. UNIX_PATH_MAX.c #include <stdio. if(child == 0) { /* now inside newly created connection handling process */ return connection_handler(connection_fd).sun_family = AF_UNIX. return 1.h> int main(void) { struct sockaddr_un address. sizeof(struct sockaddr_un)) != 0) { printf("bind() failed\n"). address./demo_socket").h> #include <string. sizeof(struct sockaddr_un)). sun_family = AF_UNIX. nbytes). return 0. } TU 26. sizeof(struct sockaddr_un)) != 0) { printf("connect() failed\n"). buffer).7 POST-LAB QUESTIONS JN 1. 26. 0). printf("MESSAGE FROM SERVER: %s\n". ". return 1. 256./demo_socket").in JNTU World socket_fd = socket(PF_UNIX. 0. write(socket_fd. Define socket? 2. } /* start with a clean address structure */ memset(&address.5 PRE-LAB QUESTIONS 1. } nbytes = snprintf(buffer. buffer. buffer. snprintf(address. return 1.in) . (struct sockaddr *) &address. W nbytes = read(socket_fd.alljntuworld. Write a program to design a TCP client – server application which takes IP address. sizeof(struct sockaddr_un)). Port number and string to be echoed as command line inputs in client application and implements echo service. 256). close(socket_fd). or if(connect(socket_fd. if(socket_fd < 0) { printf("socket() failed\n"). ld address. "hello from a client"). 26. 44 Downloaded From JNTU World (http://www.www. Explain about IPV6 socket address structure and compare it with IPV4 and unix socket address structures. UNIX_PATH_MAX.alljntuworld.sun_path. buffer[nbytes] = 0. Differentiate between connection oriented and connection less communication.6 LAB ASSIGNMENT 1. SOCK_STREAM. c-compiler 27. sizeof(sendBuff)). 3.www.sin_family = AF_INET.c #include <sys/socket.listen() in client and server respectively. listen(listenfd. time_t ticks. 5. (struct sockaddr*)&serv_addr. connfd = 0. bind(listenfd.h> #include <errno.sin_port = htons(5000).3 PROGRAM LOGIC 1.s_addr = htonl(INADDR_ANY).h> #include <unistd. memset(sendBuff. serv_addr.in) . serv_addr. 45 Downloaded From JNTU World (http://www. vi –editor.alljntuworld. Provide socket address (port. 10). Connect client and server with socket() fuction. serv_addr.h> #include <arpa/inet. 6.1 OBJECTIVE Write client and server programs (using c) for interaction between server and client processes using Internet Domain sockets.4 DESCRIPTION / PROCEDURE TU W Server. Use fprintf() and read() for sending and receiving the data for communication between server and client. 27. memset(&serv_addr. listenfd = socket(AF_INET.h> #include <stdio. '0'.2 RESOURCE/REQUIREMENTS Linux operating system. Use connect() and bind(). 4.h> #include <netinet/in.in JNTU World EXPERIMENT 27 27.sin_addr. sizeof(serv_addr)). 2.h> #include <stdlib. If successful use accept() in server.h> #include <string. Ip-address) of Internet Domain Sockets.h> or ld 27. JN char sendBuff[1025].h> #include <sys/types. 0). sizeof(serv_addr)). struct sockaddr_in serv_addr.h> #include <time. '0'. char *argv[]) { int listenfd = 0.alljntuworld. Close the connection int main(int argc. SOCK_STREAM. sleep(1). ticks = time(NULL). snprintf(sendBuff.h> #include <errno. n = 0. struct sockaddr_in serv_addr. (struct sockaddr*)NULL. close(connfd). } JN memset(recvBuff. serv_addr. ctime(&ticks)).www.h> #include <stdlib.in JNTU World while(1) { connfd = accept(listenfd. if((sockfd = socket(AF_INET.h> #include <netinet/in. 46 Downloaded From JNTU World (http://www.h> #include <unistd. 0)) < 0) { printf("\n Error : Could not create socket \n"). sendBuff.alljntuworld.alljntuworld. return 1.in) . '0'. "%. strlen(sendBuff)). } memset(&serv_addr.argv[0]). ld } } W #include <sys/socket.sin_port = htons(5000). serv_addr. sizeof(serv_addr)).sizeof(recvBuff)).h> #include <sys/types. '0'.h> or Client.h> #include <string.h> #include <arpa/inet. SOCK_STREAM. return 1. if(argc != 2) { printf("\n Usage: %s <ip of server> \n". write(connfd.h> #include <stdio.h> #include <netdb. char recvBuff[1024]. NULL). char *argv[]) { int sockfd = 0.24s\r\n".sin_family = AF_INET.c TU int main(int argc. sizeof(sendBuff). Explain about IPV6 socket address structure and compare it with IPV4 and unix socket address structures. 47 Downloaded From JNTU World (http://www. return 1. recvBuff. stdout) == EOF) { printf("\n Error : Fputs error\n").in) .5 PRE-LAB QUESTIONS 1. Which system call is used to UDP Client Server Communication.alljntuworld. JN 27. } return 0. sizeof(recvBuff)-1)) > 0) { recvBuff[n] = 0. &serv_addr. if(fputs(recvBuff.sin_addr)<=0) { printf("\n inet_pton error occured\n").alljntuworld. } W if(n < 0) { printf("\n Read error \n").7 POST-LAB QUESTIONS 1. (struct sockaddr *)&serv_addr. return 1. sizeof(serv_addr)) < 0) { printf("\n Error : Connect Failed \n"). } or while ( (n = read(sockfd.www. } } TU 27. 27. Write a program to implement UDP client server application in which client takes an file name from the command line and sends to the server. argv[1].6 LAB ASSIGNMENT 1. } ld if( connect(sockfd. Server returns the content of received file to the client.in JNTU World if(inet_pton(AF_INET. } if ((shm = shmat(shmid. vi –editor. } s = shm. exit(0). 48 Downloaded From JNTU World (http://www.1 OBJECTIVE Implement shared memory form of IPC 28. while (*shm Linux operating system. int shmid.h> #include <sys/shm.in JNTU World EXPERIMENT 28 28.h> #include <sys/shm.alljntuworld. char *shm. for (c = 'a'. *s = NULL. c-compiler= '*') sleep(1). char *shm. c <= 'z'.alljntuworld. } shm_client.h> #define SHMSZ 27 main() { char c.h> #define SHMSZ 27 main() { int shmid. iii) Take a memory and attach this two process using shmat() iv) Now both the process can access the common memory.www. key = 5678. key_t key.in) .3 PROGRAM LOGIC i) Create two process . *s.4 DESCRIPTION / PROCEDURE JN TU W or #include <sys/types. key_t key. exit(1). exit(1). vi –editor. 28. c++) *s++ = c. ii) Use shmget() to connect the processes. if ((shmid = shmget(key. key = 5678.h> #include <sys/ipc. IPC_CREAT | 0666)) < 0) { perror("shmget"). 0)) == (char *) -1) { perror("shmat").2 RESOURCE/REQUIREMENTS Linux operating system. c-compiler ld 28.h> #include <stdio.c #include <sys/types.h> #include <stdio. *s.h> #include <sys/ipc. SHMSZ. NULL. *s != NULL. shmget() has how many parameters. } for (s = shm. JN TU W 28.www. What are the advantages of using shared memory? 2. NULL. Which system call used to assign or allocate memory to the process? 49 Downloaded From JNTU World (http://www.5 PRE-LAB QUESTIONS 1. s++) putchar(*s). } or 28.alljntuworld.in JNTU World if ((shmid = shmget(key. exit(1). 0666)) < 0) { perror("shmget").in) .7 POST-LAB QUESTIONS 1.e. 28. Write a c-program to implement shred memory among more than one process with effecting critical region (i. exit(1). exit(0).6 LAB ASSIGNMENT 1. 0)) == (char *) -1) { perror("shmat"). ld *shm = '*'. } if ((shm = shmat(shmid. putchar('\n').alljntuworld. synchronization). SHMSZ. JNTU World W or ld www.in JN TU DATA MINING LAB 50 Downloaded From JNTU World (http://www.alljntuworld.in) .alljntuworld. Find some training manuals for loan officers or perhaps a suitable textbook on finance.in) . Interview her and try to represent her knowledge in the form of production rules. You have to develop a system to help a loan officer decide whether the credit of a customer is good.alljntuworld. or 2. Translate this knowledge from text form to production rule form. 1. 4.alljntuworld. Common sense. German phone rates are much higher than in Canada so fewer people own telephones. the unit of currency. a bank cannot afford to make too many bad loans. a bank wants to make as many loans as possible. The goal is the classify the applicant into one of two categories. Assessing the credit worthiness of an applicant is of crucial importance. There are millions of these in Germany (many from Turkey). On the other hand. Find a loan officer who is willing to talk. and not too lenient. There are 20 attributes used in judging a loan applicant. approve a loan application. Interest on these loans is the ban‟s profit source. W The German Credit Data : Actual historical credit data is not always easy to come by because of confidentiality rules. Too many bad loans could lead to the collapse of the bank. The bank‟s loan policy must involve a compromise not too strict. On the one hand. Books. Imagine yourself as a loan officer and make up reasonable rules which can be used to judge the credit worthiness of a loan applicant. In spite of the fact that the data is German. you first and foremost need some knowledge about the world of credit . JN Subtasks : (Turn in your answers to the following tasks) 51 Downloaded From JNTU World (http://www. Foreign_worker. 3. Find records of actual cases where competent loan officers correctly judged when not to.in JNTU World Credit Risk Assessment Description: The business of banks is making loans. (Unless you really can consult a real loan officer !) A few notes on the German dataset : DM stands for Deutsche Mark. or bad. A bank‟s business rules regarding loans must consider two opposing factors. ld To do the assignment. It is very hard to get German citizenship if you were not born of German parents. Here is one such dataset (original) Excel spreadsheet version of the German credit data (download from web).www. TU Owns_telephone. You can acquire such knowledge in a number of ways. Knowledge Engineering. Case histories. you should probably make use of it for this assignment. good or bad. worth about 90 cents Canadian (but looks and acts like a quarter). csv”. JN TU W 1.4 OUTPUT: or 5) Clicking on any attribute in the left panel will show the basic statistics on that selected attribute. 3) Select Preprocess Tab.www.2 RESOURCES: Weka mining tool ld 1.in JNTU World EXPERIMENT-1 1.1 OBJECTIVE: List all the categorical (or nominal) attributes and the real-valued attributes separately. 4) Go to OPEN file and browse the file that is already stored in the system “bank.alljntuworld.in) . 2) Select EXPLORER present in Applications.3 PROCEDURE: 1) Open the Weka GUI Chooser. 52 Downloaded From JNTU World (http://www.alljntuworld. 1. The best known constraints are minimum thresholds on support and confidence. customers also buy butter. The confidence of a rule is defined. Suppose one of the large itemsets is Lk. One is to find those itemsets whose occurrences exceed a predefined threshold in the database. In practical applications. An example rule for the supermarket could be meaning that if milk and bread is bought.bread. 2. The problem is usually decomposed into two sub problems. those itemsets are called frequent or large itemsets. Those processes iterated until the antecedent becomes empty. To illustrate the concepts. we use a small example from the supermarket domain. the itemset {milk. I2. 53 Downloaded From JNTU World (http://www. W Note: this example is extremely small.5 in the database. and datasets often contain thousands or millions of transactions.in JNTU World EXPERIMENT-2 2.3 THEORY: Association rule mining is defined as: Let be a set of n binary attributes called items. The support supp(X) of an itemset X is defined as the proportion of transactions in the data set which contain the itemset. Confidence can be interpreted as an estimate of the probability P(Y | X). Let be a set of transactions called the database.in) . Lk = {I1. Ik}. by checking the confidence this rule can be determined as interesting or not. For example. 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. the rule has a confidence of 0. I2.alljntuworld. In the example database.2 RESOURCES: Weka mining tool or ld 2. JN ALGORITHM: Association rule mining is to find out association rules that satisfy the predefined minimum support and confidence from a given database.butter. a rule needs a support of several hundred transactions before it can be considered statistically significant. constraints on various measures of significance and interest can be used.4 since it occurs in 40% of all transactions (2 out of 5 transactions). … .www.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. The second problem is to generate association rules from those large itemsets with the constraints of minimal confidence. TU To select interesting rules from the set of all possible rules.alljntuworld. Then other rule are generated by deleting the last items in the antecedent and inserting it to the consequent. the probability of finding the RHS of the rule in transactions under the condition that these transactions also contain the LHS .4 = 0.Y C I and X Π Y=Φ . A rule is defined as an implication of the form X=>Y where X. … . further the confidences of the new rules are checked to determine the interestingness of them. Ik1} and {Ik}. Each transaction in D has a unique transaction ID and contains a subset of the items in I.2 / 0. association rules with this itemsets are generated in the following way: the first rule is {I1.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. which means that for 50% of the transactions containing milk and bread the rule is correct. bread} has a support of 2 / 5 = 0. The set of items is I = {milk. Where · (Ck: Candidate itemset of size k) · (Lk: frequent itemset of size k) or Apriori Pseudocode Apriori (T.in JNTU World Since the second subproblem is quite straight forward.0 -M 0. 5) Go to Associate Tab.9 -D 0. most of the researches focus on the first subproblem.alljntuworld.www.alljntuworld. The Apriori algorithm finds the frequent sets L In Database D.associations. · Join Step.0 -c -1 54 Downloaded From JNTU World (http://www.Apriori -N 10 -T 0 -C 0. ld o Any (k − 1) itemset that is not frequent cannot be a subset of a frequent k itemset. · Find frequent set Lk − 1. o Ck is generated by joining Lk − 1with itself · Prune Step.4 PROCEDURE: 1) Given the Bank database for mining. 4) Select only Nominal values.in) .05 -U 1. 2) Select EXPLORER in WEKA GUI Chooser. 6) Select Apriori algorithm from “Choose “ button present in Associator weka.t) W K<2 TU for candidates c € C(t) count[c]<count[ c]+1 L(k)<{ c € C(k)| count[c] ≥ £ K<K+ 1 JN return Ụ L(k) k 2.1 -S -1. 3) Load “Bank.£) L<{ Large 1itemsets that appear in more than transactions } while L(k1)≠ Φ C(k)<Generate( Lk − 1) for transactions t € T C(t)Subset(Ck.csv” in Weka by Open file in Preprocess tab. hence should be removed. www.alljntuworld.alljntuworld.in JNTU World 7) Select Start button 8) Now we can see the sample rules.5 OUTPUT: 55 Downloaded From JNTU World (http://www.in) . JN TU W or ld 2. JN TU W 3.csv”.www. or 4) Go to OPEN file and browse the file that is already stored in the system “bank.in JNTU World EXPERIMENT-3 3.3 PROCEDURE: 1) Open the Weka GUI Chooser.alljntuworld. 5) Clicking on any attribute in the left panel will show the basic statistics on that selected attribute.alljntuworld.4 OUTPUT: 56 Downloaded From JNTU World (http://www.2 RESOURCES: ld Weka mining tool 3. 3) Select Preprocess Tab.in) .1 OBJECTIVE: *What attributes do you think might be crucial in making the bank assessment? 3. 2) Select EXPLORER present in Applications. Train a decision tree using the complete dataset as the training data. the target attribute has only two possible values: for example. not a classification algorithm.Naive Bayes uses Bayes' Theorem. floating point values would indicate a numerical.1 OBJECTIVE: One type of model that you can create is a decision tree. The simplest type of classification problem is binary classification. high. In the model build (training) process. In binary classification. Scoring a classification model results in class assignments and probabilities for each case. The historical data for a classification project is typically divided into two data sets: one for building the model.2 RESOURCES: Weka mining tool ld 4.in JNTU World EXPERIMENT-4 4. the other attributes would be the predictors. 4. and biomedical and drug response modeling. Naive Bayes . For example. Classification has many applications in customer segmentation. TU Classifications are discrete and do not imply order. credit analysis. Credit rating would be the target. A predictive model with a numerical target uses a regression algorithm. medium. marketing. high credit rating or low credit rating. or unknown credit rating. Multiclass targets have more than two values: for example. which are conditional statements that reveal the logic used to build the tree. Different classification algorithms use different techniques for finding relationships. which can then be applied to a different data set in which the class assignments are unknown. medium. target. These relationships are summarized in a model.in) . a classification algorithm finds relationships between the values of the predictors and the values of the target. Report the model obtained after training. years of residence.www. and so on. A classification task begins with a data set in which the class assignments are known. business modeling. low. a formula that calculates a probability by counting the frequency of values and combinations of values in the historical data. a classification model could be used to identify loan applicants as low. or high value would also predict the probability of each classification for each customer. The goal of classification is to accurately predict the target class for each case in the data. JN Classification models are tested by comparing the predicted values to known target values in a set of test data. the data might track employment history. medium. Different Classification Algorithms: Oracle Data Mining provides the following algorithms for classification: Decision Tree . a classification model that predicts credit risk could be developed based on observed data for many loan applicants over a period of time. Continuous.alljntuworld.alljntuworld. number and type of investments. the other for testing the model. a model that classifies customers as low. and the data for each customer would constitute a case. rather than a categorical. or high credit risks.3 THEORY: or Classification is a data mining function that assigns items in a collection to target categories or classes. home ownership or rental. For example. For example. 57 Downloaded From JNTU World (http://www.Decision trees automatically generate rules. W In addition to the historical credit rating. 9) Click Start.alljntuworld.csv”.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.www. ld 6) Here the c4. clicking the button choose and select tree j48 7) Select Test options “Use training set” 8) if need select attribute. 10) Now we can see the output details in the Classifier output. JN TU W 4. 3) Select Preprocess Tab.4 PROCEDURE: 1) Open Weka GUI Chooser. 2) Select EXPLORER present in Applications.in JNTU World 4.in) .5 OUTPUT: or 11) Right click on the result list and select “visualize tree”option . 5) Go to Classify tab. 4) Go to OPEN file and browse the file that is already stored in the system “bank. in JN TU W The decision tree constructed by using the implemented C4.alljntuworld.in) .5 algorithm 59 Downloaded From JNTU World (http://www.alljntuworld.JNTU World or ld www. ...F1......3 THEORY: 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.....F1.. W 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....F2).Fn) which can be rewritten as follows. Because independent variables are assumed.. 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....Fn|C..F1)p(F3.......Fn) =p(C) p(F1.Fn)] In plain English the above equation can be written as Posterior= [(prior *likehood)/evidence] JN In practice we are only interested in the numerator of that fraction.F1.2 RESOURCES: ld Weka mining tool 5.F3.... 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 .in JNTU World EXPERIMENT-5 5. The problem is that if the number of features n is large or when a feature can take on a large number of values.....p(Fn|C.........in) .F2) = p(C)p(F1|C) p(F2|C.F1.. conditional on several feature variables F1 through Fn..Fn|C......F2) =p(C)p(F1|C) p(F2|C.. a naive Bayes classifier considers all of these properties to independently contribute to the probability that this fruit is an apple. 60 Downloaded From JNTU World (http://www.. The numerator is equivalent to the joint probability model p(C.F2......www....alljntuworld..............Fn1) Now the "naive" conditional independence assumptions come into play: assume that each feature Fi is conditionally independent of every other feature Fj for j≠i .. Even though these features depend on the existence of the other features..Fn) over a dependent class variable C with a small number of outcomes or classes.... We therefore reformulate the model to make it more tractable. round.Fn)=[{p(C)p(F1.. and classify credit good/bad for each of the examples in the dataset....Fn|C........F1..... we write P(C|F1..F1)p(F3.Fn|C) =p(C)p(F1|C) p(F2... then basing such a model on probability tables is infeasible. using repeated applications of the definition of conditional probability: p(C...Fn|C)}/p(F1. For example.F1........ and about 4" in diameter........ so that the denominator is effectively constant...... Using Bayes‟ theorem. a fruit may be considered to be an apple if it is red. since the denominator does not depend on C and the values of the features Fi are given.1 OBJECTIVE: Suppose you use your above model trained on the complete dataset..alljntuworld. ... it is computationally expensive to evaluate P(X/Ci) • Naïve Assumption of “class conditional independence” • P(X/Ci) = n P( xk/ Ci) 61 Downloaded From JNTU World (http://www...alljntuworld.... then the corresponding naive Bayes model has (k − 1) + n r k parameters..Fn)=p(C)p(F1|C)p(F2|C)...Fj)=p(Fi|C) and so the joint model can be expressed as p(C.C2...in) .. since they factor into a so called class prior p(C) and independent probability distributions p(Fi|C).. the conditional distribution over the class variable C can be expressed like this: p(C|F1.….F1. a constant if the values of the feature variables are known.x3. often k = 2 (binary classification) and r = 1 (Bernoulli variables as features) are common... In practice.. i.alljntuworld.....Fn...Fn)= p(C) πp(Fi|C) Z where Z is a scaling factor dependent only on F1.. If there are k classes and if a model for each p(Fi|C=c) can be expressed in terms of r parameters... and so the total number of parameters of the naive Bayes model is 2n + 1.x2.www....in JNTU World This means that p(Fi|C..e.... xn) • Let there me „m‟ Classes : C1. where n is the number of binary features used for prediction • P(D/h) : Probability of D given h Naïve Bayes Classifier : Derivation • D : Set of tuples TU – Each Tuple is an „n‟ dimensional attribute vector – X : (x1. P(h/D)= P(D/h) P(h) P(D) • P(h) : Prior probability of hypothesis h • P(D) : Prior probability of training data D W • P(h/D) : Probability of h given D or ld Models of this form are much more manageable.= p(C)π p(Fi|C) This means that under the above independence assumptions. j <> i JN • Maximum Posteriori Hypothesis – P(Ci/X) = P(X/Ci) P(Ci) / P(X) – Maximize P(X/Ci) P(Ci) as P(X) is constant Naïve Bayes Classifier : Derivation • With many attributes..C3…Cm • NB classifier predicts X belongs to Class Ci iff – P (Ci/X) > P(Cj/X) for 1<= j <= m . 6) Go to Classify tab.923 0.www.4 PROCEDURE: 1) Given the Bank database for mining.9979 % TU Mean absolute error Root relative squared error 52.. 0. 5) Go to OPEN file and browse the file that is already stored in the system “bank.106 0.914 0.2636 Relative absolute error 27.914 0. 3) Select EXPLORER present in Applications.csv”.9137 % Total Number of Instances 600 JN === Detailed Accuracy By Class === TP Rate FP Rate Precision Recall F-Measure ROC Area Class 0.936 NO 0. 7) Choose Classifier “Tree” 8) Select “NBTree” i. ld 4) Select Preprocess Tab.936 0.alljntuworld. 2) Use the Weka GUI Chooser.894 0. Navie Baysiean tree.948 0.e.in JNTU World k=1 • P(X/Ci) = P(x1/Ci) * P(x2/Ci) *…* P(xn/ Ci) 5.3333 % 7. 11) Now start weka.923 Weighted Avg.in) .alljntuworld. 9) Select Test options “Use training set” or 10) if need select attribute.052 0.845 92.5 OUTPUT: W === Evaluation on training set === === Summary === Correctly Classified Instances 554 Incorrectly Classified Instances 46 Kappa statistic 0.894 0. 12) Now we can see the output details in the Classifier output.1389 Root mean squared error 0.931 0.6667 % 0.924 0.948 0.081 0.935 0.923 62 Downloaded From JNTU World (http://www.936 YES 0. 5. alljntuworld.alljntuworld.in JNTU World === Confusion Matrix === A b 245 29 17 309 JN TU W or ld <-. b = NO 63 Downloaded From JNTU World (http://www.classified as a = YES .www.in) . .....F1... Because independent variables are assumed... root mean squared error.3 THEORY: 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..Fn|C..p(Fn|C.F3......F1.Fn|C) =p(C)p(F1|C) p(F2... and mean absolute error for weather data set? 6. round. The problem is that if the number of features n is large or when a feature can take on a large number of values.....Fn|C.F2) = p(C)p(F1|C) p(F2|C...F2)......F2) =p(C)p(F1|C) p(F2|C....... Even though these features depend on the existence of the other features...F1)p(F3... For example.. a naive Bayes classifier considers all of these properties to independently contribute to the probability that this fruit is an apple...www.. 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 ..2 RESOURCES: ld Weka mining tool 6. The numerator is equivalent to the joint probability model p(C..F1...F2.....alljntuworld. then basing such a model on probability tables is infeasible....F1..... so that the denominator is effectively constant.Fn) which can be rewritten as follows.....1 OBJECTIVE: *Find out the correctly classified instances.. W 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..Fn) over a dependent class variable C with a small number of outcomes or classes.. a fruit may be considered to be an apple if it is red.Fn)] In plain English the above equation can be written as Posterior= [(prior *likehood)/evidence] JN In practice we are only interested in the numerator of that fraction... conditional on several feature variables F1 through Fn..... TU Using Bayes‟ theorem......... We therefore reformulate the model to make it more tractable. kappa statistics....F1.Fn)=[{p(C)p(F1...Fn1) Now the "naive" conditional independence assumptions come into play: assume that each feature Fi is conditionally independent of every other feature Fj for j≠i . since the denominator does not depend on C and the values of the features Fi are given.F1)p(F3...Fn) =p(C) p(F1.... we write P(C|F1...in JNTU World EXPERIMENT-6 6..... using repeated applications of the definition of conditional probability: p(C......Fn|C...in) ......F1.Fn|C)}/p(F1. 64 Downloaded From JNTU World (http://www..... and about 4" in diameter.......alljntuworld. .x2. xn) • Let there me „m‟ Classes : C1.......Fn....alljntuworld.Fn)= p(C) πp(Fi|C) Z where Z is a scaling factor dependent only on F1...www.C2.C3…Cm • NB classifier predicts X belongs to Class Ci iff – P (Ci/X) > P(Cj/X) for 1<= j <= m .…. If there are k classes and if a model for each p(Fi|C=c) can be expressed in terms of r parameters..e. where n is the number of binary features used for prediction • P(D/h) : Probability of D given h Naïve Bayes Classifier : Derivation • D : Set of tuples TU – Each Tuple is an „n‟ dimensional attribute vector – X : (x1.in) .x3.. P(h/D)= P(D/h) P(h) P(D) • P(h) : Prior probability of hypothesis h • P(D) : Prior probability of training data D W • P(h/D) : Probability of h given D or ld Models of this form are much more manageable.. In practice.Fj)=p(Fi|C) and so the joint model can be expressed as p(C.. i. and so the total number of parameters of the naive Bayes model is 2n + 1.F1..... a constant if the values of the feature variables are known.. since they factor into a so called class prior p(C) and independent probability distributions p(Fi|C).....= p(C)π p(Fi|C) This means that under the above independence assumptions.. often k = 2 (binary classification) and r = 1 (Bernoulli variables as features) are common. then the corresponding naive Bayes model has (k − 1) + n r k parameters. the conditional distribution over the class variable C can be expressed like this: p(C|F1.......in JNTU World This means that p(Fi|C..alljntuworld.Fn)=p(C)p(F1|C)p(F2|C).. j <> i JN • Maximum Posteriori Hypothesis – P(Ci/X) = P(X/Ci) P(Ci) / P(X) – Maximize P(X/Ci) P(Ci) as P(X) is constant Naïve Bayes Classifier : Derivation • With many attributes.. it is computationally expensive to evaluate P(X/Ci) • Naïve Assumption of “class conditional independence” • P(X/Ci) = n P( xk/ Ci) 65 Downloaded From JNTU World (http://www. e.4 PROCEDURE: 1) Given the Bank database for mining.914 0. 7) Choose Classifier “Tree” 8) Select “NBTree” i.894 0.2636 Relative absolute error 27. 3) Select EXPLORER present in Applications.914 0.936 YES 0.081 0.www.5 OUTPUT: W === Evaluation on training set === === Summary === Correctly Classified Instances 554 Incorrectly Classified Instances 46 Kappa statistic 0.9137 % Total Number of Instances 600 JN Root relative squared error === Detailed Accuracy By Class === TP Rate FP Rate Precision Recall F-Measure ROC Area Class 0.052 0.6667 % 0.931 0.alljntuworld.923 0.923 66 Downloaded From JNTU World (http://www.csv”.894 0.948 0. 9) Select Test options “Use training set” or 10) if need select attribute. 6..9979 % TU Mean absolute error 52. 5) Go to OPEN file and browse the file that is already stored in the system “bank.936 0. 11) Now start weka.106 0. ld 4) Select Preprocess Tab.948 0. 2) Use the Weka GUI Chooser. Navie Baysiean tree.936 NO 0.3333 % 7.935 0.923 Weighted Avg.in JNTU World k=1 • P(X/Ci) = P(x1/Ci) * P(x2/Ci) *…* P(xn/ Ci) 6.in) .845 92. 12) Now we can see the output details in the Classifier output.alljntuworld.924 0. 6) Go to Classify tab. 0.1389 Root mean squared error 0. b = NO 67 Downloaded From JNTU World (http://www.alljntuworld.www.in) .in JNTU World === Confusion Matrix === A b 245 29 17 309 JN TU W or ld <-.classified as a = YES .alljntuworld. 3 percent) and the "Incorrectly Classified Instances" (7. 4) 5) Click Start(WEKA will run this test data set through the model we already created." it shows the number of false positives and false negatives. in the first row (the 0.2 RESOURCES: 7.1 OBJECTIVE: Is testing on the training set as you did above a good idea? Why or Why not? 7. in the "Confusion Matrix. ) Compare the output results with that of the 4th experiment 7.www.936). select the Supplied test set radio button ld Weka Mining tool Click Set 3) Choose the file which contains records that were not in the training set we used to create the model. we say that upon initial analysis. Finally. and the false negatives are 17 in this matrix. which indicates that the model will not break down with unknown data. The false positives are 29. 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. One final step to validating our classification tree.alljntuworld. we see the accuracy of the model. TU Based on our accuracy rate of 92.4 OUTPUT: or 2) W This can be experienced by the different problem solutions while doing practice. or when future data is applied to it. 68 Downloaded From JNTU World (http://www.3 percent. this is a good model.alljntuworld.in JNTU World EXPERIMENT-7 7. Other important numbers are in the "ROC Area" column.in) . The important numbers to focus on here are the numbers next to the "Correctly Classified Instances" (92.3 PROCEDURE: 1) In Test options.6 percent). 69 Downloaded From JNTU World (http://www.. classify or generalize. In decision analysis. Does your accuracy increase/decrease? Why? ld 8. The vector x is comprised of the input variables.alljntuworld. there are edges to children for each of the possible values of that input variable. x1. 2) Use the Weka GUI Chooser. 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. rather the resulting classification tree can be an input for decision making. This page deals with decision trees in data mining. used in data mining and machine learning.csv”.2 RESOURCES: Weka mining tool 8. y) The dependent variable. The recursion is completed when the subset at a node all has the same value of the target variable..in) . Each interior node corresponds to one of the input variables.3 THEORY: W or Decision tree learning. x3 etc. is the target variable that we are trying to understand. 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. Train a Decision Tree again using cross .. 5) Go to OPEN file and browse the file that is already stored in the system “bank. In data mining.in JNTU World EXPERIMENT-8 8.. trees can be described also as the combination of mathematical and computational techniques to aid the description.validation and report your results. TU A tree can be "learned" by splitting the source set into subsets based on an attribute value test.4 PROCEDURE: 1) Given the Bank database for mining. Data comes in records of the form: JN (x. 3) Select EXPLORER present in Applications.1 OBJECTIVE: One approach for solving the problem encountered in the previous question is using cross-validation? Describe what is cross -validation briefly.alljntuworld. Y. a decision tree can be used to visually and explicitly represent decisions and decision making. a decision tree describes data but not decisions. categorization and generalization of a given set of data. x2. The goal is to create a model that predicts the value of a target variable based on several input variables. x3. In data mining. y) = (x1. that are used for that task. xk. Decision tree learning is a common method used in data mining.www. x2. 4) Select Preprocess Tab. or when splitting no longer adds value to the predictions. leaves represent classifications and branches represent conjunctions of features that lead to those classifications. This process is repeated on each derived subset in a recursive manner called recursive partitioning. 8. alljntuworld. 7) Choose Classifier “Tree” 8) Select J48 9) Select Test options “Cross-validation”.5 OUTPUT: ld 14)Compare the output results with that of the 4th experiment === Stratified cross-validation === === Summary === Correctly Classified Instances Incorrectly Classified Instances Kappa statistic 539 89.in) .8333 % 61 10.in JNTU World 6) Go to Classify tab. 10) Set “Folds” Ex:10 11) if need select attribute.alljntuworld. 12) now Start weka.167 Root mean squared error 0.1667 % 0.www. 13)now we can see the output details in the Classifier output.305 70 Downloaded From JNTU World (http://www.7942 Mean absolute error 0. 15) check whether the accuracy increased or decreased? JN TU W or 8. 898 0.alljntuworld.889 0.2344 % Total Number of Instances 600 === Detailed Accuracy By Class === FP Rate Precision Recall F-Measure 0.883 NO === Confusion Matrix === a b <-.911 0.071 0.861 0.929 0.www.929 0.classified as 236 38 | a = YES 0.886 0.6511 % Root relative squared error 61.alljntuworld. 0.898 ROC Area ld TP Rate 71 Downloaded From JNTU World (http://www.139 0.in) .899 W 0.883 YES 0.909 0.in JNTU World Relative absolute error 33.108 Class or Weighted Avg.861 0.898 0.883 JN TU 23 303 | b = NO 0. in) .www. 72 Downloaded From JNTU World (http://www. or 9. 4) Select Preprocess Tab.csv”.attribute.3 PROCEDURE: W 5) Go to OPEN file and browse the file that is already stored in the system “bank. 6) In the "Filter" panel. This will show a popup window with list available filters. 7) Select “weka.alljntuworld. 22) Compare the output results with that of the 4th experiment 23) Check whether the accuracy increased or decreased. 13) Go to OPEN file and browse the file that is newly saved (attribute deleted file) 14) Go to Classify tab. 21) Right click on the result list and select ” visualize tree “option . 20) Now we can see the output details in the Classifier output.Remove” 8) Next.unsupervised. in the filter box you will see "Remove -R 1" 11) Click the "Apply" button to apply this filter to the data. 24) Check whether removing these attributes have any significant effect. JN 15) Choose Classifier “Tree” 16) Select j48 tree 17) Select Test options “Use training set” 18) If need select attribute.2 RESOURCES: Weka mining tool 1) Given the Bank database for mining. 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. 9. 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" . click on the "Choose" button.in JNTU World EXPERIMENT-9 9. Now. 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. 19) Now start weka. or "personal -status" (attribute 9). 3) Select EXPLORER present in Applications.1 OBJECTIVE ld Check to see if the data shows a bias against "foreign workers" (attribute 20). 2) Use the Weka GUI Chooser. Did removing these attributes have any significant effect? Discuss. To remove an attribute you can use the preprocess tab in Weka's GUI Explorer.alljntuworld.filters. www.alljntuworld.4 OUTPUT: 73 Downloaded From JNTU World (http://www.in JNTU World JN TU W or ld 9.alljntuworld.in) . which indicates that the model will not break down with unknown data. and the false negatives are 17 in this matrix.alljntuworld. 74 Downloaded From JNTU World (http://www. Other important numbers are in the "ROC Area" column. ) 5) Compare the output results with that of the 4th experiment 10.3 percent. What problem do you have and what is the solution? 10. finally.4 OUTPUT: W This can be experienced by the different problem solutions while doing practice. in the "Confusion Matrix.3 PROCEDURE: 1) In Test options.936). 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.6 percent). The false positives are 29.in) . we see the accuracy of the model. or when future data is applied to it. or 4) Click Start (WEKA will run this test data set through the model we already created.2 RESOURCES: 10.in JNTU World EXPERIMENT-10 10. this is a good model.arff‟ dataset in Weka and run the ID3 classification algorithm." it shows the number of false positives and false negatives. select the Supplied test set radio button 2) click Set ld Weka Mining tool 3) Choose the file which contains records that were not in the training set we used to create the model.1 OBJECTIVE: *Load the „weather. we say that upon initial analysis.alljntuworld.3 percent) and the "Incorrectly Classified Instances" (7.www. One final step to validating our classification tree. The important numbers to focus on here are the numbers next to the "Correctly Classified Instances" (92. TU Based on our accuracy rate of 92. in the first row (the 0. W 3) Select EXPLORER present in Applications.2 RESOURCES: 11. 7) The go to Classify tab. 3.1 OBJECTIVE: ld Another question might be. or Weka mining tool. Try out some combinations.in) .in JNTU World EXPERIMENT-11 11. With this step only the attributes necessary for classification are left in the attributes panel. (You had removed two attributes in problem 7. 7.alljntuworld.www.alljntuworld. For example. 13) Now we can see the output details in the Classifier output. do you really need to input so many attributes to get good results? Maybe only a few would do. 8) Choose Classifier “Tree” TU 9) Select j48 10) Select Test options “Use training set” 11) If need select attribute. 17 (and 21. 15) Compare the output results with that of the 4th experiment. 75 Downloaded From JNTU World (http://www.3 PROCEDURE: 1) Given the Bank database for mining. 5. 4) Select Preprocess Tab. Remember to reload the arff data file to get all the attributes initially before you start selecting the ones you want). 11. you could try just having attributes 2. 5) Go to OPEN file and browse the file that is already stored in the system “bank. 12) Now start Weka. the class attribute (naturally)). 6) Select some of the attributes from attributes list which are to be removed. 14) Right click on the result list and select “visualize tree” option. 2) Use the Weka GUI Chooser. JN 16) Check whether the accuracy increased or decreased? 17) Check whether removing these attributes have any significant effect. 10.csv”. alljntuworld.in) .www.4 OUTPUT: 76 Downloaded From JNTU World (http://www.in JNTU World JN TU W or ld 11.alljntuworld. 3 PROCEDURE: 6) Go to Classify tab. 22) Compare the results with that of experiment 6. 7) Choose Classifier “Tree” 8) Select j48 W 5) Go to OPEN file and browse the file that is already stored in the system “bank. 77 Downloaded From JNTU World (http://www. 19) Now start weka. give a higher cost to the first case (say cost 5) and lower cost to the second case.in) .1 OBJECTIVE: ld Sometimes. You can do this by using a cost matrix in Weka. 13) Click Ok 14) Click start. 9) Select Test options “Training set”. 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). 10) Click on “more options”. 11) Select cost sensitive evaluation and click on set button TU 12) Set the matrix values and click on resize. 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. 4) Select Preprocess Tab. 15) We can see the output details in the Classifier output 16) Select Test options “Cross-validation”. Then close the window. or 12.alljntuworld.alljntuworld. Instead of counting the misclassifications equally in both cases.in JNTU World EXPERIMENT-12 12.2 RESOURCES: Weka mining tool 1) Given the Bank database for mining. 17) Set “Folds” Ex: 10 JN 18) if need select attribute.www. 2) Use the Weka GUI Chooser. 3) Select EXPLORER present in Applications. 20) Now we can see the output details in the Classifier output.csv”. 21) Compare results of 15th and 20th steps. in JNTU World JN TU W or ld 12.www.alljntuworld.in) .4 OUTPUT: 78 Downloaded From JNTU World (http://www.alljntuworld. 79 Downloaded From JNTU World (http://www.alljntuworld.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 W or This will be based on the attribute set.www.in JNTU World EXPERIMENT-13 13.alljntuworld.in) . and the requirement of relationship among attribute we want to study. This can be viewed based on the database and user requirement. 80 Downloaded From JNTU World (http://www.csv”.e.in) . Compare the accuracy results. 8) Choose Classifier “Tree” 9) Select “NBTree” i. Navie Bayesian tree. 5) Go to OPEN file and browse the file that is already stored in the system “bank. JN 6) Select some of the attributes from attributes list 7) Go to Classify tab.in JNTU World EXPERIMENT-14 14.1 OBJECTIVE: *Run the J48 and 1Bk classifiers using-the cross-validation strategy with various fold levels.4 PROCEDURE: 1) Given the Bank database for mining. 3) Select EXPLORER present in Applications. 4) Select Preprocess Tab.alljntuworld.www. 2) Use the Weka GUI Chooser.alljntuworld.. select show properties 12) Now change unprune mode “false” to “true”. 14) If need select attribute. 16) Now we can see the output details in the Classifier output. 13) Change the reduced error pruning % as needed. Hold out strategy with three percentage levels. Compare the accuracy results. 14. 10) Select Test options “Use training set” 11) Right click on the text box besides choose button.2 RESOURCES: 14.3 THEORY: Reduced-error pruning Each node of the (over-fit) tree is examined for pruning ld Weka mining tool Pruning a node consists of Removing the sub-tree rooted at the pruned node Making the pruned node a leaf node Assigning the pruned node the most common classification of the training instances attached to that node Pruning nodes iteratively W 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. 15) Now start Weka. alljntuworld.in JNTU World 17) Right click on the result list and select “visualize tree” option. JN TU W or ld 14.5 OUTPUT: 81 Downloaded From JNTU World (http://www.in) .www.alljntuworld. Does your accuracy increase? Weka mining tool 15. 5) Go to OPEN file and browse the file that is already stored in the system “bank.alljntuworld. one approach is to use Reduced Error Pruning -Explain this idea briefly. select show properties 12) Now change unprone mode “false” to “true”.1 OBJECTIVE: You can make your Decision Trees simpler by pruning the nodes. report your accuracy using the pruned model.2 RESOURCES: A node is pruned (removed) only if the resulting pruned tree performs no worse than the original over the validation set Pruning a node consists of • Removing the sub-tree rooted at the pruned node W • Making the pruned node a leaf node • Assigning the pruned node the most common classification of the training instances attached to that node Pruning nodes iteratively • 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. 82 Downloaded From JNTU World (http://www. 6) Select some of the attributes from attributes list 7) Go to Classify tab.www.in) . JN 4) Select Preprocess Tab.csv”.3 THEORY: Reduced-error pruning or Each node of the (over-fit) tree is examined for pruning ld 15. 10) Select Test options “Use training set” 11) Right click on the text box besides choose button.in JNTU World EXPERIMENT-15 15. 13) Change the reduced error pruning % as needed. 8) Choose Classifier “Tree” 9) Select “NBTree” i. Navie Bayesian tree. 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.alljntuworld.4 PROCEDURE: 1) Given the Bank database for mining. 3) Select EXPLORER present in Applications.e.. 2) Use the Weka GUI Chooser. in JNTU World 14) If need select attribute. 16) Now we can see the output details in the Classifier output.www.5 OUTPUT: 83 Downloaded From JNTU World (http://www. 17) Right click on the result list and select ” visualize tree “option. JN TU W or ld 15.alljntuworld.in) . 15) Now start weka.alljntuworld. csv”. 14) Right click on the result list and select ” visualize tree “option .in) . 12) Now start weka.csv”.www. or 16.2 RESOURCES: Weka mining tool. 4) Select Preprocess Tab. Make up your own small Decision Tree consisting of 2 . PART and OneR. Rank the performance of j48. 3) Select EXPLORER present in Applications.trees. 5) Go to OPEN file and browse the file that is already stored in the system “bank.classifiers. 16.3 PROCEDURE: W 4) Select Preprocess Tab. 1) Given the Bank database for mining. 3) Select EXPLORER present in Applications. yes. 10) Select Test options “Use training set” 84 Downloaded From JNTU World (http://www. 2) Use the Weka GUI Chooser. 10) Select Test options “Use training set” TU 11) If need select attribute. 13) Now we can see the output details in the Classifier output. 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). (or) java weka.arff JN Procedure for “OneR”: 1) Given the Bank database for mining. Sometimes just one attribute can be good enough in making the decision. Report the rule obtained by training a one R classifier. 8) Choose Classifier “Rules” 9) Select “OneR”. PART.in JNTU World EXPERIMENT-16 16.alljntuworld. train this model and report the set of rules obtained.3 levels and convert it into a set of rules.1 OBJECTIVE: ld (Extra Credit): How can you convert a Decision Trees into "if –then -else rules". 5) Go to OPEN file and browse the file that is already stored in the system “bank.J48 -t c:\temp\bank. 6) Select some of the attributes from attributes list 7) Go to Classify tab. 6) Select some of the attributes from attributes list 7) Go to Classify tab. 2) Use the Weka GUI Chooser. 8) Choose Classifier “Trees Rules” 9) Select “J48”.alljntuworld. There also exist different classifiers that output the model in the form of rules -one such classifier in Weka is rules. 12) Now start weka.alljntuworld.www. 10) Select Test options “Use training set” 11) if need select attribute. 6) Select some of the attributes from attributes list 7) Go to Classify tab. Attribute relevance with respect to the class – relevant attribute (science) W IF accounting=1 THEN class=A (Error=0. Procedure for “PART”: 1) Given the Bank database for mining. 9) Select “PART”. 13) Now we can see the output details in the Classifier output. or 8) Choose Classifier “Rules”. ld 5) Go to OPEN file and browse the file that is already stored in the system “bank.csv”. Coverage = 7 instance) IF accounting=0 THEN class=B (Error=4/13.4 OUTPUT: 85 Downloaded From JNTU World (http://www. 4) Select Preprocess Tab.alljntuworld. 2) Use the Weka GUI Chooser.in JNTU World 11) if need select attribute. Coverage = 13 instances) JN TU 12. 3) Select EXPLORER present in Applications. 12) Now start weka.in) . 13) Now we can see the output details in the Classifier output. alljntuworld.in) .in 86 Downloaded From JNTU World (http://www.JNTU World JN TU One R W J48 or ld www.alljntuworld. www.alljntuworld.in JNTU World JN TU W 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 Naïve 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 Naïve Bayes perform better? 17.2 RESOURCES: Weka mining tool. 1) Given the Bank database for mining. 2) Use the Weka GUI Chooser. 3) Select EXPLORER present in Applications. 4) Select Preprocess Tab. 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. W 6) Select some of the attributes from attributes list 8) Choose Classifier “Trees Rules” 9) Select “J48”. 10) Select Test options “Use training set” 11) If need select attribute. TU 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 . (or) java weka.classifiers.trees.J48 -t c:\temp\bank.arff JN Procedure for “OneR”: 1) Given the Bank database for mining. 2) Use the Weka GUI Chooser. 3) Select EXPLORER present in Applications. 4) Select Preprocess Tab. 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 “Rules” 9) Select “OneR”. 10) Select Test options “Use training set” 11) if need select attribute. 88 Downloaded From JNTU World (http://www.alljntuworld.in) www.alljntuworld.in JNTU World 12) Now start weka. 13) Now we can see the output details in the Classifier output. Procedure for “PART”: 1) Given the Bank database for mining. 2) Use the Weka GUI Chooser. 3) Select EXPLORER present in Applications. 4) Select Preprocess Tab. 5) Go to OPEN file and browse the file that is already stored in the system “bank.csv”. ld 6) Select some of the attributes from attributes list 7) Go to Classify tab. 8) Choose Classifier “Rules”. 9) Select “PART”. 11) if need select attribute. 12) Now start weka. or 10) Select Test options “Use training set” 13) Now we can see the output details in the Classifier output. Attribute relevance with respect to the class – relevant attribute (science) IF accounting=1 THEN class=A (Error=0, Coverage = 7 instance) 17.4 OUTPUT: JN TU J48 W IF accounting=0 THEN class=B (Error=4/13, Coverage = 13 instances) 89 Downloaded From JNTU World (http://www.alljntuworld.in) www.in JNTU World JN TU PART W or ld One R 90 Downloaded From JNTU World (http://www.alljntuworld.in) .alljntuworld.
Report "Linux Programming and Data Mining Lab Manual"