LINUX PROGRAMMING AND DATA MINING LAB MANUAL



LINUX PROGRAMMING AND DATA MINIG LAB MANUAL

IV-BTECH

VID

Contents

|S.No |Topic |Page no |

| |Week1 |7 |

| |1. Write a shell script that accepts a file name, starting and ending line numbers as arguments and displays all the | |

|3. |lines between the given line numbers. | |

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

| |Week 2 |10 |

| |5. Write a shell script that accepts a list of file names as its arguments, counts and reports the occurrence of each | |

|4. |word that is present in the first argument file on other argument files | |

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

| |Week 3 |13 |

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

|5. | | |

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

| |Week 4 |15 |

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

|6. |A. cat B. ls C. mv | |

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

| |Week 5 |19 |

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

|7. | | |

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

| |Week 6 |29 |

| |16. Write a C program to create a child process and allow the parent to display “parent” and the child to | |

|8. |display “child” on the screen. | |

| |17. Write a C program to create a Zombie process. | |

| |18. Write a C program that illustrates how an orphan is created. | |

| |Week 7 |31 |

| |19. Write a C program that illustrates how to execute two commands concurrently with a command pipe. | |

|9. |Ex: - ls –l | sort | |

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

| |Week 8 |40 |

| |23. Write a C program to allow cooperating processes to lock a resource for exclusive use, using a) Semaphores | |

|10. |b) flock or lockf system calls. | |

| |24. Write a C program that illustrates suspending and resuming processes using signals | |

| |Week 9 |41 |

|11. |25. Write a C program that implements a producer-consumer system with two processes. | |

| | (Using Semaphores). | |

| |26. Write client and server programs (using c) for interaction between server and client processes using Unix Domain | |

| |sockets. | |

|12. | |47 |

| |Week 10 | |

| |27. Write client and server programs (using c) for interaction between server and client processes using Internet | |

| |Domain sockets. | |

| |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 4 {print $0}’ 5 lines.dat

I/P: line1

line2

line3

line4

line5

O/P: line1

line5

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

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

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

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.

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

#include

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

#include

#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

#include

#include

#include

#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 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

#include

#include

#include

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

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

#include

#include

#includemain()

{

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.

The Dirent structure contains the inode number and the name. The maximum length of a filename component is NAME_MAX, which is a system-dependent value. opendir returns a pointer to a structure called DIR, analogous to FILE, which is used by readdir and closedir. This information is collected into a file called dirent.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 call stat takes a filename and returns all of the information in the inode for that file, or -1 if there is an error. That is,

char *name;

struct stat stbuf;

int stat(char *, struct stat *);

stat(name, &stbuf);

fills the structure stbuf with the inode information for the file name. The structure describing the value returned by stat is in , 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 like dev_t and ino_t are defined in, which must be included too.

The st_mode entry contains a set of flags describing the file. The flag definitions are also included in; 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 program fsize. If the mode obtained from stat indicates 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 function fsize.

#include

#include

#include "syscalls.h"

#include /* flags for read and write */

#include /* typedefs */

#include /* 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 function fsize prints the size of the file. If the file is a directory, however, fsize first calls dirwalk to handle all the files in it. Note how the flag names S_IFMT and S_IFDIR are used to decide if the file is a directory. Parenthesization matters, because the precedence of & is 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 function dirwalk is 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. Since fsize calls dirwalk on 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 to readdir returns a pointer to information for the next file, or NULL when 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 of opendir, readdir, and closedir for a specific system. The following routines are for Version 7 and System V UNIX systems; they use the directory information in the header, 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 type ino_t is a typedef that describes the index into the inode list. It happens to be unsigned short on 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 the typedef is better. A complete set of ``system'' types is found in .

opendir opens the directory, verifies that the file is a directory (this time by the system call fstat, which is like stat except 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;

}

closedir closes the directory file and frees the space:

/* closedir: close directory opened by opendir */

void closedir(DIR *dp)

{

if (dp) {

close(dp->fd);

free(dp);

}

}

Finally, readdir uses read to read each directory entry. If a directory slot is not currently in use (because a file has been removed), the inode number is zero, and this position is skipped. Otherwise, the inode number and name are placed in a static structure and a pointer to that is returned to the user. Each call overwrites the information from the previous one.

#include /* local directory structure */

/* readdir: read directory entries in sequence */

Dirent *readdir(DIR *dp)

{

struct direct dirbuf; /* local directory structure */

static Dirent d; /* return: portable structure */

while (read(dp->fd, (char *) &dirbuf, sizeof(dirbuf))

== sizeof(dirbuf)) {

if (dirbuf.d_ino == 0) /* slot not in use */

continue;

d.ino = dirbuf.d_ino;

strncpy(d.name, dirbuf.d_name, DIRSIZ);

d.name[DIRSIZ] = '\0'; /* ensure termination */

return &d;

}

return NULL;

}

15. Write a C program that demonstrates redirection of standard output to a file.

Ex: ls > f1.

Description:

An Inode number points to an Inode. An Inode is a data structure that stores the following information about a file :

• Size of file

• Device ID

• User ID of the file

• Group ID of the file

• The file mode information and access privileges for owner, group and others

• File protection flags

• The timestamps for file creation, modification etc

• link counter to determine the number of hard links

• Pointers to the blocks storing file’s contents

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.

#include

#include

main()

{

int childpid;

if (( childpid=fork())0)

{

}

else

printf(“Child process”);

}

17. Write a C program to create a Zombie process.

If child terminates before the parent process then parent process with out child is called zombie process

#include

#include

main()

{

int childpid;

if (( childpid=fork())0)

{

Printf(“child process”);

exit(0);

}

else

{

wait(100);

printf(“parent process”);

}

}

18. Write a C program that illustrates how an orphan is created.

#include

main()

{

int id;

printf("Before fork()\n");

id=fork();

if(id==0)

{

printf("Child has started: %d\n ",getpid());

printf("Parent of this child : %d\n",getppid());

printf("child prints 1 item :\n ");

sleep(25);

printf("child prints 2 item :\n");

}

else

{

printf("Parent has started: %d\n",getpid());

printf("Parent of the parent proc : %d\n",getppid());

}

printf("After fork()");

}

Week 7

19. Write a C program that illustrates how to execute two commands concurrently with a command pipe.

Ex: - ls –l | sort

AIM: Implementing Pipes

Description:

A pipe is created by calling a pipe() function.

int pipe(int filedesc[2]);

It returns a pair of file descriptors filedesc[0] is open for reading and filedesc[1] is

open for writing. This function returns a 0 if ok & -1 on error.

ALGORITHM:

The following is the simple algorithm for creating, writing to and reading from a

pipe.

1) Create a pipe through a pipe() function call.

