ge2155 work book

60
KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY GE2155 - COMPUTER PRACTICE LABORATORY – II LABORATORY MANUAL DEPARTMENT OF INFORMATION TECHNOLOGY

Upload: felieju

Post on 16-Oct-2014

158 views

Category:

Documents


1 download

TRANSCRIPT

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

GE2155 - COMPUTER PRACTICE LABORATORY – II

LABORATORY MANUAL

DEPARTMENT OF INFORMATION TECHNOLOGY

CONTENTS

Ex No. Title of the Exercise Page No

1. BASIC UNIX COMMANDS

2. DIRECTORY COMMANDS

3. FILE COMMANDS

4. UNIX PIPES

5. UNIX FILTERS

6. EMPLOYEE PAYROLL PROCESSING

7. STUDENT MARK LIST EVALUATION

8. SUM OF DIGITS

11. ODD NUMBER LIST

12. ARMSTRONG NUMBER

13. FACTORIAL OF N NUMBERS

14. FIBONACCI SERIES

15. DYNAMIC STORAGE ALLOCATION

16. POINTERS

17. FUNCTIONS

18. FILE HANDILING

2

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

BASIC UNIX COMMANDS

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

To make the user to work in the UNIX environment.

STEP 2: ACQUISITION

Commands:

1. Date Command

[azar@linuxserver ~]$ date

2. Calendar Command

[azar@linuxserver ~]$ cal

3. Echo Command$ echo <Message>[azar@linuxserver ~]$ echo be brave

4. Who Command

[azar@linuxserver ~]$ who

5. Precision Calculator

[azar@linuxserver ~]$ bc

6. To exit from unix

[azar@linuxserver ~]$ exit

3

OUTPUT:

STEP 3: PRACTICE/TESTING

Questions:

1. List the features of UNIX operating system.

2. What is the use of ‘echo’ command?

3. What the command used to get out of UNIX environment?

RESULT:

4

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

DIRECTORY COMMANDS

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

To understand the various directory commands in UNIX environment.

STEP 2: ACQUISITION

Commands:

1. Creating new directory

$ mkdir <dirname>

2. To Change Directory Command

$ cd <dirname> [azar@linuxserver ~]$ cd students

3. To know current working directory [azar@linuxserver students]$ pwd

4. Exit from current Directory

[azar@linuxserver students]$ cd

5. To Remove a directory

$ rmdir <dirname>

5

OUTPUT:

STEP 3: PRACTICE/TESTING

Questions:

1. How to create directory in UNIX operating system?

2. How to change the working directory in UNIX operating system

RESULT:

6

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

FILE COMMANDS

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

To understand the various commands for creating and manipulate the file operations.

STEP 2: ACQUISITION

Commands:

1. Creating new File

$ cat > filename[azar@linuxserver ~]$ cat>file1

2. To display contents of a file

$ cat filename[azar@linuxserver ~]$ cat file1

3. Concatenating multiple files

$ cat file1 file2 > file3[azar@linuxserver ~]$ cat file1 file2 > file3[azar@linuxserver ~]$ cat file3

4. To copying a file

$ cp old-file new-file[azar@linuxserver ~]$ cp file3 file4

5. To Removing a file

$ rm filename

6. To Moving a file

$ mv old-file new-file[azar@linuxserver ~]$ mv file4 file5

7

7. Listing Files & Directories

$ ls[azar@linuxserver ~]$ ls

8. To Counting number of words in a file

$ wc filename[azar@linuxserver ~]$ wc file3

9. Sort Command

Ascending order: $ sort filename

Descending order: $ sort –r filename[azar@linuxserver ~]$ sort -r file3

10. To remove duplicate RECORDS

$ uniq filename[azar@linuxserver ~]$ uniq file3

11. To compare two files

[azar@linuxserver ~]$ cmp file1 file2

8

STEP 3: PRACTICE/TESTING

Questions:

1. How to create file in UNIX operating system?

2. How to copy contents of one file to another?

3. What the command used to get remove the data in the files?

RESULT:

9

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

UNIX PIPES

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

To understand the use of pipes commands in UNIX.

STEP 2: ACQUISITION

Commands:

1.) $ command1 | command2[azar@linuxserver ~]$ who|wc

2.) $ command1 | tee file | command2

$ who | tee out-file | wc[azar@linuxserver ~]$ who|tee pipefile1|wc

3.) Message Command$ mesg y/n

4.) Write Command - To send message to a particular users.

$ write <user-name>[azar@linuxserver ~]$ write azar

5.) WALL Command$ wall <Message>[azar@linuxserver ~]$ wall hello

6.) MAIL CommandTo send a Mail:

