14. ASSIGNMENT TOPICS WITH...

122
14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following commands we use in linux are: Ls: Displays list files in current directory Eg: ls al Chmod: Change the file access permissions to give read/write/execute permissions to a file or files Eg: chmod +x myscript.sh Cat: Shows the contents of a file Eg: cat hello.txt Chown: Change the file owner / group owner. Eg: chown bahmni:bahmni -R /var/www/ Grep: Finds matching data in text inputs using regular expressions. Eg: cat hello.txt | grep "bahmni" Pwd: Shows the current directory path (pwd = present working directory). Cd-Change into the specified directory. Eg: cd /home Tail: Display the last few contents of a text file. Useful for seeing log files. Eg: tail -f /var/log/openmrs/openmrs.log Cp: Copies a file from one location to another. Eg: cp abc.txt /home Mv : Moves a file to another location. Also used for renaming a file. Eg: mb abc1.txt abc2.txt Rm:Delete a file or a directory Eg: rm /tmp/hello Mkdir: Create a new directory. Eg: mkdir music

Transcript of 14. ASSIGNMENT TOPICS WITH...

Page 1: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I

1. LINUX UTILITIES

The following commands we use in linux are: Ls: Displays list files in current directory

Eg: ls al Chmod: Change the file access permissions to give read/write/execute permissions

to a file or files Eg: chmod +x myscript.sh Cat: Shows the contents of a file

Eg: cat hello.txt Chown: Change the file owner / group owner.

Eg: chown bahmni:bahmni -R /var/www/ Grep: Finds matching data in text inputs using regular expressions.

Eg: cat hello.txt | grep "bahmni" Pwd: Shows the current directory path (pwd = present working directory).

Cd-Change into the specified directory.

Eg: cd /home Tail: Display the last few contents of a text file. Useful for seeing log files.

Eg: tail -f /var/log/openmrs/openmrs.log Cp: Copies a file from one location to another.

Eg: cp abc.txt /home Mv : Moves a file to another location. Also used for renaming a file.

Eg: mb abc1.txt abc2.txt Rm:Delete a file or a directory

Eg: rm /tmp/hello

Mkdir: Create a new directory. Eg: mkdir music

Page 2: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Disk commands:

Df: Print disk space usage (short for 'disk free') Du: Print disk usage report for current directory. Tar: For creating or extracting tar files

-xvf <filename.tar> extract tar file -cvf <filename.tar> compress files and folders -tvf archive.tar List all files in archive.tar verbosely. Kill: Terminate a process / Raise a Signal.

Eg: kill -9 <pid>.

2. SED

sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(which particularly distinguishes it from other types of editors.

Normally sed is invoked like this: sed SCRIPT INPUTFILE... For example, to replace all occurrenc sed 's/hello/world/' input.txt > output.txt

If you do not specify INPUTFILE, or if INPUTFILE is -, sed filters the contents of the standard input. The following commands are equivalent:

sed 's/hello/world/' input.txt > output.txt sed 's/hello/world/' < input.txt > output.txt cat input.txt | sed 's/hello/world/' - > output.txt

sed writes output to standard output. Use -i to edit files in-place instead of printing to standard output. See also the W and s///w commands for writing output to other files. The following command modifies file.txt and does not produce any output:

sed -i 's/hello/world/' file.txt

By default sed prints all processed input (except input that has been modified/deleted by commands such as d). Use -n to suppress output, and the p command to print specific lines. The following command prints only line 45 of the input file:

Page 3: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

sed -n '45p' file.txt

sed treats multiple input files as one long stream. The following example prints the first line of the first file (one.txt) and the last line of the last file (three.txt). Use -s to reverse this behavior.

sed -n '1p ; $p' one.txt two.txt three.txt

Without -e or -f options, sed uses the first non-option parameter as the script, and the following non-option parameters as input files. If -e or -f options are used to specify a script, all non-option parameters are taken as input files. Options -e and -f can be combined, and can appear multiple times (in which case the final effective script will be concatenation of all the individual scripts).

The following examples are equivalent:

sed 's/hello/world/' input.txt > output.txt sed -e 's/hello/world/' input.txt > output.txt sed --expression='s/hello/world/' input.txt > output.txt echo 's/hello/world/' > myscript.sed sed -f myscript.sed input.txt > output.txt sed --file=myscript.sed input.txt > output.txt

3. AWK

Awk is a scripting language used for manipulating data and generating reports.The awk command programming language requires no compiling, and allows the user to use variables, numeric functions, string functions, and logical operators. Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that define text patterns that are to be searched for in each line of a document and the action that is to be taken when a match is found within a line. Awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then performs the associated actions.

Awk is abbreviated from the names of the developers Aho, Weinberger, and Kernighan.

Page 4: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

What can we do with awk ?

1. AWK Operations:

(a) Scans a file line by line (b) Splits each input line into fields (c) Compares input line/fields to pattern (d) Performs action(s) on matched lines

2. Useful For:

(a) Transform data files (b) Produce formatted reports

3. Programming Constructs:

(a) Format output lines (b) Arithmetic and string operations (c) Conditionals and loops

Syntax:

awk options 'selection _criteria {action }' input-file > output-file

Options:

-f program-file : Reads the AWK program source from the file program-file, instead of from the first command line argument. -F fs : Use fs for the input field separator

Sample Commands

Example: Consider the following text file as the input file for all cases below.

$cat > employee.txt ajay manager account 45000 sunil clerk account 25000 varun manager sales 50000 amit manager account 47000 tarun peon sales 15000 deepak clerk sales 23000 sunil peon sales 13000

Page 5: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

satvik director purchase 80000

1. Default behavior of Awk :By default Awk prints every line of data from the specified file.

$ awk '{print}' employee.txt

Output:

ajay manager account 45000 sunil clerk account 25000 varun manager sales 50000 amit manager account 47000 tarun peon sales 15000 deepak clerk sales 23000 sunil peon sales 13000 satvik director purchase 80000

In the above example, no pattern is given. So the actions are applicable to all the lines. Action print without any argument prints the whole line by default, so it prints all the lines of the file without failure.

2. Print the lines which matches with the given pattern.

$ awk '/manager/ {print}' employee.txt

Output:

ajay manager account 45000 varun manager sales 50000 amit manager account 47000

In the above example, the awk command prints all the line which matches with the

3. Spliting a Line Into Fields :For each record i.e line, the awk command splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4 respectively. Also, $0 represents the whole line.

$ awk '{print $1,$4}' employee.txt

Output:

Page 6: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

ajay 45000 sunil 25000 varun 50000 amit 47000 tarun 15000 deepak 23000 sunil 13000 satvik 80000

In the above example, $1 and $4 represents Name and Salary fields respectively.

Built In Variables In Awk

-in variables include the field variables $1, $2, $3, and so on ($0 is the entire line) that break a line of text into individual words or pieces called fields.

NR: NR command keeps a current count of the number of input records. Remember that records are usually lines. Awk command performs the pattern/action statements once for each record in a file.

NF: NF command keeps a count of the number of fields within the current input record.

FS: FS command contains the field separator character which is used to divide fields on the input line. The defabe reassigned to another character (typically in BEGIN) to change the field separator.

RS: RS command stores the current record separator character. Since, by default, an input line is the input record, the default record separator character is a newline.

OFS: OFS command stores the output field separator, which separates the fields when Awk prints them. The default is a blank space. Whenever print has several parameters separated with commas, it will print the value of OFS in between each parameter.

ORS: ORS command stores the output record separator, which separates the output lines when Awk prints them. The default is a newline character. Print automatically outputs the contents of ORS at the end of whatever it is given to print

4. SHELL RESPONSIBILITIES

The shell provides you with an interface to the UNIX system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output.

Page 7: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

A shell is an environment in which we can run our commands, programs, and shell scripts. There are different flavors of shells, just as there are different flavors of operating systems. Each flavor of shell has its own set of recognized commands and functions.

Shell Prompt:

The prompt, $, which is called command prompt, is issued by the shell. While the prompt is displayed, you can type a command.

The shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words.

Following is a simple example of date command which displays current date and time:

$date ThuJun 25 08:30:19 MST 2009

You can customize your command prompt using environment variable PS1 explained in Environment tutorial.

Shell Types:

In UNIX there are two major types of shells:

1. The Bourne shell. If you are using a Bourne-type shell, the default prompt is the $ character. 2. The C shell. If you are using a C-type shell, the default prompt is the % character.

There are again various subcategories for Bourne Shell which are listed as follows:

Bourne shell ( sh) Korn shell ( ksh) Bourne Again shell ( bash) POSIX shell ( sh)

The different C-type shells follow:

C shell ( csh) TENEX/TOPS C shell ( tcsh)

The original UNIX shell was written in the mid-1970s by Stephen R. Bourne while he was at AT&T Bell Labs in New Jersey.

Page 8: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

The Bourne shell was the first shell to appear on UNIX systems, thus it is referred to as "the shell".

The Bourne shell is usually installed as /bin/sh on most versions of UNIX. For this reason, it is the shell of choice for writing scripts to use on several different versions of UNIX.

5). SHELL SCRIPTING

Shell Scripts:

The basic concept of a shell script is a list of commands, which are listed in the order of execution. A good shell script will have comments, preceded by a pound sign, #, describing the steps.

There are conditional tests, such as value A is greater than value B, loops allowing us to go through massive amounts of data, files to read and store data, and variables to read and store data, and the script may include functions.

Shell scripts and functions are both interpreted. This means they are not compiled.

We are going to write a many scripts in the next several tutorials. This would be a simple text file in which we would put our all the commands and several other required constructs that tell the shell environment what to do and when to do it.

Example Script:

Assume we create a test.sh script. Note all the scripts would have .sh extension. Before you add anything else to your script, you need to alert the system that a shell script is being started. This is done using the shebang construct. For example:

#!/bin/sh

This tells the system that the commands that follow are to be executed by the Bourne shell. It's called a shebang because the # symbol is called a hash, and the !symbol is called a bang.

To create a script containing these commands, you put the shebang line first and then add the commands:

#!/bin/bash pwd ls

Page 9: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Shell Comments:

You can put your comments in your script as follows:

#!/bin/bash # Author : Zara Ali # Copyright (c) Tutorialspoint.com # Script follows here: pwd ls

Now you save the above content and make this script executable as follows:

$chmod +x test.sh

Now you have your shell script ready to be executed as follows:

$./test.sh

This would produce following result:

/home/amrood index.htm unix-basic_utilities.htm unix-directories.htm test.sh unix-communication.htm unix-environment.htm

Note: To execute your any program available in current directory you would execute using ./program_name

6. Extended Shell Scripts:

Shell scripts have several required constructs that tell the shell environment what to do and when to do it. Of course, most scripts are more complex than above one.

The shell is, after all, a real programming language, complete with variables, control structures, and so forth. No matter how complicated a script gets, however, it is still just a list of commands executed sequentially.

Following script use the read command which takes the input from the keyboard and assigns it as the value of the variable PERSON and finally prints it on STDOUT.

#!/bin/sh # Author : Zara Ali

Page 10: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

# Copyright (c) Tutorialspoint.com # Script follows here: echo"What is your name?" read PERSON echo"Hello, $PERSON"

Here is sample run of the script:

$./test.sh Whatis your name? ZaraAli Hello,ZaraAli

UNIT-II

1. FILE AND ITS TYPES

A file system is a logical collection of files on a partition or disk. A partition is a container for information and can span an entire hard drive if desired. Your hard drive can have various partitions which usually contain only one file system, such as one file system housing the /file system or another containing the /home file system. One file system per partition allows for the logical maintenance and management of differing file systems. Everything in Unix is considered to be a file, including physical devices such as DVD-ROMs, USB devices, and floppy drives. Directory Structure Unix uses a hierarchical file system structure, much like an upside-down tree, with root (/) at the base of the file system and all other directories spreading from there. A Unix filesystem is a collection of files and directories that has the following propert It has a root directory (/) that contains other files and directories. Each file or directory is uniquely identified by its name, the directory in which it resides, and a unique identifier, typically called an inode. By convention, the root directory has an inode number of 2 and the lost+found directory has an inode number of 3. Inode numbers 0 and 1 are not used. File inode numbers can be seen by specifying the -i option to ls command. It is self-contained. There are no dependencies between one filesystem and another. The directories have specific purposes and generally hold the same types of information for

S.No. Directory & Description /

Page 11: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

This is the root directory which should contain only the directories needed at the top level of the file structure /bin This is where the executable files are located. These files are available to all users /dev These are device drivers /etc Supervisor directory commands, configuration files, disk configuration files, valid user lists, groups, ethernet, hosts, where to send critical messages /lib Contains shared library files and sometimes other kernel-related files /boot Contains files for booting the system /home Contains the home directory for users and other accounts /mnt Used to mount other temporary file systems, such as cdrom and floppy for the CD-ROM drive and floppy diskette drive, respectively /proc Contains all processes marked as a file by process number or other information that is dynamic to the system /tmp Holds temporary files used between system boots /usr Used for miscellaneous purposes, and can be used by many users. Includes administrative commands, shared files, library files, and others /var Typically contains variable-length files such as log and print files and any other type of file that may contain a variable amount of data /sbin Contains binary (executable) files, usually for system administration. For example, fdisk and ifconfig utlities /kernel Contains kernel files

2. KERNEL SUPPORT FOR FILES:

UNIX supports the sharing of open files between different processes. Kernel has three data structures are used and the relationship among them determines the effect one process has on another with regard to file sharing.

Page 12: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

1. Every process has an entry in the process table. Within each process table entry is a table of open file descriptors, which is taken as a vector, with one entry per descriptor. Associated with each file descriptor are a. The file descriptor flags. b. A pointer to a file table entry. 2. The kernel maintains a file table for all open files. Each file table entry contains a. The file status flags for the file(read, write, append, sync, nonblocking, etc.), b. The current file offset, c. A pointer to the v-node table entry for the file. 3. Each open file (or device) has a v-node structure. The v-node contains information about the type of file and pointers to functions that operate on the file. For most files the v- node also contains the i-node for the file. This information is read from disk when the file is opened, so that all the pertinent information about the file is readily available. The arrangement of these three tables for a single process that has two different files open one file is open on standard input (file descriptor 0) and the other is open standard output (file descriptor 1). Here, the first process has the file open descriptor 3 and the second process has file open Descriptor. 4. Each process that opens the file gets its own file table entry, but only a single v- node table entry. One reason each process gets its own file table entry is so that each process has its own current offset for the file. HEADER FILES: #include&lt;fcntl.h&gt; #include&lt;types.h&gt; DATA STRUCTURES: mode_t: struct_t: off_t:

