Sie sind auf Seite 1von 33

VELAMMAL

INSTITUTE OF TECHNOLOGY
(Approved By AICTE ,NewDelhi & Affiliated to ANNA UNIVERSITY, Chennai)

"Velammal Gardens", Chennai - Kolkatta High Road, Panchetti, Thiruvallur - 601 204

DEPARTMENT OF INFORMATION TECHNOLOGY


COMPUTER PRACTICE LABORATORY II (GE2155) MANUAL

Name

: M.Rajeswari

Branch : B.E (Electronics and Communication Engineering)

M.Rajeswari

Dept. of Information Technology

GE2155

COMPUTER PRACTICE LABORATORY II

0 1 2 2

LIST OF EXPERIMENTS

1. UNIX COMMANDS Study of Unix OS - Basic Shell Commands - Unix Editor 2. SHELL PROGRAMMING Simple Shell program - Conditional Statements - Testing and Loops 3. C PROGRAMMING ON UNIX Dynamic Storage Allocation-Pointers-Functions-File Handling

15

15

15

TOTAL : 45 PERIODS

HARDWARE / SOFTWARE REQUIREMENTS FOR A BATCH OF 30 STUDENTS Hardware 1 UNIX Clone Server 33 Nodes (thin client or PCs) Printer 3 Nos. Software OS UNIX Clone (33 user license or License free Linux) Compiler - C

M.Rajeswari

Dept. of Information Technology UNIX COMMANDS

EX No 1

STUDY OF UNIX OPERATING SYSTEM

An operating system (commonly abbreviated OS and 0/5) is the software component of a computer system that is responsible for the management and coordination of activities and the sharing of the limited resources of the computer. The operating system acts as a host for applications that are am on the machine. As a host, one of the purposes of an operating system is to handle the details of the operation of the hardware.. Operating systems offer a number of services to application programs and users. Applications access these services through application programming interfaces (APIs) or system calls. By invoking these interfaces, the application can request a service from the operating system, pass parameters, and receive the results of the operation. Under Unix, the operating system consists of many of these utilities along with the master control program, the kernel. The kernel provides services to start and stop programs, handle the file system and other common low level tasks that most programs share, and, perhaps most importantly, schedules access to hardware to avoid conflicts if two programs try to access the same resource or device simultaneously. To mediate such access, the kernel was given special rights on the system, leading to the division between user-space and kernel-space. I.Kernel 2.Shell 3. Users shell programs. It is the main part of the operating system. It controls all the resources, users, hardware and tasks that are present in the system. So we can define kernel as the Master program which act as the heart of the operating system. Shell 1. Receives user command from the shell 2. Communicates with the hardware. 3. Control all the computer resources 4. Runs scheduler. 5. Run all the programs.

The user cannot directly interact with the kernel. During the login of the user the kernel starts an interactive program for each user. This program is known as shell. The shell acts as an interface between the user and kernel. The shell will act as an interpreter. When user gives some command the shell anaiyses those commands and passes them to the kernel. The kernel then submits these commands to hardware and the required actions are carried out. This process is shown below:

M.Rajeswari PARTS OF UNIX OPERATING SYSTEM

Dept. of Information Technology

The UNIX operating system have three parts. They are Kernel

FILES AND DIRECTORIES A file is a collection of information that is assigned a name that is used to identify that file. The file is always stored in secondary storage medium. A directory is a special type file that contains a list of file names. All files are grouped together into directory for easier access. A directory can have one or more directories rn it called sub-directories. In LINUX files and directories are arranged in a hierarchical manner. DIRECTORY HIERARCHY OF UNIX In UNIX every file is assigned to some directory. The root directory is the main directory and named as I. All the other files and directories are its subdirectories.

M.Rajeswari EX No 2 ls --- lists your files

Dept. of Information Technology BASIC SHELL COMMANDS