$ mail <user-name>[azar@linuxserver ~]$ mail azar

To receive a Mail:$ mail[azar@linuxserver ~]$ mail

10

STEP 3: PRACTICE/TESTINGQuestions:

1. Why pipes commands are used in UNIX operating system?

2. How to send a message to others in UNIX environment?

3. How to send a mail to others in UNIX environment?

RESULT:

11

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

UNIX FILTERS

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

To understand the use of Filter commands in UNIX.

STEP 2: ACQUISITION

Commands:

1.) Head Filter$ head –n <File-Name>

[azar@linuxserver ~]$ head -3 file3

2.) Tail Filter$ tail –n <File-Name>

[azar@linuxserver ~]$ tail -3 file3

3.) Grep Command$ grep <pattern> <file>

[azar@linuxserver ~]$ grep bha file2

4.) Adding Line Numbers$ nl <File-Name>[azar@linuxserver ~]$ nl file1 [azar@linuxserver ~]$ nl file2

5.) CUT Command

$ cut –c1 <file-Name>

[azar@linuxserver ~]$ cut -c1 file2

6.) PASTE Command$ paste <File-Name>[azar@linuxserver ~]$ paste file7

12

7.) Compare Command

$ cmp file1 file2[azar@linuxserver ~]$ cmp file1 file2

8.) Common Command

$ comm file1 file2[azar@linuxserver ~]$ comm file1 file2

13

STEP 3: PRACTICE/TESTING

Questions:

1. Why filter commands are used in UNIX operating system?

2. How to compare two files contents in UNIX environment?

3. How to add line numbers in the file contents?

RESULT:

14

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

EMPLOYEE PAYROLL PROCESSING

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

To understand about the shell programming in UNIX and calculate the employee

payroll processing.

STEP 2: ACQUISITION

ALGORITHM:

Step1: Start the program execution.

Step2: Get the employee name, id & basic salary details.

Step3: Fix the DA, PF & HRA percentages to calculate the Gross Salary.

Step4: Calculate the Gross Salary.

Step5: Display the calculated Cross Salary of the employees.

Step6: Stop the program execution.

15

PROGRAM:

echo "Enter Employee Name:"

read name

echo "Enter Employee Id No:"

read id

echo "Enter the Basic Salary"

read basic

let hra=10

let epf=15

let da=10

let net=$basic

let t=$basic*$hra/100

let u=$basic*epf/100

let v=$basic*da/100

let net=$net+$t+$u+$v

echo "Name : $name"

echo "Emp.Id :$id"

echo "Net Salary:$net"

INPUT:

OUTPUT:

16

STEP 3: PRACTICE/TESTING

Questions:

1. How to create shell programming in UNIX environment?

2. Which command used for decision making in UNIX?

RESULT:

17

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

STUDENT MARK LIST EVALUATION

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

To understand about the conditional statements in shell programming by evaluate the

student mar list.

STEP 2: ACQUISITION

ALGORITHM:

Step1: Start the program execution.

Step2: Get the details of student name, roll number & subject’s marks.

Step3: Check the constrains of the subject marks.

Step4: Judge the student results bases on the constraints & calculate the total marks &

average.

Step5: Display the calculated results & total marks of the student.

Step6: Stop the program execution.

18

PROGRAM:

echo "Enter the Student Name :"

read name

echo "Enter Roll Number :"

read rollno

echo "Enter Marks-1 :"

read mark1

echo "Enter Marks-2 :"

read mark2

echo "Enter Marks-3 :"

read mark3

let total=$mark1+$mark2+$mark3

let average=$total/3

echo "Name : $name"

echo "Roll : $rollno"

echo "Total :$total"

echo "Avg : $average"

let i=0

if(($mark1 > 49))

then

let i=$i+1

else

echo

fi

if(($mark2 > 49))

then

let i=$i+1

else

echo

fi

if(($mark3 > 49))

then

let i=$i+1

19

else

echo

fi

if(($i==3))

then

echo "Pass"

else

echo "Fail"

fi

INPUT:

OUTPUT:

20

STEP 3: PRACTICE/TESTING

Questions:

1. How to get the input in shell programming?

By use the command “read”.

2. How to assign a value to a variable?

let i=0;

RESULT:

21

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

SUM OF DIGITS

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

To understand about the conditional statements and arithmetic operators in shell

programming by calculating sum of the digits.

STEP 2: ACQUISITION

ALGORITHM:

Step1: Start the program execution.

Step2: Get the particular number as the input.

Step3: Check whether that particular number is single digit number or as multiple

digit number.

Step4: Separate the given numbers as individual digits.

Step5: Add all the individual digits.