Open: This system call is used to open the file Read: This system call used to read the content which is written in the file. Write: This is used to write the content in the file. Iseek:This system call is used to reposition read/write file offset. Close:This system call is used to close a file descriptor. Stat,fstat:This system call is used to get file status Chmod:This system call is used to change permissions of a file Chown: The chown command stands for "change owner" and is used to change the owner of a file. Link: This system call is used to creates a hard link from an existing directory entry to a new directory entry.

Page 13: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

3. DIRECTORIES:

A directory is a file the solo job of which is to store the file names and the related information. All the files, whether ordinary, special, or directory, are contained in directories.

linux uses a hierarchical structure for organizing files and directories. This structure is often referred to as a directory tree. The tree has a single root node, the slash character (/), and all other directories are contained below it.

/cd :$cd ~username /pwd: $pwd /user0/home/amrood /ls: $ls /usr/local X11 bin gimp jikes sbin ace doc include lib share atalk etc info man ami /mkdir: $mkdir dirname /cd: $cd dirname $cd /usr/local/bin /mv: $mv olddir newdir

4. KERNEL SUPPORT FOR DIRECTORIES.

Creating Directories: We will now understand how to create directories. Directories are created by the following

$mkdir dirname Here, directory is the absolute or relative pathname of the directory you want to create. For

$mkdir mydir Creates the directory mydir $mkdir /tmp/test-dir This command creates the directory test-dir in the /tmpdirectory. The mkdir command produces no output if it successfully creates the requested directory. If you give more than one directory on the command line, mkdircreates each of the

$mkdir docs pub Creates the directories docs and pub under the current directory.

Page 14: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Creating Parent Directories We will now understand how to create parent directories. Sometimes when you want to create a directory, its parent directory or directories might not exist. In this case, $mkdir /tmp/amrood/test mkdir: Failed to make directory "/tmp/amrood/test"; No such file or directory In such cases, you can specify the -p option to the mkdircommand. It creates all the

$mkdir -p /tmp/amrood/test The above command creates all the required parent directories. Removing Directories Directories can be deleted using the rmdir $rmdir dirname Note file or sub-directory inside this directory. You can re $rmdir dirname1 dirname2 dirname3 The above command removes the directories dirname1, dirname2, and dirname3, if they are empty. The rmdircommand produces no output if it is successful. Changing Directories You can use the cd command to do more than just change to a home directory. You can use it to change to any directory by specifying a valid absolute or relative path. The syntax is as

$cd dirname Here, dirname is the name of the directory that you want to change to. For example, the

$cd /usr/local/bin Changes to the directory /usr/local/bin. From this directory, you can cd to the directory /usr/home/amrood $cd ../../home/amrood Renaming Directories The mv (move) command can also be used to rename a directory. The syntax is as follows

$mv olddir newdir You can rename a directory mydir to yourdir $mv mydir yourdir The directories . (dot) and .. (dot dot)

Page 15: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

The filename . (dot) represents the current working directory; and the filename .. (dot dot) represents the directory one level above the current working directory, often referred to as the parent directory. If we enter the command to show a listing of the current working directories/files and use the -a option to list all the files and the -l option to provide the long listing, we will receive the following result. $ls -la drwxrwxr-x 4 teacher class 2048 Jul 16 17.56 . drwxr-xr-x 60 root 1536 Jul 13 14:18 .. ---------- 1 teacher class 4210 May 1 08:27 .profile -rwxr-xr-x 1 teacher class 1948 May 12 13:42 memo

5. FILE PERMISSIONS.

Linux permissions dictate 3 things you may do with a file, read, write and execute. They are referred to in Linux by a single letter each.

r read - you may view the contents of the file. w write - you may change the contents of the file. x execute - you may execute or run the file if it is a program or script.

Every file in Unix has the following

Owner permissions of the file can perform on the file.

Group permissions is a member of the group that a file belongs to, can perform on the file.

Other (world) permissions other users can perform on the file.

File Access Modes The permissions of a file are the first line of defense in the security of a Unix system. The basic building blocks of Unix permissions are the read, write, and execute permissions,

Read Grants the capability to read, i.e., view the contents of the file.

Write Grants the capability to modify, or remove the content of the file.

Execute User with execute permissions can run a file as a program.

Page 16: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Directory Access Modes Directory access modes are listed and organized in the same manner as any other file.

Read Access to a directory means that the user can read the contents. The user can look at the filenames inside the directory.

Write Access means that the user can add or delete files from the directory.

Execute Executing a directory doesn't really make sense, so think of this as a traverse permission.

A user must have execute access to the bin directory in order to execute the ls or the cd command.

Changing Permissions To change the file or the directory permissions, you use the chmod (change mode) command. There are two ways to use chmod the symbolic mode and the absolute mode.

Using chmod in Symbolic Mode The easiest way for a beginner to modify file or directory permissions is to use the symbolic mode. With symbolic permissions you can add, delete, or specify the permission set you want by using the operators in the following table.

S.No. Chmod operator & Description

1 +Adds the designated permission(s) to a file or directory.

2 - Removes the designated permission(s) from a file or directory.

3 = Sets the designated permission(s).

chown chown command stands for "change owner" and is used to change the owner of a file.

chgrp chgrp command stands for "change group" and is used to change the group of a file.

Page 17: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

SUID and SGID File Permission Additional permissions are given to programs via a mechanism known as the Set User ID (SUID) and Set Group ID (SGID) bits.

When you execute a program that has the SUID bit enabled, you inherit the permissions of that program's owner. Programs that do not have the SUID bit set are run with the permissions of the user who started the program.

This is the case with SGID as well. Normally, programs execute with your group permissions, but instead your group will be changed just for this program to the group owner of the program.

UNIT-III

1. Process and Kernel support for process:

The UNIX process table may be thought of as a data structure describing all of the processes that are currently loaded.

Viewing Processes

We can see what processes are running by using the ps command. Here is some sample

output:

The PID column gives the PIDs, the TTY column shows which terminal started the process, the STAT column shows the current status, TIME gives the CPU time used so far and the COMMAND column shows the command used to start the process. Let&#39;s take a closer look at some of these:

The initial login was performed on virtual console number one (v01). The shell is running bash. Its status is s, which means sleeping. This is because it&#39;s waiting for the X Windows sytem to finish.

X Windows was started by the command startx. It won&#39;t finish until we exit from X. It too is sleeping.

The fvwm is a window manager for X, allowing other programs to be started and windows to be arranged on the screen.

This process represents a window in the X Windows system. The shell, bash, is running in the new window. The window is running on a new pseudo terminal (/dev/ptyp0) abbreviated

pp0.

Page 18: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

This is the EMACS editor session started from the shell mentioned above. It uses the pseudo terminal.

This is a clock program started by the window manager. It&#39;s in the middle of a one-minute wait between updates of the clock hands.

Process environment

Let&#39;s look at some other processes running on this Linux system. The output has been abbreviated for clarity:

Here we can see one very important process indeed:

In general, each process is started by another, known as its parent process. A process so started is known as a child process.

When UNIX starts, it runs a single program, the prime ancestror and process number one:

init.

One such example is the login procedure init starts the getty program once for each terminal that we can use to long in.

These are shown in the ps output like this:

When interacting with user server through a shell session, there are many pieces of information that user shell compiles to determine its behavior and access to resources. Some of these settings are contained within configuration settings and others are determined by user input.

One way that the shell keeps track of all of these settings and details is through an area it maintains called the environment. The environment is an area that the shell builds every time that it starts a session that contains variables that define system properties. In this guide, we will discuss how to interact with the environment and read or set environmental and shell variables interactively and through configuration files. We will be using an Ubuntu 12.04 VPS as an example, but these details should be relevant on any Linux system.

Every time a shell session spawns, a process takes place to gather and compile information that should be available to the shell process and its child processes. It obtains the data for these settings from a variety of different files and settings on the system.

Page 19: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Kernel support for process:

Basically the environment provides a medium through which the shell process can get or set settings and, in turn, pass these on to its child processes. The kernel runs the show, i.e. it manages all the operations in a Unix flavored environment.

The kernel architecture must support the primary Unix requirements. These requirements fall in two categories namely, functions for process management and functions for file management (files include device files). Process management entails allocation of resources

including CPU, memory, and offers services that processes may need. The file management in itself involves handling all the files required by processes, communication with device

drives and regulating transmission of data to and from peripherals. The kernel operation gives the user processes a feel of synchronous operation, hiding all underlying asynchronism in peripheral and hardware operations (like the time slicing by clock). In summary, we can say that the kernel handles the following operations :

1. It is responsible for scheduling running of user and other processes.

2. It is responsible for allocating memory.

3. It is responsible for managing the swapping between memory and disk.

4. It is responsible for moving data to and from the peripherals.

5. It receives service requests from the processes and honors them.

2. Zombie Process: its

process descriptor stays in memory (the process descriptor only takes a tiny amount of

notified that its child process has died with the SIGCHLD signal. The parent process is then supposed to execute the other information. This allows the parent process to get information from the dead process. After wait() is called, the zombie process is completely removed from memory. When a child exits, the parent process will receive a SIGCHLD signal to indicate that one of its children has finished executing; the parent process will typically call the wait() status, and will cause the child to be reaped, or removed from the process table.

Page 20: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

3. Orphan Process

An orphan process is a computer process whose parent process has finished or terminated, though it remains running itself.

In a Unix-like operating system any orphaned process will be immediately adopted by the special init system process. This operation is called re-parenting and occurs automatically. Even though technically the process has the init process as its parent, it is still called an orphan process since the process that originally created it no longer exists.

A process can be orphaned unintentionally, such as when the parent process terminates or crashes. The process group mechanism in most Unix-like operation systems can be used to help protect against accidetry to terminate all the child processes with the SIGHUP process signal, rather than letting them continue to run as orphans.

4. Signals:

Signals are software interrupts sent to a program to indicate that an important event has occurred. The events can vary from user requests to illegal memory access errors. Some signals, such as the interrupt signal, indicate that a user has asked the program to do something that is not in the usual flow of control SIGHUP: Hang up detected on controlling terminal or death of controlling process SIGINT: Issued if the user sends an interrupt signal (Ctrl + C) SIGQUIT: Issued if the user sends a quit signal (Ctrl + D) SIGFPE: Issued if an illegal mathematical operation is attempted SIGKILL: If a process gets this signal it must quit immediately and will not perform any clean-up operations SIGALRM: Alarm clock signal (used for timers)

Terminate the process.

Ignore the signal.

Dump core. This creates a file called core containing the memory image of the process when it received the signal.

Stop the process.

Continue a stopped process.

5. alarm(), kill(), raise(), pause(), abort(), sleep() system calls:

Alarm():

The alarm function allows us to get a timer that will expire at a specified time in the future.

Page 21: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

signal, its default action is to terminate the process. #include&lt;unistd.h&gt; unsigned int alarm(unsigned int seconds); Returns: 0 or number of seconds until previously set alarm. The seconds value is the number of clock seconds in the future when the signal

should be generated. There is only one of the alarm clocks per process. If, when we call alarm, there is a

previously registered alarm clock for the process that has not yet expired, the number of seconds left for that alarm clock to return as the value of this function. That previously registered alarm clock is replaced by the new value. If there is a previously registered alarm clock for the process that has not yet

seconds left for that previous alarm clock is still returned as the value of the function. Although the default action for SIGALRM is terminating the process, most processes use an alarm clock catch this signal.

Kill( ) and raise( ):

The kill function sends a signal to a process or a group of processes. The raise function allows a process to send a signal to itself.

#include&lt;sys/types.h&gt;

#include&lt;signal.h&gt;

int kill(pid_t pid, int signo); int raise(int signo);

Both return: 0 if OK, -1 on error

There are four different conditions for the pid argument to kill: pid &gt; 0 The signal is sent to the process whose process ID is pid.

pid = 0 The signal is sent to all processes whose process group ID equals the process

group ID of the sender and for which the sender has permission to send thesignal.

pid&lt;0 The signal is sent to all processes whose process group ID equals the absolute value

of pid and for which the sender has permission to send the signal.

pid = -1 unspecified.

Pause( ): The pause function suspends the calling process until a signal is caught.

Page 22: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

#include&lt;unistd.h&gt; int pause(void); Returns: -1 with errno set to EINTR The only time pause returns is if a signal handler is executed and that handler returns. In that case, pause returns -1 with errno set to EINTR.

Abort( ):

abort function causes abnormal program termination.

#include&lt;stdlib.h&gt; void abort(void);

This function never returns. This function sends the SIGABRT signal to the process. A process should not ignore

this signal. abort overrides the blocking or ignoring of the signal by the process.

sleep ():

#include&lt;unistd.h&gt;

unsigned int sleep(unsigned int seconds);

Returns: 0 or number of unslept seconds.

This function causes the calling process to be suspended until either:

The amount of clock that is specified by seconds has elapsed or A signal is caught by the process and the signal handler returns.

In case 1 the return value is 0 when sleep returns early, because of some signal being caught

case 2, the return value is the number of unslept seconds.

Sleep can be implemented with an alarm function. If alarm is used however, there can be

interaction between the two functions.

Page 23: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

UNIT-IV

1. Inter-process Communication

Interprocess Communication-

Interprocess communication (IPC) includes thread synchorization and data exchange between threads beyond the process boundaries. If threads belong to the same process, they execute in the same address space, i.e. they can access global (static) data or heap directly, without the help of the operating system. However, if threads belong to different processes, they cannot access each others address spaces without the help of the operating system.

There are two fundamentally different approaches in IPC:

processes are residing on the samecomputer processes are residing on differentcomputers

The first case is easier to implement because processes can share memory either in the user space or in the system space. This is equally true for uniprocessors and multiprocessors. In the second case the computers do not share physical memory, they are connected via I/O device(for example serial communication or Ethernet). Therefore the processes residing in different computers can not use memory as a means for communication.

IPC between processes on a Single System

Most of this chapter is focused on IPC on a single computer system, including four general approaches:

Sharedmemory Messages Pipes Sockets

The synchronization objects considered in the previous chapter normally work across the process boundaries (on a single computer system). There is one addition necessary however: the synchronization objects must be named. The handles are generally private to the process, while the object names, like file names, are global and known to all processes. h = init_CS("xxx"); h = init_semaphore(20,"xxx"); h = init_event("xxx");

Page 24: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

h = init_condition("xxx"); h = init_message_buffer(100,"xxx"); IPC between processes on different systems

IPC between processes on different systems

IPC is Inter Process Communication, more of a technique to share data across different processes withinone machine, in such a way that data passing binds the coupling of different processes.

The first,isusing memory mapping techniques, where a memory map is created, andothers open the memory map forreading/writing...

The second is, using sockets, to communicate with one another...this has a high overhead,as each process would have to open up the socket, communicate across...althougheffective

The third, is to use a pipe or a named pipe, a very goodexample

2. PIPES

A pipe is a serial communication device (i.e., the data is read in the order in which it was written),which allows a unidirectional communication. The data written to end is read back from the other end. The pipe is mainly used to communicate between two threads in a single process or between parent and child process. Pipes can only connect the related process. In shell, the symbol can be used to create pipe. In pipes the capacity of data is limited. (i.e.) If the writing process is faster than the reading process which consumes the data, the pipe cannot store the data. In this situation the writer process will block until more capacity becomes available. Also if the reading process tries to read data when there is no data to read, it will be blocked until the data becomes available. By this, pipes automatically synchronize the twoprocess.

Creatingpipes:

The pipe() function provides a means of passing data between two programs and also allows to read and write the data. #include<unistd.h> int pipe(int file_descriptor[2]); pipe()function is passed with an array of file descriptors. It will fill the array with new file

Page 25: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

descriptors and returns zero. On error, returns -1 and sets the errno to indicate the reason of failure. The file descriptors are connected in a way that is data written to file_ descriptor[1] can be read back from the file_descriptor[0]. (Note: As this uses file descriptors and not the file streams, we must use read and write system calls to access the data.) Pipes are originally used in UNIX and are made even more powerful in Windows 95/NT/2000. Pipes are implemented in file system. Pipes are basically files with only two file offsets: one for reading another for writing. Writing to a pipe and reading from a pipe is strictly in FIFO manner. (Therefore pipes are also called FIFOs). For efficiency, pipes are in-core files, i.e. they reside in memory instead on disk, as any other global data structure. Therefore pipes must be restricted in size, i.e. number of pipe blocks must be limited. (In UNIX the limitation is that pipes use only direct blocks.)Since the pipes have a limited size and the FIFO access discipline, the reading and writing processes are synchronized in a similar manner as in case of message buffers. The access functions for pipes are the same as for files: WriteFile() and ReadFile().

3. FIFO

Similar to pipes, but allows for communication between unrelated processes. This is done by naming the communication channel and making it permanent. Like pipe, FIFO is the unidirectional data stream. FIFO creation:

int mkfifo ( const char *pathname, mode_t mode ); - makesa FIFO special file with namepathname. (mode specifies the FIFO's permissions, as common in UNIX-like file systems). - A FIFO special file is similar to a pipe, except that it is createdin a different way. Instead of being an anonymous communications channel, a FIFO special file is entered into the file system by calling mkfifo() Once a FIFO special file has been created, any process can open it for reading or writing, in the same

Page 26: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

way as an ordinary file. A First-in, first-out(FIFO) file is a pipe that has a name in the filesystem. It is also called as named pipes.

Creation of FIFO: We can create a FIFO from the command line and within a program. To create from command line we can use either mknod or mkfifo commands.

$ mknod filename p $ mkfifo filename (Note: The mknod command is available only n older versions, you can make use of mkfifo in new versions.) To create FIFO within the program we can use two system calls. They are,

#include<sys/types.h> #include<sys/stat.h> int mkfifo(const char *filename,mode_t mode); int mknod(const char *filename, mode_t mode|S_IFIFO,(dev_t) 0); If we want to use the mknod function we have to use ORing process of fileaccess mode with S_IFIFO and the dev_t value of 0.Instead of using this we can use the simple mkfifo function.

Accessing FIFO: Let us first discuss how to access FIFO in command line using file commmands. The useful feature of named pipes is, as they appear in the file system, we can use them in commands. We can read from the FIFO(empty) $ cat< /tmp/my_fifo Now, let us write to the FIFO. $ echo "Simple!!!"> /tmp/my_fifo (Note: These two commands should be executed in different terminals because first command will be waiting for some data to appear in the FIFO.)

Page 27: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

4. Message Queues

A message queue is a linked list of messages stored within the kernel and identified by a message queue identifier. A new queue is created or an existing queue opened by msgget(). New messages are added to the end of a queue by msgsnd(). Every message has a positive long integer type field, a non-negative length, and the actual data bytes (corresponding to the length), all of which are specified to msgsnd() when the message is added to a queue. Messages are fetched from a queue by msgrcv()first-in, first-out order. Instead, we can fetch messages based on their type field. All processes can exchange information through access to a common system message queue. The sending process places a message (via some (OS) message-passing module) onto a queue which can be read by another process. Each message is given an identification or type so that processes can select the appropriate message. Process must share a common key in order to gain access to the queue in the first place.

System calls used for message queues:

ftok(): is use to generate a unique key.

msgget(): either returns the message queue identifier for a newly created message

queue or returns the identifiers for a queue which exists with the same key value.

msgsnd(): Data is placed on to a message queue by calling msgsnd().

msgrcv(): messages are retrieved from a queue.

msgctl(): It performs various operations on a queue. Generally it is use to

Page 28: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

destroy message queue.

5. Semaphores

Semaphores are not used to exchange a large amount of data. Semaphores are used synchronization among processes. Other synchronization mechanisms include record locking and mutexes. Why necessary? Examples include: shared washroom, common rail segment, and common bank account. Further comments: 1) The semaphore is stored in the kernel: Allows atomic operations on the semaphore. Processes are prevented from indirectly modifying the value.

2) A process acquires the semaphore if it has a value of zero. The value of the semaphore is then incremented to a process releases the semaphore, the value of the semaphore is decremented. 3) If the semaphore has non-zero value when a process tries to acquire it, that process blocks. 4) In comments 2 and 3, the semaphore acts as a customer counter. In most cases, it is a resource counter. 5) the semaphore is available. This is better (more efficient) than busy waiting such as TEST&SET. 6) The kernel maintains information on each semaphore internally, using a data structure struct semis_ds that keeps track of permission, number of semaphores, etc. 7) Apparently, a semaphore in Unix is not a single binary value, but a set of nonnegative integer values 8) There are 3 (logical) types of semaphores: Binary semaphore have a value of 0 or 1. Similar to a mutex lock. 0 means locked; 1 means unlocked.