ls -l --- lists your files in 'long format', which contains lots of useful information, e.g. the exact size of the file, who owns the file and who has the right to look at it, and when it was last modified. ls -a --- lists all files, including the ones whose filenames begin in a dot, which you do not always There are many more options, for example to list files by size, by date, recursively etc. more filename --- shows the first part of a file, just as much as will fit on one screen. Just hit the space bar to see more or q to quit. You can use /pattern to search for a pattern. emacs filename --- is an editor that lets you create and edit a file. . mv filename1 filename2 --- moves a file (i.e. gives it a different name, or moves it into a different directory cp filename1 filename2 --- copies a file rm filename --- removes a file. It is wise to use the option rm -i, which will ask you for confirmation before actually deleting anything. diff filename1 filename2 --- compares files, and shows where they differ

wc filename --- tells you how many lines, words, and characters there are in a file chmod options filename --- lets you change the read, write, and execute permissions on your files. The default is that only you can look at them and change them, but you may sometimes want to change these permissions. For example, chmod o+r filename will make the file readable for everyone, and chmod o-r Directories Directories, like folders on a Macintosh, are used to group files together in a hierarchical structure. mkdir dirname --- make a new directory cd dirname --- change directory. You basically 'go' to another directory, and you will see the files in that directory when you do 'ls'. You always start out in your 'home directory', and you can get back there by typing 'cd' without arguments. 'cd ..' will get you one level up from your current position. You don't have to walk along step by step ..

M.Rajeswari

Dept. of Information Technology

SHELL PROGRAMMING The shell is a program that interprets whatever user type at the terminal and responds accordingly. The command given from the user is moved to the shell, the shell analyses and interprets these commands into machine understandable form. The shell acts as an interface between the user and the system. Various + The Bourne shell + The Korn shell + The C shell PARTS OF UNIX OPERATING SYSTEM The Unix Operating System has three parts. They are 1. Kernel 2. Shell 3. Users shell program. KERNEL (HEART OF A UNIX SYSTEM) It is the core of the Operating System. It controls all the all the functions of an Operating System, such as memory and file management etc and it keep track of programs that are executing. When the computer is switch on or boot up, this program in UNIX is loaded into the computers main memory, where it remains until the computer is shut down. Once the Kernel is loaded to memory, it is ready to carry out the user requests. The user makes requests to a shell, which interprets them, and passes them on to the Kemel. The Kemel is responsible for Functions of a Shell Command line interpretation 1.Program initiation 2.Transmission Output redirection 3.Pipeline connection 4.Substitution of filenames 5.Maintenance of variables 6. Environment control 7.Shell programming

M.Rajeswari EX No 3

Dept. of Information Technology UNIX EDITOR

UNIX system starts a special version of a shell called an interactive shell, and shows a shell prompt, usually in the form of a dollar sing ($), a percent sign( %), or a pound sign (#), When we type a line of input at a shell prompt, the shett tries to interpret it. Input to a shell prompt is sometimes called a command line.The basic format Modes in vi There are three basic modes of vi: Command mode This is the default when you enter vi. In command mode, most letters, or short sequences of letters, that you type will be interpreted as commands, without explicitly pressing Enter . If you press Esc when you're in command mode, your terminal will beep at you. This is a very good way to tell when you're in command mode. Insert mode In insert mode, whatever you type is inserted in the file at the cursor position. Type a (lowercase letter a, for append) to enter insert mode from command mode; press Esc to end insert mode, and return to command mode. Line mode Use line mode to enter line oriented commands. To enter line mode from command mode, type a colon . Your cursor moves to the bottom of the screen, by a colon prompt. Type a line mode command, then press Enter. Any sensible command from the Unix line editor ex will work, and a few are good to know about. These commands are indicated in this handout by a colon in front of the command. Each time you use a line mode command, you must type a colon to enter line mode, then type the command by the colon prompt at the bottom of the screen, then press Enter when you finish typing the command.

Starting vi and Saving Files

Starting vi: vi filename Start editing filename, create it if necessary

Saving the file you're working on and/or leaving vi: :wq :q! write the file to disk and quit Quit without saving any changes

M.Rajeswari Moving the Cursor

Dept. of Information Technology

Many commands take number prefixes; for example 5w moves to the right by 5 words. Type: H J K L $ ^ Enter G :n w E B Ctrl-b To Move To: one space to the left (also try left arrow) one line down (also try down arrow) one line up (also try up arrow) one space to the right (also try right arrow) end of current line beginning of current line beginning first word on the next line end of file line n; use :0 to move the beginning of the file beginning of next word; 5w moves to the beginning of the 5th word to the right end of next word beginning of previous word one page up

Searching for Text Type: /string ?string N To: search down for string search up for string repeat last search from present position

M.Rajeswari Inserting Text Type: a A i I o O :r newfile To: append starting right of cursor append at the end of the current line insert starting left of cursor insert at beginning of the current line

Dept. of Information Technology

open line below cursor, then enter insert mode open line above cursor, then enter insert mode add the contents of the file newfile starting below the current line

Deleting Text Type: X Dw Dd Cw Cc S D C U To: Delete single character; 5x deletes 5 characters Delete word; 5dw deletes 5 words Delete line; 5dd deletes ... well you get the idea! Delete word, leaves you in insert mode (i.e. change word) change line -- delete line and start insert mode change character -- delete character and start insert mode Delete from cursor to end of line change from cursor to end of line -- delete and start insert mode undo last change

M.Rajeswari

Dept. of Information Technology

Cutting and Pasting Type: Xp Yy "ayy P "aP P "ap To: transpose two characters (two commands, x followed by p) yank (i.e. copy) one line into a general buffer (5yy to yank 5 lines) yank into the buffer named a put the general buffer back before the current line put from buffer a before current line put the general buffer back after the current line put from buffer a after the current line

Miscellaneous Commands Type: Ctrl-g To: show line number of current line

10

M.Rajeswari

Dept. of Information Technology SIMPLE SHELL PROGRAMS

EX No 4 Aim:

CHECKING OF EVEN OR ODD NUMBER

To write a shell program to find the given number is even or odd. Algorithm: 1. Start the program. 2. Enter the number to check even or odd, 3. Get the remainder by dividing the number by 2 (use % operator). 4. Check if remainder is zero then given number is even otherwise given number . 5. Stop the program Program: #ODD-EVEN checking program echo Enter a number read a z=expr$a%2 if test $z eq 0 then echo Even number else echo Odd number fi Output: Enter the number 22 Even Number. Result: Thus the program is written and executed for checking the given number is even or Odd.

11

M.Rajeswari Ex. No 5 Aim:

Dept. of Information Technology CHECK POSITIVE OR NEGATIVE NUMBER

To write a shell program to check the given number is positive or negative. Algorithm: 1. Start the program. 2. Get the number n from the user. 3. C heck if n <0 then print negative number. If n > 0 then print positive number otherwise print zero. 4. Stop the program. Program: echo Enter a number read n if test $n-gt0 then echo Positive number elif test $n -it 0 then echo Negative number else echo zero Output : enter the number:72 (positive number) enter a number : -3 (negative) Result: Thus the program is written and executed for checking the given number positive or Negative.

12

M.Rajeswari Ex. No: 6 Aim:

Dept. of Information Technology GENERATING ARMSTRONG NUMBERS

To write a shell program to print the Armstrong numbers between the given limit. Algorithm: 1. Start the program. 2. Get the boundary value from the user. 3. Initialize i = 1. 4. If i n then assign number = i. 5. If i not equal to number then separate the digits of the number by dividing the number by 10. 6. The remainder value is cubed and it is added to the sum value. 7. Change the number = number I 10. 8. Repeat steps 5 to 7. 9. Check the number and the summed value. If both are same then that number is an Armstrong number. 10. Increment the i value by 1. 11. Repeat steps4to 10. 12. Stop the program. Program: #Armstrong Number Generation echo Enter the boundary read n jt 1 while test $i ne $no do flag = 0 no = $i while test $i ne 30 do r =expr$i%10 flag = expr $flag + $r $r K $r i=expr$iI 10 do if test $flag eq $no then echo is an Armstrong Number fi

sf0
expr$i+l done

13

M.Rajeswari Output Enter the boundary 200 1 is an Armstrong number 153 is an Armstrong number.

Dept. of Information Technology

Result Thus the program is written and executed for displaying Armstrong numbers between the given limits.

14

M.Rajeswari

Dept. of Information Technology

Ex. No: 7

GENERATING FIBONACCI SERIES

Aim: To write a shell program to generate the Fibonacci series for the given limit. Algorithm 1. Start the program. 2. Get the limit number n from the user. 3. Assigna=-1,b=lands=O. 4. Ifs<nthens=a+bandassigna=bandb=s. 5. Print the value of s. 6. Repeat step4. 7. Stop the program. Program #Fibonacci series echo Program to generate Fibonacci series echo Enter the range to be displayed read n a=0 b= 1 echo fibo series echo $a echo $b i=2 while test $i -It $n do c = expr $a + $b echo $c i=expr$i+ 1 a = $b b = $c done

15

M.Rajeswari

Dept. of Information Technology

Output : Program to generate Fibonacci series Enter the range to be displayed 5 0 11 1 2 3 Result: Thus the Fibonacci series is generated.

16

M.Rajeswari

Dept. of Information Technology

EX NO :8

INTERCHANGING TWO NUMBERS

Aim To write a shell program to interchange two numbers using a temporary variable and without using a temporary variable. Algorithm Using the Temporary Variable 1.Enter the two numbers. 2.Declare the temporary variable C and do tne tollowing assignments c=a, a=bandb=c. 3.Display the result. Without using temporary variable 1.Enter the two numbers by using a and b variable. 2. Then using the expressions. a= a+b,b=abanda=a-b, values areinterchanged. 3.Display the result.

IProgram: 1
#Swap using Temporary variable echo swapping using temporary variable echo Enter a read a echo Enter b read b
c = $a a = $b b = $c

echo After swapping echo Number a is $a echo Number b is $b program 2 (using the Tempoary Variable) Using Temporary variable Program : 2 #swap without Temporary variable echo Swapping without using temporary variable echo Enter a read a echo Enter b read b a = expr $a + $b b = expr $a $b a = expr $a $b echo After swapping
-

17

M.Rajeswari echo a is $a echo b is $b

Dept. of Information Technology

Output Swapping using temporary variable Enter a 10 Enter b 15 After swapping ais 15 bis 10 Swapping without using temporary variable Enter a 6 Enter b 7 After swapping ais7 b is 6 Result: Thus the program is written and executed for swapping of two numbers.

18

M.Rajeswari

Dept. of Information Technology

Ex.No: 9

CALCULATING FACTORIAL OF A GIVEN NUMBER

Aim : To write a shell program to find the factorial of a given number. Algorithm : 1.Read the number. 2.Initialize the value of i and factorial f to 1. 3.While the value of i is less than the number find the value of factorial by multiplying i and f. 4.Increment the value of i by 1. 5. Display the result. Program #Factorial program echo Enter a number read n i=1 f= 1 if test $n -eq 0 then echo factorial is 1 else while test $i -le $n do f= expr $f\ * $1 i=expr$i+ 1 done echo Factorial is $f fi Output: Enter a number 5 Factorial is 120 Result: Thus the program is written and executed to find the factorial of the given number.

19

M.Rajeswari

Dept. of Information Technology

EX NO:10

GIVEN STRING IS PALINDROME OR NOT

Aim: To write a shell program to check whether the given string is palindrome or Algorithm: 1.Enter the input. 2. Find the length of the given string. 3.Using while ioop until the length is greater than zero do the following step 4. 4.temp = expr $11 cut c $len len is decremented by 1. 5.If the reverse string is equal to input string,print the given string is palindrome else not palindrome.

Program: rev = len=expr$1 Iwcc while [ $len ge 0] do temp=expr$1 Icutc$len len = expr $ len 1 rev = echo $rev $temp done if [ $i = $rev] then echo The Given string is palindrome else echo Non palindrome fi

Input & Output MalayalaM The Given string is palindrome Result: Thus the program is written and executed to check the given string is palindrome or not.

20

M.Rajeswari Ex. No: 11

Dept. of Information Technology

CALCULATING TELEPHONE BILL

Aim:
To write a shell script program to print the bill for the customer of telephone company at the following rates: For the first 50 calls rate Rs. 25 (fixed rate) For next 100 calls Rs. 6 (per 10 calls) For next 200 calls Rs. 7 (per 10 calls) For more than 350 calls Rs. 8 (per 5 calls)

Algorithm: 1.Enter the number of calls. 2.If the number of calls are less than 25, initialize sum = 25, else if the call number is between 51 and 150, calculate sum 1 = call * 0.6 else if the I number of calls are between 151 and 350, calculate sum 2 = call * 0.7 else I calculate for Rupees 8. 3.Display the result. Program: #Telephone Bill while test ch do echo Telephone Bill echo ********* read call if test $call le 50 then sum 25 echo charge $sum elif test $call -gt 51 && test $call -le 150 then sum1 = echo sum + $call \* 0.6 bc echo charge $suml elif test $call -gt 151 && test $call -le 250 then sum2 = echo sum + $call \* 0.7 I bc echo charge $sum2 else sum3 = echo $sum2 + $call \ * 0.4 I bc echo charge $sum3 fi echo do you want to continue(y/n) read ch done

21

M.Rajeswari Output: Telephone Bill


* ** * * * ** *

Dept. of Information Technology

Enter the number of calls 27 Charge 25 Result: Thus the program is written and executed to print the telephone Bill.

22

M.Rajeswari

Dept. of Information Technology

Ex. No:12

SORTING THE CONTENT OF THE FILE

Aim To write a shell program to sort the given names in a file. Algorithm: 1.Start the program 2.Get the file name in which sorting is to due 3.Enter the names to be sorted. 4.Using while loop, the names are compared using>> and names are sorted 5.Display the sorted names. Program: echo enter the file name read x echo enter the number of names read num echo enter $num strings while test $num -gt 0 do read name echo $name >> $x num = expr $num -1 done echo the sorted names are: sort $x rm $x Output: Enter the file name Sort 1 Enter the number of names: 5 Enter 5 strings: Computer History Biology Maths

23

M.Rajeswari The sorted strings are: Biology Computer Maths History Result:

Dept. of Information Technology

Thus the program is written and executed for sorting the content of the file.

24

M.Rajeswari

Dept. of Information Technology C PROGRAMMING ON UNIX

Ex.No:13 Aim:

SWAPPING OF TWO NUMBERS

To write a program to swap the given two values in the function, the values are passed as call by address. Algorithm: 1.. Start the program. 2. Interchange two values using temporary variables 3. The values after swapping are displayed. 4. End the program. Program: #include <stdio.h> void swap (int *, int*); main()

{
int x = 5, y = 10; clrscr(); printf(values before swapping are %d %d, x, y); swap(&x, &y); printf(\nThe values after swapping are %d %d, x,y); getchO;

}
void swap (int *a, int *b)

{
int t;
t=

*a = *b; *b = t;

}
Output: The values before swapping are 5 1 0 The values after swapping are 1 0 5 Result: Thus the program is written and executed to swap two numbers using pointers

25

M.Rajeswari

Dept. of Information Technology

Ex. No: 14

COPY ONE FILE TO ANOTHER FILE

Aim To write a C program to copy one file to another. Algorithm: 1. Start the program. 2 Open the source file in read and write mode. 3. Create the new file and open with new file name. 4. Read each character from the source file and write the character into the destination file until end of the character. 5. Stop the program. Program: #include <stdio.h> #include <fcntl.h> #include <types.h> main()

{
int fd, fdl, fd2, n; char buff[80j; fd = open(Bensam.txt, ORDWR); fd2 = open(Jovita.txt, O_RDWR); Iseek(fd,0,0); while((n=read(fd, buff, I ))>O)

{
write(fd2,buff,n),

}
printf(File copied); } Content of Bensam.txt This is Bensam Output: File Copied Content of Jovita.txt This is Bensam Result : Thus the program is written and executed to copy the content of one file to another file.

26

M.Rajeswari

Dept. of Information Technology

Ex. No :15

ALLOCATE MEMORY FOR ARRAY USING CALLOC

Aim: To write a program in C to allocate memory for array using calloc. Algorithm: 1. Start the program. 2. Declare the pointer variable. 3. Allocate the memory space using mallocQ. 4. If pointerData = = NULL, allocation is not done. Else done successfully. 5. Get number of items to be stored and the numbers. 6. Print the entered numbers 7. Stop the program. Program: #include <stdio.h> #include <stdlib.h> int main 0

{
int i, n; int *pojnterData; printf (Enter number of items to be stored: ); scanf (%d, &i); pointerData = (int*) calloc (i, sizeof(int)); if (pointerData==NULL) exit (1); for (n =0; n < i; n++)

{
printf (Enter number #%d: , n); scanf (%d, &pointerData[ n

}
printf (You have entered: ); for (n =0; n <i; n++) printf (%d , pointerData[ n free (pointerData); return 0; }

27

M.Rajeswari

Dept. of Information Technology

Output Enter number of items to be stored: 4 Enter number #0:3 Enter number #1:2 Enter number #2:1 Enter number #3:5 You have entered: 3 2 1 5 Result: Thus the program is written and executed to allocate memory for array using calloc.

Ex. No: 16

ALLOCATE SPACE FOR A STRING DYNAMICALLY AND

28

M.Rajeswari

Dept. of Information Technology PRINT THE STRING BACKWARDS

Aim To write a program in c to allocate space for a string dynamically and print the string backwards Algorithm: 1.Start the program. 2.Declare the pointer variable s. 3.Allocate the memory space using malloc. 4.Ifs = = NULL, allocation is not done. Else done successfully. 5.Get the string from the user 6.Read the string from backward and print the result. 7.Stop the program. Program #include <stdlib.h> #include <stdio.h> #inciude <string.h>
mt main(void)

{
char *S; register int i;
s = malloc(80);

if(!s) { printf(Memory request failed.\n); exit(1);

}
gets(s); for(i = strien(s) 1; i >= 0; i--) putchar(s[ i ]);
-

free(s); return 0;

}
Output: Hai iaH Result: Thus the program is written and executed to allocate space for a string dynamically and print the string backwards.

29

M.Rajeswari Ex. No: 17 SORTING TECHNIQUES

Dept. of Information Technology

Aim: To write a program in C using pointer to sort the given the values in ascending and descending order pass the values in array as arguments. Algorithm: 1. Start the program. 2. Declare the array variable 3. Call the function get data, in which the scanf function is used to get the values. 4. Call the function shown, the array is passed as argument, sorting takes place. 5. Display the result. 6. Stop the program. Program: #include <stdio.h> #define MAX 50 main()

{
int arr[MAX],n; int getdata(int*,int);

void show(int* ,int); n = getdata(arr,MAX); show(arr,N);

}
int getdata(int*a, intn)

{
int x,i =0;

printf(Enter the array elements one by one); whiie(i<n)

{
scanf(%d,&x) (a+i) = x; i + +;
} return i; }

void show(int*a,int n)

{
intl; for(i=0; i<n; ++i)

{
if(a[i]>a[j])

30

M.Rajeswari

Dept. of Information Technology

{ }
printf(%d, *a(j));

}
Output

Enter the array elements one by one 17 16 9 4 3 27 3 4 9 16 17 27 Result: Thus the Program is written, executed and output is tested.

31

M.Rajeswari

Dept. of Information Technology

Ex. No: 18 Aim:

COUNT THE NUMBER OF CHARACTERS

To write a C program that counts the number of characters and number of lines in a file. Algorithm: 1. Start the program. 2. Open the file in the read mode. 3. Get each character from the file the end of file. 4. Increment the counter variable for the character and number of lines is determined. 5. While the end of file is encountered, the loop gets terminated. The number of character and lines are displayed. 6. End the program. Program: #include <stdio.h> void main() FILE *fopen( ),*fp; intch,nc,nlines; char fname [30]; nlines = 0; nc = 0; printf(enter file name); fflish(stdin(; scanf(%s,fname)); fp=fopen(fname,r); if(fp= = NULL) printf(cannot open the file %s for reading,fname); exit(0);

}
ch=getc(fp); while(ch ! =EOF) } Output : Enter file name A.txt There are 7 character in A.txt There are 2 lines.

32

M.Rajeswari

Dept. of Information Technology

Result: Thus the Program is written, executed and output is tested.

33

Das könnte Ihnen auch gefallen