2) Use write() function to write the data into the pipe. The syntax is as follows

write(int [],ip_string,size);

int [] – filedescriptor variable, in this case if int filedesc[2] is the variable, then use the filedesc[1] as the first parameter.

ip_string – The string to be written in the pipe.

Size – buffer size for storing the input

3) Use read() function to read the data that has been written to the pipe.

The syntax is as follows

read(int [], char,size);

Program:

#include

#include

main()

{

int pipe1[2],pipe2[2],childpid;

if(pipe(pipe1)0)

if(write(1,buff,n)!=n)

printf("data write error");

if(n0)

{

wfd=open(FIFO1,1);

rfd=open(FIFO2,0);

client(rfd,wfd);

while (wait((int *) 0 ) !=childpid);

close(rfd);

close(wfd);

unlink(FIFO1);

unlink(FIFO2);

}

else

{

rfd=open(FIFO1,0);

wfd=open(FIFO2,1);

server(rfd,wfd);

close(rfd);

close(wfd);

}

}

client(int readfd,int writefd)

{

int n;

char buff[1024];

printf ("enter s file name");

if(fgets(buff,1024,stdin)==NULL)

printf("file name read error");

n=strlen(buff);

if(buff[n-1]=='\n')

n--;

if(write(writefd,buff,n)!=n)

printf("file name write error");

while((n=read(readfd,buff,1024))>0)

if(write(1,buff,n)!=n)

printf("data write error");

if(n0)

        {

                strcpy(mesg ->mtext,buff);

                if(msgsnd(mid,mesg,strlen(mesg ->mtext),0)== -1)

                     printf(“\n Message Write Error”);

        }

        if((mid=msgget(1006,0))0)

                write(1,mesg.mtext,n);

                count++;

        if((n= = -1)&(count= =0))

                printf(“\n No Message Queue on Queue:%d”,mid);

 }

22. Write a C program that receives the messages (from the above message queue as specified in (21)) and displays them.

Aim: To create a message queue

DESCRIPTION:

Message passing between processes are part of operating system, which are done through a message queue. Where messages are stored in kernel and are associated with message queue identifier (“msqid”). Processes read and write messages to an arbitrary queue in a way such that a process writes a message to a queue, exits and other process reads it at later time.

ALGORITHM:

Before defining a structure ipc_perm structure should be defined which is done by including following file.

#include

#include

A structure of information is maintained by kernel, it should contain following.

