LINUX PROGRAMMING AND DATA MINING LAB MANUAL - VVIT

desertcockatooData Management

Nov 20, 2013 (3 years and 11 months ago)

293 views

LINUX PROGRAMMING AND DATA MIN
I
G

LAB MANUAL

IV
-
BTECH
















VID







COMPUTER SCIENCE AND ENGINEERING
I
N

S
VIDYA VIKAS INSTITUTE OF TECHNOLOGY
LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
2



LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
3




Contents


S.No

Topic

Page
no



3.

Week1


1. Write a shell script that accepts a file name, sta
rting 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 scr
ipt to count the number of lines in a file that do not contain
vowels.

1
3

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


LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
4




6.

Week 4


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




A. cat B. ls C. mv

1
5

12. Write a program that takes one or more file/directory names as command line
input and reports the follow
ing 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.

1
9

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 th
e parent to display
“parent” and the child to display “child” on the screen.

2
9

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

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
5


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


11.

Week 9

25. Write a C pro
gram that implements a producer
-
consumer system with two
processes.

4
1


(
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 an
d 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 r
eal
-
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.

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

Re
duced error pruning for training Decision Trees using cross
-
validation



78

24.

Convert a Decision Trees into "if
-
then
-
else rules".

81




LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
6










































LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
7


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



2.

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


Aim: To wri
te 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 1
b.sh test1

the contents before deleting

test1

hello

h
ello

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
8


bangalore

mysore city

enter the word to be deleted

city

after deleting

hello

hello

Bangalore


$ sh
1
b.sh

no argument passed






3. Write a shell script that displays a list of all the files in the current di
rectory to which the
user has read, w
rite 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

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
9




fi

4.

Write a shell
script that receives any number of file names as a
rguments 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 scri
pt 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



LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
10


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



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

hell
o ATRI

hello

$ sh 1.sh test test1

Word = hello, Count = 3

Word = ATRI, Count = 2










LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
11







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

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
12







LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
13


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, wor
ds 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);

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
14


}

// Case of an error exit from the loop

if(n1 ==
-
1){

perror("Reading problem ");

exit(2);

}

close(fd2);

exit(0);

}






























LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
15



Week 4



11. Implement in C the followi
ng 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 f
d1;


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

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
16




#define FALSE 0

#define TRUE 1



extern int alphasort();



char pathname[MAXPATHLEN];



main() {

int count,i;

struct dirent **files;

int file_select();



if (getwd(p
athname) == 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("N
umber 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

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
17


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

#in
clude<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 pr
ogram 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 Execu
te 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”);

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
18


fclose(stream);

exit(1);

}

}

if(fclose(stream))==EOF)

{

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

exit(1);

}

return();

}









LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
19


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: Decl
are 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: Displ
ay 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 dirn
ame[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

...

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
20


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;

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
21



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

<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 g
roup 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_c
time; /* time originally created */


};

Most of these values are explained by the comment fields. The types like

dev_t

and

ino_t

are
defined in
<sys/types.h>
, which must be included too.

The

st_mode

entry contains a set of flags describing the file. Th
e 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 */

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
22



#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 b
e 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 argum
ent to the
function

fsize.


#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(*++ar
gv);

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
23



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 director
y. 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)


{


struc
t 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", stbu
f.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

ca
lls

dirwalk

on each directory, the two functions call each
other recursively.


#define MAX_PATH 1024

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
24




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


|| strc
mp(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);

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
25



}

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<sys/dir.h>, which looks like this:


#ifndef DIRSIZ


#define DIRSIZ 14


#end
if


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 syste
m, so the

typedef

is better. A complete set of
``system'' types is found in

<sys/types.h>.

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 descripto
r), 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)


{

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
26



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;


}

clos
edir

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 d
irectory 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 t
o the user. Each call overwrites the information from the previous one.


#include <sys/dir.h> /* local directory structure */

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
27




