Sie sind auf Seite 1von 74

Ex.No.

: 01 Date : BASIC UNIX COMMANDS

AIM: To execute and implement the basic UNIX commands. COMMANDS AND THEIR OUTPUT: 1. [root@localhost root]# who am I Identifies the current working System root pts/0 Mar 9 19:21 (:0.0) 2. [root@localhost root]# cat > msb Creates a New file msb My Name is Barnabas 3. [root@localhost root]# cat msb Opens the created file msb" My Name is Barnabas 4. [root@localhost root]# cat >> msb Appends the details in the file msb I'm studying in Vel Tech Multi Tech Engineering college [root@localhost root]# cat msb My Name is Barnabas I'm studying in Vel Tech Multi Tech Engineering college 5. [root@localhost root]# pwd /root Shows the present working directory

6. [root@localhost root]# mkdir barnabas Creates a directory named barnabas 7. [root@localhost root]# cd barnabas [root@localhost barnabas]# 8. [root@localhost barnabas]# cd .. [root@localhost root]# Enters into the directory barnabas

Comes out of the directory barnabas

9. [root@localhost root]# ls Shows the list of files & directories anaconda-ks.cfg arm.sh count.sh great.sh install.log msb swap3.sh arith.sh barnabas fib.sh group_1280.jpg install.log.syslog Shell swap.sh 10. [root@localhost root]# ls -l List the permission of files / Directories total 264 -rw-r--r-- 1 root root 1093 Mar 11 2010 anaconda-ks.cfg -rw-r--r-- 1 root root 223 Mar 9 18:04 arith.sh -rw-r--r-- 1 root root 260 Mar 9 18:18 arm.sh drwxr-xr-x 2 root root 4096 Mar 9 19:24 barnabas drwxr-xr-x 2 root root 4096 Mar 9 19:11 Shell 1

11. [root@localhost root]# wc msb 2 10 65 msb

Shows the no. of lines, words & characters.

12. [root@localhost root]# wc -l msb Shows the no. of lines alone. 2 msb 13. [root@localhost root]# wc -w msb Shows the no. of words alone. 10 msb 14. [root@localhost root]# wc -c msb Shows the no. of characters alone. 65 msb 15. [root@localhost root]# echo Hello Hello 16. [root@localhost root]# cal 4 1988 April 1988 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 Prints Hello

Prints the calendar of April 1988

17. [root@localhost root]# cal Prints the calendar if the present month March 2010 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 18. [root@localhost root]# date Prints the cuurent time. Thu Mar 9 19:26:27 IST 2010 19. [root@localhost root]# time l Prints the System Run-time real 0m0.005s user 0m0.000s sys 0m0.000s 20. [root@localhost root]# tty /dev/pts/0 Prints the Terminal Mode of the System.

21. [root@localhost root]# bc Open the Binary Calculator bc 1.06 Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc. This is free software with ABSOLUTELY NO WARRANTY.

For details type `warranty'. 2

123+246 369 22. [root@localhost root]# grep m msb Prints the sentence with the letter m My Name is Barnabas I'm studying in Vel Tech Multi Tech Engineering college 23. [root@localhost root]# tail -1 msb Prints the last line of the file msb I'm studying in Vel Tech Multi Tech Engineering college 24. [root@localhost root]# head -1 msb Prints the first line of the file msb My Name is Barnabas 25. [root@localhost root]# uname Linux Prints the username

26. [root@localhost root]# uname r Prints the release of the software. 2.4.20-8 27. [root@localhost root]# uname v Prints the version of release #1 Thu Mar 13 17:54:28 EST 2010 28. [root@localhost root]# sort msb1 Sorts in ascending order Aravind Balaji Dinesh Ganesh Kamalesh 29. [root@localhost root]# sort -r msb1 Sorts in descending order Kamalesh Ganesh Dinesh Balaji Aravind 30. [root@localhost root]# man cal Opens manual on cal

31. [root@localhost root]# rmdir barnabas Removes the directory barnabas 32. [root@localhost root]# rm msb Removes the file msb rm: remove regular file `msb'? y

