LINUX PROGRAMMING AND DATA MINING LAB MANUAL

LINUX PROGRAMMING AND DATA MINIGLAB MANUAL

IV-BTECH

VID

Contents

S.No / Topic / Page no
3. / Week1
1. 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. / 7
2. Write a shell script that deletes all lines containing a specified word in one or more files supplied as arguments to it.
3. 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.
4. 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.
4. / Week 2
5. 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 / 10
6. Write a shell script to list all of the directory files in a directory.
7. Write a shell script to find factorial of a given integer.
5. / Week 3
8. Write an awk script to count the number of lines in a file that do not contain vowels. / 13
9. Write an awk script to find the number of characters, words and lines in a file.
10. Write a c program that makes a copy of a file using standard I/O and system calls
6. / Week 4
11. Implement in C the following UNIX commands using System calls
A. cat B. ls C. mv / 15
12. 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.
7. / Week 5
13. Write a C program to emulate the UNIX ls –l command. / 19
14. Write a C program to list for every file in a directory, its inode number and file name.
15. Write a C program that demonstrates redirection of standard output to a file.
Ex: ls > f1.
8. / Week 6
16. 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. / 29
17. Write a C program to create a Zombie process.
18. Write a C program that illustrates how an orphan is created.
9. / Week 7
19. Write a C program that illustrates how to execute two commands concurrently with a command pipe.
Ex: - ls –l | sort / 31
20. Write C programs that illustrate communication between two unrelated processes using named pipe
21. Write a C program to create a message queue with read and write permissions to write 3 messages to it with different priority numbers.
22. Write a C program that receives the messages (from the above message queue as specified in (21)) and displays them.
10. / Week 8
23. Write a C program to allow cooperating processes to lock a resource for exclusive use, using a) Semaphores b) flock or lockf system calls. / 40
24. Write a C program that illustrates suspending and resuming processes using signals
11. / Week 9
25. Write a C program that implements a producer-consumer system with two processes. / 41
(Using Semaphores).
26. Write client and server programs (using c) for interaction between server and client processes using Unix Domain sockets.
12. / Week 10
27. Write client and server programs (using c) for interaction between server and client processes using Internet Domain sockets. / 47
28. Write a C program that illustrates two processes communicating using shared memory
13. / Listing of categorical attributes and the real-valued attributes separately. / 55
14. / Rules for identifying attributes. / 56
15. / Training a decision tree. / 59
16. / Test on classification of decision tree. / 63
17. / Testing on the training set . / 67
18. / Using cross –validation for training. / 68
19. / Significance of attributes in decision tree. / 71
20. / Trying generation of decision tree with various number of decision tree. / 74
21. / Find out differences in results using decision tree and cross-validation on a data set. / 76
22. / Decision trees. / 78
23. / Reduced error pruning for training Decision Trees using cross-validation / 78
24. / Convert a Decision Trees into "if-then-else rules". / 81

Week1

  1. 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.

Aim: ToWrite 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.

Script:

$ awk ‘NR<2 || NR> 4 {print $0}’ 5 lines.dat

I/P: line1

line2

line3

line4

line5

O/P: line1

line5

  1. Write a shell script that deletes all lines containing a specified word in one or more files supplied as arguments to it.

Aim: To write a shell script that deletes all lines containing a specified word in one or more files supplied as arguments to it.

Script:

clear

i=1