Counting semaphore has a value 0. Used for counting resources, like the producer-

consumer example. Note that value =0 is similar to a lock (resource not available).

Set of counting semaphores one or more semaphores, each of which is a counting semaphore.

9) There are 2 basic operations performed with semaphores: Wait waits until the semaphore is > 0, then decrements it.

Post increments the semaphore, which wakes waiting processes.

Page 29: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

UNIT-V

1. Shared Memory

Using a pipe or a message queue requires multiple exchanges of data through the kernel. Shared memory can be used to bypass the kernel for faster processing.

The kernel maintains information about each shared memory segment, including permission, size, access time, etc in struct shmid_ds .struct shmid_ds looks like struct semid_ds or as struct msqid_ds .

A share memory segment is created using: int shmget(key_t key, int size, int shmflag); size --- size of the shared memory segment in bytes. shmflag --- same as for msgget() and

--- shmid, the shared memory identifier, -1 on error.

Page 30: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Attach to the shared memory segment using: char *shmat(int shmid, char *shmaddr, int shmflag) shmid --- return value of shmget, that is, the id of the created shared memory.shmaddr--- 0: let the kernel select the address. shmflag--- SHM_RDONLY for read_only access. returns the starting address of the shared memory, and thus we can read/write on the shared memory after getting its starting address.

Detach the shared memory segment using: char *shmdt(char *shmaddr) shmaddr --- the return value of shmat(), that is, the starting address of the shared memory. returns 1 on failure. To remove a shared memory segment: Int shmctl(int shmid, int cmd, shmid_ds *buf); cmd--- IPC_RMID to delete, e.g., shmctl(shmid, IPC_RMID,

2. Socket

Sockets allow communication between two different processes on the same or different machines. To be more precise, it's a way to talk to other computers using standard Unix file descriptors. In Unix, every I/O action is done by writing or reading a file descriptor. A file descriptor is just an integer associated with an open file and it can be a network connection, a text file, a terminal, or something else.

To a programmer, a socket looks and behaves much like a low-level file descriptor. This is because commands such as read() and write() work with sockets in the same way they do with files and pipes.

Sockets were first introduced in 2.1BSD and subsequently refined into their current form with 4.2BSD. The sockets feature is now available with most current UNIX system releases.

Where is Socket Used?

A Unix Socket is used in a client-server application framework. A server is a process that performs some functions on request from a client. Most of the application-level protocols like FTP, SMTP, and POP3 make use of sockets to establish connection between client and server and then for exchanging data.

Socket Types

There are four types of sockets available to the users. The first two are most commonly used and the last two are rarely used.

Processes are presumed to communicate only between sockets of the same type but there is no restriction that prevents communication between sockets of different types.

Page 31: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Stream Sockets through the stream socC". These sockets use TCP (Transmission Control Protocol) for data transmission. If delivery is impossible, the sender receives an error indicator. Data records do not have any boundaries.

Datagram Sockets

you build a packet with the destination information and send it out. They use UDP (User Datagram Protocol).

3. Socket in client/server model

Most of the Net Applications use the Client-Server architecture, which refers to two processes or two applications that communicate with each other to exchange some information. One of the two processes acts as a client process, and another process acts as a server.

Client Process

This is the process, which typically makes a request for information. After getting the response, this process may terminate or may do some other processing.

Example, Internet Browser works as a client application, which sends a request to the Web Server to get one HTML webpage.

Server Process

This is the process which takes a request from the clients. After getting a request from the client, this process will perform the required processing, gather the requested information, and send it to the requestor client. Once done, it becomes ready to serve another client. Server processes are always alert and ready to serve incoming requests.

Example or requests from Internet Browsers and as soon as it gets any request from a browser, it picks up a requested HTML page and sends it back to that Browser.

Note that the client needs to know the address of the server, but the server does not need to know the address or even the existence of the client prior to the connection being established. Once a connection is established, both sides can send and receive information.

Page 32: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

2-tier and 3-tier architectures

There are two types of client-

2-tier architecture type of architecture may have some security holes and performance problems. Internet Explorer and Web Server work on two-tier architecture. Here security problems are resolved using Secure Socket Layer (SSL).

3-tier architectures

all the security checks and load balancing in case of heavy load. A middleware takes all requests from the client and after performing the required authentication, it passes that request to the server. Then the server does the required processing and sends the response back to the middleware and finally the middleware passes this response back to the client. If you want to implement a 3-tier architecture, then you can keep any middleware like Web Logic or WebSphere software in between your Web Server and Web Browser.

Types of Server

T

Iterative Server client and after completing the first request, it takes request from another client. Meanwhile, another client keeps waiting.

Concurrent Servers many requests at a time because one process may take longer and another client cannot wait for so long. The simplest way to write a concurrent server under Unix is to fork a child process to handle each client separately.

How to Make Client

The system calls for establishing a connection are somewhat different for the client and the server, but both involve the basic construct of a socket. Both the processes establish their own sockets.

Create a socket with the socket() system call.

Connect the socket to the address of the server using the connect() system call.

Send and receive data. There are a number of ways to do this, but the simplest way is to use the read() and write() system calls.

Page 33: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

How to make a Server

Create a socket with the socket() system call.

Bind the socket to an address using the bind() system call. For a server socket on the Internet, an address consists of a port number on the host machine.

Listen for connections with the listen() system call.

Accept a connection with the accept() system call. This call typically blocks the connection until a client connects with the server.

Send and receive data using the read() and write()system calls.

Client and Server Interaction

Following is the diagram showing the complete Client and Server interaction.

4. Socket Addresses

sockaddr

The first structure is sockaddr

struct sockaddr {

unsignedshort sa_family;

char sa_data[14];

};

This is a generic socket address structure, which will be passed in most of the socket

Page 34: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Attribute Values Description

sa_family

AF_INET

AF_UNIX

AF_NS

AF_IMPLINK

It represents an address family. In most of the Internet-based applications, we use AF_INET.

sa_data Protocol-specific Address

The content of the 14 bytes of protocol specific address are interpreted according to the type of address. For the Internet family, we will use port number IP address, which is represented by sockaddr_in structure defined below.

sockaddr in

struct sockaddr_in {

shortint sin_family;

unsignedshortint sin_port;

struct in_addr sin_addr;

unsignedchar sin_zero[8];

};

Page 35: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Attribute Values Description

sa_family

AF_INET

AF_UNIX

AF_NS

AF_IMPLINK

It represents an address family. In most of the Internet-based applications, we use AF_INET.

sin_port Service Port A 16-bit port number in Network Byte Order.

sin_addr IP Address A 32-bit IP address in Network Byte Order.

sin_zero Not Used You just set this value to NULL as this is not being used.

in addr

This structure is used only in the above structure as a structure field and holds 32 bit netid/hostid.

struct in_addr {

unsignedlong s_addr;

};

Page 36: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Attribute

Values

Description

s_addr service port

A 32-bit IP address in Network Byte Order.

hostent

This structure is used to keep information related to host.

struct hostent {

char*h_name;

char**h_aliases;

int h_addrtype;

int h_length;

char**h_addr_list

#define h_addr h_addr_list[0]

};

Page 37: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Attribute Values Description

h_name ti.com etc.

It is the official name of the host. For example, tutorialspoint.com, google.com, etc.

h_aliases TI It holds a list of host name aliases.

h_addrtype AF_INET It contains the address family and in case of Internet based application, it will always be AF_INET.

h_length 4 It holds the length of the IP address, which is 4 for Internet Address.

h_addr_list in_addr For Internet addresses, the array of pointers h_addr_list[0], h_addr_list[1], and so on, are points to structure in_addr.

NOTE h_addr is defined as h_addr_list[0] to keep backward compatibility.

Page 38: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

5.Socket APIs

This list is a summary of functions or methods provided by the Berkeley sockets API library:

socket() creates a new socket of a certain socket type, identified by an integer number, and allocates system resources to it.

bind() is typically used on the server side, and associates a socket with a socket address structure, i.e. a specified local port number and IP address.

listen() is used on the server side, and causes a bound TCP socket to enter listening state.

connect() is used on the client side, and assigns a free local port number to a socket. In case of a TCP socket, it causes an attempt to establish a new TCP connection.

accept() is used on the server side. It accepts a received incoming attempt to create a new TCP connection from the remote client, and creates a new socket associated with the socket address pair of this connection.

send() and recv(), or write() and read(), or sendto() and recvfrom(), are used for sending and receiving data to/from a remote socket.

close() causes the system to release resources allocated to a socket. In case of TCP, the connection is terminated.

gethostbyname() and gethostbyaddr() are used to resolve host names and addresses. IPv4 only.

select() is used to suspend, waiting for one or more of a provided list of sockets to be ready to read, ready to write, or that have errors.

poll() is used to check on the state of a socket in a set of sockets. The set can be tested to see if any socket can be written to, read from or if an error occurred.

getsockopt() is used to retrieve the current value of a particular socket option for the specified socket.

setsockopt() is used to set a particular socket option for the specified socket.

Page 39: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

16. Unit Wise Question Bank

UNIT-I

Two Mark Question with Answer:

1) What is Linux?