/* 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 demonstr
ates 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



T
he file mode information and access privileges for owner, group and others

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
28




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
























LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
29


Week 6


16. Write a C program to create a child process and allow the parent t
o display “parent” and
the
child to display “child” on the screen.

#include<stdio.h>

#include<string.h>

main()

{


int childpid;




if (( childpid=fork())<0)



{


printf("cannot fork");


}


else if(childpid >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
zomb
ie process


#include<stdio.h>

#include<string.h>

main()

{


int childpid;




if (( childpid=fork())<0)


{


printf("cannot fork");


}


else if(childpid >0)


{


Printf(“child process”);


exit(0);

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
30




}

else

{

wait(100);


printf(“parent

process”);

}


}


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




#include<stdio.h>


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("Pa
rent of the parent proc : %d
\
n",getppid());


}



printf("After fork()");


}







LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
31


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


D
ESCRIPTION
:


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 fo
r 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() fun
ction 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<stdio.h>

#include<string.h>

main()

{


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


if(pipe(pipe1)<0 || pipe(pipe2) < 0)

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
32



printf("pipe creation error");


if (( childpid=fork())<0)


{


printf("cannot fork");


}


else


if(childpid >0)


{


close(pipe1[0]);


close(pipe2[1]);


client(pip
e2[0],pipe1[1]);


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


close(pipe1[1]);


close(pipe2[0]);


exit(0);


}


else


{


close(pipe1[1]);


close(pipe2[0]);


server(pipe1[0],pipe2[1]);


close(pipe1[0]);


close(pipe2[1]);


exit(0);


}

}

client(int readfd,int writefd)

{

int n;

char buff[1024];


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(n<0)

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
33



printf("data error");

}

server(int readfd,int writefd)

{


char buff[1024],errmsg[50];


int n,fd;


n=read(rea
dfd,buff,1024);


buff[n]='
\
0';


if((fd=open(buff,0))<0)


{


sprintf(buff,"file does nit exist");


write(writefd,buff,1024);


}


else


{


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


write(writefd,buff,n);


}

}


20. Wr
ite C programs that illustrate communication between two unre
lated processes using
named pipe.


AIM:

Implementing IPC using a FIFO (or) named pipe.

D
ESCRIPTION
:

Another kind of IPC is FIFO(First in First Out) is sometimes also called as named
pipe.It is l
ike a pipe, except that it has a name.Here the name is that of a file that multiple
processes can open(), read and write to. A FIFO is created using the mknod() system call.
The syntax is as follows


int mknod(char *pathname, int mode, int dev);

The pathna
me is a normal Unix pathname, and this is the name of the FIFO.

The mode argument specifies the file mode access mode.The dev value is ignored for a
FIFO.

Once a FIFO is created, it must be opened for reading (or) writing using either the open
system call,

or one of the standard I/O open functions
-
fopen, or freopen.


ALGORITHM:

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
34





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


FIFO.

1)

Create a fifo through mknod() function call.

2)

Use write()
function to write the data into the fifo. 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 fifo.


Size


buffer size for storing the input

3)

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

The syntax is as follows

read(int [], char,size);


PROGRAM:

#define FIFO1 "Fifo1"

#define FIFO2 "Fifo2"

#i
nclude<stdio.h>

#include<string.h>

#include<sys/types.h>

#include<fcntl.h>

#include<sys/stat.h>

main()

{


int childpid,wfd,rfd;


mknod(FIFO1,0666|S_IFIFO,0);


mknod(FIFO2,0666|S_IFIFO,0);


if (( childpid=fork())==
-
1)


{


printf("cannot fork");


}


else


if(childpid >0)


{

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
35



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(FIF
O1,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=strle
n(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(n<0)


pri
ntf("data error");

}

server(int readfd,int writefd)

{


char buff[1024],errmsg[50];


int n,fd;


n=read(readfd,buff,1024);

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
36



buff[n]='
\
0';


if((fd=open(buff,0))<0)


{


sprintf(buff,"file does nit exist");


write(writefd,buff,1024);



}


else


{


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


write(writefd,buff,n);


}

}


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.



#include <
stdio.h>


#include <sys/ipc.h>


#include <fcntl.h>


#define MAX 255









struct mesg









{















long type;















char mtext[MAX];









}

*mesg;









char buff[MAX];


main()


{









int mid,fd,n,count=0;;









if((mid=ms
gget(1006,IPC_CREAT | 0666))<0)









{

















printf(“
\
n Can’t create Message Q”);

















exit(1);









}









printf(“
\
n Queue id:%d”, mid);









mesg=(struct mesg *)malloc(sizeof(struct mesg));









mesg
-
>type=6;









fd=open(“fact”,O_RDONLY);









while(read(fd,buff,25)>0)









{

















strcpy(mesg
-
>mtext,buff);

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
37


















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






















printf(“
\
n Message Write Error”);









}











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









{

















printf(“
\
n Can’t create Message Q”);

















exit(1);









}









while((n=msgrcv(mid,&mesg,MAX,6,IPC_NOWAIT))>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




DESCRIPTIO
N:

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 que
ue 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 <sys/types
.h>


#include <sys/ipc.h>


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

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
38



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_lrpi
d; /*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()” functio
n 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 follo
wing



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 re
turn
-
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);


LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
39


*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 <sys/type.h>


#include <sys/ipc.h>


#include <sys/msg.h>



#include <sys/errno.h>


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


err_sys(“Server: cant get message queue 1”);

If((writeid= msgget(MKEY@, PERMS | IPC_CREAT))<0)


err_sys(“Server : cant get message queue 2”);


server(readid,writeid);

exit(0);

}



Client process:


#include “msgq.h


main()

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
40


{


int readid, writeid;


/* open queues which server has already created it */