33. [root@localhost root]# chmod 111 msb1

Modifies the permission of the file 3

[root@localhost root]# ls -l -rw-r--r-- 1 root root -rw-r--r-- 1 root root -rw-r--r-- 1 root root drwxr-xr-x 2 root root -rw-r--r-- 1 root root ---x--x--x 1 root root -rw-r--r-- 1 root root

1093 Mar 11 2010 anaconda-ks.cfg 223 Mar 9 18:04 arith.sh 260 Mar 9 18:18 arm.sh 4096 Mar 9 19:11 Shell 3020 Mar 11 2010 install.log.syslog 39 Mar 9 19:30 msb1 97 Mar 9 18:31 swap3.sh

RESULT: Thus the basic UNIX commands were executed.

Ex.No: 02 Date: ARITHMETIC OPERATION USING SHELL PROGRAM

AIM: To write a shell Program to do the Arithmetic Operations like Addition, Subtraction, Multiplication, Division.

ALGORITHM: Step 1: Start

Step 2: Read the values of a and b

Step 3: Write the expression to calculate sum, difference, Product, Quotient and Remainder. Step 4: Print the Results Step 5: Stop.

SHELL PROGRAM: echo "Enter Two Numbers" read a b c=`expr $a + $b` d=`expr $a - $b` e=`expr $a \* $b` f=`expr $a / $b` g=`expr $a % $b` echo "Sum = $c" echo "Difference = $d" echo "Product = $e" echo "Quotient = $f" echo "Remainder = $g"

SAMPLE OUTPUT: Enter Two Numbers 12 10 Sum = 22 Difference = 2 Product = 120 Quotient = 1 Remainder = 2

RESULT: Thus the shell Program to do arithmetic operation was executed without any errors.

Ex. No.: 03 Date : GREATEST OF THREE NUMBERS AIM: To write a shell program to find the greatest of three Numbers.

ALGORITHM: Step 1: Start Step 2: Read the value of a, b and c Step 3: Check if a is greater than b and a is greater than c Step 4: Then print A is Greater Step 5: If Step 3 is not satisfied check if b is greater than c Step 6: Then print B is Greater Step 7: Else Print C is greater Step 8: Stop.

SHELL PROGRAM: echo "Enter Three Numbers" read a b c if [ $a -gt $b -a $a -gt $c ] then echo "$a is Greater" elif [ $b -gt $c ] then echo "$b is Greater" else echo "$c is Greater" fi

SAMPLE OUTPUT: Enter Three Numbers 12 14 16 16 is Greater

RESULT: Thus the program to find the greatest of three Numbers was executed and the output was verfied.

10

Ex. No.: 04 Date : CHECKING WHETHER THE GIVEN NUMBER IS ARMSTRONG OR NOT

AIM: To check whether the given Number is Armstrong Number or not using Shell Programming.

ALGORITHM: Step 1: Start Step 2: Read the value of num Step 3: Store the value of num in x and initialize the value of sum = 0. Step 4: When the value of num is greater then 0, solve the following expressions Step 5: y = num/10; z=y*y*y; sum=num+z; num=num/10 Step 6: Close the While Loop Step 7: Check if x is equal to sum Step 8: Print the Result Step 9: Stop.

11

SHELL PROGRAM: echo "Enter a Number" read num x=$num sum=0 while [ $num -gt 0 ] do y=`expr $num % 10` z=`expr $y \* $y \* $y` sum=`expr $sum + $z` num=`expr $num / 10` done if [ $x -eq $sum ] then echo "$x is an armstrong Number" else echo "$x is not an armstrong Number" fi

12

SAMPLE OUTPUT: Enter a Number 153 153 is an armstrong Number [root@localhost root]# sh arm.sh Enter a Number 123 123 is not an armstrong Number

RESULT: Thus the Shell Program to check whether the given Number is Armstrong or not was executed and the output was verified.

13

Ex No.: 05 Date : FIBONACCI SERIES AIM: To write a Shell Program to print the Fibonacci series.