Step5: Display the calculated results.

Step6: Stop the program execution.

22

PROGRAM:

echo "Enter the Number:"

read Num

let sum=0

while(($Num!=0))

do

let x=$Num%10

let sum=$sum+$x

let Num=$Num/10

done

echo "The Result :$sum"

INPUT:

OUTPUT:

STEP 3: PRACTICE/TESTING

Questions:

1. What is the use of ‘let’ command in UNIX?

2. What is the use of $ symbol?

RESULT:

.

23

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

ODD NUMBER LIST

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

To understand about the conditional statements and arithmetic operators in shell

programming by find the odd numbers.

STEP 2: ACQUISITION

ALGORITHM:

Step1: Start the program execution.

Step2: Get the particular number ‘N’ as the input.

Step3: Add all the numbers from 1 to N.

Step4: Display the addition values.

Step5: Stop the program execution.

PROGRAM:

echo " enter the number"

read n

echo " the odd numbers upto $n are:"

let q=1

while ((q<=n))

do

echo "$q"

let q=$q+2

done

24

INPUT:

OUTPUT:

STEP 3: PRACTICE/TESTING

Questions:

1. What is the use of ‘read’ command in UNIX?

2. How to store shell programming in UNIX environment?

RESULT: .

25

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

ARMSTRONG NUMBER

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

To understand about the conditional statements and arithmetic operators in shell

programming by finding the Armstrong number.

STEP 2: ACQUISITION

ALGORITHM:

Step1: Start the program execution.

Step2: Get the particular number as the input.

Step3: Check whether that particular number is single digit number or as multiple

digit number.

Step4: Separate the given numbers as individual digits.

Step5: To check whether the given number is Armstrong or not.

Step6: Stop the program execution.

26

PROGRAM:

echo "Enter the number to find the given number is armstrong"

read n

let m=$n

let s=0

while(($m != 0))

do

let c=$m%10

let s=$c*$c*$c+$s

let m=$m/10

done

if(($n==$s))

then

echo "It is an ARMSTRONG NUMBER"

else

echo "It is not an ARMSTRONG NUMBER"

fi

INPUT:

OUTPUT:

INPUT:

OUTPUT:

27

STEP 3: PRACTICE/TESTING

Questions:

1. What are the arithmetic operators used in UNIX?

2. How to close the if block statement?

RESULT:

.

28

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

FACTORIAL OF N NUMBERS

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

To understand about the conditional statements and arithmetic operators in shell

programming by calculating factorial of N numbers.

STEP 2: ACQUISITION

ALGORITHM:

Step1: Start the program execution.

Step2: Get the particular number as the input.

Step3: Perform the summation of multiplication of the numbers from 1 to N.

Step4: Display the summation value.

Step5: Stop the program execution.

PROGRAM:

echo "Enter the value "read nlet s=1let i=1while((i<=n))dolet s=$s*$ilet i=$i+1doneecho "The factorial of $n=$s"

29

INPUT:

OUTPUT:

STEP 3: PRACTICE/TESTING

Questions:

1. What is the factorial number?

2. What is the condition statement used in UNIX?

RESULT:

30

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

FIBONACCI SERIES

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

To write and execute a UNIX shell program to calculate the Fibonacci series

for the given number.

.

STEP 2: ACQUISITION

ALGORITHM:

Step1: Start the program execution.

Step2: Get the particular number ‘N’ as the input.

Step3: Perform the cumulative addition of the numbers from 1 to N by adding the

current number with the next number.

Step4: Display the summation value.

Step5: Stop the program execution.

PROGRAM:echo "Enter the number (how many times the FIBONACII is to be generated)"read nlet p=0let c=1let i=3echo " "echo "$p"echo "$c"while(($i <= $n))dolet sum=$p+$cecho "$sum"let p=$clet c=$sumlet i=$i+1done

31

INPUT:

OUTPUT:

STEP 3: PRACTICE/TESTING

Questions:

1. What is the Fibonacci series for the given number?

2. What are the condition statements used in UNIX?

RESULT:

32

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

C PROGRAMMING

DYNAMIC STORAGE ALLOCATION

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

The objective of this program is to introduce students to handle the different storage

system. It means program can its memory while it is running. It allows us to allocate addition

memory space or to release unwanted space at the time of program execution (runner).

AIM:

To write and execute a C program in UNIX of to print variables from memory

address.

ALGORITHM:

Step 1: Start program.

Step 2: Initialize the data variables.

Step 3: Determine the sizeof() function.

Step 4: Initialize the number of bytes allocated to pointer *n..

Step 5: Reads the value for for loop.