If ( (wirteid =msgget(MKEY1,0))<0)


err_sys(“client : cant access msgget message queue 1”);

if((readid=
msgget(MKEY2,0))<0)



err_sys(“client : cant msgget messages
queue 2”):


client(readid,writeid);


/*delete msg queuu */



If (msgctl(readid, IPC_RMID,( struct msqid_ds *)0)<0)



err_sys(“Client: cant RMID message queue1”);


if(msgctl(writeid, IPC_RMID, (struct msqid_ds *) 0) <0)


err_sys(“Client: cant RMID m
essage queue 2”);


exit(0);

}


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.


PROGRAM:


#include<stdio.h>

#include<stdlib.h>

#include<error.
h>

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/sem.h>

int main(void)

{

key_t key;

int semid;

union semun arg;

if((key==ftok("sem demo.c","j"))==
-
1)

{

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
41


perror("ftok");

exit(1);

}

if(semid=semget(key,1,0666|IPC_CREAT))==
-
1)

{

perror("semget"):

ex
it(1);

}

arg.val=1;

if(semctl(semid,0,SETVAL,arg)==
-
1)

{

perror("smctl");

exit(1);

}

return 0;

}


OUTPUT:

semget

smctl

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


#include<sys/types.h>

#include<signal.h>

//
suspend the process(same as hitting crtl+z)

kill(pid,SIGSTOP);


//continue the process

kill(pid,SIGCONT);


Week 9


25. Write a C program that implements a producer
-
consumer system with two processes. (using
Semaphores).



Algorithm:


1. Start

2. create s
emaphore using semget( ) system call

3. if successful it returns positive value

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
42


4. create two new processes

5. first process will produce

6. until first process produces second process cannot consume

7. End.


Source code:


#include<stdio.h>

#include<stdlib
.h>

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/sem.h>

#include<unistd.h>

#define num_loops 2

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