Linux is an operating system based on UNIX, and was first introduced by Linus Torvalds. It is

based on the Linux Kernel, and can run on different hardware platforms manufactured by Intel,

MIPS, HP, IBM, SPARC and Motorola. Another popular element in Linux is its mascot, a penguin

figure named Tux.

2) What is the difference between UNIX and LINUX?

Unix originally began as a propriety operating system from Bell Laboratories, which later on

spawned into different commercial versions. On the other hand, Linux is free, open source and

intended as a non-propriety operating system for the masses.

3) Write a short notes on BASH?

BASH is short for Bourne Again SHell. It was written by Steve Bourne as a replacement to the

original Bourne Shell (represented by /bin/sh). It combines all the features from the original

version of Bourne Shell, plus additional functions to make it easier and more convenient to use. It

has since been adapted as the default shell for most systems running Linux.

4) What is Linux Kernel?

The Linux Kernel is a low-level systems software whose main role is to manage hardware

resources for the user. It is also used to provide an interface for user-level interaction.

5 ) What are the basic components of Linux?

The operating system, Linux has all of these components: kernel, shells and GUIs, system utilities,

and application program. What makes Linux advantageous over other operating system is that

every aspect comes with additional features and all codes for these are ownloadable for free.

Page 40: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Three Mark Question with Answer:

1) How do you change permissions under Linux?

Assuming you are the system administrator or the owner of a file or directory, you can grant

permission using the chmod command. Use + symbol to add permission or symbol to deny

permission, along with any of the following letters: u (user), g (group), o (others), a (all), r (read),

w (write) and x (execute).

For example the command chmod go+rw FILE1.TXT grants read and write access to the file

FILE1.TXT, which is assigned to groups and others.

2) What is the pwd command?

command, and is used to display the current location in the directory tree.

3) What are the kinds of permissions under Linux?

There are 3 kinds of permissions under Linux:

Read: users may read the files or list the directory

Write: users may write to the file of new files to the directory

Execute: users may run the file or lookup a specific file within a directory

4)What are environmental variables?

Linux programs. Another common term for environmental variables is global shell variables.

5) What is redirection?

Redirection is the process of directing data from one output to another. It can also be used to direct

an output as an input to another process.

Five Mark Question with Answer:

1. UNIX/ LINUX operating system and features of UNIX/LINUX.

Linux is a Unix-like computer operating system assembled under he model of free and opensource

software development and distribution. The defining component of Linux is the Linuxkernel, an

Page 41: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

operating system kernel first released 5 October 1991 by Linus Torvalds.

Linux was originally developed as a free operating system for Intel x86-based personal

computers. It has since been ported to more computer hardware platforms than any other operating

system. It is a leading operating system on servers and other big iron systems such as mainframe

computers and supercomputers more than 90% of today's 500 fastestsupercomputers run some

variant of Linux, including the 10 fastest. Linux also runs on embedded systems (devices where

the operating system is typically built into the firmware and highly tailored to the system) such as

mobile phones, tablet computers, network routers, televisions and video gameconsoles; the

Android system in wide use on mobile devices is built on the Linuxkernel.

Basic Features

Following are some of the important features of Linux Operating System.

Portable -

way. A Linux kernel and application program supports their installation on anykind of

hardwareplatform.

Open Source - Linux source code is freely available and it is community based

Linux operating system and it is continuouslyevolving.

Multi-User - Linux is a multiuser system means multiple users can accesssystem resources

like memory/ ram/ application programs at sametime.

Multiprogramming Linux is a multiprogramming system means multipleapplications

can run at sametime.

Hierarchical File System - Linux provides a standard file structure in whichsystem

files/ user files are arranged.

Shell - Linux provides a special interpreter program which can be used to execute

commands of the operating system. It can be used to do various types of operations,call

application programsetc.

Security - Linux provides user security using authentication features like password

protection/ controlled access to specific files/ encryption ofdata.

Page 42: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Advantages:

1. Low cost: spend time and money to obtain licenses since Linux and

much of its software come with the GNU General Public License. User can start to work

immediately without worrying that user software may stop working anytime because the

free trial version expires. Additionally, there are large repositories from which user can

freely download high quality software for almost any task user can thinkof.

2. Stability:

down over time due to memory leaks. Continuous up- times of

hundreds of days (up to a year or more) are notuncommon.

3. Performance: Linux provides persistent high performance on workstations andon

networks. It can handle unusually large numbers of users simultaneously, and can make old

computers sufficiently responsive to be usefulagain.

4. Network friendliness: Linux was developed by a group of programmers over theInternet

and has therefore strong support for network functionality; client and server systems can be

easily set up on any computer running Linux. It can perform tasks such as network backups

faster and more reliably than alternativesystems.

5. Flexibility: Linux can be used for high performance server applications, desktop

applications, and embedded systems. User can save disk space by only installing the

components needed for a particular use. User can restrict the use of specific computers by

installing for example only selected office applications instead of the wholesuite.

6. Compatibility: It runs all common Unix software packages and can process all common

file formats.

7. Choice: The large number of Linux distributions gives user a choice. Each distribution is

developed and supported by a different organization. User can pick the one user like

best;the core functionalities are the same; most software runs on most distributions.

8. Fast and easy installation: Most Linux distributions come with user-friendly installation

and setup programs. Popular Linux distributions come with tools that make installation of

additional software very user friendly aswell.

9. Full use of hard disk: Linux continues work well even when the hard disk is almost full.

10. Multitasking: Linux is designed to do many things at the same time; e.g., a largeprinting

w down user otherwork.

Page 43: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

11. Security:

access permission systems prevent access by unwanted visitors or viruses. Linux users

haveto option to select and safely download software, free of charge, from online

repositories containing thousands of high quality packages. No purchase transactions

requiring credit card numbers or other sensitive personal information arenecessary.

12. Open Source: If user develop software that requires knowledge or modification of the

are Open Source aswell.

--------------------------------------------------------------------------------------------------------------

2. Disk Utilities with example

du takes a single argument, specifying a pathname for du to work; if it is not specified, the current

directory is used. The SUS mandates for du the following options:

-a, display an entry for each file (and not directory) contained in the current directory

-H, calculate disk usage for link references specified on the command line

-k, show sizes as multiples of 1024 bytes, not 512-byte

-L, calculate disk usage for link references anywhere

-s, report only the sum of the usage in the current directory, not for each file

-x, only traverse files and directories on the device on which the pathname argument is specified.

Other UNIX and Unix-like operating systems may add extra options. For example, BSD and GNU

du specify a -h option, displaying disk usage in a format easier to read by the user, adding units

with the appropriate SI prefix

$ du -sk *

152304 directoryOne

1856548 directoryTwo

Sum of directories in human-readable format (Byte, Kilobyte, Megabyte, Gigabyte, Terabyte and

Petabyte):

$ du -sh *

149M directoryOne 1.8G directoryTwo

Page 44: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Disk usage of all subdirectories and files including hidden files within the current directory (sorted

by filesize) :

$ du -sk .[!.]* *| sort -n

Disk usage of all subdirectories and files including hidden files within the current directory (sorted

by reverse filesize) :

$ du -sk .[!.]* *| sort nr

The weight of directories:

$ du -d 1 -c -h

df command : Report file system disk space usage

Df command examples - to check free disk space

Type df -h or df -k to list free disk space:

$ df -h OR

$ df k Output:

Filesystem Size Used Avail Use% Mountedon

/dev/sdb1 20G 9.2G 9.6G 49%

/varrun 393M 144k 393M 1%

/var/run varlock 393M 0 393M 0%

/var/lock procbususb 393M 123k 393M 1%

/proc/bus/usbudev 393M 123k 393M 1%

/devdevshm 393M 0 393M 0%

/dev/shmlrm 393M 35M 359M 9%

/lib/modules/ 2.6.20-15-generic/volatile

/dev/sdb5 29G 5.4G 22G 20%/media/docs

/dev/sdb3 30G 5 9 G 23G 21%/media/isomp3s

/dev/sda1 8.5G 4.3G 4.3G 51%/media/xp1

/dev/sda2 12G 6.5G 5.2G 56%/media/xp2

/dev/sdc1 40G 3.1G 35G 9%/media/backup

Page 45: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

du command examples:

du shows how much space one ore more files or directories is using.

$ du -sh 103M

-s option summarize the space a directory is using and -h option provides "Human-readable"

output.

-----------------------------------------------------------------------------------------------------------

3. SED and its addresses?

sed instruction format(Sed Addresses):

address determines which lines in the input file are to be processed by thecommand(s)

if no address is specified, then the command is applied to each inputline

addresstypes:

Single-Lineaddress

Set-of-Linesaddress

Rangeaddress

Nested

address Single-LineAddress

Specifies only one line in the inputfile

special: dollar sign ($) denotes last line of inputfile

Examples:

show only line3

sed -n -e '3 p' input-file

show only lastline

sed -n -e '$ p' input-file

Page 46: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

substitute e with online10

sed -e '10 s/endif/fi/' input-file

Set-of-Lines Address

use regular expression to matchlines

written between twoslashes

process only lines thatmatch

may match severallines

lines may or may not beconsecutives

Examples:

sed - -file sed -n - -file

Range Address

Defines a set of consecutivelines

Format:

start-addr,end-addr (inclusive)

Examples:

10,50 line-number,line-number 10,

/R.E/ line-number,/RegExp/

/R.E./,10 /RegExp/,line-number

/R.E./,/R.E/ /RegExp/,/RegExp/ Example: RangeAddress

% sed -n - -file

Print lines between BEGIN and END,inclusive

Page 47: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

BEGIN

Line 1 of input Line 2 of input Line3 of input END

Line 4 of input

Line 5 of input

Nested Address

Nested address contained within anotheraddress

Example:

print blank lines between line 20 and 30

20,30{

/^$/ p

}

---------------------------------------------------------------------------------------------------------------

4. awk

created by: Aho, Weinberger, andKernighan

scripting language used for manipulating data and generatingreports

awkoperations:

scans a file line byline

splits each input line intofields

compares input line/fields topattern

performs action(s) on matchedlines

Useful for:

transform datafiles

produce formattedreports

Page 48: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Programming constructs:

format outputlines

arithmetic and stringoperations

conditionals and loops

awk Syntax

awk [options] f scriptfilefile(s)

Options:

-F to change input field separator

-f to namescriptfile

awkProgram

consists of patterns &actions:

pattern {action}

if pattern is missing, action is applied to alllines

if action is missing, the matched line isprinted

must have either pattern oraction

Example:

awk '/for/' testfile

prints all lines containing string for intext file.

awk Scripts

awk scripts are divided into three majorparts:

Page 49: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following
Page 50: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

comment lines start with#

BEGIN:pre-processing

performs processing that must be completed before the file processing starts(i.e., before

awk starts reading records from the inputfile)

useful for initialization tasks such as to initialize variables and to create report

headings

BODY:Processing

contains main processing logic to be applied to inputrecords

like a loop that processes input data one record at atime:

if a file contains 100 records, the body will be executed 100 times, onefor eachrecord

END:post-processing

contains logic to be executed after all input data have beenprocessed

Logic such as printing report grand total should be performed in this part ofthe script.

----------------------------------------------------------------------------------------------------------------

5. Shell and its responsibilities?

The shell has similarities to the DOS command processor Command.com (actually

Dos was design as a poor copy of UNIX shell), it's actually much more powerful,

really a programming language in its own right.

A shell is always available on even the most basic UNIX installation. User has to go

through the shell to get other programs to run. User can write programs using the shell.

Users use the shell to administrate user UNIX system.

For example: ls -al | more

is a short shell program to get a long listing of the present directory and route the

output through the more command.

Page 51: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Working with BourneShell

The Bourne shell, or sh, was the default Unix shell of Unix Version 7. It

wasdeveloped by Stephen Bourne, of AT&T BellLaboratories.

A Unix shell, also called "the command line", provides the traditional user

interfacefor the Unix operating system and for Unix-like systems. Users direct the

operation of the computer by entering command input as text for a shell toexecute.

There are many different shells in use. Theyare

Bourne shell(sh)

C shell(csh)

Korn shell(ksh) Bourne Again shell(bash)

When we issue a command the shell is the first agency to acquire the information. It

accepts and interprets user requests. The shell examines &rebuilds the commands

&leaves the execution work to kernel. The kernel handles the h/w on behalf ofthese

commands &all processes in thesystem.

The shell is generally sleeping. It wakes up when an input is keyed inat the prompt.

This input is actually input to the program that represents theshell.

Shell responsibilities

1. ProgramExecution

2. Variable and FilenameSubstitution

3. I/ORedirection

4. PipelineHookup

5. EnvironmentControl

6. Interpreted ProgrammingLanguage

1.ProgramExecution:

The shell is responsible for the execution of all programs that user request fromuser

terminal.

Each time user type in a line to the shell, the shell analyzes the line and

thendetermines what todo.

Page 52: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Thelinethatistypedtotheshell isknownmoreformallyasthecommandline.Theshell scans

this command line and determines the name of the program to be executed and what

arguments to pass to theprogram.

2. Variable and FilenameSubstitution:

Like any other programming language, the shell lets user assign values to variables.

Whenever user specify one of these variables on the command line, preceded by

adollar sign, the shell substitutes the value assigned to the variable atthatpoint.

3. I/ORedirection:

It is the shell's responsibility to take care of input and output redirection on

thecommand line. It scans the command line for the occurrence of the special

redirection characters<,

>, or >>.

4. PipelineHookup:

Just as the shell scans the command line looking for redirection characters, it also

looks for the pipe character |. For each such character that it finds, it connects the

standard output from the command preceding the | to the standard input of the one

following the|. It then initiates execution of bothprograms.

5. EnvironmentControl:

The shell provides certain commands that let user customize user environment. User

environment includes home directory, the characters that the shell displays

topromptuser to type in a command, and a list of the directories to be searched

whenever user request that a program be executed.

6. Interpreted ProgrammingLanguage:

The shell has its own built-in programming language. This language is interpreted,

meaning that the shell analyzes each statement in the language one line at a time

andthen executes it. This differs from programming languages such as C and

FORTRAN, in which the programming statements are typically compiled into a

machine-executable form before they areexecuted.

Page 53: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Programs developed in interpreted programming languages are typically easier to

debug and modify than compiled ones. However, they usually take much longer to

executethan their compiledequivalents.

-------------------------------------------------------------------------------------------------------

MULTIPLE CHOICE

1. The kernel interacts with the machine

_____________

a. hardware