ALGORITHM: Step 1: Start Step 2: Read the Value of n Step 3: Initialize i=2; a=0; b=1. Step 4: Print the Value of a and b Step 5: When i is lesser than n, solve the expression c=a+b Step 6: Print c Step 7: Swap the Values of b to a and c to b Step 8: Solve the expression i=i+1 Step 9: Stop.

14

SHELL PROGRAM: echo "Enter the Limit" read n i=2 echo "Fibonacci Series" echo "----------------" a=0 b=1 echo $a echo $b while [ $i -lt $n ] do c=`expr $a + $b` echo $c a=$b b=$c i=`expr $i + 1` done

15

SAMPLE OUTPUT: Enter the Limit 8 Fibonacci Series ---------------0 1 1 2 3 5 8 13

RESULT: Thus the Shell Program to print the Fibonacci Series was executed and the output was verified.

16

Ex. No.: 06(a) Date : SWAPPING OF VALUES (Using 3rd Variable)

AIM:

To write a Shell Program to swap two values using 3rd variable.

ALGORITHM: Step 1: Read the Values of a and b

Step 2: Swap the values of a to the third variable temp

Step 3: Swap the value of b to a and b to temp

Step 4: Print the swapped values of a and b Step 5: Stop.

17

SHELL PROGRAM: echo "Enter Two Numbers" read a b temp=$a a=$b b=$temp echo $a $b

18

SAMPLE OUTPUT: Enter Two Numbers 12 10 10 12

RESULT:

Thus the program to swap two values using 3rd variable was executed and the output was verified.

19

Ex. No.: 06(b) Date : SWAPPING OF VALUES (Without using 3 rd Variable)

AIM:

To write a Shell Program to swap two values without using 3rd Variable.

ALGORITHM: Step 1: Start Step 2: Read the values of a and b Step 3: Solve the expression, a=a + b; b=a b; a=a b Step 4: Print the swapped values of a and b Step 5: Stop

20

SHELL PROGRAM: echo "Enter Two Numbers" read a b a=`expr $a + $b` b=`expr $a - $b` a=`expr $a - $b` echo $a $b

21

SAMPLE OUTPUT: Enter Two Numbers 12 10 10 12

RESULT: Thus the Shell Program to swap two values without using third variable was executed and the output was verified.

22

Ex. No.: 07 Date : NUMBER OF DIGITS IN AN INTEGER

AIM: To count the number of digits in an integer, using Shell Programming.

ALGORITHM: Step 1: Start Step 2: Read the value of a Step 3: Initialize the value of c as 0 Step 4: When the value of a is not equal to 0, solve n=a %10. Step 5: If the value of n is not equal to 0, solve c=c + 1(increment) Step 6: End if condition Step 7: Solve the expression a=a / 10 Step 8: Repeat steps 4 to 7 until a is equal to 0 Step 9: Print the value of c as the number of digits in an integer. Step 10: Stop.

23

SHELL PROGRAM: echo "Enter a Number" read a c=0 while [ $a -ne 0 ] do n=`expr $a % 10` if [ $n -ne 0 ] then c=`expr $c + 1` fi a=`expr $a / 10` done echo "The Number of Digits in the Integer is $c"

24

SAMPLE OUTPUT: Enter a Number 123456789 The Number of Digits in the Integer is 9

RESULT: Thus the Shell Program to calculate the number of digits of an integer was executed and the output was verified.

25

Ex. No. 08 Date : ARITHMETIC OPERATIONS USING SHELL PROGRAMMING (Using SWITCH - CASE)

AIM: To write a Shell Program to perform Arithmetic Operations using Switch Case

ALGORITHM: Step 1: Start Step 2: Read the two Numbers. Step 3: Get the operation choice from the User Step 4: Give the expressions for each case and solve them. Step 5: Print the Result Step 6: Stop

26