{

int sem_set_id;

int child_pid,i,sem_val;

struct sembuf sem_op;

int rc;

struct timespec delay;

clrscr();

sem_set_id=se
mget(ipc_private,2,0600);

if(sem_set_id==
-
1)

{

perror(“main:semget”);

exit(1);

}

printf(“semaphore set created,semaphore setid‘%d’
\
n ”,

sem_set_id);

child_pid=fork();

switch(child_pid)

{

case
-
1:

perror(“fork”);

exit(1);

case 0:

for(i=0;i<num_loops;i++)

{

sem_op.sem_num=0;

sem_op.sem_op=
-
1;

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
43


sem_op.sem_flg=0;

semop(sem_set_id,&sem_op,1);

printf(“producer:’%d’
\
n”,i);

fflush(stdout);

}

break;

default:

for(i=0;i<num_loops;i++)

{

printf(“consumer:’%d’
\
n”,i);

fflush(stdout);

sem_op.sem_num=0;

sem_op.sem_op=1;

sem
_op.sem_flg=0;

semop(sem_set_id,&sem_op,1);

if(rand()>3*(rano_max14));

{

delay.tv_sec=0;

delay.tv_nsec=10;

nanosleep(&delay,null);

}

}

break;

}

return 0;

}




Output:

semaphore set created

semaphore set id ‘327690’

producer: ‘0’

consumer:’0’

producer:’1’



consumer:’1’


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


Server.c


LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
44


#include <stdio.h>

#include <sys/socket.h>

#include <sys/un.h>

#include <sys/types.h>

#include <unis
td.h>

#include <string.h>


int connection_handler(int connection_fd)

