Tuesday, 26 April 2011

unix interview questions

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. What is 'inode'?

All UNIX files have its description stored in a structure called 'inode'. The inode contains info about the file-size, its location, time of last access, time of last modification, permission and so on. Directories are also represented as files and have an associated inode. In addition to descriptions about the file, the inode contains pointers to the data blocks of the file. If the file is large, inode has indirect pointer to a block of pointers to additional data blocks (this further aggregates for larger files). A block is typically 8k.
Inode consists of the following fields:

File owner identifier
File type
File access permissions
File access times
Number of links
File size
Location of the file data

3. Brief 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).

4.  What are the Unix system calls for I/O?
- Open: to open a file. Syntax: open (pathname, flag, and mode).
- Create: To create a file. Syntax: create (pathname, mode).
- Close: To close a file. Syntax: close (filedes).
- Read: To read data from a file that is opened. Syntax: read (filedes, buffer, bytes)
- Write: To write data to a file that is opened. Syntax: write (filedes, buffer, bytes)
- Lseek: To position the file pointer at given location in the file.
Syntax: lseek (filedes, offset, from).
- Dup: To make a duplicate copy of an existing file descriptor. Syntax: dup (filedes).
- Fcntl: To make the changes to the properties of an open file.
Syntax: fcntl (filedes, cmd, arg).

5.  What is a FIFO? 
FIFO are otherwise called as 'named pipes'. FIFO (first-in-first-out) is a special file which is said to be data transient. Once data is read from named pipe, it cannot be read again. Also, data can be read only in the order written. It is used in interprocess communication where a process writes to one end of the pipe (producer) and the other reads from the other end (consumer).

6.  What are links and symbolic links in UNIX file system? 
A link is a second name (not a file) for a file. Links can be used to assign more than one name to a file, but cannot be used to assign a directory more than one name or link filenames on different computers.
Symbolic link 'is' a file that only contains the name of another file.Operation on the symbolic link is directed to the file pointed by the it.Both the limitations of links are eliminated in symbolic links.
Commands for linking files are:
Link ln filename1 filename2
Symbolic link ln -s filename1 filename2

7.  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.

8. What is the difference between internal and external commands?

Internal commands are commands that are already loaded in the system. They can be executed any time and are independent. On the other hand, external commands are loaded when the user requests for them. Internal commands don’t require a separate process to execute them. External commands will have an individual process. Internal commands are a part of the shell while external commands require a Path. If the files for the command are not present in the path, the external command won’t execute.

9. What is difference between ps -ef and ps -auxwww?

This is indeed a good Unix Interview Command Question and I have faced this issue while ago where one culprit process was not visible by execute ps –ef command and we are wondering which process is holding the file.
ps -ef will omit process with very long command line while ps -auxwww will list those process as well.

10. How do you find how many cpu are in your system and there details?

By looking into file /etc/cpuinfo for example you can use below command:
cat /proc/cpuinfo

11. What is difference between HardLink and SoftLink in UNIX?

I have discussed this Unix Command Interview questions  in my blog post difference between Soft link and Hard link in Unix

12. What is Zombie process in UNIX? How do you find Zombie process in UNIX?

 When a program forks and the child finishes before the parent, the kernel still keeps some of its information about the child in case the parent might need it - for example, the parent may need to check the child's exit status. To be able to get this information, the parent calls 'wait()'; In the interval between the child terminating and the parent calling 'wait()', the child is said to be a 'zombie' (If you do 'ps', the child will have a 'Z' in its status field to indicate this.)
Zombie : The process is dead but have not been removed from the process table.

13. There is a file some where in your system which contains word "UnixCommandInterviewQuestions” How will find that file in Unix?

 By using find command in UNIX for details see here 10 example of using find command in Unix

14. In a file word UNIX is appearing many times? How will you count number?
  grep -c "Unix" filename

15. How do you set environment variable which will be accessible form sub shell?

 By using export   for example export count=1 will be available on all sub shell.


  1. good questions for interview , I have also shared questions collected from my friends and colleagues here Top 30 Unix Command Interview Questions asked in IB . hope it would be useful to you guys.

  2. I was searching on google and directed to this blog. I found it very informative. Great work, Keep it up.

    Interview Questions

  3. Nice questions , you have indeed cover the topic with great details. I have also blogged my experience as 5 example of symbolic link or symlink in unix . let me know how do you find i

  4. Nice post . . Very Useful information

    Dhung.com provid interview question and answer for all company.

  5. Mumbai Escorts: Way to Pleasant Experience Great warm meetings with the most beautiful Mumbai escorts girls.
    Mumbai Escorts
    Escorts in Mumbai
    Mumbai Escorts Service
    Independent Mumbai Escorts
    Escorts Service in Mumbai

    Poonam Malik Mumbai Independent Escorts Girl.

  6. Welcome to visit Mumbai escorts site, contains some special unique type Mumbai Independent escorts you never seen before.
    Mumbai Escorts
    Escorts in Mumbai
    Mumbai Escorts Service
    Mumbai Escorts Girls
    Female Escorts in Mumbai

    Welcome to Ruhi Gupta Mumbai Escorts Girls.