struct msqid_ds{

struct ipc_perm msg_perm; /*operation permission*/

struct msg *msg_first; /*ptr to first msg on queue*/

struct msg *msg_last; /*ptr to last msg on queue*/

ushort msg_cbytes; /*current bytes on queue*/

ushort msg_qnum; /*current no of msgs on queue*/

ushort msg_qbytes; /*max no of bytes on queue*/

ushort msg_lspid; /*pid o flast msg send*/

ushort msg_lrpid; /*pid of last msgrecvd*/

time_t msg_stime; /*time of last msg snd*/

time_t msg_rtime; /*time of last msg rcv*/

time_t msg_ctime; /*time of last msg ctl*/

};

To create new message queue or access existing message queue “msgget()” function is used

Syntax:

int msgget(key_t key ,int msgflag);

Msg flag values

Num val Symb value desc

0400 MSG_R Read by owner

0200 MSG_w Write by owner

0040 MSG_R >>3 Read by group

0020 MSG_W>>3 Write by group

Msgget returns msqid, or -1 if error

1. To put message on queue “msgsnd()” function is used.

Syntax:

int msgsnd(int msqid , struct msgbuf *ptr,int length, int flag);

msqid is message queue id, a unique id

msgbuf is actual content to send, a pointer to structure which contain following

struct msgbuf

{

Long mtype; /*message type >0 */

Char mtext[1]; /*data*/

};

length is the size of message in bytes

flag is

- IPC_NOWAIT which allows sys call to return immediately when no room on queue, when this is specified msgsnd will return -1 if no room on queue.

Else flag can be specified as 0

2. To receive Message “msgrcv()” function is used

Syntax:

Int msgrcv(int msqid , struct msgbuf *ptr, int length, long msgtype, int flag);

*ptr is pointer to structure where message received is to be stored

Length is size to be received and stored in pointer area

Flag has MSG_NOERROR , it returns an error if length is not large enough to receive msg, if data portion is greater than msg length it truncates and returns.

3. Variety of control operations on msg can be done through “msgctl()” function

Int msgctl(int msqid, int cmd, struct msqid_ds *buff);

IPC_RMID in cmd is given to remove a message queue from the system.

Let us create a header file msgq.h with following in it

#include

#include

#include

#include

extern int errno;

#define MKEY1 1234L

#define MKEY2 2345L

#define PERMS 0666

Server operation algorithm:

#include “msgq.h”

main()

{

Int readid, writeid;

If((readid = msgget(MSGKEY1, PERMS |IPC_CREAT))shmid, p->shmaddr, p->shmflg);

if(p->shmaddr == (char *)-1) {

perror("shmop: shmat failed");

nap--;

} else

(void) fprintf(stderr, "shmop: shmat returned %#8.8x\n",

p->shmaddr);

...

i = shmdt(addr);

if(i == -1) {

perror("shmop: shmdt failed");

} else {

(void) fprintf(stderr, "shmop: shmdt returned %d\n", i);

for (p = ap, i = nap; i--; p++)

if (p->shmaddr == addr) *p = ap[--nap];

}

...

Algorithm:

1. Start

2. create shared memory using shmget( ) system call

3. if success full it returns positive value

4. attach the created shared memory using shmat( ) system

call

5. write to shared memory using shmsnd( ) system call

6. read the contents from shared memory using shmrcv( )

system call

7. End .

Source Code:

#include

#include

#include

#include

#include

#include

#define shm_size 1024

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

{

key_t key;

int shmid;

char *data;

int mode;

if(argc>2)

{

fprintf(stderr,”usage:stdemo[data_to_writte]\n”);

exit(1);

}

if((shmid=shmget(key,shm_size,0644/ipc_creat))==-1)

{

perror(“shmget”);

exit(1);

}

data=shmat(shmid,(void *)0,0);

if(data==(char *)(-1))

{

perror(“shmat”);

exit(1);

}

if(argc==2)

printf(writing to segment:\”%s”\”\n”,data);

if(shmdt(data)==-1)

{

perror(“shmdt”);

exit(1);

}

return 0;

}

Input:

#./a.out koteswararao

Output:

writing to segment koteswararao

Data Mining Lab

Credit Risk Assessment

Description: The business of banks is making loans. Assessing the credit worthiness of an applicant is of crucial importance. You have to develop a system to help a loan officer decide whether the credit of a customer is good, or bad. A bank’s business rules regarding loans must consider two opposing factors. On the one hand, a bank wants to make as many loans as possible. Interest on these loans is the ban’s profit source. On the other hand, a bank cannot afford to make too many bad loans. Too many bad loans could lead to the collapse of the bank. The bank’s loan policy must involve a compromise not too strict, and not too lenient.

To do the assignment, you first and foremost need some knowledge about the world of credit . You can acquire such knowledge in a number of ways.

1. Knowledge Engineering. Find a loan officer who is willing to talk. Interview her and try to represent her knowledge in the form of production rules.

2. Books. Find some training manuals for loan officers or perhaps a suitable textbook on finance. Translate this knowledge from text form to production rule form.

3. Common sense. Imagine yourself as a loan officer and make up reasonable rules which can be used to judge the credit worthiness of a loan applicant.

4. Case histories. Find records of actual cases where competent loan officers correctly judged when not to, approve a loan application.

The German Credit Data :

Actual historical credit data is not always easy to come by because of confidentiality rules. Here is one such dataset ( original) Excel spreadsheet version of the German credit data (download from web).

In spite of the fact that the data is German, you should probably make use of it for this assignment, (Unless you really can consult a real loan officer !)

A few notes on the German dataset :

• DM stands for Deutsche Mark, the unit of currency, worth about 90 cents Canadian (but looks and acts like a quarter).

• Owns_telephone. German phone rates are much higher than in Canada so fewer people own telephones.

• Foreign_worker. There are millions of these in Germany (many from Turkey). It is very hard to get German citizenship if you were not born of German parents.

• There are 20 attributes used in judging a loan applicant. The goal is the classify the applicant into one of two categories, good or bad.

Subtasks : (Turn in your answers to the following tasks)

Laboratory Manual For Data Mining

EXPERIMENT-1

Aim: To list all the categorical(or nominal) attributes and the real valued attributes using Weka mining tool.

Tools/ Apparatus: Weka mining tool..

Procedure:

1) Open the Weka GUI Chooser.

