Top Most Unix Interview Questions Part – 2
1.How are devices represented in UNIX?
All devices are represented by files called special files that are located in/dev directory.Thus,device files and other files are named and accessed in the same way.
A ‘regular file’ is just an ordinary data file in the disk.A ‘block special file’ represents a device with characteristics similar to a disk (data transfer in terms of blocks).A ‘character special file’ represents a device with characteristics similar to a keyboard (data transfer is by stream of bits in sequential order).
2.Explain briefly about the directory representation in UNIX .
A Unix directory is a file containing a correspondence between filenames and inodes.A directory is a special file that the kernel maintains.Only kernel modifies directories,but processes can read directories.
The contents of a directory are a list of filename and inode number pairs.When new directories are created,kernel makes two entries named ‘.’ (refers to the directory itself) and ‘..’ (refers to parent directory).System call for creating directory is mkdir (pathname,mode).
3.How to switch to a super user status to gain privileges?
Use ‘su’ command.The system asks for password and when valid entry is made the user gains super user (admin) privileges.
4.What are shell variables?
Shell variables are special variables,a name-value pair created and maintained by the shell.
Example:
PATH,HOME,MAIL and TERM
5.What is redirection?
Directing the flow of data to the file or from the file for input or output.
Example :
ls > wc
6.How to terminate a process which is running and the specialty on command kill 0?
With the help of kill command we can terminate the process.Syntax: kill pid
Kill 0 – kills all processes in your system except the login shell
7.What is a pipe and give an example?
A pipe is two or more commands separated by pipe char ‘|’.That tells the shell to arrange for the output of the preceding command to be passed as input to the following command.
Example :
ls -l | pr
The output for a command ls is the standard input of pr.When a sequence of commands are combined using pipe,then it is called pipeline.
8.What scheme does the Kernel in Unix System V follow while choosing a swap device among the multiple swap devices?
Kernel follows Round Robin scheme choosing a swap device among the multiple swap devices in Unix System V.
9.What is a Map?
A Map is an Array,which contains the addresses of the free space in the swap device that are allocatable resources,and the number of the resource units available there.
This allows First-Fit allocation of contiguous blocks of a resource.Initially the Map contains one entry – address (block offset from the starting of the swap area) and the total number of resources.Kernel treats each unit of Map as a group of disk blocks.On the allocation and freeing of the resources Kernel updates the Map for accurate information.
10.What is the main goal of the Memory Management?
It decides which process should reside in the main memory,Manages the parts of the virtual address space of a process which is non-core resident,Monitors the available main memory and periodically write the processes into the swap device to provide more processes fit in the main memory simultaneously.
11.What is the difference between Swapping and Paging?
Swapping:
Whole process is moved from the swap device to the main memory for execution.Process size must be less than or equal to the available main memory.It is easier to implementation and overhead to the system.Swapping systems does not handle the memory more flexibly as compared to the paging systems.
Paging:
Only the required memory pages are moved to main memory from the swap device for execution.Process size does not matter.Gives the concept of the virtual memory.It provides greater flexibility in mapping the virtual address space into the physical memory of the machine.
Allows more number of processes to fit in the main memory simultaneously.Allows the greater process size than the available physical memory.Demand paging systems handle the memory more flexibly.
12.How do you execute one program from within another?
The system calls used for low-level process creation are execlp() and execvp().The execlp call overlays the existing program with the new one ,runs that and exits.
The original program gets back control only when an error occurs.execlp(path,file_name,arguments..); //last argument must be NULL A variant of execlp called execvp is used when the number of arguments is not known in advance.execvp(path,argument_array); //argument array should be terminated by NULL .
13.How to check all the running processes in Unix?
The standard command to see this is [ps].But [ps] only shows you the snapshot of the processes at that instance.If you need to monitor the processes for a certain period of time and need to refresh the results in each interval,consider using the [top] command.
$> ps -ef
If you wish to see the % of memory usage and CPU usage,then consider the below switches
$> ps aux
If you wish to use this command inside some shell script,or if you want to customize the output of [ps] command,you may use “-o” switch like below.By using “-o” switch,you can specify the columns that you want [ps] to print out.
$>ps -e -o stime,user,pid,args,%mem,%cpu
14.How to check if a file is present in a particular directory in Unix?
Using command,we can do it in many ways.Based on what we have learnt so far,we can make use of [ls] and [$?] command to do this.See below:
$> ls -l file.txt; echo $?
If the file exists,the [ls] command will be successful.Hence [echo $?] will print 0.If the file does not exist,then [ls] command will fail and hence [echo $?] will print
15.How to check if the last command was successful in Unix?
To check the status of last executed command in UNIX,you can check the value of an inbuilt bash variable [$?].See the below example:
$> echo $?
16.How to list down file/folder lists alphabetically?
Normally [ls -lt] command lists down file/folder list sorted by modified time.If you want to list then alphabetically,then you should simply specify: [ls -l]
17.How to fail a shell script programmatically?
Just put an [exit] command in the shell script with return value other than 0.this is because the exit codes of successful Unix programs is zero.So,suppose if you write
exit -1
inside your program,then your program will throw an error and exit immediately.
18.How to check the command line arguments in a UNIX command in Shell Script?
In a bash shell,you can access the command line arguments using $0,$1,$2,- variables,where $0 prints the command name,$1 prints the first input parameter of the command,$2 the second input parameter of the command and so on.
19.How to test if a zip file is corrupted in Linux?
Use “-t” switch with the inbuilt [unzip] command
$> unzip -t file.zip
20.How to show the non-printable characters in a file?
Open the file in VI editor.Go to VI command mode by pressing [Escape] and then [:].Then type [set list].This will show you all the non-printable characters,e.g.Ctrl-M characters (^M) etc.,in the file.
21.How to reverse a string in unix?
Pretty easy.Use the [rev] command.
$> echo “unix” | rev
xinu
22.How to remove the last line/ trailer from a file in Unix script?
Always remember that [sed] switch ‘$’ refers to the last line.So using this knowledge we can deduce the below command:
$> sed -i ‘$ d’ file.txt
23.How to remove certain lines from a file in Unix?
If you want to remove line to line from a given file,you can accomplish the task in the similar method shown above.
Here is an example:
$> sed -i ‘5,7 d’ file.txt
The above command will delete line 5 to line 7 from the file file.txt
24.How to check the command line arguments in a UNIX command in Shell Script?
In a bash shell,you can access the command line arguments using $0,$1,$2,… variables,where $0 prints the command name,$1 prints the first input parameter of the command,$2 the second input parameter of the command and so on.
25.How do you create special files like named pipes and device files?
The system call mknod creates special files in the following sequence.
1.kernel assigns new inode,
2.sets the file type to indicate that the file is a pipe,directory or special file,
3.If it is a device file,it makes the other entries like major,minor device numbers.
For example:
If the device is a disk,major device number refers to the disk controller and minor device number is the disk.
26.Discuss the mount and unmount system calls.
The privileged mount system call is used to attach a file system to a directory of another file system; the unmount system call detaches a file system.When you mount another file system on to your directory,you are essentially splicing one directory tree onto a branch in another directory tree.
The first argument to mount call is the mount point,that is ,a directory in the current file naming system.The second argument is the file system to mount to that point.When you insert a cdrom to your unix system’s drive,the file system in the cdrom automatically mounts to /dev/cdrom in your system.
27.How does the inode map to data block of a file?
Inode has 13 block addresses.The first 10 are direct block addresses of the first 10 data blocks in the file.The 11th address points to a one-level index block.The 12th address points to a two-level (double in-direction) index block.
The 13th address points to a three-level(triple in-direction)index block.This provides a very large maximum file size with efficient access to large files,but also small files are accessed directly in one disk read.
28.List the system calls used for process management:
System calls Description
fork() To create a new process
exec() To execute a new program in a process
wait() To wait until a created process completes its execution
exit() To exit from a process execution
getpid() To get a process identifier of the current process
getppid() To get parent process identifier
nice() To bias the existing priority of a process
brk() To increase/decrease the data segment size of a process
29.What is a shell?
A shell is an interactive user interface to an operating system services that allows an user to enter commands as character strings or through a graphical user interface.
The shell converts them to system calls to the OS or forks off a process to execute the command.System call results and other information from the OS are presented to the user through an interactive interface.Commonly used shells are sh,csh,ks etc.
30.What are various IDs associated with a process?
Unix identifies each process with a unique integer called ProcessID.The process that executes the request for creation of a process is called the ‘parent process’ whose PID is ‘Parent Process ID’.
Every process is associated with a particular user called the ‘owner’ who has privileges over the process.The identification for the user is ‘UserID’.Owner is the user who executes the process.Process also has ‘Effective User ID’ which determines the access privileges for
accessing resources like files.
getpid() -process id
getppid() -parent process id
getuid() -user id
geteuid() -effective user id