SHELL PROGRAM: echo "Enter Two Numbers" read a b echo "What do you want to do? (1 to 5)" echo "1) Sum" echo "2) Difference" echo "3) Product" echo "4) Quotient" echo "5) Remainder" echo "Enter your Choice" read n case "$n" in 1) echo "The Sum of $a and $b is `expr $a + $b`";; 2) echo "The Difference between $a and $b is `expr $a - $b`";; 3) echo "The Product of the $a and $b is `expr $a \* $b`";; 4) echo "The Quotient of $a by $b is `expr $a / $b`";; 5) echo "The Remainder of $a by $b is `expr $a % $b`";; esac

27

SAMPLE OUTPUT: [root@localhost Shell]# sh arith_switch.sh Enter Two Numbers 12 10 What do you want to do? (1 to 5) 1) Sum 2) Difference 3) Product 4) Quotient 5) Remainder Enter your Choice 4 The Quotient of 12 by 10 is 1

RESULT: Thus the Shell program to perform arithmetic operations using Switch Case was executed and the output was verified.

28

Ex. No. 09 Date : FACTORIAL OF A NUMBER

AIM: To write a Shell Program to find the factorial of a Number.

ALGORITHM: Step 1: Start Step 2: Read the Number as n Step 3: Assign the value of p as 0 and solve the expression i = n-1 Step 4: While the value of i is greater than or equal to 1 do the following steps. Step 5: Solve the expression n=n*1 and i=i-1 (Decrementing Operation) Step 6: Then print the value of n as the factorial of the given number. Step 7: Stop.

29

SHELL PROGRAM: echo "Enter a Number" read n i=`expr $n - 1` p=1 while [ $i -ge 1 ] do n=`expr $n \* $i` i=`expr $i - 1` done echo "The Factorial of the given Number is $n"

30

SAMPLE OUTPUT:

[root@localhost Shell]# sh fact.sh Enter a Number 5 The Factorial of the given Number is 120

RESULT: Thus the Shell Program to find the factorial of the Number was executed and the output was verified.

31

Ex. No.: 10 Date : CHECKING FOR PRIME NUMBER

AIM: To write a Shell Program to check if the Number is a Prime Number or a Composite Number.

ALGORITHM: Step 1: Start Step 2: Read the Number as n Step 3: Initialize the value of t to 0 and solve the expression i=n-1 Step 4: While i is greater than or equal to 2, perform the following steps. Step 5: Solve p=n%i Step 6: Check the condition (p=0) and then solve t=t+1 Step 7: End the If Condition Step 8: Solve the expression i=i-1 Step 9: If t is greater than 0, print The Number is not a Prime Number Step 10: If the condition is not satisfied, print The Number is a Prime Number

32

SHELL PROGRAM: echo "Enter a Number" read n i=`expr $n - 1` t=0 while [ $i -ge 2 ] do p=`expr $n % $i` if [ $p -eq 0 ] then t=`expr $t + 1` fi i=`expr $i - 1` done if [ $t -gt 0 ] then echo "The Number $n is not a Prime Number" else echo "The Number $n is a Prime Number" fi

33

SAMPLE OUTPUT:

[root@localhost Shell]# sh prime.sh Enter a Number 2 The Number 2 is a Prime Number [root@localhost Shell]# sh prime.sh Enter a Number 4 The Number 4 is not a Prime Number

RESULT: Thus the Shell Program to check if the given number is prime or not, was executed and the output was verified.

34

SUMS OF DIGITS OF AN INTEGER Ex. No. 11 AIM: To write a Shell Program to calculate the sum of digits of an Integer.

ALGORITHM: Step 1: Start Step 2: Read the Integer as num Step 3: Initialize the value of sum=0. Step 4: While the value of num is greater than 0, solve the following expressions. Step 5: Find the remainder of num by 10 and store in y Step 6: Add sum with y and store it in sum Step 7: Divide num by 10 and store the value in num Step 7: Close the while loop Step 8: Print the Result Step 9: Stop

35

SHELL PROGRAM: echo "Enter a Number" read num sum=0 while [ $num -gt 0 ] do y=`expr $num % 10` sum=`expr $sum + $y` num=`expr $num / 10` done echo "The Sum of the Digits of the Integer is $sum"

36