2) Select EXPLORER present in Applications.

3) Select Preprocess Tab.

4) Go to OPEN file and browse the file that is already stored in the system “bank.csv”.

5) Clicking on any attribute in the left panel will show the basic statistics on that selected attribute.

SampleOutput:

[pic]

EXPERIMENT-2

Aim: To identify the rules with some of the important attributes by a) manually and b) Using Weka .

Tools/ Apparatus: Weka mining tool..

Theory:

Association rule mining is defined as: Let be a set of n binary attributes called items. Let be a set of transactions called the database. Each transaction in D has a unique transaction ID and contains a subset of the items in I. A rule is defined as an implication of the form X=>Y where X,Y C I and X Π Y=Φ . The sets of items (for short itemsets) X and Y are called antecedent (left hand side or LHS) and consequent (righthandside or RHS) of the rule respectively.

To illustrate the concepts, we use a small example from the supermarket domain.

The set of items is I = {milk,bread,butter,beer} and a small database containing the items (1 codes presence and 0 absence of an item in a transaction) is shown in the table to the right. An example rule for the supermarket could be meaning that if milk and bread is bought, customers also buy butter.

Note: this example is extremely small. In practical applications, a rule needs a support of several hundred transactions before it can be considered statistically significant, and datasets often contain thousands or millions of transactions.

To select interesting rules from the set of all possible rules, constraints on various measures of significance and interest can be used. The bestknown constraints are minimum thresholds on support and confidence. The support supp(X) of an itemset X is defined as the proportion of transactions in the data set which contain the itemset. In the example database, the itemset {milk,bread} has a support of 2 / 5 = 0.4 since it occurs in 40% of all transactions (2 out of 5 transactions).

The confidence of a rule is defined . For example, the rule has a confidence of 0.2 / 0.4 = 0.5 in the database, which means that for 50% of the transactions containing milk and bread the rule is correct. Confidence can be interpreted as an estimate of the probability P(Y | X), the probability of finding the RHS of the rule in transactions under the condition that these transactions also contain the LHS .

ALGORITHM:

Association rule mining is to find out association rules that satisfy the predefined minimum support and confidence from a given database. The problem is usually decomposed into two subproblems. One is to find those itemsets whose occurrences exceed a predefined threshold in the database; those itemsets are called frequent or large itemsets. The second problem is to generate association rules from those large itemsets with the constraints of minimal confidence.

Suppose one of the large itemsets is Lk, Lk = {I1, I2, … , Ik}, association rules with this itemsets are generated in the following way: the first rule is {I1, I2, … , Ik1} and {Ik}, by checking the confidence this rule can be determined as interesting or not. Then other rule are generated by deleting the last items in the antecedent and inserting it to the consequent, further the confidences of the new rules are checked to determine the interestingness of them. Those processes iterated until the antecedent becomes empty. Since the second subproblem is quite straight forward, most of the researches focus on the first subproblem. The Apriori algorithm finds the frequent sets L In Database D.

· Find frequent set Lk − 1.

· Join Step.

o Ck is generated by joining Lk − 1with itself

· Prune Step.

o Any (k − 1) itemset that is not frequent cannot be a subset of a

frequent k itemset, hence should be removed.

Where · (Ck: Candidate itemset of size k)

· (Lk: frequent itemset of size k)

Apriori Pseudocode

Apriori (T,£)

L ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download

To fulfill the demand for quickly locating and searching documents.

It is intelligent file search solution for home and business.

Literature Lottery

Related searches