Unix Interview Questions and Answers
Unix Interview Questions and Answers
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 se!uential order . What is 'inode'? All "#$% files have its description stored in a structure called 'inode'. The inode contains info about the file&si'e, its location, time of last access, time of last modification, permission and so on. (irectories are also represented as files and have an associated inode. $n addition to descriptions about the file, the inode contains pointers to the data blocks of the file. $f 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 )k. $node consists of the following fields* +ile owner identifier +ile type +ile access permissions +ile access times #umber of links +ile si'e ,ocation of the file data Brief about the directory representation in UNIX A "ni- directory is a file containing a correspondence between filenames and inodes. A directory is a special file that the kernel maintains. .nly kernel modifies directories, but processes can read directories. The contents of a directory are a list of filename and inode number pairs. /hen new directories are created, kernel makes two entries named '.' (refers to the directory itself and '..' (refers to parent directory . 0ystem call for creating directory is mkdir (pathname, mode . What are the Unix system calls for I !? 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 e-isting file descriptor dup1(oldfd,newfd & duplicate to a desired file descriptor fcntl(filedes,cmd,arg & change properties of an open file ioctl(filedes,re!uest,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. How do you chan"e #ile $ccess %ermissions? 2very file has following attributes* owner's user $( ( 34 bit integer owner's group $( ( 34 bit integer +ile access mode word 'r w - &r w -& r w -' (user permission&group permission&others permission r&read, w&write, -&e-ecute To change the access mode, we use chmod(filename,mode . 2-ample 3* To change mode of myfile to 'rw&rw&r&&' (ie. read, write permission for user & read,write permission for group & only read permission for others we give the args as* chmod(myfile,5446 . 2ach operation is represented by discrete values 'r' is 6 'w' is 1 '-' is 3 Therefore, for 'rw' the value is 4(671 . 2-ample 1* To change mode of myfile to 'rw-r&&r&&' we give the args as* chmod(myfile,5866 . What are lin&s and symbolic lin&s in UNIX file system? A link is a second name (not a file for a file. ,inks 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. 0ymbolic link 'is' a file that only contains the name of another file..peration on the symbolic link is directed to the file pointed by the it.9oth the limitations of links are eliminated in symbolic links. :ommands for linking files are* ,ink ln filename3 filename1 0ymbolic link ln &s filename3 filename1 What is a #I#!?
+$+. are otherwise called as 'named pipes'. +$+. (first&in&first&out is a special file which is said to be data transient. .nce data is read from named pipe, it cannot be read again. Also, data can be read only in the order written. $t 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 . How do you create special files li&e named pipes and device files? The system call mknod creates special files in the following se!uence. 3. kernel assigns new inode, 1. sets the file type to indicate that the file is a pipe, directory or special file, ;. $f it is a device file, it makes the other entries like major, minor device numbers. +or e-ample* $f the device is a disk, major device number refers to the disk controller and minor device number is the disk. 'iscuss 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. /hen 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. /hen you insert a cdrom to your uni- system's drive, the file system in the cdrom automatically mounts to /dev/cdrom in your system. How does the inode map to data bloc& of a file? $node has 3; block addresses. The first 35 are direct block addresses of the first 35 data blocks in the file. The 33th address points to a one&level indeblock. The 31th address points to a two&level (double in&direction indeblock. The 3;th address points to a three&level(triple in&direction inde- block. This provides a very large ma-imum file si'e with efficient access to large files, but also small files are accessed directly in one disk read. What is a shell? A shell is an interactive user interface to an operating system services that allows an user to enter commands as character strings or through a graphical user interface. The shell converts them to system calls to the .0 or forks off a process to e-ecute the command. 0ystem call results and other information from the .0 are presented to the user through an interactive interface. :ommonly used shells are sh,csh,ks etc.
Brief about the initial process se(uence while the system boots up) /hile booting, special process called the 'swapper' or 'scheduler' is created with =rocess&$( 5. The swapper manages memory allocation for processes and influences :=" allocation. The swapper inturn creates ; children* the process dispatcher, vhand and dbflush with $(s 3,1 and ; respectively. This is done by e-ecuting the file /etc/init. =rocess dispatcher gives birth to the shell. "ni- keeps track of all the processes in an internal data structure called the =rocess Table (listing command is ps &el . What are various I's associated with a process? "ni- identifies each process with a uni!ue integer called =rocess$(. The process that e-ecutes the re!uest for creation of a process is called the 'parent process' whose =$( is '=arent =rocess $('. 2very process is associated with a particular user called the 'owner' who has privileges over the process. The identification for the user is '"ser$('. .wner is the user who e-ecutes the process. =rocess also has '2ffective "ser $(' which determines the access privileges for accessing resources like files. getpid( &process id getppid( &parent process id getuid( &user id geteuid( &effective user id *xplain for&+, system call) The >fork( ' used to create a new process from an e-isting process. The new process is called the child process, and the e-isting process is called the parent. /e 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 5 returned to him. %redict the output of the followin" pro"ram code main( ? fork( < printf(@Aello /orldB@ < C Answer* Aello /orldBAello /orldB 2-planation* The fork creates a child that is a duplicate of the parent process. The child begins from the fork( .All the statements after the call to fork( will be
e-ecuted twice.(once by the parent process and other by child . The statement before fork( is e-ecuted only by the parent process. %redict the output of the followin" pro"ram code main() { fork(); fork(); fork(); printf("Hello World!"); } Answer* @Aello /orld@ will be printed ) times. 2-planation* 1Dn times where n is the number of calls to fork( -ist the system calls used for process mana"ement. 0ystem calls (escription fork( To create a new process e-ec( To e-ecute a new program in a process wait( To wait until a created process completes its e-ecution e-it( To e-it from a process e-ecution getpid( To get a process identifier of the current process getppid( To get parent process identifier nice( To bias the e-isting priority of a process brk( To increase/decrease the data segment si'e of a process How can you "et set an environment variable from a pro"ram?. Eetting the value of an environment variable is done by using >getenv( '. 0etting the value of an environment variable is done by using >putenv( '. How can a parent and child process communicate? A parent and child can communicate through any of the normal inter&process communication schemes (pipes, sockets, message !ueues, shared memory , but also have some special ways to communicate that take advantage of their relationship as a parent and child. .ne of the most obvious is that the parent can get the e-it status of the child.
What is a /ombie? /hen 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 e-ample, the parent may need to check the child's e-it status. To be able to get this information, the parent calls >wait( '< $n the interval between the child terminating and the parent calling >wait( ', the child is said to be a >'ombie' ($f you do >ps', the child will have a >F' in its status field to indicate this. What are the process states in Unix? As a process e-ecutes it changes state according to its circumstances. "niprocesses have the following states* Gunning * The process is either running or it is ready to run . /aiting * The process is waiting for an event or for a resource. 0topped * The process has been stopped, usually by receiving a signal. Fombie * The process is dead but have not been removed from the process table. What Happens when you execute a pro"ram? /hen you e-ecute a program on your "#$% 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. 2ach process has process conte-t, which is everything that is uni!ue about the state of the program you are currently running. 2very time you e-ecute a program the "#$% system does a fork, which performs a series of operations to create a process conte-t and then e-ecute your program in that conte-t. The steps include the following* Allocate a slot in the process table, a list of currently running programs kept by "#$%. Assign a uni!ue process identifier (=$( to the process. i:opy the conte-t of the parent, the process that re!uested the spawning of the new process. Geturn the new =$( to the parent process. This enables the parent process to e-amine or control the process directly. After the fork is complete, "#$% runs your program. What Happens when you execute a command? /hen you enter 'ls' command to look at the contents of your current working directory, "#$% does a series of things to create an environment for ls and the run it* The shell has "#$% perform a fork. This creates a new process that the shell will use to run the ls program. The shell has "#$% perform an e-ec 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 conte-t, replacing the te-t and data of the shell. The ls program performs its task, listing the contents of the current directory.
What is a 'aemon? A daemon is a process that detaches itself from the terminal and runs, disconnected, in the background, waiting for re!uests and responding to them. $t can also be defined as the background process that does not belong to a terminal session. Hany system functions are commonly performed by daemons, including the sendmail daemon, which handles mail, and the ##T= daemon, which handles "02#2T news. Hany other daemons may e-ist. 0ome of the most common daemons are* init* Takes over the basic running of the system when the kernel has finished the boot process. inetd* Gesponsible for starting network services that do not have their own stand&alone daemons. +or e-ample, inetd usually takes care of incoming rlogin, telnet, and ftp connections. cron* Gesponsible for running repetitive tasks on a regular schedule. What is 'ps' command for? The ps command prints the process status for some or all of the running processes. The information given are the process identification number (=$( ,the amount of time that the process has taken to e-ecute so far etc. How would you &ill a process? The kill command takes the =$( as one argument< this identifies which process to terminate. The =$( of a process can be got using 'ps' command. What is an advanta"e of executin" a process in bac&"round? The most common reason to put a process in the background is to allow you to do something else interactively without waiting for the process to complete. At the end of the command you add the special background symbol, I. This symbol tells your shell to e-ecute the given command in the background. 2-ample* cp J.J ../backupI (cp is for copy How do you execute one pro"ram from within another? The system calls used for low&level process creation are e-eclp( and e-ecvp( . The e-eclp call overlays the e-isting program with the new one , runs that and e-its. The original program gets back control only when an error occurs. e-eclp(path,fileKname,arguments.. < //last argument must be #",, A variant of e-eclp called e-ecvp is used when the number of arguments is not known in advance. e-ecvp(path,argumentKarray < //argument array should be terminated by #",,
What is I%0? What are the various schemes available? The term $=: ($nter&=rocess :ommunication describes various ways by which different process running on some operating system communicate between each other. Larious schemes available are as follows* =ipes* .ne&way communication scheme through which different process can communicate. The problem is that the two processes should have a common ancestor (parent&child relationship . Aowever this problem was fi-ed with the introduction of named&pipes (+$+. . Hessage Mueues * Hessage !ueues can be used between related and unrelated processes running on a machine. 0hared Hemory* This is the fastest of all $=: 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. 9ut this scheme needs synchroni'ation. Larious forms of synchronisation are mute-es, condition&variables, read& write locks, record&locks, and semaphores. What is the difference between 1wappin" and %a"in"? 0wapping* /hole process is moved from the swap device to the main memory for e-ecution. =rocess si'e must be less than or e!ual to the available main memory. $t is easier to implementation and overhead to the system. 0wapping systems does not handle the memory more fle-ibly as compared to the paging systems. =aging* .nly the re!uired memory pages are moved to main memory from the swap device for e-ecution. =rocess si'e does not matter. Eives the concept of the virtual memory. $t provides greater fle-ibility 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 si'e than the available physical memory. (emand paging systems handle the memory more fle-ibly.
What is ma2or difference between the Historic Unix and the new B1' release of Unix 1ystem 3 in terms of 4emory 4ana"ement? Aistoric "ni- uses 0wapping N entire process is transferred to the main memory from the swap device, whereas the "ni- 0ystem L uses (emand
=aging N only the part of the process is moved to the main memory. Aistoric "ni- uses one 0wap (evice and "ni- 0ystem L allow multiple 0wap (evices. What is the main "oal of the 4emory 4ana"ement? $t decides which process should reside in the main memory, Hanages the parts of the virtual address space of a process which is non&core resident, Honitors the available main memory and periodically write the processes into the swap device to provide more processes fit in the main memory simultaneously. What is a 4ap? A Hap 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 +irst&+it allocation of contiguous blocks of a resource. $nitially the Hap contains one entry N address (block offset from the starting of the swap area and the total number of resources. Oernel treats each unit of Hap as a group of disk blocks. .n the allocation and freeing of the resources Oernel updates the Hap for accurate information. What scheme does the 5ernel in Unix 1ystem 3 follow while choosin" a swap device amon" the multiple swap devices? Oernel follows Gound Gobin scheme choosing a swap device among the multiple swap devices in "ni- 0ystem L. What is a 6e"ion? A Gegion is a continuous area of a processPs address space (such as te-t, data and stack . The kernel in a QGegion TableP that is local to the process maintains region. Gegions are sharable among the process. What are the events done by the 5ernel after a process is bein" swapped out from the main memory? /hen Oernel swaps the process out of the primary memory, it performs the following* Oernel decrements the Geference :ount of each region of the process. $f the reference count becomes 'ero, swaps the region out of the main memory, Oernel allocates the space for the swapping process in the swap device, Oernel locks the other swapping process while the current swapping operation is going on, The Oernel saves the swap address of the region in the region table.
Is the %rocess before and after the swap are the same? 7ive reason) =rocess before swapping is residing in the primary memory in its original form. The regions (te-t, data and stack may not be occupied fully by the process, there may be few empty slots in any of the regions and while swapping Oernel do not bother about the empty slots while swapping the process out. After swapping the process resides in the swap (secondary memory device. The regions swapped out will be present but only the occupied region slots but not the empty slots that were present before assigning. /hile swapping the process once again into the main memory, the Oernel referring to the =rocess Hemory Hap, it assigns the main memory accordingly taking care of the empty slots in the regions. What do you mean by u8area +user area, or u8bloc&? This contains the private data that is manipulated only by the Oernel. This is local to the =rocess, i.e. each process is allocated a u&area. What are the entities that are swapped out of the main memory while swappin" the process out of the main memory? All memory space occupied by the process, processPs u&area, and Oernel stack are swapped out, theoretically. =ractically, if the processPs u&area contains the Address Translation Tables for the process then Oernel implementations do not swap the u&area. What is #or& swap? fork( is a system call to create a child process. /hen the parent process calls fork( system call, the child process is created and if there is short of memory then the child process is sent to the read&to&run state in the swap device, and return to the user state without swapping the parent process. /hen the memory will be available the child process will be swapped into the main memory. What is *xpansion swap? At the time when any process re!uires more memory than it is currently allocated, the Oernel performs 2-pansion swap. To do this Oernel reserves enough space in the swap device. Then the address translation mapping is adjusted for the new virtual address space but the physical memory is not allocated. At last Oernel swaps the process into the assigned space in the swap device. ,ater when the Oernel swaps the process into the main memory this assigns memory according to the new address translation mapping.
How the 1wapper wor&s? The swapper is the only process that swaps the processes. The 0wapper operates only in the Oernel mode and it does not uses 0ystem calls instead it uses internal Oernel functions for swapping. $t is the archetype of all kernel process. What are the processes that are not bothered by the swapper? 7ive 6eason) Fombie process* They do not take any up physical memory. =rocesses locked in memories that are updating the region of the process. Oernel swaps only the sleeping processes rather than the Qready&to&runP processes, as they have the higher probability of being scheduled than the 0leeping processes. What are the re(uirements for a swapper to wor&? The swapper works on the highest scheduling priority. +irstly it will look for any sleeping process, if not found then it will look for the ready&to&run process for swapping. 9ut the major re!uirement for the swapper to work the ready&to&run process must be core&resident for at least 1 seconds before swapping out. And for swapping in the process must have been resided in the swap device for at least 1 seconds. $f the re!uirement is not satisfied then the swapper will go into the wait state on that event and it is awaken once in a second by the Oernel. What are the criteria for choosin" a process for swappin" into memory from the swap device? The resident time of the processes in the swap device, the priority of the processes and the amount of time the processes had been swapped out. What are the criteria for choosin" a process for swappin" out of the memory to the swap device? The processPs memory resident time, =riority of the process and The nice value. What do you mean by nice value? #ice value is the value that controls ?increments or decrementsC the priority of the process. This value that is returned by the nice ( system call. The e!uation for using nice value is* =riority R (Srecent :=" usageT/constant 7
(base& priority 7 (nice value .nly the administrator can supply the nice value. The nice ( system call works for the running process only. #ice value of one process cannot affect the nice value of the other process. What are conditions on which deadloc& can occur while swappin" the processes? All processes in the main memory are asleep. All Qready&to&runP processes are swapped out. There is no space in the swap device for the new incoming process that are swapped out of the main memory. There is no space in the main memory for the new incoming process. What are conditions for a machine to support 'emand %a"in"? Hemory architecture must based on =ages, The machine must support the QrestartableP instructions. What is 9the principle of locality:? $tPs the nature of the processes that they refer only to the small subset of the total data space of the process. i.e. the process fre!uently calls the same subroutines or e-ecutes the loop instructions. What is the wor&in" set of a process? The set of pages that are referred by the process in the last QnP, references, where QnP is called the window of the working set of the process. What is the window of the wor&in" set of a process? The window of the working set of a process is the total number in which the process had referred the set of pages in the working set of the process. What is called a pa"e fault? =age fault is referred to the situation when the process addresses a page in the working set of the process but the process fails to locate the page in the working set. And on a page fault the kernel updates the working set by reading the page from the secondary device. What are data structures that are used for 'emand %a"in"? Oernel contains 6 data structures for (emand paging. They are, =age table entries,
(isk block descriptors, =age frame data table (pfdata , 0wap&use table. What are the bits that support the demand pa"in"? Lalid, Geference, Hodify, :opy on write, Age. These bits are the part of the page table entry, which includes physical address of the page and protection bits. =age address Age :opy on write Hodify Geference Lalid =rotection How the 5ernel handles the for&+, system call in traditional Unix and in the 1ystem 3 Unix; while swappin"? Oernel in traditional "ni-, makes the duplicate copy of the parentPs address space and attaches it to the childPs process, while swapping. Oernel in 0ystem L "ni-, manipulates the region tables, page table, and pfdata table entries, by incrementing the reference count of the region table of shared regions. 'ifference between the for&+, and vfor&+, system call? (uring the fork( system call the Oernel makes a copy of the parent processPs address space and attaches it to the child process. 9ut the vfork( system call do not makes any copy of the parentPs address space, so it is faster than the fork( system call. The child process as a result of the vfork( system call e-ecutes e-ec( system call. The child process from vfork( system call e-ecutes in the parentPs address space (this can overwrite the parentPs data and stack which suspends the parent process until the child process e-its. What is B11+Bloc& 1tarted by 1ymbol,? A data representation at the machine level, that has initial values when a program starts and tells about how much space the kernel allocates for the un&initiali'ed data. Oernel initiali'es it to 'ero at run&time.
This is the Oernel process that makes rooms for the incoming pages, by swapping the memory pages that are not the part of the working set of a process. =age&0tealer is created by the Oernel at the system initiali'ation and invokes it throughout the lifetime of the system. Oernel locks a region when a process faults on a page in the region, so that page stealer cannot steal the page, which is being faulted in. Name two pa"in" states for a pa"e in memory? The two paging states are* The page is aging and is not yet eligible for swapping, The page is eligible for swapping but not yet eligible for reassignment to other virtual address space. What are the phases of swappin" a pa"e from the memory? =age stealer finds the page eligible for swapping and places the page number in the list of pages to be swapped. Oernel copies the page to a swap device when necessary and clears the valid bit in the page table entry, decrements the pfdata reference count, and places the pfdata table entry at the end of the free list if its reference count is 5. What is pa"e fault? Its types? =age fault refers to the situation of not having a page in the main memory when any process references it. There are two types of page fault * Lalidity fault, =rotection fault. In what way the #ault Handlers and the Interrupt handlers are different? +ault handlers are also an interrupt handler with an e-ception that the interrupt handlers cannot sleep. +ault handlers sleep in the conte-t of the process that caused the memory fault. The fault refers to the running process and no arbitrary processes are put to sleep. What is validity fault? $f a process referring a page in the main memory whose valid bit is not set, it results in validity fault. The valid bit is not set for those pages* that are outside the virtual address space of a process, that are the part of the virtual address space of the process but no physical address is assigned to it.
What does the swappin" system do if it identifies the ille"al pa"e for swappin"? $f the disk block descriptor does not contain any record of the faulted page, then this causes the attempted memory reference is invalid and the kernel sends a S0egmentation violationT signal to the offending process. This happens when the swapping system identifies any invalid memory reference. What are states that the pa"e can be in; after causin" a pa"e fault? .n a swap device and not in memory, .n the free page list in the main memory, $n an e-ecutable file, Harked Sdemand 'eroT, Harked Sdemand fillT. In what way the validity fault handler concludes? $t sets the valid bit of the page by clearing the modify bit. $t recalculates the process priority. $t what mode the fault handler executes? At the Oernel Hode. What do you mean by the protection fault? =rotection fault refers to the process accessing the pages, which do not have the access permission. A process also incur the protection fault when it attempts to write a page whose copy on write bit was set during the fork( system call. How the 5ernel handles the copy on write bit of a pa"e; when the bit is set? $n situations like, where the copy on write bit of a page is set and that page is shared by more than one process, the Oernel allocates new page and copies the content to the new page and the other processes retain their references to the old page. After copying the Oernel updates the page table entry with the new page number. Then Oernel decrements the reference count of the old pfdata table entry. $n cases like, where the copy on write bit is set and no processes are sharing the page, the Oernel allows the physical page to be reused by the processes. 9y doing so, it clears the copy on write bit and disassociates the page from its disk copy (if one e-ists , because other process may share the disk copy. Then it removes the pfdata table
entry from the page&!ueue as the new copy of the virtual page is not on the swap device. $t decrements the swap&use count for the page and if count drops to 5, frees the swap space. #or which &ind of fault the pa"e is chec&ed first? The page is first checked for the validity fault, as soon as it is found that the page is invalid (valid bit is clear , the validity fault handler returns immediately, and the process incur the validity page fault. Oernel handles the validity fault and the process will incur the protection fault if any one is present. In what way the protection fault handler concludes? After finishing the e-ecution of the fault handler, it sets the modify and protection bits and clears the copy on write bit. $t recalculates the process& priority and checks for signals. How the 5ernel handles both the pa"e stealer and the fault handler? The page stealer and the fault handler thrash because of the shortage of the memory. $f the sum of the working sets of all processes is greater that the physical memory then the fault handler will usually sleep because it cannot allocate pages for a process. This results in the reduction of the system throughput because Oernel spends too much time in overhead, rearranging the memory in the frantic pace. *xplain different types of Unix systems) The most widely used are* 3. 0ystem L (ATIT 1. A$% ($9H ;. 90( (9erkeley 6. 0olaris (0un U. %eni- ( A =: version of "ni*xplain &ernal and shell) Oernal* $t carries out basic operating system functions such as allocating memory, accessing files and handling communications. 0hell*A shell provides the user interface to the kernal.There are ; major shells * :&shell, 9ourne shell , Oorn shell What is ex and vi ? e- is "ni- line editor and vi is the standard "ni- screen editor.
Which are typical system directories below the root directory? (3 /bin* contains many programs which will be e-ecuted by users (1 /etc * files used by administrator (; /dev* hardware devices (6 /lib* system libraries (U /usr* application software (4 /home* home directories for different systems. 0onstruct pipes to execute the followin" 2obs) 3. .utput of who should be displayed on the screen with value of total number of users who have logged in displayed at the bottom of the list. 1. .utput of ls should be displayed on the screen and from this output the lines containing the word QpoemP should be counted and the count should be stored in a file. ;. :ontents of file3 and file1 should be displayed on the screen and this output should be appended in a file . +rom output of ls the lines containing QpoemP should be displayed on the screen along with the count. 6. #ame of cities should be accepted from the keyboard . This list should be combined with the list present in a file. This combined list should be sorted and the sorted list should be stored in a file QnewcityP. U. All files present in a directory dir3 should be deleted any error while deleting should be stored in a file QerrorlogP. *xplain the followin" commands) V V V V V V V ls W file3 banner hi&fi W message cat par.; par.6 par.U WW report cat file3Wfile3 date < who date < who W logfile (date < who W logfile
What is the si"nificance of the <tee= command? $t reads the standard input and sends it to the standard output while redirecting a copy of what it has read to the file specified by the user. What does the command < >who ? sort @lo"file A newfile= do? The input from a pipe can be combined with the input from a file . The trick is to use the special symbol S&S (a hyphen for those commands that recogni'e the hyphen as std input.
$n the above command the output from who becomes the std input to sort , meanwhile sort opens the file logfile, the contents of this file is sorted together with the output of who (rep by the hyphen and the sorted output is redirected to the file newfile. What does the command <>ls ? wc @l A fileB= do? ls becomes the input to wc which counts the number of lines it receives as input and instead of displaying this count , the value is stored in file3. Which of the followin" commands is not a filter man ; +b, cat ; +c, p" ; +d, head man 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. How is the command <>cat fileC < different from <>cat AfileC and AA redirection operators ? is the output redirection operator when used it overwrites while WW operator appends into the file. *xplain the steps that a shell follows while processin" a command) After the command line is terminated by the key, the shell goes ahead with processing the command line in one or more passes. The se!uence is well defined and assumes the following order. =arsing* The shell first breaks up the command line into words, using spaces and the delimiters, unless !uoted. All consecutive occurrences of a space or tab are replaced here with a single space. Lariable evaluation* All words preceded by a V are valuated as variables, unless !uoted or escaped. :ommand substitution* Any command surrounded by back !uotes is e-ecuted by the shell which then replaces the standard output of the command into the command line. /ild&card interpretation* The shell finally scans the command line for wild& cards (the characters J, X, Y, Z . Any word containing a wild&card is replaced by a sorted list of filenames that match the pattern. The list of these filenames then forms the arguments to the command. =ATA evaluation* $t finally looks for the =ATA variable to determine the se!uence of directories it has to search in order to hunt for the command.
What difference between cmp and diff commands? cmp & :ompares two files byte by byte and displays the first mismatch diff & tells the changes to be made to make the files identical What is the use of 9"rep: command? QgrepP is a pattern search command. $t searches for the pattern, specified in the command line with appropriate option, in a file(s . 0ynta- * grep 2-ample * grep [[m- mcafile What is the difference between cat and more command? :at displays file contents. $f the file is large the contents scroll off the screen before we view it. 0o command 'more' is like a pager which displays the contents page by page. Write a command to &ill the last bac&"round 2ob? Oill VB Which command is used to delete all files in the current directory and all its sub8directories? rm &r J Write a command to display a file:s contents in various formats? Vod &cbd fileKname c & character, b & binary (octal , d&decimal, odR.ctal (ump. What will the followin" command do? V echo J $t is similar to 'ls' command and displays all the files in the current directory. Is it possible to create new a file system in UNIX? \es, QmkfsP is used to create a new file system. Is it possible to restrict incomin" messa"e? \es, using the QmesgP command.
What is the use of the command Dls 8x chapterEB8FGD ls stands for list< so it displays the list of the files that starts with 'chapter' with suffi- '3' to 'U', chapter3, chapter1, and so on. Is 9du: a command? If so; what is its use? \es, it stands for Qdisk usageP. /ith the help of this command you can find the disk capacity and free space of the disk. Is it possible to count number char; line in a fileH if so; How? \es, wc&stands for word count. wc &c for counting number of characters in a file. wc &l for counting lines in a file. Name the data structure used to maintain file identification? QinodeP, each file has a separate inode and a uni!ue inode number. How many prompts are available in a UNIX system? Two prompts, =03 (=rimary =rompt , =01 (0econdary =rompt . How does the &ernel differentiate device files and ordinary files? Oernel checks 'type' field in the file's inode structure. How to switch to a super user status to "ain privile"es? "se QsuP command. The system asks for password and when valid entry is made the user gains super user (admin privileges. What are shell variables? 0hell variables are special variables, a name&value pair created and maintained by the shell. 2-ample* =ATA, A.H2, HA$, and T2GH What is redirection? (irecting the flow of data to the file or from the file for input or output. 2-ample * ls W wc How to terminate a process which is runnin" and the specialty on command &ill I? /ith the help of kill command we can terminate the process.
0ynta-* kill pid Oill 5 & kills all processes in your system e-cept the login shell. What is a pipe and "ive an example? A pipe is two or more commands separated by pipe char ']'. That tells the shell to arrange for the output of the preceding command to be passed as input to the following command. 2-ample * ls &l ] pr The output for a command ls is the standard input of pr. /hen a se!uence of commands are combined using pipe, then it is called pipeline. *xplain &ill+, and its possible return values) There are four possible results from this call* Qkill( P returns 5. This implies that a process e-ists with the given =$(, and the system would allow you to send signals to it. $t is system&dependent whether the process could be a 'ombie. Qkill( P returns &3, Qerrno RR 20G:AP either no process e-ists with the given =$(, or security enhancements are causing the system to deny its e-istence. (.n some systems, the process could be a 'ombie. Qkill( P returns &3, Qerrno RR 2=2GHP the system would not allow you to kill the specified process. This means that either the process e-ists (again, it could be a 'ombie or draconian security enhancements are present (e.g. your process is not allowed to send signals to JanybodyJ . Qkill( P returns &3, with some other value of QerrnoP you are in troubleB The most&used techni!ue is to assume that success or failure with Q2=2GHP implies that the process e-ists, and any other error implies that it doesn't. An alternative e-ists, if you are writing specifically for a system (or all those systems that provide a Q/procP filesystem* checking for the e-istence of Q/proc/=$(P may work.