SAMPLE OUTPUT:

[root@localhost Shell]# sh digits_sum.sh Enter a Number 123456 The Sum of the Digits of the Integer is 21

RESULT: Thus the Shell program to calculate the sum of the digits of the integer was executed and the output was verified.

37

C PROGRAMMING UNDER UNIX ENVIRONMENT Ex. No. 12 Date : MATRIX ADDITION

AIM: To write a C Program to add two matrices under UNIX Environment

ALGORITHM: Step 1: Start Step 2: Declare a[10][10], b[10][10], add[10][10], i, j, m, n. Step 3: Get the number of rows and columns as m and n respectively Step 4: Scan the values of the matrices according to the values of m and n. Step 5: Add the two matrices directly and store the values in add[10][10] Step 6: Using for loop print the values of add[10][10] Step 7: End the loop Step 8: Stop.

38

C PROGRAM: #include<stdio.h> main() { int add[10][10],a[10][10],b[10][10],i,j,m,n; printf("\nEnter the Number of Rows and Columns:\n\n "); scanf("%d%d",&m,&n); printf("Enter the 1st Matrix: \n\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&a[i][j]); printf("Enter the 2nd Matrix: \n\n"); for(i=0;i<m;i++) for(j=0;j<n;j++) scanf("%d",&b[i][j]); for(i=0;i<m;i++) { for(j=0;j<n;j++) add[i][j]=a[i][j]+b[i][j]; } printf("The Addition of the two matrices is: \n"); for(i=0;i<n;i++) { printf("\n\n"); for(j=0;j<n;j++)

39

{ printf("%d\t",add[i][j]); } } }

40

SAMPLE OUTPUT: [root@localhost C_Program]# cc matadd.c [root@localhost C_Program]# ./a.out

Enter the Number of Rows and Columns: 2 2 Enter the 1st Matrix:

1 2 3 4 Enter the 2nd Matrix:

1 2 3 4 The Addition of the two matrices is:

RESULT: Thus the C Program to add two matrices under UNIX Environment was executed and the output was verified. 41

Ex. No. 13 Date : SORTING OF INTEGERS

AIM: To write a C Program to sort an array of integers under UNIX environment

ALGORITHM: Step 1: Start Step 2: Get the number of array integers as n Step 3: Using for loop, get the n number of integers as a[] Step 4: Again using for loop check if the second number is greater than the first Step 5: If the above step is true, swap the value of second to first Step 6: Repeat Steps 4 and 5 for all the integers scanned using for loop Step 7: Again using for loop print the sorted list of values. Step 8: Stop

42

C PROGRAM: #include<stdio.h> main() { int x[50],n,i,j,temp; printf("How many Numbers ?\n"); scanf("%d",&n); printf("\nEnter the List of %d numbers:\n",n); for(i=0;i<n;i++) scanf("%d",&x[i]); for(i=0;i<n-1;i++) for(j=i+1;j<n;j++) if(x[i]>x[j]) { temp=x[i]; x[i]=x[j]; x[j]=temp; } printf("\nThe Sorted list of Numbers is \n"); for(i=0;i<n;i++) printf("%d\t",x[i]); }

43

SAMPLE OUTPUT: [root@localhost C_Program]# cc sort.c [root@localhost C_Program]# ./a.out How many Numbers ? 5

Enter the List of 5 numbers: 4 8 3 6 2

The Sorted list of Numbers is 2 3 4 6 8

RESULT: Thus the C Program to sort the given array of integers under UNIX environment was executed and the output was verified.

44

Ex. No.: 14 Date : FILE MANIPULATION AIM: To write a C Program to perform following file manipulation: 1. Read a file 2. Copy a file 3. Change the case of the entire file 4. Count the number of words, spaces and lines in a file.

45