b. software

c. operating system

d. user

2. The programs access the kernel through a set of

functions called ______

a. shell

b. system call

c. interface

d. command

3. The kernel manages the__________

a. system call

b. command interpreter

c. system memory

d. hardware

4. _________ act as interface between user and

kernel.

a. shell

b. system call

c. system memory

d. hardware

Page 54: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

5. The command which can be connected to

manipulate data in different ways is called as

_______

a. multi tasking

b. pipes

c. internal command

d. filters

6. By default, Linux uses the _________ shell.

a. sh

b. csh

c. ksh

d. bash

7. The system function can be controlled and

automated by using the _______

a. control structures

b. loops and variables

c. unix command

d. shell script

8. The features of the unix are:

a. the file and process

b. multi programming and multi tasking

c. kernel and shell

d. the system calls

9. The shell is represented

by________,_______and______.

a. sh, dsh, bsh.

b. sh, csh, ksh

c. sh, csh, bsh.

d. sh, csh, dsh.

Page 55: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

10. _______ command is used to display and

evaluate shell variables.

a. man

b. echo

c. ls

d. script

1 A 6 D

2 B 7 D

3 C 8 B

4 A 9 B

5 D 10 B

FILL IN THE BLANKS:

Father of UNIX ______.

Unix was written in ____ Language.

_____ is the core of operating system.

UNIX is a _____ system.

Flavors of Linux _________

_____ is a UNIX implementation that is constantly growing with

contributions from the free software foundation.

A command is used with_____ and _______.

_____ command is used to count the total number of lines, words, and

characters contained in a file.

____ tells the user current directory.

Pipes connects the standard ______ of one command to the standard

______ of another.

er

command.

Page 56: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

The shell is a _____ that runs when a user logs in and terminates when

user logs out.

1 Linus Torvalds 7 Options, arguments

2 C 8 WC

3 Kernel 9 pwd

4 Command Based 10 Output, input

5 Red Hat, 11 Command substitution

6 LINUX 12 Process

UNIT II

Two Mark Question with Answer:

1) What is grep command?

grep a search command that makes use of pattern-based searching. It makes use of options

and parameters that is specified along the command line and applies this pattern into

searching the required file output.

2) Explain linux file system?

Linux File system Hierarchy Standard (FHS)

File system hierarchy standard describes directory structure and its content in Unix and Unix

like operating system. It explains where files and directories should be located and what it

should contain.

The Root Directory

All the directories in the Linux system comes under the root directory which is represented

by a forward slash (/). Everything in your system can be found under this root directory even

if they are stored in different virtual or physical directories.

Page 57: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

3) What is the advantage of open source?

Open source facilitates you to distribute your software, including source codes freely to

anyone who is interested. So, you can add features and even debug and correct errors of the

source code.

4) write a short notes on Inode?

A structure that is maintained in a separate area of the harddisk.

File attributes are stored in theinode.

Every file is associated with a table called theinode.

The inode is accessed by the inodenumber.

5) List the attributes of inode?

Inode contains the following attributes of file: file type, file permissions , no.of links,UID of

the owner, GID of the group owner, file size date and time of last modification, last access,

change.

Three mark Question with Answer

1)Write about lseek()?

The lseek system call sets the read/write pointer of a file descriptor,

fildes. You use it to set where in the file the next read or write will

occur.

The offset parameter is used to specify the position and the whence

parameter specifies how the offset is used.

Page 58: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

whencecan be one of the following:

2)Write system call dup and dup2.

The dup system calls provide a way of duplicating a file descriptor,

giving two or more, different descriptors that access the same file.

3) List the use of softlink.

Soft link(symbolic links):Refer to a symbolic path indicating the abstract

location of another file.

Used to provide alternative means of referencingfiles.

Users may create links for files using ln command by specifying soption.

4) write short notes on hardlink.

hard links : Refer to the specific location of physical data.

A hard link is a UNIX path name for afile.

Most of the files have only one hard link. However users may create

additional hard linksfor files using lncommand.

Page 59: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

5) List the functions of directory.

The directory functions are declared in a header file, dirent.h. They use a

structure, DIR, as a basis for directory manipulation.

Here are these functions:

Five mark Question with Answer:

1. LINUX File system

Linux file structure files are grouped

according to purpose. Ex: commands, data

files, documentation. Parts of a Unix

directory tree are listed below. All

directories are grouped under the root

entry "/". That part of the directory tree is

left out of the below diagram.

1. / Root

Every single file and directory

starts from the rootdirectory.

Only root user has write privilege

under thisdirectory.

Please note

2. /bin UserBinaries

Contains binaryexecutables.

Page 60: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Common linux commands user need to use in single-user modes are located under

this directory.

Commands used by all the users of the system are locatedhere.

For example: ps, ls, ping, grep,cp.

3. /sbin SystemBinaries

Just like /bin, /sbin also contain binaryexecutables.

But, the linux commands located under this directory are used typically by system

administrator, for system maintenancepurpose.

For example: iptables, reboot, fdisk, ifconfig,swapon

4. /etc ConfigurationFiles

Contains configuration files required by allprograms.

This also contains startup and shutdown shell scripts used to start/stop individual

programs.

For example: /etc/resolv.conf,/etc/logrotate.conf

5. /dev DeviceFiles

Contains devicefiles.

These include terminal devices, usb, or any device attached to thesystem.

For example: /dev/tty1,/dev/usbmon0

6. /proc ProcessInformation

Contains information about systemprocess.

Thisisapseudofilesystemcontainsinformationaboutrunningprocess.Forexample:

/proc/{pid} directory contains information about the process with that particular pid.

This is a virtual filesystem with text information about system resources. For

example:

/proc/uptime

7. /var VariableFiles

Page 61: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

var stands for variablefiles.

Content of the files that are expected to grow can be found under thisdirectory.

This includes system log files (/var/log); packages and database files (/var/lib);

emails (/var/mail); print queues (/var/spool); lock files (/var/lock); temp files needed

across reboots(/var/tmp);

8. /tmp TemporaryFiles

Directory that contains temporary files created by system andusers.

Files under this directory are deleted when system isrebooted.

9. /usr UserPrograms

Contains binaries, libraries, documentation, and source-code for second

levelprograms.

binaryunder

/bin, look under /usr/bin. For example: at, awk, cc, less, scp

binary under /sbin, look under /usr/sbin. For example: atd, cron, sshd, useradd,userdel

/usr/lib contains libraries for /usr/bin and/usr/sbin

/usr/local contains users programs that user install from source. For example, when

user

install apache from source, it goes under /usr/local/apache2

10. /home HomeDirectories

Home directories for all users to store their personalfiles.

For example: /home/john,/home/nikita

11. /boot Boot LoaderFiles

Contains boot loader relatedfiles.

Kernel initrd, vmlinux, grub files are located under/boot

For example: initrd.img-2.6.32-24-generic,vmlinuz-2.6.32-24-generic

Page 62: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

12. /lib SystemLibraries

Contains library files that supports the binaries located under /bin and/sbin

Library filenames are either ld* orlib*.so.*

For example: ld-2.11.1.so,libncurses.so.5.7

13. /opt Optional add-onApplications

opt stands foroptional.

Contains add-on applications from individualvendors.

add-on applications should be installed under either /opt/ or /opt/sub-directory.

14. /mnt MountDirectory

Temporary mount directory where sysadmins can mountfilesystems.

15. /media Removable MediaDevices

Temporary mount directory for removabledevices.

For examples, /media/cdrom for CD-ROM; /media/floppy for floppy

drives;

/media/cdrecorder for CD writer

16. /srv ServiceData

srv stands forservice.

Contains server specific services relateddata.

For example, /srv/cvs contains CVS relateddata.

---------------------------------------------------------------------------------------------------------------

2. Files attributes and I/O operations.

File attributes

Attribute valuemeaning

Filetype type of thefile

Page 63: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Accesspermission file access permission for owner, groupandothersHardlinkcount

no.of hard links of afile.

UID file owner userID.

GID the file groupID.

Filesize file size inbytes.

Inode number system inode number of the file.

File system ID file system ID where the file is stored.

I/O operations:

To provide a higher level interface to device and disk files, UNIIX provides a number of

standard libraries.

Low-level File Access

Each running program, called a process, has associated with it a number of file descriptors.

When a program starts, it usually has three of these descriptors already opened. These are:

Page 64: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

The write system call arranges for the first nbytes bytes from buf to be written to the file

associated with the file descriptor fields.

With this knowledge, let's write our first program, simple_write.c:

Here is how to run the program and its output.

$ simple_write Here is some data

$

read

The read system call reads up to nbytes of data from the file associated with the file

descriptor fields and places them in the data area buf.

This program, simple_read.c, copies the first 128 bytes of the standard input to the standard

output.

Page 65: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

If user run the program, user should see:

$ echo hello there | simple_read hello there

$ simple_read < draft1.txt Files

Open: To create a new file descriptor we need to use the open system call.

openestablishes an access path to a file or device.

The name of the file or device to be opened is passed as a parameter, path, and the oflags

parameter is used to specify actions to be taken on opening the file.

The oflags are specified as a bitwise OR of a mandatory file access mode and other optional

Page 66: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

modes. The open call must specify one of the following file access modes:

The call may also include a combination (bitwise OR) of the following optional modes in the

oflags parameter:

Initial Permissions

When we create a file using the O_CREAT flag with open, we must use the three parameter

form. mode, the third parameter, is made form a bitwise OR of the flags defined in the header

file sys/stat.h. These are:

Page 67: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

For example:

Has the effect of creating a file called myfile, with read permission for the owner and

execute permission for others, and only those permissions.

umask

The umask is a system variable that encodes a mask for file permissions to be used when a

file is created.

User can change the variable by executing the umask command to supply a new value.

The value is a three-digit octal value. Each digit is the results of ANDing values from 1, 2,

or 4.

Page 68: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

For example, to block 'group' write and execute, and 'other' write, the umask would be:

Values for each digit are ANDed together; so digit 2 will have 2 &

1, giving 3. The resulting umask is 032.

Close: We use close to terminate the association between a file

descriptor, fildes, and its file.

----------------------------------------------------------------------------------------------------------------

Page 69: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

3. File permissions and links used in file system.

chmod

Command:

chmod command allows user to alter / Change access rights to files and directories.

File Permission is given for users, group and others as,

SYNTAX:

The Syntax is

chmod [options] [MODE] FileName

File Permission

# File Permission

0 None

1 execute only

2 write only

Read Write Execute

User

Group

Others

Permission 000 Symbolic

Page 70: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

3 write and execute

4 read only

5 read and execute

6 read and write

7 set all permissions OPTIONS:

-c Displays names of only those files whose permissions are being changed

-f Suppress most error messages

-R Change files and directories recursively

EXAMPLE:

1. To view user files with what permission they are: ls-alt

This command is used to view user files with what permission they are.

2. To make a file readable and writable by the group andothers. chmod 066file1.txt

3. To allow everyone to read, write, and execute thefile

chmod 777 file1.txt

ln command is used to create link to a file (or) directory. It helps to provide soft link for

desired files. Inode will be different for source and destination.

SYNTAX:

The Syntax is

ln [options] existingfile(or directory)name newfile(or directory)name

-v Output version information and exit.

Page 71: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

OPTIONS:

-f Link files without questioning the user, even if the mode of target forbids

writing. This is the default if the standard input is not a terminal.-n Does not overwrite existing files.

-s Used to create soft links. EXAMPLE:

1. ln -s file1.txtfile2.txt

Creates a symbolic link to 'file1.txt' with the name of 'file2.txt'. Here inode for

'file1.txt' and 'file2.txt' will be different.

2. ln -s abc xyz

Creates a symbolic link to 'abc' with the name of 'xyz'.

-------------------------------------------------------------------------------------------------------------

4.Directory manipulation functions.

The directory functions are declared in a header file, dirent.h. They use a structure, DIR, as

a basis for directory manipulation.

Here are these functions:

opendir

The opendir function opens a directory and establishes a directory stream.

Page 72: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

readdir

The readdir function returns a

pointer to a structure detailing

the next directory entry in the

directory stream dirp.

The dirent structure containing directory entry details included the following entries:

telldir

The telldir function returns a value

that records the current position in a

directory stream.

Page 73: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

seekdir

The seekdir function sets

the directory entry pointer

in the directory stream

given by dirp.

closedir

The closedir function closes a directory

stream and frees up the resources associated

with it.

-------------------------------------------------------------------------------------------------------------

5) Write about File and record locking. File locking is applicable only for regularfiles.

It allows a process to impose a lock on a file so that other processes can not

modifythe file until it is unlocked by theprocess.

Write lock: it prevents other processes from setting any overlapping read / write

lockson the locked region of afile.

Read lock: it prevents other processes from setting any overlapping write locks onthe

locked region of afile.

Write lock is also called a exclusive lock and read lock is also called a sharedlock.

fcntl API can be used to impose read or write locks on either a segment or

anentirefile.

Functionprototype:

#include<fcntl.h>

All file locks set by a process will be unlocked when the processterminates.

Page 74: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

----------------------------------------------------------------------------------------------------------------

Fill in the Blanks

1. A device file is created in UNIX via the ______ command.

2. _____ stores system administrative files and programs.

3. _____ keeps tracks of all all files.

4. Files are identified by _____.

5. System call used for symbolic links _____.

6. The ____ in a file inode record specifies how many file table entries are

pointing to the file inode record.

7. _____ function sets file pointer to beginning of file.

8. A ____ link is a UNIX path name for a file.

9. _____ sets default file creation mask.

10. _____ access mode flag uses opens the file for read and write.

11. The ____ function helps a user to query or set access control flags and

close on exec flag of any file descriptor.

12. _____ is also called a shared lock.

1 Mknod 7 rewinddir

2 /etc 8 hard

3 Inode table 9 umask

4 Path name 10 O_RDWR

5 Symlink 11 fcntl

6 Reference count 12 read

Page 75: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Multiple Choice

1. The commonly used unix commands are found in______ directories.

a. /etc

b. lib and/usr/lip

c. /bin and/usr/bin

d. /usr/include

2. vi provides the repeat factor by using thefollowing mode commands

a. i/p mode, ex mode

b. ex mode, command mode

c. i/p mode, command mode

d. ex mode

3. What command is used to abort the editingprocess and quit the editing mode without

savingthe buffer

a. :w

b. : x and :wq

c. :q

d. :wq

4. ps options are _______

a. ps e,ps c, ps u

b. ps c, ps e

c. ps f, ps u,ps-a

d. ps f,ps c, ps a

5. Unix provides a _________ command to see howmuch of the disk is being used and

what partof it lies free

a. du

b. df

c. dfspace

d. ulimit

6. du command is used to know_______________

