Unix Interview Questions and Answers
Question - 1 : - What does fork() do?
Answer - 1 : - The fork() function is used to create a new process from an
existing process. The new process is called the child process, and the
existing process is called the parent. You can tell which is which by
checking the return value from fork(). The parent gets the
child's pid returned to him, but the child gets 0 returned to him. Thus
this simple code illustrate's the basics of it.
Question - 2 : - How can I get/set an environment variable from a program?
Answer - 2 : - Getting the value of an environment variable is done by using `getenv()'.
#include
char *getenv(const char *name);
Setting the value of an environment variable is done by using `putenv()'.
#include
int putenv(char *string);
The string passed to putenv must *not* be freed or made invalid, since a
pointer to it is kept by `putenv()'. This means that it must either be a
static buffer or allocated off the heap. The string can be freed if the
environment variable is redefined or deleted via another call to `putenv()'.
Remember that environment variables are inherited; each process has a
separate copy of the environment. As a result, you can't change the value
of an environment variable in another process, such as the shell.
Suppose you wanted to get the value for the `TERM' environment variable.
You would use this code:
char *envvar;
envvar=getenv("TERM");
printf("The value for the environment variable TERM is ");
if(envvar)
{
printf("%s\n",envvar);
}
else
{
printf("not set.\n");
}
Now suppose you wanted to create a new environment variable called `MYVAR',
with a value of `MYVAL'. This is how you'd do it.
static char envbuf[256];
sprintf(envbuf,"MYVAR=%s","MYVAL");
if(putenv(envbuf))
{
printf("Sorry, putenv() couldn't find the memory for %s\n",envbuf);
&
Question - 3 : - How can I read the whole environment?
Answer - 3 : - If you don't know the names of the environment variables, then the
`getenv()' function isn't much use. In this case, you have to dig deeper
into how the environment is stored.
A global variable, `environ', holds a pointer to an array of pointers to
environment strings, each string in the form `"NAME=value"'. A `NULL'
pointer is used to mark the end of the array. Here's a trivial program to
print the current environment (like `printenv'):
#include
extern char **environ;
int main()
{
char **ep = environ;
char *p;
while ((p = *ep++))
printf("%s\n", p);
return 0;
}
In general, the `environ' variable is also passed as the third, optional,
parameter to `main()'; that is, the above could have been written:
#include
int main(int argc, char **argv, char **envp)
{
char *p;
while ((p = *envp++))
printf("%s\n", p);
return 0;
}
However, while pretty universally supported, this method isn't actually
defined by the POSIX standards. (It's also less useful, in general.)
Question - 4 : - How can I renew or release an IP in Linux?
Answer - 4 : - Coming from a Microsoft operating system to Linux you may be surprised to see there is not an option for ifconfig to release and renew an IP address. Below are two different methods of how this can be done at the command line.
Question - 5 : - Define a single-user system.
Answer - 5 : - A personal computer which possesses an operating system designed to operate by only one user at a given time is known as a single-user system. Single user system becomes more popular since low-cost hardware and availability of a wide range of software to perform different tasks.
Question - 6 : - List a few significant features of UNIX?
Answer - 6 : - The following are a few features of UNIX;
Machine independent
Portability
Multi-user operations
Unix Shells
Hierarchical file system
Pipes and filters
Background processors
Utilities
Development tools
Question - 7 : - What is Shell?
Answer - 7 : - The program which serves as an interface between the user and the system called a shell. It is the layer of programming that understands and executes the commands a user enters. In some systems, it’s also called a command interpreter.
Question - 8 : - What are the basic responsibilities of a shell?
Answer - 8 : - Following are the responsibilities of a shell;
Program Execution
Input/ output redirection
Filename and variable substitution
Pipeline hookup
Environment control
Integrated programming language
Question - 9 : - Describe the usage and functionality of the command rm –r * in UNIX?
Answer - 9 : - The command rm –r * erases all files in a directory with its subdirectories.
rm is for deleting files
-r is to delete directories and subdirectories with files within
* is indicate all entries
Question - 10 : - Describe the term directory?
Answer - 10 : - A directory in UNIX is a specialised form of a file that maintains a list of all the files which are included in it.