C PROGRAM: #include<stdio.h> #include<string.h> main() { char ch,ct; int choice,tr,letters=0,words=0,spaces=0,lines=0; FILE *fp1,*fp2; do { fp1=fopen("data1.txt","r"); printf("\nEnter u'r Choice\n"); printf("\n1.Read the file\n2.Copy the file\n3.Change\n4.Count\n5.Exit\n"); scanf("%d",&choice); switch(choice) { case 1: while(!feof(fp1)) { ch=getc(fp1); printf("%c",ch); } break; case 2: fp2=fopen("data2.txt","w"); fseek(fp1,0L,SEEK_SET); while(!feof(fp1)) { ct=fgetc(fp1); tr=fputc(ct,fp2); } fclose(fp1); fclose(fp2); fp2=fopen("data2.txt","r"); 46

printf("The Content of the copied file is "); while(!feof(fp2)) { ch=fgetc(fp2); printf("%c",ch); } fclose(fp2); break; case 3: fp2=fopen("data2.txt","r"); while(!feof(fp2)) { ch=getc(fp2); if((ch<91)&&(ch>=65)) { ch=ch+32; printf("%c",ch); } else if((ch>=97)&&(ch<=122)) { ch=ch-32; printf("%c",ch); } else { printf("%c",ch); } } break; case 4: fp2=fopen("data2.txt","r"); while(!feof(fp2)) { ch=fgetc(fp2); if(ch==' ') 47

{ spaces++; } else if (ch=='\n') { lines++; } else { letters++; } } printf("\nTotal lines: %d",lines); printf("\nTotal words: %d",spaces+lines); printf("\nTotal spaces: %d",spaces); printf("\nTotal letters: %d",spaces+lines+words); fclose(fp2); break; } } while(choice!=5); }

48

SAMPLE OUTPUT: Enter u'r Choice 1.Read the file 2.Copy the file 3.Change 4.Count 5.Exit 1 Wisdom better than the Rubies. ? Enter u'r Choice

1.Read the file 2.Copy the file 3.Change 4.Count 5.Exit 2 The Content of the copied file is Wisdom better than the Rubies. ?? Enter u'r Choice 1.Read the file 2.Copy the file 3.Change 4.Count 5.Exit 3 wISDOM BETTER THAN THE rUBIES. ?? Enter u'r Choice 1.Read the file 2.Copy the file 3.Change 49

4.Count 5.Exit 4 Total lines: 1 Total words: 5 Total spaces: 4 Total letters: 5

RESULT: Thus the FILE MANIPULATION operation was performed and the output was verified.

50

Ex.No.: 15 Date : GREAEST AND SMALLEST NUMBER IN AN ARRAY

AIM: To write a C Program to find the greatest and smallest number in an array of integers.

ALGORITHM: Step 1: Start Step 2: Get the maximum limit for the array as n Step 3: Using for loop, get the array of integers to the value of n Step 4: Again using for loop, sole the following conditions. Step 5: If the first digit is greater than the second, perform the following swap conditions. Step 6: Swap the value of a[0] to temp, a[j] to a[i] and a[i] to temp. Step 7: Print the result Step 8: Stop

C PROGRAM: 51

#include<stdio.h> main() { int a[100],i,j,n,temp; printf("\nEnter the maximum limit: "); scanf("%d",&n); printf("\nEnter %d numbers: \n",n); for(i=0;i<n;i++) scanf("%d",&a[i]); for(i=0;i<n;i++) { for(j=i+1;j<n;j++) { if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } } printf("\nThe smallest number is %d",a[0]); printf("\nThe biggest number is %d",a[n-1]); }

52

SAMPLE OUTPUT: [root@localhost C_Program]# cc maxmin.c [root@localhost C_Program]# ./a.out

Enter the maximum limit: 5

Enter 5 numbers: 9 6 2 10 15 The smallest number is 2 The biggest number is 15

RESULT: Thus the C Program to find the greatest and smallest number in an array was executed and the output was verified.

53

Ex. No.: 16 Date : TO CHECK WHETHER THE NUMBER IS A PALINDROME OR NOT

AIM: To write a C Program to check if the given number is a palindrome or not