a. know available disk space

Page 76: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

b. disk space used by specified directory

c. disk space used by specified file

d. disk space used by specified files and directories

7. mount o ro mounts a file system in _____ mode

a. remounts in read mode

b. read only

c. remounts in write mode

d. allows binary execution

8. The unix system creates files and directorieswith 666 and 777 as the default

permissions,these default permissions can be changed by________ in the systems

startup scripts.

a. unlink

b. ulimit

c. unmask

d. umask

9. ____________ command restricts on themaximum size of a file that a user is

permitted tocreate

a. unmask

b. umask

c. unlimit

d. ulimit

10. Which command is used to mount file system?

a. mount

b. umount

c. mnt

d. umnt

Page 77: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

1 C 6 D

2 C 7 B

3 C 8 D

4 C 9 D

5 B 10 A

UNIT III

Two Mark Question with Answer:

1)Write about fork API .

fork Function

The only way a new process is created by the UNIX kernel is when an existing process calls

the fork function.

#include<sys/types.h>

#include<unistd.h> pid_t fork(void);

Return: 0 is child, process ID of child in parent, -1 on error.

2) Write the syntax of kill command.

kill COMMAND:

kill command is used to kill the background process.

SYNTAX:

The Syntax iskill [-s] [-l] %pid

3) What is a PATH variable

The PATH variable contains a list of directories (called path prefixes) that are

separated by colors. For example, the name=value environment string

PATH=/bin:/usr/bin:usr/local/bin/:.

Specifies four directories to search, where last one is current working directory.

Page 78: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

4) Write a program using alarm()

5) Write about abort Function

abort function causes abnormal program termination.

#include<stdlib.h> void abort(void);

This function never returns.

This function sends the SIGABRT signal to the process. A process should not ignore this

signal. abort overrides the blocking or ignoring of the signal by the process.

Three Mark Question with Answer:

1. What is a process? List its characteristics?

A process is a running program or script and consists of

program/script itself and

necessary to ensure a correct program flow.

Characteristics of a process are (among others)

t process (PPID),

Page 79: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Normally, when a process has been started from a shell, the shell cannot be used for other

input until the end of the process. But processes and programs can also be run in the

background. To enable this feature, the command line which calls the process/program must

2)Write about fork()

The forkfunction is used to create a process from within a process.

The resultant new process created by fork() is known as child process while the

original process (from which fork() was called) becomes the parent process.

The function fork() is called once (in the parent process) but it returns twice. Once it

returns in the parent process while the second time it returns in the child process.

Syntax: pid_t fork(void);

3)Explain waidpid()?

The function waitpid() that can be used by the parent process to query the change

state of a particular child.

The signature of waitpid() is :

pid_t waitpid(pid_t pid, int *status, int options);

By default, waitpid() waits only for terminated children, but this behavior is

modifiable via the options argument, as described below.

The value of pid can be:

< -1 : Wait for any child process whose process group ID is equal to the absolute

value of pid.

-1 : Wait for any child process.

0 : Wait for any child process whose process group ID is equal to that of the calling

process.

>0 : Wait for the child whose process ID is equal to the value of pid.

Page 80: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

4)Explain exec family?

The exec functions replace the program running in a process with another program.

Functions that contain the letter p in their names (execvp and execlp) accept a

program name and search for a program by that name in the current execution path;

contain the p must be given the full path of the program to be

executed.

Functions that contain the letter v in their names (execv, execvp, and execve) accept

the argument list for the new program as a NULL-terminated array of pointers to

strings.

Functions that contain the letter l (execl, execlp, and execle) accept the argument list

Functions that contain the letter e in their names (execve and execle) accept an

additional argument, an array of environment variables.The argument should be a

NULL-terminated array of pointers to character strings. Each character string should

5)What is a signal?

Signals are mechanisms for communicating with and manipulating processes in

Linux.

A signal is a special message sent to a process. Signals are asynchronous; when a

process receives a signal, it processes the signal immediately, without finishing the

current function or even the current line of code.There are several dozen different

signals, each with a different meaning. Each signal type is specified by its signal

number, but in programs, user usually refer to a signal by its name. In Linux, these are

defined in /usr/include/bits/signum.h.

Page 81: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Five mark Question with Answer:

1. Process and Kernel support for process.

The UNIX process table may be thought of as a data structure describing all of the processes

that are currently loaded.

Viewing Processes

We can see what processes are running by using the ps command. Here is some sample

output:

The PID column gives the PIDs, the TTY column shows which terminal started the process,

the STAT column shows the current status, TIME gives the CPU time used so far and the

COMMAND column shows the command used to start the process. Let's take a closer look at

some of these:

The initial login was performed on virtual console number one (v01). The shell is running

bash. Its status is s, which means sleeping. This is because it's waiting for the X Windows

sytem to finish.

X Windows was started by the command startx. It won't finish until we exit from X. It too is

sleeping.

The fvwm is a window manager for X, allowing other programs to be started and windows to

be arranged on the screen.

This process represents a window in the X Windows system. The shell, bash, is running in

the new window. The window is running on a new pseudo terminal (/dev/ptyp0) abbreviated

pp0.

This is the EMACS editor session started from the shell mentioned above. It uses the pseudo

terminal.

This is a clock program started by the window manager. It's in the middle of a one-minute

Page 82: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

wait between updates of the clock hands.

Process environment

Let's look at some other processes running on this Linux system. The output has been

abbreviated for clarity:

Here we can see one very important process indeed:

In general, each process is started by another, known as its parent process. A process so

started is known as a child process.

When UNIX starts, it runs a single program, the prime ancestror and process number one:

init.

One such example is the login procedure init starts the getty program once for each terminal

that we can use to long in.

These are shown in the ps output like this:

When interacting with user server through a shell session, there are many pieces of

information that user shell compiles to determine its behavior and access to resources. Some

of these settings are contained within configuration settings and others are determined by user

input.

One way that the shell keeps track of all of these settings and details is through an area it

maintains called the environment. The environment is an area that the shell builds every time

that it starts a session that contains variables that define system properties.

In this guide, we will discuss how to interact with the environment and read or set

environmental and shell variables interactively and through configuration files. We will be

using an Ubuntu 12.04 VPS as an example, but these details should be relevant on any Linux

system.

Every time a shell session spawns, a process takes place to gather and compile information

that should be available to the shell process and its child processes. It obtains the data for

these settings from a variety of different files and settings on the system.

Page 83: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Kernel support for process

Basically the environment provides a medium through which the shell process can get or set

settings and, in turn, pass these on to its child processes

The kernel runs the show, i.e. it manages all the operations in a Unix flavored environment.

The kernel architecture must support the primary Unix requirements.

These requirements fall in two categories namely, functions for process management and

functions for file management (files include device files). Process management entails

allocation of resources including CPU, memory, and offers services that processes may need.

The file management in itself involves handling all the files required by processes,

communication with device drives and regulating transmission of data to and from

peripherals. The kernel operation gives the user processes a feel of synchronous operation,

hiding all underlying asynchronism in peripheral and hardware operations (like the time

slicing by clock).

In summary, we can say that the kernel handles the following operations :

1. It is responsible for scheduling running of user and otherprocesses.

2. It is responsible for allocating memory.

3. It is responsible for managing the swapping between memory and disk.

4. It is responsible for moving data to and from the peripherals.

5. It receives service requests from the processes and honors them.

----------------------------------------------------------------------------------------------------------------

2. System call interface for process management.

fork Function

The only way a new process is created by the UNIX kernel is when an existing process calls

the fork function.

#include<sys/types.h>

Page 84: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

#include<unistd.h> pid_t fork(void);

Return: 0 is child, process ID of child in parent, -1 on error

The new process created by fork is called child process. This is called once, but return twice

that is the return value in the child is 0, while the return value in the parent is the process ID

process can have more than one child, so there is no function that allows a process to obtain

the process IDs of its children. The reason fork return 0 to the child is because a process can

have only a single parent, so that child can always call getppid to obtain the process ID of its

parent.

Both the child and parent contain executing with the instruction that follows the call to fork.

heap and stack. This is a copy fo

of memory. Often the parent and child share the text segment, if it is read-only.

There are two users for fork:

1. When a process wants to duplicate itself so that the parent and child can each execute

different sections of code at the same time. This is common for network servers_ the parent

waits for a service requests from a client. When the request arrives, the parent calls fork and

lets the child handle the request. The parent goes back to waiting for the next service request

to arrive.

When a process wants to execute a different program, this is common for shells. In this case

the child does an exec right after it returns from the fork.

vfork Function

The function vfork has the same calling sequence and share return values as fork. But the

semantics of the two functions differ. vfork is intended to create a new process when the

purpose of the new process is to exec a new program. vfork creates the new process, just like

fork, without fully copying the address space of the parent into the child, since the child

the child just calls exec right after the vfork. Instead,

while the child is running, until it calls either exec or exit, the child runs in the address space

of the parent. This optimization provides an efficiency gain on some paged virtual memory

Page 85: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

implementations of UNIX.

Another difference between the two functions is that vfork guarantees that the child runs first,

until the parent resumes.

wait and waitpid Functions

When a process terminates, either normally or abnormally, the parent is notified by the kernel

sending the parent SIGCHLD signal. Since the termination of a child is an asynchronous

event, this signal is the asynchronous notification from the kernel to the parent. The default

action for this signal is to be ignored. A parent may want for one of its children to terminate

A process that calls wait and waitpid can

1. block (if all of its children are still running).

2. return immediately with termination status of a child ( if a child has terminated and is

waiting for its termination status to be fetched) or

3. return immediately with an error (if it down have any child process).

If the process is calling wait because it received SIGCHLD signal, we expect wait to return

immediately. But, if we call it at any random point in time, it can block.

#include<sys/types.h>

#include<sys/wait.h>

pid_t wait(int *statloc);

pid_t waitpid(pid_t pid, int *statloc, int options); Both return: process ID if OK, o or -1 on

error The difference between these two functions is

1. wait can block the caller until a child process terminates, while waitpid has an option

that prevents it from blocking.

2. waitpid does not wait for the first child to terminate, it has a number of options that

control which process it waits for.

Page 86: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

exit Function

There are three ways for a process to terminate normally, and two forms of abnormal

termination.

1. Normal termination:

a. Executing a return from the main function. This is equivalent to calling exit

b. Calling the exit function

c. Calling the _exit function

2. Abnormal termination

a. Calling abort: It generates the SIGABRT signal

b. When the process receives certain signals. The signal can be generated by the process

it-self.

Regardless of how a process terminates, the same code in the kernel is eventually

executed. This kernel code closes all the open descriptors for the process, releases the

memory.

----------------------------------------------------------------------------------------------------------------

3. What is Signal? List different types of signals?

A signal is an event generated by the UNIX system in response to some condition, upon

receipt of which a process may in turn take some action. Signal names are defined in the

header file signal.h. They all begin with SIG and include:

Additional signals include:

If the shell and terminal driver are configured normally, typing the interrupt character

(Ctrl-C) at the keyboard will result in the SIGINT signal being sent to the foreground

process. This will cause the program to terminate.

----------------------------------------------------------------------------------------------------------------

4. Define reliable signals and unreliable signals? Explain functions kill(), raise()?

Signal Functions

The system calls related to signals are explained in the following sections.

Unreliable signals

The signals could get lost a signal could occur and the process would never know about it.

Page 87: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Here, the process has little control over a signal, it could catch the signal or ignore it, but

blocking of a signal is not possible.

Reliable signals

Linux supports both POSIX reliable signals (hereinafter "standard signals") and POSIX real-

time signals.

kill and raise Functions

The kill function sends a signal to a process or a group of processes. The raise function

allows a process to send a signal to itself.

#include<sys/types.h>

#include<signal.h>

int kill(pid_t pid, int signo); int raise(int signo);

Both return: 0 if OK, -1 on error

There are four different conditions for the pid argument to kill: pid > 0 The signal is sent to

the process whose process ID is pid.

pid = 0 The signal is sent to all processes whose process group ID equals the process

group ID of the sender and for which the sender has permission to send thesignal.

pid<0 The signal is sent to all processes whose process group ID equals the absolute value

of pid and for which the sender has permission to send the signal.

pid = -1 unspecified.

----------------------------------------------------------------------------------------------------------------

5. Write short notes on alarm(), pause(), abort(), sleep()?

alarm and pause Functions

The alarm function allows us to get a timer that will expire at a specified time in the future.

signal, its default action is to terminate the process.

#include<unistd.h>

unsigned int alarm(unsigned int seconds);

Page 88: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Returns: 0 or number of seconds until previously set alarm.

The seconds value is the number of clock seconds in the future when the signal should be

generated. There is only one of the alarm clocks per process. If, when we call alarm, there is a

previously registered alarm clock for the process that has not yet expired, the number of

seconds left for that alarm clock to return as the value of this function. That previously

registered alarm clock is replaced by the new value.

If there is a previously registered alarm clock for the process that has not yet expired and if

that previous alarm clock is still returned as the value of the function.

Although the default action for SIGALRM is terminating the process, most processes use an

alarm clock catch this signal.

The pause function suspends the calling process until a signal is caught.

#include<unistd.h> int pause(void);

Returns: -1 with errno set to EINTR

The only time pause returns is if a signal handler is executed and that handler returns. In that

case, pause returns -1 with errno set to EINTR.

abort Function

abort function causes abnormal program termination.

#include<stdlib.h> void abort(void);

This function never returns.

This function sends the SIGABRT signal to the process. A process should not ignore

this signal. abort overrides the blocking or ignoring of the signal by the process.

Page 89: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

sleep Function

#include<unistd.h>

unsigned int sleep(unsigned int seconds);

Returns: 0 or number of unslept seconds.

This function causes the calling process to be suspended until either:

1. The amount of clock that is specified by seconds has elapsed or

2. A signal is caught by the process and the signal handler returns.

In case 1 the return value is 0 when sleep returns early, because of some signal being caught

case 2, the return value is the number of unslept seconds.

Sleep can be implemented with an alarm function. If alarm is used however, there can be

interaction between the two functions.

----------------------------------------------------------------------------------------------------------------

Fill in the blanks

1. A ____ has a Process Table that keeps track of all active process.

2. Process table contains ___, ___, ___ segments.

3. ___ is an integer identification number that is unique per process in an entire

operating system.

4. The _____ system call is used to create a child process.

5. The pipe system creates a communication channel between two _____ process.

6. The important aspect of process control is ______.

7. _____ are triggered by events.

8. SIGTERM used for _____.

9. The sleep function is only one use of ____ API.

10. The primary use of signals is for _____.

1 Kernel 6 Signal handling

2 Text, data, stack 7 Signals

3 PID 8 Process termination

4 Fork 9 Alarm

