Unix Imp Questions
Unix Imp Questions
Unix Imp Questions
UNIX Kernel is heart of the operating system. UNIX kernal is loaded first when UNIX system is
booted. It handles allocation of devices, cpu, memory from that ponint on.
Kernal is the heart of UNIX operating system, it works as interface between the Shell & the
hardware converts user given code into machine understandable language.
xinetd (it is a super-daemon, it is responsible for invoking other Internet servers when they are
needed)
inetd (same as xinetd, but with limited configuration options)
sendmail/postfix (to send/route email)
Apache/httpd (web server)
Browser Running one daemon for each of the services could significantly increase the load.
However if you are running big site (with many user) it is advisable to use dedicated daemon. For
example web server or MySQL database server.
A server process run runs one time, when called by a daemon. Once done it will stop. For
example telnetd (in.telnetd) or ftpd called from xinetd/inetd daemon. By calling server process
from daemon you can save the load and memory. Use a server process for small services such
as ftpd, telnetd
OR
sqlplus -s userid/password@database
In case you want to connect to SQL database
sqsh -S Servername -U username -P password
In case od SYBASE
isql -S Servername -U username -P password
what is difference between fork() and vfork()?
you should refer to a man page. both create a child process.
fork - parent and child are in 'running' status. they share all page tables until one of them does a
write, then on-demand paging will create private page copy of the dirty page for the modifying
process.
vfork - parent is suspended until child exits or exec's. mainly memory and stack are shared to the
child.
underneath, they both are implemented using the same system call clone, except the flags to
clone differ. which in-turn cause the process status and semantics to change as noted above.
Or
vfork() differs from fork() only in that the child process can share code and data with the calling
process (parent process). This speeds cloning activity significantly at a risk to the integrity of the
parent process if vfork() is misused.
vfork() is a higher performance version of fork() that is provided on some systems where a
performance advantage can be attained.
you can think of the functions you generally write/use from libc/libc++ as function calls. you can
think of clone, read etc as system calls. your library call will setup appropriate values and call a
special CPU instr to jump to the kernel mode, where a table lookup for the requested system call
is performed and code jumps there.
Wrong...the dot is not supposed to be in your path because when you enter a command, your
environment i.e. your path will try and execute from your pwd which is never a good thing.
Yes, . means current working directory and having current directory into your path could create
problem like.. I can have 'ls' command written in current directory to delete all the files
How to find some particular lines in unix Vi editor which starts with a particular word and
end with another particular word?? For ex - There are 10 lines in the editor and only 4 lines
start with the word APPLE and end with the word MANGO.Now how to find out these
particular lines in Unix?
You can try this cmd
syntax
esc + ?word (Press 'escape' key, type word-to-find)
esc+?APPLE [*] MANGO
'esc` then :/word-to-find then `enter`
What is kernel? What is the difference between kernel and microkernel? What is the
difference between unix and Linux? What is relational database,hierarchal database. and
network database? their difference?
1) kernel: The kernel is the part of the Operating System(OS) that interacts directly with the
hardware of computer ,through device drivers that are built into the kernel. It provides set of
services that can be used by programs,insulating these programs from the underlying hardware.
Major functions of kernel are:
1. to manage computer memory
2.to control acces to the computer
3.to maintain file system
4.to handle interrupts
5.to handel errors
6.to perform input and output services(which allows computers to interact with terminals,storage
devices and printers) and
7.to allocate the resources of the computer(such as CPU and input/output devices) among users.
2) micro_kernel: A micro-kernel is a minimal operating system that performs only the essential
functions of an operating system. All other operating system functions are performed by system
processes.
do well. Unix was developed using ‘C’ Language. Unix was the foundation on which Linux was
built. Unix has Character based environment while Linux has Graphical as well as Character
based environment. Unix has less numbers of utilities & Features as compare to Linux.Ex:
Number of shells and editors available in Linux is more than Unix.
Explain Stickybit?
The ‘Sticky Bit’ is a permission bit that can be set on either a file or a directory.
If it is set on a file, then that file will remain in memory after execution, thus ’sticking’ in memory.
This is useful when running a multi-user program (such as a bulletin board system that I ran
once) to make the program execute faster for the next user. This was a common programming
tactic earlier in the history of computer programming when speed and disk space were at a
premium.
If the sticky bit is set on a directory, only the owner of files in that directory will be able to modify
or delete files in that directory - even if the permissions set on those files would otherwise allow it.
It executes from user and behaves as it executing from the root.
What is Semaphore?
A data object that represents the right to use a limited resource, used for synchronization and
communication between asynchronous processes.
A semaphore is a type of Interprocess communication resource used for synchronization and
mutual exclusion between any two asynchronous processes.
What is the command to find all of the files which have been accessed within the last one
month?
find / -mtime -30 –print
find / -atime -30
1. PIPE
2. NAMED PIPE(FIFO)
3. Message Queue
4. Shared memory
5. Semaphore
6. Sockets
What is INODE?
The UNIX disk object that represents the existence of a file. The inode records owner and group
IDs, Fstype, Date & time last access, last modification, n/o links, size of file, addresses of blocks
where the file is physically present and permissions
The inode is the focus of all file activity in the file system. There is a unique inode allocated for
each active file, each current directory, each mounted-on file, text file, and the root. An inode is
"named" by its device/i-number pair.
You can check the inode number of a file by typing ls -li
To add to the definition, when you create a hard link, it is actually another file gets created with
the same inode number. so in a way there is only one inode number which is associated with two
file names having diffrent locations on the disk but acutually refering to the same file. In this case
when you call a delete (rm) on one of hte files you are actually not deleting the file, indeed you
are deleting a reference to the file.
A unique number associated with each filename. This number is used to look up an entry in the
inode table which gives information on the type, size, and location of the file and the userid of the
owner of the file.
How would you print just the 25th line in a file (smallest possible script please)?
sed -n '25p' filename.txt
tail +25 file| head -1
cat -n filename | grep '25'
How do you search the string for vowel's occurrence and number of occurrences of each
vowel
cat filename|tr -d
'eioubcdfghjklmnpqrstvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890()~`!@#$%^&*"()
_+{}|:"<>?,./;][=-/n ' |grep a|tr -d '|wc –m
If you have a string "one two three", Which shell command would you use to extract the
strings?
echo $string | cut -d" " -f1
echo $string | cut -d" " -f2
echo $string | cut -d" " -f3
echo "one two three" | cut -d" " -f 1,2,3
or
echo "one two three" | awk '{print $1 $2 $3}'
cut , awk, sed and others are not "SHELL" commands. One way could be with bash: string="one
two three"tab=($string)$ echo ${tab[0]}one$ echo ${tab[1]}two$ echo ${tab[2]}threehttp://www.big-
up.org
What is use of "cut" command ?Give some examples. Can we use "awk" or "sed" instead
of "cut" ? If yes then give some examples?
This utility is use to cut out columns from a table or fields from each line of a file.
cut can be used as a filter.
Either the -b, -c, or -f option must be specified.
-b list
The list following -b specifies byte positions (for instance, -b1-72 would pass the first 72 bytes of
each line). When -b and -n are used together, list is adjusted so that no multi-byte character is
split.
-c list
The list following -c specifies character positions (for instance, -c1-72 would pass the first 72
characters of each line).
-d delim
The character following -d is the field delimiter (-f option only). Default is tab. Space or other
characters with special meaning to the shell must be quoted. delim can be a multi-byte character.
Cut - Utility used to cut/Strip out the required data/text from the source.
Cut can be used in three modes,
Stripping by Character
cut -c 1-3
STriping by Byte length
cut -b -1-72
Stripping by delimiter and fields.
cut -d "|" -f1
where
-d "|" -> Deleimter used in input text to seperate columns
-f1 -> Field/Column number
while processing Huge input files, Cut's perfomance is far better than awk
Cut is a powerful command in Unix, Cutting fields we can use the command , for example we
would like to extraxct first 3 col
cut -c1-3 sample (Sample is a file name)
To extract first and 3rd col
cut -d;-f1,-f3 sample
CUT is used to get the specific portion(column) of information from a files separated by a specific
charecter(ex: CSV file)
How to find see the file which is created today,s date with time after 10 a.m to 5 p.m?
find . -atime 0 -type f -exec ls -ltr {} ; | grep "`date +%b %d`"
This command will show the files which are created today. But for ur second part of the
quesiton i needto analyse more,
Explanation:
find . -atime 0 -type f -exec ls -ltr {}
This will list the files where are accessed and created today.
grep "`date +%b %d`"
This part of the command will filter out unwanted files which were not created today.
set -u -a
rm -f out2
find $PWD -atime 0 -type f -exec ls -ltr {} ; | grep "`date '+%b %d'`" | tr -s " " | tr -d ":" > out2
echo "-------Files Modified Today--------"
cat out2 | while read line
do
timechk=`echo $line | cut -d " " -f8`
filename=`echo $line | cut -d " " -f9`
if [ $timechk -gt 1000 -a $timechk -lt 1700 ]
then
echo $filename
fi
done
echo "-----------------------------------"
How will you list only the empty lines in a file (using grep)?
grep "^$" filename.txt
grep -c "^$" filename.txt
grep "^[ ]*$" filename.txt
In character set (between [ and ] one space and tab is given)
this command will gives all the blank line including those having space and tabs (if pressed)only
How would you get the character positions 10-20 from a text file?
cat filename.txt | cut -c 10-20
cut -c10-20 <filename.txt>
When you login to a c shell, which script would be run first? (before the terminal is ready
for the user)
.profile file will execute first when a user logs in.
or C shell , first /etc/.login script is run & after that
~/.login is run & then ~/.cshrc is run.
What are the commands to make a file hidden and to delete all hidden files in the folder?
Rename the file so that it starts with (.)
For example if you have a file called file.txt just rename it to .file.txt
mv file.txt .file.txt
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.
KILL <PID> : Kills the process with specific ProcessID
KILL -9 <PID> : Kills a stubburn process which can't killed using KILL command
KILL 0 : Kills all the processes and some times this may result in system crash also causing
heavy core dump
Kill -9 <proc. id>is used to killl a process and all its child processes
Child processes will not be killed with kill -9 command.
what is the command to list files from directory in decreasing order of their file size.
l -rt|sort -knr5
Here ll -rt list the files along with permission, owner, group, etc...
sort will sort the output of ll -rt according to 5th field which is the file size.
Hence the output of list will be sorted according to the file size and it will be listed.
Which of the following commands is not a filter?(a)man , (b) cat , (c) pg , (d) head?
Ans: man pg cat
A filter is a program which can receive a flow of data from std input, process (or filter) it and
send the result to the std output.
What is difference between Hard and Soft mount?
Hardmount in Unix is the same as the normal filesystem mount used mainly for mounting local
filesysytems.Once a filesystem is hard mounted you should use umount command to unmount
it.It remains mounted till you umount it.
Soft mount on the other hand allows automatic unmounting if the filesystem is idle for a specified
timeout period. It is mainly used for network filesystems like NFS It can be configured using
Autofs and the network filesystem can be soft mounted.
Example
I have a file with the following info as comma separated. It shows username, id number and what
subjects they are enrolled in. The subjects are separated by colons.
UserName,ID,Subject
jsmith,235456939,MATH29879:PHYS45979
jdoe,750378929,ENGL36899:JAPN92576
I want it to have one username with one course per line. So that it would look like:
jsmith,235456939,MATH29879
jsmith,235456939,PHYS45979
jdoe,750378929,ENGL36899
jdoe,750378929,JAPN92576
Does anyone have any ideas?
Answer
1) Say your file name is "r1" having following data
STUDENT1,ID1,SUB11
STUDENT2,ID2,SUB21:SUB22
STUDENT3,ID3,SUB31:SUB32:SUB33
STUDENT4,ID4,SUB41:SUB42:SUB44
{FS=","}
{NAME=$1;ID=$2;SUBJECT=$3}
{split(SUBJECT,SUB,":")
{printf "%s,%s,%sn",NAME,ID,SUB[1]}
{if(SUB[2]!=NULL){printf "%s,%s,%sn",NAME,ID,SUB[2]}
{if(SUB[3]!=NULL){printf "%s,%s,%sn",NAME,ID,SUB[3]}
{if(SUB[4]!=NULL){printf "%s,%s,%sn",NAME,ID,SUB[4]}
{if(SUB[5]!=NULL){printf "%s,%s,%sn",NAME,ID,SUB[5]}
awk -f prg.awk r1
You can add more line with SUB[#] depending of number of subjects
Sorry i forgot to add a closing curly brace } at the end of prg.awk
Without which the program will not work
Create it as script with name like format.sh and pass your file as argument.
it will output the data in your desired format
$ cat input.file
jsmith,235456939,MATH29879:PHYS45979
jdoe,750378929,ENGL36899:JAPN92576
$ cat output.file
jsmith,235456939,MATH29879
jdoe,750378929,ENGL36899
use below command:
$ cat t.txt
jsmith,235456939,MATH29879:PHYS45979
jdoe,750378929,ENGL36899:JAPN92576
What is the difference between grep and find commands?a)provides information on help
b)grep searches in a file whereas find searches for files and directories.
c)both a) and b)
d) none of the above
Write command to delete all the files from a directory which have been created after a
specified time.
Say you want to delete some files ends with data which are 2 days old.
Write a shell script to list only the hidden files in the current directories?
ls -A | grep ^[.] Works on Linux
ls -a | grep ^[.]
ls -la | grep -v ^d | awk {'print $9'} | grep ^[.]
What is the o/p of following command $a =*, echo $a, echo "$a"?
command $a =*, variable 'a' will have all the files and directories in the current location.. simply
the result of "ls".
echo $a - this will give the list of files and directories
echo "$a" - this wil show value assinged to a.. ie., *
How will u list the contents of parent directory from the current directory?
ls -l <directory path>
What are the default system wide permissions for a file and directory?
Directory - 777 - rwxrwxrwx
File - - 055 - ---r_x r_x
If a file permission are 000, can the superuser still read and write to that file.?
No, he will not be able to read or write, Inorder to do that, he needs to change the
permissions.
How will u make sure that no one can see the files which u had created?
Create the file starting with .(DOT) It will be hidden. or example cat > .sharvan
Soft link:-This is a Symbolic link between files.The actual file or directory , must be residing at
any available partitions of the harddisk, Soft Link is just a "shortcut" (in windows terms) or "link"
created with a new file name at the working directory or at current working partition of hard disk.
Even when you don't require it, you can confidently delete this "soft link" as it doesn't remove the
actual file or directory. The reason is, the actual file or diretcory's inode is different from the
"softlink" created file's inode, in any unix system.Hard link:-It is the replica of the actual file or
directory , which must be residing at any available partitions. This is a duplicate file copy of it's
orginal , which can be created at current , working partition.When we remove or delete , this
hardlink, it removes the original file or directory too.The reson is , they share the same inode , in
any unix file system.With regards,Siddhun.M.Karthik
softlink is useful when you want to refer files from other computer /filesystem through NFS .hard
link doesn't allow this.
1.who | tee /dev/pts/4 | cut -d " " -f 1 | uniq | echo "no of users logged in are `wc -l`"
who - displays all the logged in users tee - pass the who output to terminal output /dev/pts/4 and
also to cut command.
where /dev/pts/4 is the current terminal. use tty to get your current terminal
cut -d " " -f1 will cut the first field of who o/p which is login name
uniq command is used to merge duplicate logins to single entry. This might be in some cases a
single user might have logged in from different terminals and in such case they should not be
counted as separate users.
wc -l give total number of line which is here is the total users logged in.
eg o/p: root pts/3 Nov 14 14:30 root pts/4 Nov 21 12:25 no of users logged in are 1
2. ls | tee /dev/pts/4 | grep -c "poem" > count | cat count
3. sort -m fil1 file2 | tee /dev/pts/4 | sort -o outputfile
4. sort - file1 -o newcity enter extra string from keyboard and press Ctrl+d key combination
to finish the input
5. rm -rf /dir 2>errorlog where 2 denotes "standard error" which is here redirected to
errorlog file instead of user terminal
The cut command cuts bytes, characters, or fields from each line of a file and writes these bytes,
characters, or fields to standard output. If you do not specify the File parameter, the cut command
reads standard input.The more command reads files and displays the text one screen at a time.
The command pauses after each screen and prints the word More at the bottom of the screen. If
you then press a carriage return, the more command displays an additional line. If you press the
space bar, the more command displays another full screen of text. Note: On some terminal
models, the more command clears the screen, instead of scrolling.
How does the kernel differentiate device files and ordinary files?
Kernel checks 'type' field in the file's inode structure.
device filles are of 2 types --- character device file and block device file
type field in the file's inode structure
b--- block device file
c--- character device file
What are the difference between Daemons in Unix and service processes in Windows?
both are same. daemons are the background processes in unix.
similarly background processes in windows are called as service agents or service processes.
Advantages of Unix
- Unix is more flexible and can be installed on many different types of machines, including
main-frame computers, supercomputers and micro-computers.
- Unix is more stable and does not go down as often as Windows does, therefore requires
less administration and maintenance.
- Unix has greater built-in security and permissions features than Windows.
- Unix possesses much greater processing power than Windows.
- Unix is the leader in serving the Web. About 90% of the Internet relies on Unix operating
systems running Apache, the world's most widely used Web server.
- Software upgrades from Microsoft often require the user to purchase new or more
hardware or prerequisite software. That is not the case with Unix.
- The mostly free or inexpensive open-source operating systems, such as Linux and BSD,
with their flexibility and control, are very attractive to (aspiring) computer wizards. Many of
the smartest programmers are developing state-of-the-art software free of charge for the
fast growing "open-source movement”.
- Unix also inspires novel approaches to software design, such as solving problems by
interconnecting simpler tools instead of creating large monolithic application programs.