2015-12-27

In this post, you will find all the possible and most recent Job Interview questions and answers of UNIX  for OS category which you might be asked by your employer. These interview questions and answers will help you to get job.

1. Brief about the initial process sequence while the system boots up?

While booting, special process called the 'swapper' or 'scheduler' is created with Process-ID 0. The swapper manages memory allocation for processes and influences CPU allocation. The swapper inturn creates 3 children:

the process dispatcher,

vhand and

dbflush

with IDs 1,2 and 3 respectively.

This is done by executing the file /etc/init. Process dispatcher gives birth to the shell. Unix keeps track of all the processes in an internal data structure called the Process Table (listing command is ps -el).

2. Briefly describe the Shell’s responsibilities ?

-program execution

– variable and file name substitution

– I/O redirection

– pipeline hookup

– environment control

– interpreted programming language

3. Differentiate cmp command from diff command?

The cmp command is used mainly to compare two files byte by byte, after which the first encountered mismatch is shown. On the other hand, the diff command is used to indicate the changes that is to be made in order to make the two files identical to each other.

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

5. Enumerate some of the most commonly used network commands in UNIX?

– telnet – used for remote login

– ping – an echo request for testing connectivity

– su – user switching command

– ftp – file transfer protocol used for copying files

– finger – information gathering command

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

7. How do you access command line arguments from within a shell script?

Arguments passed from the command line to a shell script can be accessed within the shell script by using a $ (dollar sign) immediately followed with the argument's numeric position on the command line.

Details:

For example, $1 would be used within a script to access the first argument passed from the command line, $2 the second, $3 the third and so on. Bonus: $0 contains the name of the script itself.

8. How do you check how much space left in current drive ?

By using "df" command in UNIX. For example "df -h ." will list how full your current drive is. This is part of anyone day to day activity so I think this Unix Interview question will be to check anyone who claims to working in UNIX but not really working on it.

9. How do you determine and set the path in UNIX?

Each time you enter a , a variable named PATH or path will define in which directory the shell will search for that command. In cases wherein an error message was returned, the reason maybe that the command was not in your path, or that the command does not exist. You can also manually set the path using the “set path = [directory path]” command.

10. How do you terminate a shell script if statement?

With fi, which is "if" spelled backwards.

Details:

The shell script example below uses an if statement to check if a file assigned to the variable myfile exists and is a regular file:

#!/bin/ksh

myfile=$1

if [ -f $myfile ]

then

echo "$myfile exists"

fi

exit 0

11. How to create tar archive or tar file in Unix?

Most of use use either winzip or winrar in windows machine to zipping or creating archives of content so when we move to command line interface like Unix or Linux we struggle without those tools. UNIX

tar command is similar to winzip or winrar and you can use UNIX tar command to create both compressed or uncompressed (zipped) archives in UNIX.

In this example of tar command we will create tar file including all the files and directories or selected files and directories in Unix.

here is our directory

stock_trader@system:~/test ls -lrt

total 0

-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 11:42 equity

drwxrwxrwx+ 1 stock_trader Domain Users 0 Jul 15 14:33 stocks/

-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 15:30 currency

it has two files and one directory. now we will create a tar file with all these contents.

stock_trader@system:~/test tar -cvf trading.tar *

currency

equity

stocks/

stocks/online_stock_exchanges.txt

You see unix tar command is creating tar file with name "trading" with contents shown above. just to review here "-c" is used to create tar file "v" is used to be verbose and "f" is used to tell tar file name. You can see the tar file here

stock_trader@system:~/test ls -lrt

-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 11:42 equity

drwxrwxrwx+ 1 stock_trader Domain Users 0 Jul 15 14:33 stocks/

-r--r--r-- 1 stock_trader Domain Users 0 Jul 15 15:30 currency

-rw-r--r-- 1 stock_trader Domain Users 10K Jul 18 12:29 trading.tar

12. How to find a process and kill that ?

Another classic UNIX interview questions. Answer of this question is simple if you are familiar with ps, grep and kill command. by using "ps -ef" you can get list of all process and then use grep to find your process and get the PID of that process. Once you got PID you can use kill command to kill that process .

13. How to find all the links in a folder in UNIX or Linux ?

This is a tricky UNIX question as there is no specific command to find all symbolic links. Though you have ln command for creating and updating soft links but nothing which gives you all the links in a directory. You need to use ls command which list everything in directory and then you need to list all the links, as they starts with "l" as first characters, as shown in above article .

here is the actual UNIX command to find all links in a directory :

linux@nyj872:~ ls -lrt

total 2.0K

-rw-r--r-- 1 Linux Domain Users 0 Dec 6 2011 a

drwxr-xr-x+ 1 Linux Domain Users 0 Sep 19 12:30 java/

lrwxrwxrwx 1 Linux Domain Users 4 Sep 19 12:31 version_1.0 -> java/

linux@nyj872:~ ls -lrt | grep '^l'

lrwxrwxrwx 1 Linux Domain Users 4 Sep 19 12:31 version_1.0 -> java/