ALGORITHM: Step 1: Start Step 2: Get the number as n Step 3: Swap the value of n to c. Step 4: While the value of n is greater than or equal to 1, solve the following expressions. Step 5: Store the remainder of n by 10 as a; Solve b=(b*10)+a; and n=n/10 Step 6: If the value of c is equal to b, print The number is a palindrome. Step 7: If the above condition is not satisfied, print The number is not a palindrome. Step 8: Stop.

54

C PROGRAM: #include<stdio.h> main() { int a,c,n,b=0; printf("\nEnter a number: "); scanf("%d",&n); c=n; while(n>=1) { a=n%10; b=(b*10)+a; n=n/10; } if(c==b) printf("\nThe number is a palindrome\n"); else printf("\nThe number is not a palindrome\n"); }

55

SAMPLE OUTPUT: [root@localhost C_Program]# cc palin.c [root@localhost C_Program]# ./a.out Enter a number: 12321 The number is a palindrome

[root@localhost C_Program]# ./a.out Enter a number: 123 The number is not a palindrome

RESULT: Thus the C Program to check whether the number is a palindrome or not was executed and the output was verified.

56

Ex. No.:17 Date : SYSTEM CALL FOR A READ ONLY FILE

AIM: To write C Program to perform system call to open for a read-only file.

ALGORITHM: Step 1: Start Step 2: Initialize the integer variables- i and fd. Also initialize the string variable, buf[100]. Step 3: Open the already created file aa as a read only and store its contents in fd. Step 4: Read fd, buf, 100 Step 5: Using for loop, print the character array, buf[i] Step 6: The data already stored in aa along with the garbage value will be printed as output. Step 7: Stop Note: A file aa with data must be created before the program is compiled and is run, to avoid segmentation error. Header Files: fcntl.h File control Options unistd.h Standard symbolic constants and types.

57

C PROGRAM: #include<stdio.h> #include<fcntl.h> #include<unistdio.h> main() { int i,fd; char buf[100]; fd=open("aa",O_RDONLY); read(fd,buf,100); for(i=0;i<100;i++) { printf("%c",buf[i]); } }

58

SAMPLE OUTPUT: [root@localhost C_Program]# cc syscall-r.c [root@localhost C_Program]# ./a.out My name is Barnabas t B <@PB@,X@&8Z@0 @ @c@TB

RESULT: Thus the C Program for system call to open a read-only file was executed and the output was verified.

59

Ex. No.: 18 Date : SYSTEM CALL FOR WRITE-ONLY FILE

AIM: To write a C Program to perform system call to open a write-only file.

ALGORITHM: Step 1: Start Step 2: Initialize the integer variables i and fd with a character variable buf[100]. Step 3: Get the data form the user and store it as buf. Step 4: Open the file aa as write-only and store it in fd. Step 5: Write the data given by the user to the file and fd Step 6: Stop Note: After the program is run, open the file aa to find the garbage values stored in it. A file aa with data must be created before the program is compiled and is run, to avoid segmentation error. Header Files: fcntl.h File control Options unistd.h Standard symbolic constants and types. 60

C PROGRAM: #include<stdio.h> #include<fcntl.h> #include<unistd.h> main() { int i,fd; char buf[100]; printf("Give data: "); scanf("%s",buf); fd=open("aa",O_WRONLY); write(fd,buf,sizeof(buf)); }

61

SAMPLE OUTPUT:

[root@localhost C_Program]# cc syscall-w.c [root@localhost C_Program]# ./a.out Give data: I study in VEC [root@localhost C_Program]# cat aa IBe B B Bt <@PB@,X@-8Z@0 @ @c@TBB

RESULT: Thus the C Program to perform system call to opena write-only file was executed and the output was verified.

62

Ex. No.: 19 Date : IMPLEMENTATION OF COPY COMMAND

AIM: To write a C Program to implement copy command in UNIX.

ALGORITHM: Step 1: Start Step 2: Open the file to be copied, and store it in fp1 Step 3: Open a new file, where the contents are to be copied and store it in fp2. Step 4: Copy all the characters one by one until the End of File. Step 5: Close all the files. Step 6: Stop.

63