5 Related 10 Process control

Page 90: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

UNIT-IV

Two mark Question with Answer

1)write semaphore datastructure

2) What are pipes

A pipe is a communication device that permits unidirectional communication. Data

devices; the data is always read from the pipe in the same order it was written. Typically,

a pipe is used to communicate between two threads in a single process or between parent

and child processes.

In a shell, the symbol | creates a pipe.For example, this shell command causes the

shell to produce two child processes, one for ls and one for less:

% ls | less

3) What is IPC?

Interprocess Communication-

Interprocess communication (IPC) includes thread synchorization and data

exchange between threads beyond the process boundaries. If threads belong

to the same process, they execute in the same address space, i.e. they can

access global (static) data or heap directly, without the help of the

operating system. However, if threads belong to different processes, they

cannot access each others address spaces without the help of the operating

Page 91: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

system.

4) Write about mkfifo()

int mkfifo ( const char *pathname, mode_t mode );

-

(mode specifies the FIFO's permissions,

as common in UNIX-like file systems).

-

a different way. Instead of being an anonymous

communications channel, a FIFO special file is

entered into the file system by calling mkfifo()

5) What isMessagequeue?

This is an easy way of passing message between two process. It provides a

way of sending a block of data from one process to another. The main

advantage of using this is, each block of data is considered to have a type, and a

receiving process receives the blocks of data having different type

valuesindependently.

Five mark Question with Answer

1. Explain Inter Process Communication?

Inter-process Communication- -process communication (IPC) is the

Processes communicate with each other and with the kernel to coordinate their

activities. Linux supports a number of Inter-Process Communication (IPC)

mechanisms. Signals and pipes are two of them but Linux also supports the System

V IPC mechanisms named after the Unix release in which they first appeared.

Inter Process Communication (IPC) includes thread synchronization and data

exchange between threads beyond the process boundaries. If threads belong to the

same process, they execute in the same address space, i.e. they can access global

(static) data or heap directly, without the help of the operating system. However, if

Page 92: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

threads belong to different processes, they cannot access each other address spaces

without the help of the operating system.

There are two fundamentally different approaches in IPC:

Processes are residing on the samecomputer

Processes are residing on differentcomputers

The first case is easier to implement because processes can share memory either in

the user space or in the system space. This is equally true for uniprocessors and

multiprocessors.

In the second case the computers do not share physical memory, they are connected

via I/O device (for example serial communication or Ethernet). Therefore the

processes residing in different computers can not use memory as a means for

communication.

IPC between processes on a Single System

Most of this chapter is focused on IPC on a single computer system, including

four general approaches:

Sharedmemory

Messages

Pipes

Sockets

----------------------------------------------------------------------------------------------------------------

2. What are pipes? Explain named and unnamed pipes?

Pipes

Pipes are used to allow one or more processes to have information "flow" between them. The

most common example of this is with the shell.

$ ls | wc -l

Page 93: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

-out from the left side (ls) is connected to the std-in on the right side

(wc -l).As far the each program is concerned, it is reading or writing as it normally does. Both

processes are running concurrently.

There are 2 types of pipes:

unnamed pipes

named pipes

The examples we seen at the shell command line are unnamed. They are created, used and

One end is for reading and one end is for writing. When you are done with a pipe, it is closed

like any other file.

Creating unnamed pipes

#include <unistd.h>

int pipe(int fd[2]);

Returnsfile descriptors in the fd array.

fd[0] is for read

fd[1] write

Returns 0 on successful creation of pipe and -1 otherwise.

Each end of the pipe is closed individually using normal close() system call. Pipes are only

Reading from pipes

When reading from a pipe:

read() will return 0 (end of file) when the write end of the pipe is closed. if write end of the is

still open and there is no data, read() will block until input become available. if a read() tries

to get more data than is currently in pipe, read() will only contain the number of bytes

actually read. Subsequent reads will block until more data are available.

Writing to pipes

When writing to a pipe:

If read end of pipe is closes, a write() will fail and process will be sent SIGPIPE signal.

Default SIGPIPE handler terminates.

Page 94: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Named pipes

exist as special files

within a file system. (file type p) They exist until they are removed with rm or unlink() They

can be used with unrelated process not just descendants of the pipe creator.

Created with:

mknod utility

mknod() system call

Creating named pipes

From the shell

$ mknod mypipe p

From a C program

mknod ( "mypipe", SIFIFO, 0 );

Either way you create it, this will result in a special file being created on the filesystem.

$ ls -l mypipe

prw-r r-- 1 srs users 0 Nov 6 22:28 mypipe

Once a named pipe is created, processes can open(), read() and write() them just like any

other file. Unless you specify O_NONBLOCK, or O_NDELAY, on the open:

opens for reading will block until a process opens if for writing.

opens for writing will block until a process opens it for reading.

3. Write short notes on semaphore and mutex.

Semaphores

Semaphores are used to protect critical regions of code or data structures. Remember

that each access of a critical piece of data such as a VFS inode describing a directory is

made by kernel code running on behalf of a process. It would be very dangerous to

allow one process to alter a critical data structure that is being used by another process.

One way to achieve this would be to use a buzz lock around the critical piece of data

that is being accessed, but this is a simplistic approach that would degrade system

performance.

Instead Linux uses semaphores to allow just one process at a time to access critical

regions of code and data; all other processes wishing to access this resource will be

made to wait until it becomes free. The waiting processes are suspended, other

processes in the system can continue to run as normal.

Page 95: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

A Linux semaphore data structure contains the following information:

count

This field keeps track of the count of processes wishing to use this resource. A positive

value means that the resource is available. A negative or zero value means that

processes are waiting for it. An initial value of 1, means that one and only one process

at a time can use this resource. When processes want this resource they decrement the

count and when they have finished with this resource they increment the count.

waking

This is the count of processes waiting for this resource which is also the number of

process waiting to be awakened when this resource becomes free.

wait queue

When processes are waiting for this resource they are put onto this wait queue,

Suppose the initial count for a semaphore is 1, the first process to come along will see

that the count is positive and decrement it by 1, making it 0. The process now ``owns''

the critical piece of code or resource that is being protected by the semaphore. When

optimal case is where there are no other processes contending for ownership of the

critical region. Linux has implemented semaphores to work efficiently for this, the

most common case.

Mutexes:

Mutexes are used to prevent data inconsistencies due to race conditions. A race

condition often occurs when two or more threads need to perform operations on the

same memory area, but a result of computations depends on the order in which these

operations are performed. Mutexes are used for serializing shared resources. Anytime a

global resource is accessed by more than one thread the resource should have a Mutex

associated with it. One can apply a mutex to protect a segment of memory ("critical

region") from other threads. Mutexes can be applied only to threads in a single process

and do not work between processes as do semaphores

----------------------------------------------------------------------------------------------------------------

4. Explainmessage queue from System V?

The message queue from System V

A process can invoke msgsnd() to send a message. User needs to pass the IPC

identifier of the receiving message queue, the size of the message and a message structure,

Page 96: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

including the message type and text.

On the other side, a process invokes msgrcv() to receive a message, passing the IPC

identifier of the message queue, where the message should get stored, the size and a

value t. t specifies the message returned from the queue, a positive value means the first

message with its type equal to t is returned, a negative value returns the last message equal

to type t and zero returns the first message of the queue.

Those functions are defined in include/linux/msg.h and implemented in ipc/msg.c

There are limitations upon the size of a message (max), the total number of

messages (mni) and the total size of all messages in the queue (mnb):

$ sysctl kernel.msg{max,mni,mnb}

kernel.msgmax = 8192

kernel.msgmni = 1655

kernel.msgmnb = 16384

.

POSIX Message Queue

The POSIX standard defines a message queue mechanism based on System V IPC's

message queue, extending it by some functionalities:

Simple file-based interface to the application

Support for message priorities

Support for asynchronous notification

Timeouts for blocking operations.

5. List APIs for semaphore?

Semaphores are not used to exchange a large amount of data. Semaphores are used

synchronization among processes. Other synchronization mechanisms include record

locking and mutexes.

1) The semaphore is stored in the kernel: Allows atomic operations on the

semaphore. Processes are prevented from indirectly modifying the value.

2) A process acquires the semaphore if it has a value of zero. The value of the

semaphore is then incremented to 1. When a process releases the semaphore, the

value of the semaphore is decremented.

3) If the semaphore has non-zero value when a process tries to acquire it, that

process blocks.

Page 97: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

4) In point 2 and 3, the semaphore acts as a customer counter. In most cases, it is

a resource counter.

until the semaphore is available. This is better (more efficient) than busy waiting

such as TEST&SET.

6) The kernel maintains information on each semaphore internally, using a data

structure struct semis_ds that keeps track of permission, number of semaphores,

etc.

7) Apparently, a semaphore in Unix is not a single binary value, but a set of

nonnegative integer values

8) There are 3 (logical) types of semaphores:

Binary semaphore have a value of 0 or 1. Similar to a mutex lock. 0 means

locked; 1 means unlocked.

Counting semaphore

producer-consumer example. Note that value =0 is similar to a lock (resource

not available).

Set of counting semaphores one or more semaphores, each of which is a

counting semaphore.

9) There are 2 basic operations performed with semaphores:

Wait waits until the semaphore is > 0, then decrements it.

Post increments the semaphore, which wakes waiting processes.

A semaphore set is created using

int semget(key_t key, int nsems, int semflag);

-- returns int semid; the id of the semaphore set; -1 on error.

nsems # of semaphores, use multiple semaphores for multiple resources.

semflag Same as msgflag, sets permission and creation options.

Page 98: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

The System V IPC semaphore

functions

are semget(), semop() and semc

tl().

semget() - Create a new

semaphore set or access an

existing set with

the semget() system call.

semop() - Perform semaphore operations.

semctl() - Change its ownership or permissions if user are the semaphore creator.

POSIX semaphores are much lighter weight than are System V semaphores. A POSIX

semaphore structure defines a single semaphore, not an array of semaphores. The POSIX

semaphore functions are:

sem_open() - Connects to, and optionally creates, a named semaphore.

sem_init() - Initializes a semaphore structure (internal to the calling program, so not a

named semaphore).

sem_close() - Ends the connection to an open semaphore.

sem_unlink() - Ends the connection to an open semaphore and removes the semaphore

when the last process closes it.

sem_destroy() - Initializes a semaphore structure (internal to the calling program, so not a

named semaphore).

sem_getvalue() - Copies the value of the semaphore into the specified integer

sem_wait(), sem_trywait() - Blocks while the semaphore is held by other processes or

returns an error if the semaphore is held by another process.

sem_post() - Increments the count of the semaphore.

Page 99: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Multiple Choice:

Fill in the Blanks

1. _____ is a mechanism whereby two or more processes communicate with each

other to perform tasks.

2. BSD UNIX provides ______ for processes running on different machines to

communicate.

3. ______ allows processes on the same machine to exchange formatted data.

4. msgctl used to manipulate the _____ of the message queue.

5. Message API msgctl uses _____ file APIs.

6. The _____ function returns the number of bytes written to the mtext buffer.

7. ____ provides a method to synchronize the execution of multiple processes.

8. A process can also use multiple ______ sets.

9. The semaphore table entry data type is ______.

10. The _____ header defines a data type struct semid_ds.

1 Inter Process Communication 6 msgrcv

2 Socket 7 semaphore

3 Messages 8 Semaphore

4 Control data 9 Struct semid_ds

5 Stat, unlink, chmod, chown 10 Sys/sem.h

Page 100: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

UNIT-V

Five mark Question with Answer:

1)Write about kernel support for Shared memory?

Shared Memory:

Shared memory is a highly efficient way of data sharing between the

running programs. It allows two unrelated processes to access the same

logical memory. It is the fastest form of IPC because all processes share

the same piece of memory. I also avoids copying data unnecessarily.

As kernel does not synchronize the processes, it should be handled by the

user. Semaphore can also be used to synchronize the access to shared

memory.

Usage of shared memory:

To use the shared memory, first of all one process should allocate the

segment, and then each process desiring to access the segment should attach

the segment. After accessing the segment, each process should detach it.

It is also necessary to deallocatethe segment without fail.

Allocating the shared memory causes virtual pages to be created. It is

important to note that allocating the existing segment would not create new

pages, but will return the identifier for the existing pages.

All the shared memory segments are allocated as the integral multiples of

the system's page size, which is the number of bytes in a page of memory.

Unix kernel support for shared memory

There is a shared memory table in the kernel address space that keeps

track of allshared memory regions created in thesystem.

Each entry of the tables store the followingdata:

Page 101: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

1. Name

2. Creator user ID and groupID.

3. Assigned owner user ID and groupID.

4. Read-write access permission of theregion.

5. The time when the last process attached to theregion.

6. The time when the last process detached from theregion.

7. The time when the last process changed control data of theregion.

8. The size, in no. of bytes of theregion.

--------------------------------------------------------------------------------------------------------------

2)Explain APIs for shared memory?

shmget

Open and create a sharedmemory.

Functionprototype:

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/shm.h>

int shmget ( key_t key, int size, int flag);

Function returns a positive descriptor if it succeeds or -1 if itfails.

Shmat

Attach a shared memory to a process virtual addressspace.

Functionprototype:

#include<sys/types.h>

#include<sys/ipc.h>

Page 102: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

#include<sys/shm.h>

void * shmat ( int shmid, void *addr, int flag );

Function returns the mapped virtual address of he shared memory if it

succeeds or -1 ifit fails.

Shmdt

Detach a shared memory from the process virtual addressspace.

Functionprototype:

#include<sys/types.h>

#include<sys/ipc.h>

#include<sys/shm.h>

int shmdt ( void *addr );

Function returns 0 if it succeeds or -1 if itfails.

Shmctl

Query or change control data of a shared memory or deletethememory.

Functionprototype:

#include<sys/types.h>

#include<sys/ipc.h>

Page 103: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

#include<sys/shm.h>

int shmctl ( int shmid, int cmd, struct shmid_ds *buf );

Function returns 0 if it succeeds or -1 if itfails.

3)Explain Berkeley Socket?

Sockets allow communication between two different processes on the same or different

machines. To be more precise, it's a way to talk to other computers using standard Unix

file descriptors. In Unix, every I/O action is done by writing or reading a file descriptor. A

file descriptor is just an integer associated with an open file and it can be a network

connection, a text file, a terminal, or something else.

To a programmer, a socket looks and behaves much like a low-level file descriptor. This

is because commands such as read() and write() work with sockets in the same way they

do with files and pipes.

Sockets were first introduced in 2.1BSD and subsequently refined into their current form

with 4.2BSD. The sockets feature is now available with most current UNIX system

releases.

A Unix Socket is used in a client-server application framework. A server is a process that

