Lexyaac Program Vtu Lab 06is58

  • Uploaded by: jain
  • 0
  • 0
  • December 2019
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Lexyaac Program Vtu Lab 06is58 as PDF for free.

More details

  • Words: 1,816
  • Pages: 8
11

System Programming Laboratory (ISL68)

1a. Non – recursive shell script that accepts any number of arguments and prints them in the Reverse order, (For example, if the script in named rargs, then execute in rargs A B C should produce C B A on the standard output). echo “Non-Recursive Shell script to prints the Arguments in the Reverse order.” echo “The Total Number of Arguments are: $# ” echo “The Specified $# Arguments are: $# ” temp=$# echo “The Arguments in the Reverse order are: ” while [ $temp –ge 1 ] do eval echo \$$temp temp = `expr $temp – 1` done

1b. C program that creates a child process to read commands from the standard input and execute them ( a minimal implementation of a shell – like program). You can assume that no arguments will be passed to the commands to be executed. #include<stdio.h> #include<sys/types.h> main() { int pid; char cmd[15]; pid=fork(); if(pid<0) { printf(“Fork Error!…\n”); exit(0); } else if(pid>=0) { printf(“\nEnter the Command: ”); scanf(“%s”, cmd); system(cmd); exit(0); } }

Function Used: Name: Fork() : Create a child process Synopsis: #include<stdio.h> #include<sys/types.h> pid_t fork(void); Department of Information Science & Engineering

BCE, Shravanabelagola

12

System Programming Laboratory (ISL68)

Description: Fork creates a child process that differs from the parent process only in its PID and PPID, and in the fact that resource utilizations are set to 0. file locks and pending signals are not inherited. Under Linux, fork is implemented using copy_on_write pages, so the only penalty incurred by fork is the time and memory required to duplicate the parent’s page tables and to create a unique task structure for the child Return Values: On success, the PID of the child process is returned in the parent’s thread of execution, and a 0 is returned in the child’s thread of execution. On failure, a –1 will be returned in the parent’s context, no child process will be created, and errno will be set appropriately. Errors: EAGAIN fork cannot allocate sufficient memory to copy the parent’s page tables and allocate a task structure for the child. ENOMEM fork failed to allocate the necessary kernel structures because memory is tight. 2a. Shell script that accepts two file names as arguments, checks if the permissions for these files are identical and if the permissions are identical, outputs the common permissions, otherwise outputs each file name followed by its permissions. temp1=`ls –l $1 | cut –c 1-10` temp2=`ls –l $2 | cut –c 1-10` if [ $temp1 = $temp2 ] then echo “The files have common Permissions” echo $temp1 else echo “The permission for $1 is: ” echo $temp1 echo “The permission for $2 is: ” echo $temp2 fi

2b. C Program to create a file with 16 bytes of arbitrary data from the beginning and another 16 bytes of arbitrary data from an offset of 48. Display the file contents to demonstrate how the hole in file is handled. #include<stdio.h> #include<sys/stat.h> #include char buf1[] = “abcdefghijklmnop”; char buf2[] = “ABCDEFGHIJKLMNOP”; int main( ) { int fd; fd=create(“file.dat”,0); write(fd,buf1,16); Department of Information Science & Engineering

BCE, Shravanabelagola

13

System Programming Laboratory (ISL68)

lseek(fd, 48,SEEK_SET); write(fd, buf2,16); system(“cat file.dat”); exit(0); }

3a. Shell function that takes a valid directory names as an argument and length of any file in that hierarchy and writes this maximum value to the standard output. clear ls –lR $1 | grep –v ^d | sort –nr +4 –5 | echo `sed –n ‘lp’` > file1 string = `cut –d “ ” –f9 file1` length=`cut –d “ ” –f5 file1` echo “The Maximum length file is: $string: ” echo “ ” echo “ The Length of the file $string is: $length”

3b. C program that accepts valid file names as command line arguments and for each of the arguments, prints the type of the file (Regular file, Directory file, character special file, Block special file, Symbolic link etc). #include<stdio.h> #include<stdlib.h> main(int c, char *b[]) { char str[50]; int i; for(i=1;i
4a. Shell script that accepts path names and creates all the components in that path names as directories. For example, if the script name is mpe, then the command mpe a/b/c/d should create directories a, a/b, a/b/c and a/b/c/d. echo $1 > temp tr ‘/’ ‘ ’ < temp > temp1 set `cat temp1` while [ $# -gt 0 ] do mkdir $1 cd $1 shift done

Department of Information Science & Engineering

BCE, Shravanabelagola

14

System Programming Laboratory (ISL68)

4b. C program that accepts one command – line argument, executes the arguments as a shell command, determines the time taken by it and prints the time values, use the “times”, function and the “tms” structure. The code need not include error checking. #include<stdio.h> #include<stdlib.h> #include<sys/types.h> #include #include<sys/times.h> main(int argc, char *argv[]) { int i; struct tms, tms1, tms2; clock_t start, finish; long clktck=sysconf(_SC_CLK_TCK); start=times(&tms1); for(i=1;i<argc;i++) system(argv[i]); finish=times(&tms2);

printf(“Real printf(“User (double) clktck); }

Time taken: %7.2f”, (finish – start) / (double) clktck); Time taken: %7.2f”, (tms2.tms_utime – tms1.tms_utime) /