{


int nbytes;


char buffer[256];



nbytes = read(connection_fd, buffer, 256);


buffer[nbytes] = 0;



printf("MESSAGE FROM CLIENT: %s
\
n", buffer);


nbytes = snprintf(buffer, 256, "hello f
rom the server");


write(connection_fd, buffer, nbytes);



close(connection_fd);


return 0;

}


int main(void)

{


struct sockaddr_un address;


int socket_fd, connection_fd;


socklen_t address_length;


pid_t child;



socket_fd = socket(PF_UNIX, SOCK_STREAM,
0);


if(socket_fd < 0)


{


printf("socket() failed
\
n");


return 1;


}



unlink("./demo_socket");



/* start with a clean address structure */


memset(&address, 0, sizeof(struct sockaddr_un));

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
45




address.sun_family = AF_UNIX;


snprintf(address.sun_path, U
NIX_PATH_MAX, "./demo_socket");



if(bind(socket_fd,


(struct sockaddr *) &address,


sizeof(struct sockaddr_un)) != 0)


{


printf("bind() failed
\
n");


return 1;


}



if(listen(socket_fd, 5) != 0)


{


printf("listen() failed
\
n");


retu
rn 1;


}



while((connection_fd = accept(socket_fd,


(struct sockaddr *) &address,


&address_length)) >
-
1)


{


child = fork();


if(child == 0)


{


/* now inside newly created connection han
dling process */


return connection_handler(connection_fd);


}



/* still inside server process */


close(connection_fd);


}



close(socket_fd);


unlink("./demo_socket");


return 0;

}


Client.c

#include <stdio.h>

#include <sys/socket.h>

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
46


#include <sys/
un.h>

#include <unistd.h>

#include <string.h>


int main(void)

{


struct sockaddr_un address;


int socket_fd, nbytes;


char buffer[256];



socket_fd = socket(PF_UNIX, SOCK_STREAM, 0);


if(socket_fd < 0)


{


printf("socket() failed
\
n");


return 1;


}



/*

start with a clean address structure */


memset(&address, 0, sizeof(struct sockaddr_un));



address.sun_family = AF_UNIX;


snprintf(address.sun_path, UNIX_PATH_MAX, "./demo_socket");



if(connect(socket_fd,


(struct sockaddr *) &address,



sizeof(struct sockaddr_un)) != 0)


{


printf("connect() failed
\
n");


return 1;


}



nbytes = snprintf(buffer, 256, "hello from a client");


write(socket_fd, buffer, nbytes);



nbytes = read(socket_fd, buffer, 256);


buffer[nbytes] = 0;



printf("
MESSAGE FROM SERVER: %s
\
n", buffer);



close(socket_fd);


return 0;

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
47


}

Week 10


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


Server.c


#include <sys/socket.h>

#include <n
etinet/in.h>

#include <arpa/inet.h>

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>

#include <string.h>

#include <sys/types.h>

#include <time.h>


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

{


int listenfd = 0, connfd = 0;


struct

sockaddr_in serv_addr;



char sendBuff[1025];


time_t ticks;



listenfd = socket(AF_INET, SOCK_STREAM, 0);


memset(&serv_addr, '0', sizeof(serv_addr));


memset(sendBuff, '0', sizeof(sendBuff));



serv_addr.sin_family = AF_INET;


se
rv_addr.sin_addr.s_addr = htonl(INADDR_ANY);


serv_addr.sin_port = htons(5000);



bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));



listen(listenfd, 10);



while(1)

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
48



{


connfd = accept(listenfd, (struct sockaddr*)N
ULL, NULL);



ticks = time(NULL);


snprintf(sendBuff, sizeof(sendBuff), "%.24s
\
r
\
n", ctime(&ticks));


write(connfd, sendBuff, strlen(sendBuff));



close(connfd);


sleep(1);


}

}


Client.c


#include <sys/socket.h>

#i
nclude <sys/types.h>

#include <netinet/in.h>

#include <netdb.h>

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <unistd.h>

#include <errno.h>

#include <arpa/inet.h>


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

{


int sockfd = 0, n = 0;


cha
r recvBuff[1024];


struct sockaddr_in serv_addr;



if(argc != 2)


{


printf("
\
n Usage: %s <ip of server>
\
n",argv[0]);


return 1;


}



memset(recvBuff, '0',sizeof(recvBuff));


if((sockfd = socket(AF_INET, SOCK_STREAM, 0))
< 0)


{


printf("
\
n Error : Could not create socket
\
n");

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
49



return 1;


}



memset(&serv_addr, '0', sizeof(serv_addr));



serv_addr.sin_family = AF_INET;


serv_addr.sin_port = htons(5000);



if(inet_pton(AF_INET, argv[1], &
serv_addr.sin_addr)<=0)


{


printf("
\
n inet_pton error occured
\
n");


return 1;


}



if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)


{


printf("
\
n Error : Connect Failed
\
n");


return 1;



}



while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)
-
1)) > 0)


{


recvBuff[n] = 0;


if(fputs(recvBuff, stdout) == EOF)


{


printf("
\
n Error : Fputs error
\
n");


}


}



if(n < 0)


{


printf(
"
\
n Read error
\
n");


}



return 0;

}


28.
Write a C program that illustrates two processes communicating using shared memory
.


DESCRIPTION:

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
50


Shared Memory

is an efficeint means of passing data between programs. One program
will create a memory port
ion which other processes (if permitted) can access.

The problem with the pipes, FIFO’s and message queues is that for two processes to
exchange information, the information has to go through the kernel. Shared memory provides a
way around this by letting

two or more processes share a memory segment.

In shared memory concept if one process is reading into some shared memory, for
example, other processes must wait for the read to finish before processing the data.

A process creates a shared memory segment

using
shmget()|
. The original owner of a
shared memory segment can assign ownership to another user with
shmctl()
. It can also revoke
this assignment. Other processes with proper permission can perform various control functions
on the shared memory segmen
t using
shmctl()
. Once created, a shared segment can be attached
to a process address space using
shmat()
. It can be detached using
shmdt()

(see
shmop()
). The
attaching process must have the appropriate permissions for
shmat()
. Once attached, the process
c
an read or write to the segment, as allowed by the permission requested in the attach operation.
A shared segment can be attached multiple times by the same process. A shared memory
segment is described by a control structure with a unique ID that points t
o an area of physical
memory. The identifier of the segment is called the shmid. The structure definition for the shared
memory segment control structures and prototypews can be found in <
sys/shm.h
>.

shmget() is used to obtain access to a shared memory se
gment. It is prottyped by:

int shmget(key_t key, size_t size, int shmflg);

The key argument is a access value associated with the semaphore ID. The size argument is the
size in bytes of the requested shared memory. The shmflg argument specifies the initia
l access
permissions and creation control flags.

When the call succeeds, it returns the shared memory segment ID. This call is also used to get
the ID of an existing shared segment (from a process requesting sharing of some existing
memory portion).

The
following code illustrates shmget():

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

...

key_t key; /* key to be passed to shmget() */

int shmflg; /* shmflg to be passed to shmget() */

int shmid; /* return value from shmget() */

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
51


int

size; /* size to be passed to shmget() */


...


key = ...

size = ...

shmflg) = ...


if ((shmid = shmget (key, size, shmflg)) ==
-
1) {


perror("shmget: shmget failed"); exit(1); } else {


(void) fprintf(stderr, "shmget: shmget returned %d
\
n", shmid
);


exit(0);


}

...

Controlling a Shared Memory Segment

shmctl() is used to alter the permissions and other characteristics of a shared memory segment. It
is prototyped as follows:

int shmctl(int shmid, int cmd, struct shmid_ds *buf);

The process must

have an effective shmid of owner, creator or superuser to perform this
command. The cmd argument is one of following control commands:

SHM_LOCK

--

Lock the specified shared memory segment in memory. The process
must have the effective ID of superuser to

perform this command.

SHM_UNLOCK

--

Unlock the shared memory segment. The process must have the
effective ID of superuser to perform this command.

IPC_STAT

--

Return the status information contained in the control structure and
place it in the buffer
pointed to by buf. The process must have read
permission on the segment to perform this command.

IPC_SET

--

Set the effective user and group identification and access
permissions. The process must have an effective ID of owner, creator
or superuser to pe
rform this command.

IPC_RMID

--

Remove the shared memory segment.

The buf is a sructure of type struct shmid_ds which is defined in <sys/shm.h>

The following code illustrates shmctl():

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
52


.
..

int cmd; /* command code for shmctl() */

int shmid; /* segment ID */

struct shmid_ds shmid_ds; /* shared memory data structure to


hold results */

...

shmid = ...

cmd = ...

if ((rtrn = shmctl(shmid, cmd, shmid_ds)) ==
-
1) {


perror("shmctl: shmctl failed");


exit(1);


}

..

Attaching and Detaching a Shared Memory Segment

shmat() and shmdt() are used to attach and detach shared memory segments. They are prototypes
as follows:

void *shmat(int shmid, const void *shmadd
r, int shmflg);

int shmdt(const void *shmaddr);

shmat() returns a pointer, shmaddr, to the head of the shared segment associated with a valid
shmid. shmdt() detaches the shared memory segment located at the address indicated by shmaddr

. The following cod
e illustrates calls to shmat() and shmdt():

#include <sys/types.h>

#include <sys/ipc.h>

#include <sys/shm.h>

static struct state { /* Internal record of attached segments. */


int shmid; /* shmid of attached segment */


char *shmadd
r; /* attach point */


int shmflg; /* flags used on attach */


} ap[MAXnap]; /* State of current attached segments. */

int nap; /* Number of currently attached segments. */

...

char *addr; /* address work variable */

register int i; /* wo
rk area */

register struct state *p; /* ptr to current state entry */

...

p = &ap[nap++];

p
-
>shmid = ...

p
-
>shmaddr = ...

p
-
>shmflg = ...

p
-
>shmaddr = shmat(p
-
>shmid, p
-
>shmaddr, p
-
>shmflg);

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

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
53



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. w
rite to shared memory using shmsnd( ) system call

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

system call

7. End .

Source Code:

#include<stdio.h>

#include<stdlib.h>

#include<sys/ipc.h>

#include<sys/types.h>

#include<string.h>

#include<sys/shm.h
>

#define shm_size 1024

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

{

key_t key;

int shmid;

char *data;

int mode;

if(argc>2)

LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
54


{

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:

wri
ting to segment koteswararao









LINUX PROGRAMMING AND DATA MINING LAB MANUAL



VIDYA VIKAS INSTITUTE OF TECHNOLOGY

Page
55


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
de
cide 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
o
ther 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 s
ome 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 s
ome 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