Top 21 Unix/linux Interview Questions
Q1. How Can You Get/set An Environment Variable From A Program?
Getting the value of an environment variable is achieved by way of the use of `getenv()'. Setting the fee of an environment variable is achieved by using the usage of `putenv()'.
Q2. What Are Various Ids Associated With A Process?
Unix identifies every procedure with a unique integer called ProcessID. The technique that executes the request for introduction of a system is called the 'determine system' whose PID is 'Parent Process ID'. Every manner is associated with a selected consumer called the 'owner' who has privileges over the procedure. The identity for the consumer is 'UserID'. Owner is the consumer who executes the method. Process also has 'Effective User ID' which determines the get admission to privileges for gaining access to sources like files.
Getpid() -procedure identity
getppid() -discern process id
getuid() -user identity
geteuid() -effective consumer identification
Q3. Discuss The Mount And Unmount System Calls?
The privileged mount gadget name is used to connect a document system to a listing of any other record gadget; the unmount machine name detaches a document machine. When you mount any other record gadget on for your listing, you're essentially splicing one directory tree onto a branch in any other directory tree. The first argument to mount name is the mount factor, this is , a listing in the cutting-edge record naming device. The 2nd argument is the file device to mount to that point. When you insert a cdrom to your unix gadget's force, the file gadget inside the cdrom automatically mounts to /dev/cdrom to your gadget.
Q4. What Are Links And Symbolic Links In Unix File System?
A link is a 2nd name (now not a document) for a report. Links may be used to assign multiple call to a document, but can not be used to assign a directory a couple of call or link filenames on one-of-a-kind computer systems.
Symbolic hyperlink 'is' a report that most effective incorporates the call of another document.Operation on the symbolic hyperlink is directed to the file pointed through the it.Both the limitations of links are removed in symbolic links.
Commands for linking documents are:
Link ln filename1 filename2
Symbolic hyperlink ln -s filename1 filename2
Q5. What Are The Unix System Calls For I/o?
Open(pathname,flag,mode) - open report
creat(pathname,mode) - create file
near(filedes) - close an open file
examine(filedes,buffer,bytes) - examine facts from an open document
write(filedes,buffer,bytes) - write information to an open report
lseek(filedes,offset,from) - position an open document
dup(filedes) - duplicate an current report descriptor
dup2(oldfd,newfd) - duplicate to a preferred file descriptor
fcntl(filedes,cmd,arg) - change properties of an open record
ioctl(filedes,request,arg) - alternate the behaviour of an open report
The difference between fcntl anf ioctl is that the former is intended for any open document, at the same time as the latter is for device-specific operations.
Q6. Brief About The Directory Representation In Unix?
A Unix directory is a file containing a correspondence among filenames and inodes. A listing is a unique report that the kernel maintains. Only kernel modifies directories, but processes can examine directories. The contents of a listing are a list of filename and inode variety pairs. When new directories are created, kernel makes two entries named '.' (refers back to the directory itself) and '..' (refers to figure listing).
System call for creating listing is mkdir (pathname, mode).
Q7. How Can A Parent And Child Process Communicate?
A discern and baby can talk via any of the regular inter-process conversation schemes (pipes, sockets, message queues, shared reminiscence), but also have some unique ways to talk that take advantage of their relationship as a determine and toddler. One of the maximum obvious is that the determine can get the exit status of the child.
Q8. List The System Calls Used For Process Management?
System calls Description:
fork() To create a brand new procedure
exec() To execute a brand new application in a system
wait() To wait until a created manner completes its execution
exit() To go out from a process execution
getpid() To get a process identifier of the cutting-edge manner
getppid() To get determine technique identifier
first-rate() To bias the existing precedence of a method
brk() To increase/decrease the records segment size of a method.
Q9. What Is 'inode'?
All UNIX files have its description saved in a shape known as 'inode'. The inode includes information approximately the document-size, its region, time of last get admission to, time of final modification, permission and so on. Directories are also represented as documents and feature an related inode. In addition to descriptions approximately the file, the inode includes suggestions to the records blocks of the report. If the record is big, inode has oblique pointer to a block of suggestions to extra data blocks (this similarly aggregates for larger files). A block is normally 8k.
Inode consists of the subsequent fields:
File owner identifier
File kind
File get entry to permissions
File get admission to times
Number of links
File length
Location of the report statistics
Q10. What Is A Zombie?
When a program forks and the child finishes before the figure, the kernel nonetheless continues some of its data approximately the child in case the figure might want it - for instance, the figure may need to check the kid's exit repute. To be capable of get this data, the determine calls `wait()'; In the c program languageperiod between the child terminating and the discern calling `wait()', the kid is said to be a `zombie' (If you do `ps', the child may have a `Z' in its status discipline to suggest this.)
Q11. How Does The Inode Map To Data Block Of A File?
Inode has thirteen block addresses. The first 10 are direct block addresses of the first 10 facts blocks inside the file. The 11th address points to a one-stage index block. The twelfth deal with points to a -stage (double in-route) index block. The 13th deal with points to a three-stage(triple in-direction)index block. This gives a totally massive most record length with efficient get entry to to big files, but additionally small files are accessed at once in one disk study.
Q12. Brief About The Initial Process Sequence While The System Boots Up?
While booting, unique procedure known as the 'swapper' or 'scheduler' is created with Process-ID @The swapper manages memory allocation for approaches and influences CPU allocation.
The swapper inturn creates 3 kids:
the process dispatcher,
vhand and
dbflush
with IDs 1,2 and 3 respectively.
This is finished with the aid of executing the document /etc/init. Process dispatcher gives start to the shell. Unix continues track of all the approaches in an internal data shape called the Process Table (list command is ps -el).
Q13. What Is A Fifo?
FIFO are otherwise referred to as as 'named pipes'. FIFO (first-in-first-out) is a unique report that is stated to be facts temporary. Once information is examine from named pipe, it cannot be examine once more. Also, records can be read most effective in the order written. It is used in interprocess communique where a system writes to 1 quit of the pipe (manufacturer) and the opposite reads from the opposite stop (patron).
Q14. How Are Devices Represented In Unix?
All gadgets are represented via documents referred to as special files which can be located in/dev directory. Thus, device files and other documents are named and accessed in the identical way. A 'ordinary file' is simply an ordinary statistics record within the disk. A 'block unique report' represents a tool with traits similar to a disk (facts transfer in terms of blocks). A 'character unique record' represents a device with traits just like a keyboard (information transfer is by way of circulate of bits in sequential order).
Q15. Predict The Output Of The Following Program Code
Main()
Fork(); Fork(); Fork();
Printf("howdy World!");
"Hello World" could be printed eight times.
Explanation:
2^n times in which n is the wide variety of calls to fork()
Q16. How Do You Create Special Files Like Named Pipes And Device Files?
The machine name mknod creates unique documents within the following sequence:
kernel assigns new inode,
Sets the document kind to signify that the report is a pipe, directory or special file,
If it's miles a device file, it makes the opposite entries like principal, minor tool numbers.
For example:
If the tool is a disk, important tool number refers to the disk controller and minor tool variety is the disk.
Q17. Predict The Output Of The Following Program Code
Main(),
Fork();
Printf("hey World!");
Hello World!Hello World!
Explanation:
The fork creates a baby that is a replica of the figure method. The child begins from the fork().All the statements after the decision to fork() can be carried out two times.(once via the figure procedure and other by way of child). The assertion before fork() is completed most effective via the figure procedure.
Q18. How Do You Change File Access Permissions?
Every document has following attributes:
proprietor's user ID ( 16 bit integer )
proprietor's institution ID ( sixteen bit integer )
File access mode word
'r w x -r w x- r w x'
(person permission-institution permission-others permission)
r-read, w-write, x-execute
To change the access mode, we use chmod(filename,mode).
Example 1:
To alternate mode of myfile to 'rw-rw-r–' (ie. Read, write permission for consumer - study,write permission for organization - handiest read permission for others) we deliver the args as:
chmod(myfile,0664) .
Each operation is represented by means of discrete values
'r' is four
'w' is 2
'x' is 1
Therefore, for 'rw' the value is 6(four+2).
Example 2:
To change mode of myfile to 'rwxr–r–' we provide the args as:
chmod(myfile,0744).
Q19. What Are The Process States In Unix?
As a system executes it changes nation in step with its instances. Unix strategies have the subsequent states:
Running : The technique is either jogging or it is prepared to run .
Waiting : The technique is watching for an event or for a useful resource.
Stopped : The technique has been stopped, usually with the aid of receiving a signal.
Zombie : The process is useless but have now not been eliminated from the procedure desk.
Q20. What Is A Shell?
A shell is an interactive consumer interface to an running machine services that permits an person to enter instructions as character strings or via a graphical user interface. The shell converts them to device calls to the OS or forks off a method to execute the command. System name consequences and other statistics from the OS are presented to the user via an interactive interface. Commonly used shells are sh,csh,ks and many others.
Q21. Explain Fork() System Call?
The `fork()' used to create a new technique from an current procedure. The new process is referred to as the kid manner, and the present technique is called the discern. We can tell that's which with the aid of checking the return fee from `fork()'. The parent gets the kid's pid returned to him, however the child gets zero back to him.