14. How to sort output of a command in reverse order in Linux or UNIX ?

One more Linux command interview question which checks knowledge of frequently used command. you can use sort command in UNIX to sort output of any command by using PIPE. By using -r option with sort command you can sort output of any command in reverse order.

15. How to view contents of tar file in Unix or Linux?

In earlier example of tar command in Unix or Linux we have created a uncompressed tar file called "trading.tar" now in this example we will see the actual content of that tar file.

stock_trader@system:~/test tar -tvf trading.tar

-r--r--r-- stock_trader/Domain Users 0 2011-07-15 15:30 currency

-r--r--r-- stock_trader/Domain Users 0 2011-07-15 11:42 equity

drwxrwxrwx stock_trader/Domain Users 0 2011-07-15 14:33 stocks/

-rwxrwxrwx stock_trader/Domain Users 0 2011-07-15 14:33 stocks/online_stock_exchanges.txt

here option "t" is used to display content of tar file in unix while options "v" and "f" are for "verbose" and "following". now you can clearly see that all the files which we wanted to be included in tar file are there.

16. What are filters?

The term Filter is often used to refer to any program that can take input from standard input, perform some operation on that input, and write the results to standard output. A Filter is also any program that can be used between two other programs in a pipeline

17. What are shell variables?

Shell variables are a combination of a name ( identifier), and an assigned value, which exist within the shell. These variables may have default values, or whose values can be manually set using the appropriate assignment command. Examples of shell variable are PATH, TERM and HOME.

18. What are some common shells and what are their indicators?

sh – Bourne shell

csh – C SHell

bash – Bourne Again Shell

tcsh – enhanced C Shell

zsh – Z SHell

ksh – Korn SHell

19. What are some ways to debug a shell script problem?

Although this is somewhat dependent on what the problem is, there are a few commonly used methods for debugging shell script problems.

One method, which is frequently used across all programming languages, is to insert some debug statements within the shell script to output information to help pinpoint where and why the problem is being introduced.

Another method specific to shell scripting is using "set -x" to enable debugging.

20. What are the bits that support the demand paging?

Valid, Reference, Modify, Copy on write, Age. These bits are the part of the page table entry, which includes physical address of the page and protection bits.

Page address

Age

Copy on write

Modify

Reference

Valid

Protection

21. What are the differences among a system call, a library function, and a UNIX command?

A system call is part of the programming for the kernel. A library function is a program that is not part of the kernel but which is available to users of the system. UNIX commands, however, are stand-alone programs; they may incorporate both system calls and library functions in their programming.

22. What are the features and benefits of unix?

• Portability

• Machine Independent

• Multi-user operations

• Hierarchical file system

• Unix shell

• Pipes and filters

• Utilities

• Background Processing

• Software Development Tools

• Maturity

23. What are the key features of the Korn Shell?

– history mechanism with built-in editor that simulates emacs or vi

– built-in integer arithmetic

– string manipulation capabilities

– command aliasing

– arrays

– job control

24. What are the process states in Unix?

As a process executes it changes state according to its circumstances. Unix processes have the following states:

Running : The process is either running or it is ready to run .

Waiting : The process is waiting for an event or for a resource.

Stopped : The process has been stopped, usually by receiving a signal.

Zombie : The process is dead but have not been removed from the process table.

25. What are the Unix system calls for I/O?

open(pathname,flag,mode) - open file

creat(pathname,mode) - create file

close(filedes) - close an open file

read(filedes,buffer,bytes) - read data from an open file

write(filedes,buffer,bytes) - write data to an open file

lseek(filedes,offset,from) - position an open file

dup(filedes) - duplicate an existing file descriptor

dup2(oldfd,newfd) - duplicate to a desired file descriptor

fcntl(filedes,cmd,arg) - change properties of an open file

ioctl(filedes,request,arg) - change the behaviour of an open file

The difference between fcntl anf ioctl is that the former is intended for any open file, while the latter is for device-specific operations.

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

27. What code would you use in a shell script to determine if a directory exists?

The UNIX test command with the -d option can be used to determine if a directory exists.

Details:

The following test command expression would be used to verify the existence of a specified directory, which is stored in the variable $mydir:

if [ -d $mydir ]

then

command(s)

fi

If the value stored in the variable mydir exists and is a directory file, the command(s) located between then and fi will be executed.

You can consult the test command's man page ("$ man test") to see what test command options are available for use.

28. What do you mean by nice value?

Nice value is the value that controls {increments or decrements} the priority of the process. This value that is returned by the nice () system call. The equation for using nice value is: Priority = (“recent CPU usage”/constant) + (base- priority) + (nice value) Only the administrator can supply the nice value. The nice () system call works for therunning process only. Nice value of one process cannot affect the nice value of the other process.

29. What Happens when you execute a command?