while [ $i -le $# ]

do

grep -v Unix $i > $i

done

Output:

$ sh 1b.sh test1

the contents before deleting

test1

hello

hello

bangalore

mysore city

enter the word to be deleted

city

after deleting

hello

hello

Bangalore

$ sh 1b.sh

no argument passed

3. 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.

Aim: To 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.

Script:

echo "enter the directory name"

read dir

if [ -d $dir ]

then

cd $dir

ls > f

exec < f

while read line

do

if [ -f $line ]

then

if [ -r $line -a -w $line -a -x $line ]

then

echo "$line has all permissions"

else

echo "files not having all permissions"

fi

fi

done

fi

  1. 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

Aim: To write a shell script that receives any number of file names as arguments checks if every argument supplied is a file or a directory

Script:

for x in $*

do

if [ -f $x ]

then

echo " $x is a file "

echo " no of lines in the file are "

wc -l $x

elif [ -d $x ]

then

echo " $x is a directory "

else

echo " enter valid filename or directory name "

fi

done

Week 2

  1. 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.

Aim : To 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.

Script:

if [ $# -ne 2 ]

then

echo "Error : Invalid number of arguments."

exit

fi

str=`cat $1 | tr '\n' ' '`

for a in $str

do

echo "Word = $a, Count = `grep -c "$a" $2`"

done

Output :

$ cat test

hello ATRI

$ cat test1

hello ATRI

hello ATRI

hello

$ sh 1.sh test test1

Word = hello, Count = 3

Word = ATRI, Count = 2

6. Write a shell script to list all of the directory files in a directory.

Script:

# !/bin/bash

echo"enter directory name"

read dir

if[ -d $dir]

then

echo"list of files in the directory"

ls $dir

else

echo"enter proper directory name"

fi

Output:

Enter directory name

Atri

List of all files in the directoty

CSE.txt

ECE.txt

7. Write a shell script to find factorial of a given integer.

Script:

# !/bin/bash

echo "enter a number"

read num

fact=1

while [ $num -ge 1 ]

do

fact=`expr $fact\* $num`

let num--

done

echo "factorial of $n is $fact"

Output:

Enter a number

5

Factorial of 5 is 120

Week 3

8. Write an awk script to count the number of lines in a file that do not contain vowels.

9. Write an awk script to find the number of characters, words and lines in a file.

Aim : To write an awk script to find the number of characters, words and lines in a file.

Script:

BEGIN{print "record.\t characters \t words"}

#BODY section

{

len=length($0)

total_len+=len

print(NR,":\t",len,":\t",NF,$0)

words+=NF

}

END{

print("\n total")

print("characters :\t" total len)

print("lines :\t" NR)

}

10. Write a c program that makes a copy of a file using standard I/O and system calls

#include <unistd.h>

#include <fcntl.h>

int main(int argc, char *argv[]){

int fd1, fd2;

char buffer[100];

long int n1;

if(((fd1 = open(argv[1], O_RDONLY)) == -1) ||

((fd2 = open(argv[2], O_CREAT|O_WRONLY|O_TRUNC,

0700)) == -1)){

perror("file problem ");

exit(1);

}

while((n1=read(fd1, buffer, 100)) > 0)

if(write(fd2, buffer, n1) != n1){

perror("writing problem ");

exit(3);

}

// Case of an error exit from the loop

if(n1 == -1){

perror("Reading problem ");

exit(2);

}

close(fd2);

exit(0);

}

Week 4

11. Implement in C the following UNIX commands using System calls

A. cat B. ls C. mv

AIM: Implement in C the cat Unix command using system calls

#include<fcntl.h>

#include<sys/stat.h>

#define BUFSIZE 1

int main(int argc, char **argv)

{

int fd1;

int n;

char buf;

fd1=open(argv[1],O_RDONLY);

printf("Welcome to ATRI\n");

while((n=read(fd1,&buf,1))>0)

{

printf("%c",buf);

/* or

write(1,&buf,1); */

}

return (0);

}

AIM: Implement in C the following ls Unix command using system calls

Algorithm:

1. Start.

2. open directory using opendir( ) system call.

3. read the directory using readdir( ) system call.

4. print dp.name and dp.inode .

5. repeat above step until end of directory.

6. End

#include <sys/types.h>

#include <sys/dir.h>

#include <sys/param.h>

#include <stdio.h>

#define FALSE 0

#define TRUE 1

extern int alphasort();

char pathname[MAXPATHLEN];

main() {

int count,i;

struct dirent **files;

int file_select();

if (getwd(pathname) == NULL )

{ printf("Error getting pathn");

exit(0);

}

printf("Current Working Directory = %sn",pathname);

count = scandir(pathname, &files, file_select, alphasort);

if (count <= 0)

{

printf("No files in this directoryn");

exit(0);

}

printf("Number of files = %dn",count);

for (i=1;i<count+1;++i)

printf("%s \n",files[i-1]->d_name);

}

int file_select(struct direct *entry)

{

if ((strcmp(entry->d_name, ".") == 0) ||(strcmp(entry->d_name, "..") == 0))

return (FALSE);

else

return (TRUE);

}

AIM: Implement in C the Unix command mv using system calls

Algorithm:

1. Start

2. open an existed file and one new open file using open()

system call

3. read the contents from existed file using read( ) system

call

4. write these contents into new file using write system

call using write( ) system call

5. repeat above 2 steps until eof

6. close 2 file using fclose( ) system call

7. delete existed file using using unlink( ) system

8. End.

Program:

#include<fcntl.h>

#include<stdio.h>

#include<unistd.h>

#include<sys/stat.h>

int main(int argc, char **argv)

{

int fd1,fd2;

int n,count=0;

fd1=open(argv[1],O_RDONLY);

fd2=creat(argv[2],S_IWUSR);

rename(fd1,fd2);

unlink(argv[1]);

printf(“ file is copied “);

return (0);

}

12. 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.

#include<stdio.h>

main()

{

FILE *stream;

int buffer_character;

stream=fopen(“test”,”r”);

if(stream==(FILE*)0)

{

fprintf(stderr,”Error opening file(printed to standard error)\n”);

fclose(stream);

exit(1);

}

}

if(fclose(stream))==EOF)

{

fprintf(stderr,”Error closing stream.(printed to standard error)\n);

exit(1);

}

return();

}