C PROGRAM: #include<stdio.h> main(int a, char *av[2]) { char ch; FILE *fp1,*fp2; fp1=fopen(av[1],"r"); fp2=fopen(av[2],"w"); while((ch=getc(fp1))!=EOF) { putc(ch,fp2); } fclose(fp1); fclose(fp2); }

64

SMAPLE OUTPUT: [root@localhost C_Program]# cc copy.c [root@localhost C_Program]# cat>a Vel Tech Multi Tech Engineering college [root@localhost C_Program]# ./a.out a b [root@localhost C_Program]# cat b Vel Tech Multi Tech Engineering college

RESULT: Thus the C Program to implement copy command in UNIX was executed and the output was verified.

65

Ex. No.: 20 Date : IMPLEMENTATION OF WC COMMAND

AIM: To write a C Program to implement wc command in UNIX.

ALGORITHM: Step 1: Start Step 2: Initialize the variables c=0; w=0; l=0. Step 3: Using If Else condition count the no. of characters, letters and words. Step 4: Using string compare function, print the result with respect to the command given. Step 5: Stop.

66

C PROGRAM: #include<stdio.h> main(int a,char *av[3]) { FILE *f; char ch; int w=0,l=0,c=0; if(a==2) f=fopen(av[1],"r"); else f=fopen(av[2],"r"); while(!feof(f)) { ch=getc(f); c=c++; if(ch==' '||ch=='\n') w=w+1; if(ch=='\n') l=l+1; } fclose(f); if(strcmp(av[1],"-c")==0) printf("\nThe no. of characters are %d\n",c-1); else if(strcmp(av[1],"-w")==0) printf("\nThe no. of words are %d\n",w); else if(strcmp(av[1],"-l")==0) printf("\nThe number of lines are %d\n",l); else if(av[2]=='\0') printf("\n%d\t%d\t%d\n",c-1,w,l); }

67

SAMPLE OUTPUT:

[root@localhost C_Program]# cc wc.c [root@localhost C_Program]# ./a.out msb

77

10

[root@localhost C_Program]# ./a.out -l msb

The number of lines are 7 [root@localhost C_Program]# ./a.out -w msb

The no. of words are 10 [root@localhost C_Program]# ./a.out -c msb

The no. of characters are 77

RESULT: Thus the C Program to implement wc commad in UNIX was executed and the output was verified.

68

Ex. No.: 21 Date : FORK SYSTEM CALL

AIM: To write a C Program for FORK SYSTEM CALL

ALGORITHM: Step 1: Start Step 2: Open the inbuilt function fork() inside the header file, unistd.h and store it in f Step 3: When f is equal to 0, print the Child process ID, Parent ID and the process ID, using inbuilt ID functions. Step 4: Stop.

69

C PROGRAM: #include<stdio.h> #include<unistd.h> main() { int f; f=fork(); if(f==0) { printf("\nChild Process ID: %d\n",getpid()); printf("\nParent ID: %d\n",getppid()); } else { printf("\nProcess ID: %d\n",getpid()); printf("\nParent ID: %d\n",getppid()); } }

70

SAMPLE OUTPUT: [root@localhost C_Program]# cc fork.c [root@localhost C_Program]# ./a.out

Child Process ID: 3844

Parent ID: 3843

Process ID: 3843

Parent ID: 3677

RESULT: Thus the C Program for Fork System Call in UNIX was executed and the output was verified.

71

Ex. No.: 22 Date : EXECL SYSTEM CALL

AIM: To write a C Program for EXECL System Call in UNIX

ALGORITHM: Step 1: Start Step 2: Give the command for Execl, to print the Date and Time. Step 3: Stop

72

C PROGRAM: #include<stdio.h> #include<unistd.h> main() { execl("/bin/date","date",0); }

73

SAMPLE OUTPUT: [root@localhost C_Program]# cc execl.c [root@localhost C_Program]# ./a.out Tue March 18:01:37 IST 2010 [root@localhost C_Program]#

RESULT: Thus the C Program for EXECL System Call in UNIX was executed and the output was verified.

74

Das könnte Ihnen auch gefallen