When you enter 'ls' command to look at the contents of your current working directory, UNIX does a series of things to create an environment for ls and the run it: The shell has UNIX perform a fork. This creates a new process that the shell will use to run the ls program. The shell has UNIX perform an exec of the ls program. This replaces the shell program and data with the program and data for ls and then starts running that new program. The ls program is loaded into the new process context, replacing the text and data of the shell. The ls program performs its task, listing the contents of the current directory.

30. What Happens when you execute a program?

When you execute a program on your UNIX system, the system creates a special environment for that program. This environment contains everything needed for the system to run the program as if no other program were running on the system. Each process has process context, which is everything that is unique about the state of the program you are currently running. Every time you execute a program the UNIX system does a fork, which performs a series of operations to create a process context and then execute your program in that context. The steps include the following:

Allocate a slot in the process table, a list of currently running programs kept by UNIX.

Assign a unique process identifier (PID) to the process.

iCopy the context of the parent, the process that requested the spawning of the new process.

Return the new PID to the parent process. This enables the parent process to examine or control the process directly. After the fork is complete, UNIX runs your program.

31. What is a Daemon?

A daemon is a process that detaches itself from the terminal and runs, disconnected, in the background, waiting for requests and responding to them. It can also be defined as the background process that does not belong to a terminal session. Many system functions are commonly performed by daemons, including the sendmail daemon, which handles mail, and the NNTP daemon, which handles USENET news. Many other daemons may exist. Some of the most common daemons are:

init: Takes over the basic running of the system when the kernel has finished the boot process.

inetd: Responsible for starting network services that do not have their own stand-alone daemons. For example, inetd usually takes care of incoming rlogin, telnet, and ftp connections.

cron: Responsible for running repetitive tasks on a regular schedule.

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

33. What is inode?

An inode is an entry created on a section of the disk set aside for a file system. The inode contains nearly all there is to know about a file, which includes the location on the disk where the file starts, the size of the file, when the file was last used, when the file was last changed, what the various read, write and execute permissions are,

who owns the file, and other information.

34. What is IPC? What are the various schemes available?

The term IPC (Inter-Process Communication) describes various ways by which different process running on some operating system communicate between each other. Various schemes available are as follows: Pipes:

One-way communication scheme through which different process can communicate. The problem is that the two processes should have a common ancestor (parent-child relationship). However this problem was fixed with the introduction of named-pipes (FIFO).

Message Queues :

Message queues can be used between related and unrelated processes running on a machine.

Shared Memory:

This is the fastest of all IPC schemes. The memory to be shared is mapped into the address space of the processes (that are sharing). The speed achieved is attributed to the fact that there is no kernel involvement. But this scheme needs synchronization.

Various forms of synchronisation are mutexes, condition-variables, read-write locks, record-locks, and semaphores.

35. What is Kernel?

Kernel is the UNIX operating system. It is the master program that controls the computer’s resources, allotting them to different users and to different tasks. However, the kernel doesn’t deal directly with a user. Instead, it starts up a separate, interactive program, called a shell, for each user when he/she logs on.

36. What is single users system?

The personal computer (PC) is a small, general-purpose system that can execute programs to perform a wide variety of tasks. The PC, however, was designed for use by one person at a time; that is, it is Single-User oriented with MS-DOS as the de facto standard operating system for this range of machines. Single user systems became very popular due to the low cost hardware and wide range of software available for these machines.

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

38. What is the standard convention being followed when naming files in UNIX?

One important rule when naming files is that characters that have special meaning are not allowed, such as * / & and %. A directory, being a special type of file, follows the same naming convention as that of files. Letters and numbers are used, along with characters like underscore and dot characters.

39. What is UNIX?

It is a portable operating system that is designed for both efficient multi-tasking and mult-user functions. Its portability allows it to run on different hardware platforms. It was written is C and lets user do processing and control under a shell.

40. What is wrong with this interactive shell script?

echo What month is this?

read $month

echo $month is as good a month as any.

Answer: Initially, the question mark should be escaped (\?) so that it is not interpreted as a shell metacharacter. Second, it should be read month, not read $month.

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

42. What needs to be done before you can run a shell script from the command line prompt?

You need to make the shell script executable using the UNIX chmod command.

Details:

This chmod command makes the shell script file "example1" executable for the user (owner) only:

$ chmod u+x example1

this syntax makes it executable for all (everyone):

$ chmod a+x example1

You can optionally use octal notation to set UNIX permissions using the chmod command (e.g., $ chmod 755 example1). This topic is beyond the scope of this article, but you can find more information by entering "unix file permissions chmod numeric notation" in your favorite search engine.

43. What UNIX operating system command would you use to display the shell's environment variables?

Running the "env" command will display the shell environment variables.

Details:

Sample env command output:

$ env

HISTFILE=/home/lfl/.history

PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin

SHELL=/bin/ksh

HOSTNAME=livefirelabs.com

USER=lfl

MAIL=/var/spool/mail/lfl

HOME=/home/lfl

HISTSIZE=1000

It would also be good to understand the purpose of the common shell environment variables that are listed in the env command output.

Show more