Week 5

13. Write a C program to emulate the UNIX ls –l command.

ALGORITHM :

Step 1: Include necessary header files for manipulating directory.

Step 2: Declare and initialize required objects.

Step 3: Read the directory name form the user.

Step 4: Open the directory using opendir() system call and report error if the directory is not

available.

Step 5: Read the entry available in the directory.

Step 6: Display the directory entry ie., name of the file or sub directory.

Step 7: Repeat the step 6 and 7 until all the entries were read.

/* 1. Simulation of ls command */

#include<fcntl.h>

#include<stdio.h>

#include<unistd.h>

#include<sys/stat.h>main()

{

char dirname[10];

DIR *p;

struct dirent *d;

printf("Enter directory name ");

scanf("%s",dirname);

p=opendir(dirname);

if(p==NULL)

{

perror("Cannot find dir.");

exit(-1);

}

while(d=readdir(p))

printf("%s\n",d->d_name);

}

SAMPLE OUTPUT:

enter directory name iii

...

f2

14. Write a C program to list for every file in a directory, its inode number and file name.

TheDirentstructure contains the inode number and the name. The maximum length of a filename component isNAME_MAX, which is a system-dependent value.opendirreturns a pointer to a structure calledDIR, analogous toFILE, which is used byreaddirandclosedir. This information is collected into a file calleddirent.h.

#define NAME_MAX 14 /* longest filename component; */

/* system-dependent */

typedef struct { /* portable directory entry */

long ino; /* inode number */

char name[NAME_MAX+1]; /* name + '\0' terminator */

} Dirent;

typedef struct { /* minimal DIR: no buffering, etc. */

int fd; /* file descriptor for the directory */

Dirent d; /* the directory entry */

} DIR;

DIR *opendir(char *dirname);

Dirent *readdir(DIR *dfd);

void closedir(DIR *dfd);

The system callstattakes a filename and returns all of the information in the inode for that file, or-1if there is an error. That is,

char *name;

struct stat stbuf;

int stat(char *, struct stat *);

stat(name, &stbuf);

fills the structurestbufwith the inode information for the file name. The structure describing the value returned bystatis in<sys/stat.h>, and typically looks like this:

struct stat /* inode information returned by stat */

{

dev_t st_dev; /* device of inode */

ino_t st_ino; /* inode number */

short st_mode; /* mode bits */

short st_nlink; /* number of links to file */

short st_uid; /* owners user id */

short st_gid; /* owners group id */

dev_t st_rdev; /* for special files */

off_t st_size; /* file size in characters */

time_t st_atime; /* time last accessed */

time_t st_mtime; /* time last modified */

time_t st_ctime; /* time originally created */

};

Most of these values are explained by the comment fields. The types likedev_tandino_tare defined in<sys/types.h>, which must be included too.

Thest_modeentry contains a set of flags describing the file. The flag definitions are also included in<sys/types.h>; we need only the part that deals with file type:

#define S_IFMT 0160000 /* type of file: */

#define S_IFDIR 0040000 /* directory */

#define S_IFCHR 0020000 /* character special */

#define S_IFBLK 0060000 /* block special */

#define S_IFREG 0010000 /* regular */

