Linux Programming Lab Manual

introduction to unix and linux lab manual student edition linux lab manual for diploma students
LavinaKanna Profile Pic
LavinaKanna,Canada,Researcher
Published Date:13-07-2017
Your Website URL(Optional)
Comment
Department Of Computer Science & Engineering Linux Programming B. Tech IV Yr.I Sem Lab Manual ISO 9001:2000 Affiliated to Jawaharlal Nehru Technological University Dhulapally, Quthbullapur (M), Secunderabad, Telangana -500014 Linux Programming Lab Manual Computer Science & Engineering S. No. Name Of The Program Page No. Week 1 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. 2. Write a shell script that deletes all lines containing a specified 1 word in one or more files supplied as arguments to it. 5-7 1 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. Week 2 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 8-9 argument file on other argument files 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. Week 3 8. Write an awk script to count the number of lines in a file that do not 10-11 3. contain vowels. St. Martin’s Engineering College Page 1 Linux Programming Lab Manual Computer Science & Engineering 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 Week 4 11. Implement in C the following UNIX commands using System 4. calls A. cat B.ls C.mv 12. Write a program that takes one or more file/directory names as 11-13 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 Execute permissions. Week 5 13. Write a C program to emulate the UNIX ls –l command. 5. 14. Write a C program to list for every file in a directory, its inode 14-22 number and file name. 15. Write a C program that demonstrates redirection of standard Output to a file. Ex: ls f1. Week 6 16. Write a C program to create a child process and allow the parent to 6. display “parent” and the child to display “child” on the screen. 23-24 17. Write a C program to create a Zombie process. 18. Write a C program that illustrates how an orphan is created. St. Martin’s Engineering College Page 2 Linux Programming Lab Manual Computer Science & Engineering Week 7 19. Write a C program that illustrates how to execute two commands 7. concurrently with a command pipe. Ex: - ls –l sort 20. Write C programs that illustrate communication between two unrelated processes using named pipe 23-31 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. Week 8 23. Write a C program to allow cooperating processes to lock a resource 8. for exclusive use, using a) Semaphores b) flock or lockf system calls. 34-35 24. Write a C program that illustrates suspending and resuming processes using signals Week 9 9. 25. Write a C program that implements a producer-consumer system with two processes. 36-40 (Using Semaphores). 26. Write client and server programs (using c) for interaction between server and client processes using Unix Domain sockets. St. Martin’s Engineering College Page 3 Linux Programming Lab Manual Computer Science & Engineering 10. Week 10 27. Write client and server programs (using c) for interaction between server and client processes using Internet Domain sockets. 41-47 28.Write a C program that illustrates two processes communicating using shared memory St. Martin’s Engineering College Page 4 Linux Programming Lab Manual Computer Science & Engineering 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 ‘NR2 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 word in one or more files supplied as arguments to it. Aim: To write 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 1b.sh test1 the contents before deleting test1 hello hello bangalore mysore city enter the word to be deleted city St. Martin’s Engineering College Page 5 Linux Programming Lab Manual Computer Science & Engineering after deleting hello hello Bangalore sh 1b.sh no argument passed 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. 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 fi St. Martin’s Engineering College Page 6 Linux Programming Lab Manual Computer Science & Engineering 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 Aim: To write a shell script 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 St. Martin’s Engineering College Page 7 Linux Programming Lab Manual Computer Science & Engineering 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 files. 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 hello ATRI hello sh 1.sh test test1 Word = hello, Count = 3 Word = ATRI, Count = 2 St. Martin’s Engineering College Page 8 Linux Programming Lab Manual Computer Science & Engineering 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 St. Martin’s Engineering College Page 9 Linux Programming Lab Manual Computer Science & Engineering 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, words and lines in a file. Aim : To write an awk script to find the number of characters, words and lines in a file. Script: BEGINprint "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 buffer100; long int n1; if(((fd1 = open(argv1, O_RDONLY)) == -1) ((fd2 = open(argv2, O_CREATO_WRONLYO_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); // Case of an error exit from the loop if(n1 == -1) perror("Reading problem "); exit(2); St. Martin’s Engineering College Page 10 Linux Programming Lab Manual Computer Science & Engineering close(fd2); exit(0); Week 4 11. Implement in C the following UNIX commands using System calls A. cat B. ls C. mv AIM: Implement in C the cat Unix command using system calls includefcntl.h includesys/stat.h define BUFSIZE 1 int main(int argc, char argv) int fd1; int n; char buf; fd1=open(argv1,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 following 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 St. Martin’s Engineering College Page 11 Linux Programming Lab Manual Computer Science & Engineering define FALSE 0 define TRUE 1 extern int alphasort(); char pathnameMAXPATHLEN; main() int count,i; struct dirent files; int file_select(); if (getwd(pathname) == 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("Number of files = %dn",count); for (i=1;icount+1;++i) printf("%s \n",filesi-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 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 contents into new file using write system call using write( ) system call St. Martin’s Engineering College Page 12 Linux Programming Lab Manual Computer Science & Engineering 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: includefcntl.h includestdio.h includeunistd.h includesys/stat.h int main(int argc, char argv) int fd1,fd2; int n,count=0; fd1=open(argv1,O_RDONLY); fd2=creat(argv2,S_IWUSR); rename(fd1,fd2); unlink(argv1); printf(“ file is copied “); return (0); 12. Write a program 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 Execute permissions. includestdio.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”); fclose(stream); exit(1); if(fclose(stream))==EOF) fprintf(stderr,”Error closing stream.(printed to standard error)\n); exit(1); return(); St. Martin’s Engineering College Page 13 Linux Programming Lab Manual Computer Science & Engineering 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: Declare 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: Display 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 / includefcntl.h includestdio.h includeunistd.h includesys/stat.hmain() char dirname10; 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 ... f2 St. Martin’s Engineering College Page 14 Linux Programming Lab Manual Computer Science & Engineering 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 nameNAME_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; int stat(char , struct stat ); stat(name, &stbuf); St. Martin’s Engineering College Page 15 Linux Programming Lab Manual Computer Science & Engineering 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 group 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_ctime; / time originally created / ; Most of these values are explained by the comment fields. The types like dev_t and ino_t are defined insys/types.h, which must be included too. The st_mode entry contains a set of flags describing the file. The flag definitions are also included insys/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 / 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 be printed directly. If the name is a directory, St. Martin’s Engineering College Page 16 Linux Programming Lab Manual Computer Science & Engineering 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 argument 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(++argv); 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 directory. Parenthesization matters, because the precedence of & is lower than that of ==. int stat(char , struct stat ); St. Martin’s Engineering College Page 17 Linux Programming Lab Manual Computer Science & Engineering void dirwalk(char , void (fcn)(char )); / fsize: print the name of file "name" / void fsize(char name) struct 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", stbuf.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 calls dirwalk on each directory, the two functions call each other recursively. define MAX_PATH 1024 / dirwalk: apply fcn to all files in dir / void dirwalk(char dir, void (fcn)(char )) char nameMAX_PATH; Dirent dp; DIR dfd; St. Martin’s Engineering College Page 18 Linux Programming Lab Manual Computer Science & Engineering 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 strcmp(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); 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 headersys/dir.h, which looks like this: ifndef DIRSIZ define DIRSIZ 14 endif struct direct / directory entry / St. Martin’s Engineering College Page 19

Advise: Why You Wasting Money in Costly SEO Tools, Use World's Best Free SEO Tool Ubersuggest.