5a. Shell script that accepts valid log-in names as arguments and prints their corresponding home directories. If no arguments are specified, print a suitable error message. y=$# i=1 clear if [ $y –eq 0 ] then echo “Oh!… You haven’t specified any arguments at all!.. ” else while [ $i –le $y ] do loginname=$1 grep $loginname /etc/passwd > s echo “Home directory of $loginname is” head –1 s > $1 cut –d \: -f6 $1 shift i=`expr $i + 1` done fi

5b. C Program that accepts a valid directory names as a command line argument and lists all the files in the given directory as well as all the subsequent subdirectories. (The solution can be recursive or non-recursive). Department of Information Science & Engineering

BCE, Shravanabelagola

15

System Programming Laboratory (ISL68)

#include<stdio.h> #include<stdlib.h> main(int c, char *v[]) { char str[30]; printf(“Arguments which is considered as directory name is %s\n”, v[1]); sprintf(str, “ls –R %s”, v[1]); system(str); }

6a. Shell script to implement terminal locking. It should prompt the user for a password. After accepting the password entered by the user, it must prompt again for password conformation ( to retype the password). If a match occurs, it must lock the terminal and prompt for the password. If the proper password is entered, the terminal must be unlocked. Note the script must be written to disregard BREAK, Control-D, etc. No time limit need be implemented for the lock duration.

stty echo read echo read if [

–echo “Enter the Password” pass1 “Enter the Password for conformation” pass2 $pass1 != $pass2 ] then echo “Invalid Password !… ” exit

fi while true do trap ‘ ’ 1 2 15 echo “Enter the Password to Unlock” read pass3 if [ $pass3 = $pass1 ] then sty echo exit fi done

7a. Shell script that accept file names specified as arguments and creates a shell script that contains this file as well as the code to recreate these files. Thus if the script generated by your script is executed, it would recreate the original files (This is same as the “bundle” script described by Brain W.Kernighan and Rob Pike in “the UNIX Programming Environment”, Prentice – Hall India). echo “# to unbundled, sh this file” for i do echo “echo $i 1 > $2” echo “cat > $i <<`end of $i`” cat $i echo “end of $i” Department of Information Science & Engineering

BCE, Shravanabelagola

16

System Programming Laboratory (ISL68)

done

7b. Awk script to delete duplicated lines from the text file. The order of the original must remain unchanged. BEGIN { i=0 } { a[i]=$0 i++ N=i } END { For(i=0;i> )“text”; } }

8a. Shell script to find and display all the links of a file specified as the first argument to the script. The second argument, which is optional, can be used to specify the directory in which the search is to begin. If this second argument is not present, the search is to begin in current working directory. In either case, the starting directory as well as its subdirectories at all levels must be searched. The script need not include any error checking. clear fn=$1 if [ $# = 2 ] then cd $2 fi set `ls –iR $fn` lnk = $1 ls –iR1. > tmplst echo “The inode number of linked file: ” echo “$lnk” echo “The linked files are : ” grep $lnk tmplst | cut –c 8-20 Department of Information Science & Engineering

BCE, Shravanabelagola

17

System Programming Laboratory (ISL68)

8b. PERL script that echos its command line arguments, one per line after translating all lower case letters to upper case. $k= “ ”; foreach $a(@ARGV) { $k=uc($a); printf(“$k\n”); }

9a. Shell script to display the calendar for current month with current date replaced by * or ** depending on whether date has one digit or two digits. clear set `date` x=$3 echo “To day’s date is : $x/$2/$6” echo “--------------------------------” case $x in [0-9]) cal | sed “s/$x/*/”;; [1-3][0-9]) cal | sed “s/$x/**/”;; esac

9b. PERL program to convert an unsigned binary number (supplied as argument) to decimal (for example if the argument is 10110 the output should be 22). If an argument is present, if can be a valid binary number and if no argument is present, the program should display an error message. $bin=$ARGV[0]; $dec=0; $pow=1 until($bin = = 0) { $bit = $bin % 10; $dec +=$bit * $pow; $pow *=2; $bin = int($bin/10); } printf(“\n The decimal form is :%d”, $dec);

10a. AWK script that folds long line into 40 columns. Thus any line that exceeds 40 characters must be broken after 40th and is to be continued with the residue. The inputs to be supplied through a text file created by the user.

Department of Information Science & Engineering

BCE, Shravanabelagola

18

System Programming Laboratory (ISL68)

{ if(length($0) <=40) printf(“%s\n”, $0) else { x=$0 while(length(x) > 40) { printf(“%s\\n”, substr($0,1,40) x=substr($0,41,length(x) – 40) } printf(“%s\n”,x) } }

10b. C Program to do the following: Using fork( ) create a child process. The child process prints its own Process-Id and Id of its parent and then exits. The parent process waits for its child to finish (by executing the wait( )) and prints its own Process-Id and Id of its Child Process and then exits. #include<stdio.h> #include<sys/types.h> int main() { int status, ppid, mpid, pid; pid=fork(); if(pid<0) { printf(“Error in forking a child!… ”); } if(pid = = 0) { ppid=getppid(); printf(“\n Iam Child Executing … and My Parent ID is: %d”,ppid); mpid=getpid(); printf(“\n My own ID: %d”, mpid); kill(); exit(0); } pid=wait(pid,&status, 0) mpid=getpid(); printf(“\n Iam parent with ID:%d\n My child with ID: %d\n”, mpid, pid); }

Department of Information Science & Engineering

BCE, Shravanabelagola

Related Documents


More Documents from "Pranav Marathe"