Step 6: Reads the value for a each time until it reaches the variable size.

Step 7: Stop the program

33

CODING:

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

void main()

{

int *a,*n,size;

clrscr();

printf(“Enter the size…”);

scanf(“%d”,&size);

n=(int *)malloc(size * sizeof(int));

printf(“Address of the first byte is …%u”,n);

printf(“Enter the Values…”);

for(a=n;a<n+size;a++)

scanf(%d”,a);

for(a=n+size-1;a>=n;a--)

printf(“%d is stored in address %u”,*a,a);

}

34

OUTPUT:

STEP 3: PRACTICE/TESTING

Questions:

1. What are the differences between malloc() and calloc()?

2. Where are the auto variables stored?

3. How can you increase the size of a dynamically allocated array?

RESULT:

35

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

POINTERS

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

The objective of this program is to introduce students to the usage of Pointers. A

pointer is a memory variable, it contain the memory address of the another variable. It is

declared in the same manner like other variables

AIM:

To write a C program to print address and value of variables.

ALGORITHM:

Step 1: Start program.

Step 2: Assign fork() system call to pid.

Step 3: if pid is equal to -1, child process not created.

Step 4: if pid is equal to 0, child process will be created.

Step 5: Print the id of parent process and child process.

Step 6: Create another one child process in same loop.

Step 7: Print id of parent process and the child process.

Step 8: Print grandparent id.

Step 9: Stop the program

36

CODING:

#include <stdio.h>

main()

{

int a=22;

int *a;

a=&a;

printf(“\n values of a =%d”,*a);

printf(“\n Address of a = %u”,&a);

printf(“\n value at address %u=%d”,&a,*(&a));

}

OUTPUT:

37

STEP 3: PRACTICE/TESTING

Questions:

1. A pointer is a…………………..

2. Pointer always denoted b ……………………

RESULT:

.

38

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

FUNCTIONS

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

The objective of this program is to introduce students about a function, which is a set

of instruction that are used to perform specified task.

AIM:

To write a user defined function that computes ‘x’ raised to the power of ‘y’

ALGORITHM:

Step 1: Start program.

Step 2: Initialize the data variables x, pid & SIGNALRM.

Step 3: if pid & fork() is equal to 0, Alarm stopped.

Step 4: if pid is equal to 0, child process will be created.

Step 5: Print the process of alarm going to off.

Step 6: If x is equal to alarm handler function, the alarm function is done.

Step 7: Obtained the signal from the handler, the alarm goes to offline it can be

restarted.

Step 8: Stop the program.

39

CODING:

#include<stdio.h>

#include<conio.h>

Main()

{

int x,y;

int power(int,int);

clrscr();

printf(“enter x and y values…”);

scanf(“%d%d”,&x,&y);

printf(“%d to the power of %d is .. %f”,x,y,power(x,y));

}

int power(int x,int y)

{

float p=1.0;

if(y>=0)

while(y--)

p*x;

else

while(y++)

p/=x;

return(p);

}

40

OUTPUT:

STEP 3: PRACTICE/TESTING

Questions:

1. List out the types of functions?

RESULT:

41

KONGUNADU COLLEGE OF ENGINEERING AND TECHNOLOGY

FILE HANDLING

STEP 1: INTRODUCTION

OBJECTIVE OF THE EXERCISE

The objective of this program is to introduce students about a File, which is a set of

instruction that is used to perform specified task.

AIM:

To write a program for copying the file both read and write operations must be used in

the same buffer.

ALGORITHM:

Step 1: Start program.

Step 2: Initialize the data variables x, pid & SIGNALRM.

Step 3: if pid & fork() is equal to 0, Alarm stopped.

Step 4: if pid is equal to 0, child process will be created.

Step 5: Print the process of alarm going to off.

Step 6: If x is equal to alarm handler function, the alarm function is done.

Step 7: Obtained the signal from the handler, the alarm goes to offline it can be

restarted.

Step 8: Stop the program.

42

CODING:

#include<fcntl.h>

#include<sys.h>

#define BUFSIZE 1024

int main(void)

{

int fd1,fd2,n;

char buf[BUFSIZE];

fd1=open(“etc/addition”,O_RDONLY);

fd2=open(“addition1”,O_WRONLY\O_CRET\\O_TRUNC);

while((n=read(fd1,buf,BUFSIZE))>0)

write(fd2,buf,n);

close(fd1);

close(fd2);

exit(0);

}

OUTPUT:

43

STEP 3: PRACTICE/TESTING

Questions:

1. Write the syntax of open a File.

2. Write the syntax of close a File.

3. Write the syntax of read a File.

4. Write the syntax of read a File.

RESULT:

44