performs some functions on request from a client. Most of the application-level protocols

like FTP, SMTP, and POP3 make use of sockets to establish connection between client and

server and then for exchanging data.

Processes are presumed to communicate only between sockets of the same type but there

is no restriction that prevents communication between sockets of different types.

Stream Sockets

through the stream socket three items "A, B, C", they will arrive in the same order

transmission. If delivery is impossible, the sender receives an error indicator. Data

records do not have any boundaries.

Datagram Sockets

Page 104: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

They're connectionless because you don't need to have an open connection as in

out. They use UDP (User Datagram Protocol).

--------------------------------------------------------------------------------------------------------------

4)Explain Socket address structure?

sockaddr in

The second structure that helps you to reference to the

struct sockaddr_in {

short int sin_family;

unsigned short int sin_port;

struct in_addr sin_addr;

unsigned char sin_zero[8];

};

Attribute Values Description

sa_family

AF_INET

AF_UNIX

AF_NS

AF_IMPLINK

It represents an address family. In most

of the Internet-based applications, we

use AF_INET.

sin_port Service Port A 16-bit port number in Network Byte

Order.

Page 105: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

sin_addr IP Address A 32-bit IP address in Network Byte

Order.

sin_zero Not Used You just set this value to NULL as this

is not being used.

in addr

This structure is used only in the above structure as a structure field and holds 32 bit

netid/hostid.

struct in_addr {

unsigned long s_addr;

};

Attribute Values Description

s_addr service

port

A 32-bit IP address in Network Byte

Order.

Hostent

This structure is used to keep information related to host.

struct hostent {

char *h_name;

char **h_aliases;

int h_addrtype;

int h_length;

char **h_addr_list

Page 106: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

#define h_addr h_addr_list[0]

};

Attribute Values Description

h_name ti.com

etc.

It is the official name of the host. For example,

tutorialspoint.com, google.com, etc.

h_aliases TI It holds a list of host name aliases.

h_addrtype AF_INET It contains the address family and in case of Internet

based application, it will always be AF_INET.

h_length 4 It holds the length of the IP address, which is 4 for

Internet Address.

h_addr_list in_addr For Internet addresses, the array of pointers

h_addr_list[0], h_addr_list[1], and so on, are points to

structure in_addr.

NOTE

servent

This particular structure is used to keep information related to service and associated

ports.

struct servent {

char *s_name;

char **s_aliases;

Page 107: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

int s_port;

char *s_proto;

};

Here is the description of the member fields

Attribute Values Description

s_name http This is the official name of the service. For

example, SMTP, FTP POP3, etc.

s_aliases ALIAS It holds the list of service aliases. Most of the

time this will be set to NULL.

s_port 80 It will have associated port number. For

example, for HTTP, this will be 80.

s_proto TCP

UDP

It is set to the protocol used. Internet services

are provided using either TCP or UDP.

--------------------------------------------------------------------------------------------------------------

5)List socket system calls?

int socket(int domain, int type, int protocol)

Domain: protocol family

Type: semantics of the communication

Page 108: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Protocol: specific protocol

Connect to Server:

int connect(int sockfd, struct sockaddr *server_address, socketlen_t addrlen)

-1 if an error occurs

Sending data

-1 on error

Receiving data

o place the data, size of the buffer

-1 on error

Closing the socket

Bind socket to the local address and port

y_addr, socklen_t addrlen)

-1 if an error occurs

Define the number of pending connections

Page 109: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

d acceptable backlog

-1 on error

List is a summary of functions or methods provided by the Berkeley sockets

API library:

socket() creates a new socket of a certain socket type, identified by an

integernumber, and allocates system resources toit.

bind() is typically used on the server side, and associates a socket with

a socketaddress structure, i.e. a specified local port number and IPaddress.

listen() is used on the server side, and causes a bound TCP socket to enter

listeningstate.

connect() is used on the client side, and assigns a free local port

numberto a socket.In case of a TCP socket, it causes an attempt to

establish a new TCPconnection.

accept() is used on the server side. It accepts a received

incoming attempt to create a new TCP connection from the

remote client, and creates a new socket associated with the

socket address pair of thisconnection.

send() and recv(), or write() and read(), or sendto() and

recvfrom(), are used forsending and receiving data to/from a

remotesocket.

close() causes the system to release resources allocated to a

socket. In case of TCP,the connection isterminated.

gethostbyname() and gethostbyaddr() are used to resolve host

names and addresses.IPv4 only.

select() is used to pend, waiting for one or more of a

provided list of sockets to beready to read, ready to write, or

that haveerrors.

poll() is used to check on the state of a socket in a set of

sockets. The set can be testedto see if any socket can be

written to, read from or if an erroroccurred.

getsockopt() is used to retrieve the current value of a

Page 110: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

particular socket option forthe specifiedsocket.

setsockopt() is used to set a particular socket option for the

specifiedsocket.

Fill in the Blanks

1. Shared memory allows multiple processes to map a portion of their _____

addresses to a common memory region.

2. The ______ header defines a struct ipc_perm data type.

3. ______ API used to detach a shared memory from the process virtual address

space.

4. The shmget opens a shared memory whose ______ is specified in key argument.

5. A ______ can be addressed by a host Internet address or a port number.

6. Sockets may be ______ and _______.

7. _______ socket API used to assigns a name to a socket.

8. listen system call specifies the number of pending ______ messages that can be

queued for a ______ socket.

9. The socket function creates a socket of the specified ______, _______ and ______.

10. The connect system call is called in a client process in requesting a connection to a

______ socket.

1 Virtual 6 Connection based,

Connection less

2 Sys/ipc.h 7 Bind

3 Shmdt 8 Client, server

4 Key ID 9 Domain, type, protocol

5 Socket 10 server

Page 111: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

17. BEYOND SYLLABUS TOPICS WITH MATERIAL

Vi Editor

It's usually available on all the flavors of Unix system.

Its implementations are very similar across the board.

It requires very few resources.

It is more user-friendly than other editors such as the ed or the ex.

You can use the vi editor to edit an existing file or to create a new file from scratch. You

can also use this editor to just read a text file.

Starting the vi Editor

S.No. Command & Description

1 vi filename

Creates a new file if it already does not exist, otherwise opens an existing file.

2 vi -R filename

Opens an existing file in the read-only mode.

3 view filename

Opens an existing file in the read-only mode.

Following is an example to create a new file testfile if it already does not exist in the

$vi testfile

Page 112: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

|

~

~

~

~

~

~

~

~

~

~

~

~

"testfile" [New File]

You will notice a tilde (~) on each line following the cursor. A tilde represents an unused

line. If a line does not begin with a tilde and appears to be blank, there is a space, tab,

newline, or some other non-viewable character present.

You now have one open file to start working on. Before proceeding further, let us

understand a few important concepts.

Operation Modes

Command mode

saving the files, executing the commands, moving the cursor, cutting (yanking)

and pasting the lines or words, as well as finding and replacing. In this mode,

whatever you type is interpreted as a command.

Insert mode

typed in this mode is interpreted as input and placed in the file.

vi always starts in the command mode. To enter text, you must be in the insert mode for

which simply type i. To come out of the insert mode, press the Esc key, which will take

you back to the command mode.

Hint

Page 113: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

you to the command mode. You open a file using the vi editor. Start by typing some

characters and then come to the command mode to understand the difference.

Getting Out of vi

The command to quit out of vi is :q. Once in the command mode, type colon, and 'q',

followed by return. If your file has been modified in any way, the editor will warn you of

this, and not let you quit. To ignore this message, the command to quit out of vi without

saving is :q!. This lets you exit vi without saving any of the changes.

The command to save the contents of the editor is :w. You can combine the above

command with the quit command, or use :wq and return.

The easiest way to save your changes and exit vi is with the ZZ command. When you

are in the command mode, type ZZ. The ZZ command works the same way as

the :wq command.

If you want to specify/state any particular name for the file, you can do so by specifying it

after the :w. For example, if you wanted to save the file you were working on as another

filename called filename2, you would type :w filename2 and return.

Moving within a File

To move around within a file without affecting your text, you must be in the command

mode (press Esc twice). The following table lists out a few commands you can use to

S.No. Command & Description

1 k

Moves the cursor up one line

2 j

Moves the cursor down one line

3 h

Page 114: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Moves the cursor to the left one character position

4 l

Moves the cursor to the right one character position

vi is case-sensitive. You need to pay attention to capitalization when using the

commands.

Most commands in vi can be prefaced by the number of times you want the action

to occur. For example, 2j moves the cursor two lines down the cursor location.

There are many other ways to move within a file in vi. Remember that you must be in the

command mode (press Esc twice). The following table lists out a few commands to move

Given below is the list of commands to move around the file.

Control Commands

The following commands can be used with the Control Key to performs functions as

given in the table below

Given below is the list of control commands.

Editing Files

To edit the file, you need to be in the insert mode. There are many ways to enter the insert

S.No. Command & Description

1 i

Inserts text before the current cursor location

2 I

Inserts text at the beginning of the current line

Page 115: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

3 a

Inserts text after the current cursor location

4 A

Inserts text at the end of the current line

5 o

Creates a new line for text entry below the cursor location

6 O

Creates a new line for text entry above the cursor location

Deleting Characters

Here is a list of important commands, which can be used to delete characters and lines in

S.No. Command & Description

1 x

Deletes the character under the cursor location

2 X

Deletes the character before the cursor location

3 dw

Deletes from the current cursor location to the next word

4 d^

Deletes from the current cursor position to the beginning of the line

Page 116: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

5 d$

Deletes from the current cursor position to the end of the line

6 D

Deletes from the cursor position to the end of the current line

7 dd

Deletes the line the cursor is on

As mentioned above, most commands in vi can be prefaced by the number of times you

want the action to occur. For example, 2x deletes two characters under the cursor location

and 2dd deletes two lines the cursor is on.

It is recommended that the commands are practiced before we proceed further.

Change Commands

You also have the capability to change characters, words, or lines in vi without deleting

S.No. Command & Description

1 cc

Removes the contents of the line, leaving you in insert mode.

2 cw

Changes the word the cursor is on from the cursor to the lowercase w end of the word.

3 r

Replaces the character under the cursor. vi returns to the command mode after the

replacement is entered.

4 R

Page 117: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Overwrites multiple characters beginning with the character currently under the

cursor. You must use Esc to stop the overwriting.

5 s

Replaces the current character with the character you type. Afterward, you are left in

the insert mode.

6 S

Deletes the line the cursor is on and replaces it with the new text. After the new text is

entered, vi remains in the insert mode.

Copy and Paste Commands

You can copy lines or words from one place and then you can paste them at another place

S.No. Command & Description

1 yy

Copies the current line.

2 yw

Copies the current word from the character the lowercase w cursor is on, until the end

of the word.

3 p

Puts the copied text after the cursor.

4 P

Page 118: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Puts the yanked text before the cursor.

Advanced Commands

There are some advanced commands that simplify day-to-day editing and allow for more

Given below is the list advanced commands.

Word and Character Searching

The vi editor has two kinds of searches: string and character. For a string search,

the / and ? commands are used. When you start these commands, the command just typed

will be shown on the last line of the screen, where you type the particular string to look

for.

The / command searches forwards (downwards) in the file.

The ? command searches backwards (upwards) in the file.

The n and N commands repeat the previous search command in the same or the opposite

direction, respectively. Some characters have special meanings. These characters must be

preceded by a backslash (\) to be included as part of the search expression.

S.No. Character &Description

1 ^

Searches at the beginning of the line (Use at the beginning of a search expression).

2 .

Matches a single character.

3 *

Matches zero or more of the previous character.

Page 119: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

4 $

End of the line (Use at the end of the search expression).

5 [

Starts a set of matching or non-matching expressions.

6 <

This is put in an expression escaped with the backslash to find the ending or the

beginning of a word.

7 >

This helps see the '<' character description above.

The character search searches within one line to find a character entered after the

command. The f and F commands search for a character on the current line

only. f searches forwards and F searches backwards and the cursor moves to the position

of the found character.

The t and T commands search for a character on the current line only, but for t, the cursor

moves to the position before the character, and T searches the line backwards to the

position after the character.

Set Commands

You can change the look and feel of your vi screen using the following :setcommands.

Once you are in the command mode, type :set followed by any of the following

commands.

S.No. Command & Description

1 :set ic

Ignores the case when searching

Page 120: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

2 :set ai

Sets autoindent

3 :set noai

Unsets autoindent

4 :set nu

Displays lines with line numbers on the left side

5 :set sw

Sets the width of a software tabstop. For example, you would set a shift width of 4

with this command :set sw = 4

6 :set ws

If wrapscan is set, and the word is not found at the bottom of the file, it will try

searching for it at the beginning

7 :set wm

If this option has a value greater than zero, the editor will automatically "word wrap".

For example, to set the wrap margin to two characters, you would type this: :set wm =

2

8 :set ro

Changes file type to "read only"

9 :set term

Prints terminal type

10 :set bf

Page 121: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Discards control characters from input

Running Commands

The vi has the capability to run commands from within the editor. To run a command, you

only need to go to the command mode and type :! command.

For example, if you want to check whether a file exists before you try to save your file

with that filename, you can type :! ls and you will see the output of lson the screen.

You can press any key (or the command's escape sequence) to return to your vi session.

Replacing Text

The substitution command (:s/) enables you to quickly replace words or groups of words

:s/search/replace/g

The g stands for globally. The result of this command is that all occurrences on the

cursor's line are changed.

Important Points to Note

You must be in command mode to use the commands. (Press Esc twice at any time

to ensure that you are in command mode.)

You must be careful with the commands. These are case-sensitive.

You must be in insert mode to enter text.

Opening multiple files with VI/VIM editor

To open multiple files, command would be same as is for a single file; we just add the file name for second file as well.

$ vi file1 file2 file 3

Now to browse to next file, we can use

$ :n

or we can also use

$ :e filename

Page 122: 14. ASSIGNMENT TOPICS WITH MATERIALSkgr.ac.in/beta/wp-content/uploads/2018/09/LP_Course_File-converted… · 14. ASSIGNMENT TOPICS WITH MATERIALS UNIT-I 1. LINUX UTILITIES The following

Linux Programming

Run external commands inside the editor

We can run external Linux/Unix commands from inside the vi editor, i.e. without exiting the editor. To issue a command from editor, go back to Command Mode if in Insert mode & we

command is,

$ :! command

An example for this would be

$ :! df -H

Searching for a pattern

To search for a word or pattern in the text file, we use following two commands in command mode,

command searches the pattern in forward direction

command searched the pattern in backward direction

Both of these commands are used for same purpose, only difference being the direction they search in. An example would be,

$ :/ search pattern (If at beginning of the file)

$ search pattern (If at the end of the file)