/* ... */

Now we are ready to write the programfsize. If the mode obtained fromstatindicates that a file is not a directory, then the size is at hand and can be printed directly. If the name is a directory, however, then we have to process that directory one file at a time; it may in turn contain sub-directories, so the process is recursive.

The main routine deals with command-line arguments; it hands each argument to the functionfsize.

#include <stdio.h>

#include <string.h>

#include "syscalls.h"

#include <fcntl.h> /* flags for read and write */

#include <sys/types.h> /* typedefs */

#include <sys/stat.h> /* structure returned by stat */

#include "dirent.h"

void fsize(char *)

/* print file name */

main(int argc, char **argv)

{

if (argc == 1) /* default: current directory */

fsize(".");

else

while (--argc > 0)

fsize(*++argv);

return 0;

}

The functionfsizeprints the size of the file. If the file is a directory, however,fsizefirst callsdirwalkto handle all the files in it. Note how the flag namesS_IFMTandS_IFDIRare used to decide if the file is a directory. Parenthesization matters, because the precedence ofis lower than that of==.

int stat(char *, struct stat *);

void dirwalk(char *, void (*fcn)(char *));

/* fsize: print the name of file "name" */

void fsize(char *name)

{

struct stat stbuf;

if (stat(name, &stbuf) == -1) {

fprintf(stderr, "fsize: can't access %s\n", name);

return;

}

if ((stbuf.st_mode & S_IFMT) == S_IFDIR)

dirwalk(name, fsize);

printf("%8ld %s\n", stbuf.st_size, name);

}

The functiondirwalkis a general routine that applies a function to each file in a directory. It opens the directory, loops through the files in it, calling the function on each, then closes the directory and returns. Sincefsizecallsdirwalkon each directory, the two functions call each other recursively.

#define MAX_PATH 1024

/* dirwalk: apply fcn to all files in dir */

void dirwalk(char *dir, void (*fcn)(char *))

{

char name[MAX_PATH];

Dirent *dp;

DIR *dfd;

if ((dfd = opendir(dir)) == NULL) {

fprintf(stderr, "dirwalk: can't open %s\n", dir);

return;

}

while ((dp = readdir(dfd)) != NULL) {

if (strcmp(dp->name, ".") == 0

|| strcmp(dp->name, ".."))

continue; /* skip self and parent */

if (strlen(dir)+strlen(dp->name)+2 > sizeof(name))

fprintf(stderr, "dirwalk: name %s %s too long\n",

dir, dp->name);

else {

sprintf(name, "%s/%s", dir, dp->name);

(*fcn)(name);

}

}

closedir(dfd);

}

Each call toreaddirreturns a pointer to information for the next file, orNULLwhen there are no files left. Each directory always contains entries for itself, called".", and its parent,".."; these must be skipped, or the program will loop forever.

Down to this last level, the code is independent of how directories are formatted. The next step is to present minimal versions ofopendir,readdir, andclosedirfor a specific system. The following routines are for Version 7 and System V UNIX systems; they use the directory information in the header<sys/dir.h>, which looks like this:

#ifndef DIRSIZ

#define DIRSIZ 14

#endif

struct direct { /* directory entry */

ino_t d_ino; /* inode number */

char d_name[DIRSIZ]; /* long name does not have '\0' */

};

Some versions of the system permit much longer names and have a more complicated directory structure.

The typeino_tis atypedefthat describes the index into the inode list. It happens to beunsigned shorton the systems we use regularly, but this is not the sort of information to embed in a program; it might be different on a different system, so thetypedefis better. A complete set of ``system'' types is found in<sys/types.h>.

opendiropens the directory, verifies that the file is a directory (this time by the system callfstat, which is likestatexcept that it applies to a file descriptor), allocates a directory structure, and records the information:

int fstat(int fd, struct stat *);

/* opendir: open a directory for readdir calls */

DIR *opendir(char *dirname)

{

int fd;

struct stat stbuf;

DIR *dp;

if ((fd = open(dirname, O_RDONLY, 0)) == -1

|| fstat(fd, &stbuf) == -1

|| (stbuf.st_mode & S_IFMT) != S_IFDIR

|| (dp = (DIR *) malloc(sizeof(DIR))) == NULL)

return NULL;

dp->fd = fd;

return dp;