22
1. Describe the following with respect to Deadlocks in Operating Systems: a. Deadlock Avoidance b. Deadlock Prevention Answer A) Deadlock avoidance Deadlock avoidance is to avoid deadlock by only granting resources if granting them cannot result in a deadlock situation later. However, this works only if the system knows what requests for resources a process will be making in the future, and this is an unrealistic assumption. The text describes the bankers algorithm but then points out that it is essentially impossible to implement because of this a ssumption. B) Deadlock Prevention The difference between deadlock avoidance and deadlock prevention is a little subtle. Deadlock avoidance refers to a strategy where whenever a resource is requested, it is only granted if it cannot result in deadlock. Deadlock prevention strategies involve changing the rules so that processes will not make requests that could result in deadlock. Here is a simple example of such a strategy. Suppose every possible resource is numbered (easy enough in theory, but often hard in practice), and processes must make their requests in order; that is, they cannot request a resource with a number lower than any of the resources that they have been granted so far. Deadlock cannot occur in this situation. As an example, consider the dining philosophers problem. Suppose each chopstick is numbered, and philosophers always have to pick up the lower numbered chopstick before the higher numbered chopstick. Philosopher five picks up chopstick 4, philosopher 4 picks up chopstick 3, philosopher 3 picks up chopstick 2, philosopher 2 picks up chopstick 1. Philosopher 1 is hungry, and without this a ssumption, would pick up chopstick 5, thus causing deadlock. However, if the lower number rule is in effect, he/she has to pick up chopstick 1 first, and it is already in use, so he/she is blocked. Philosopher 5 picks up chopstick 5, eats, and puts both down, allows philosopher 4 to eat. Eventually everyone gets to eat. An alternative strategy is to require all processes to request all of their resources at once, and either all are granted or none are granted. Like the above strategy, this is conceptually easy but often hard to implement in practice because it assumes that a process knows what resources it will need in advance. 2. Discuss the following with respect to I/O in Operating Systems: a. I/O Control Strategies b. Program - controlled I/O c. Interrupt - controlled I/O Answer a. I/O Control Strategies Several I/O strategies are used between the computer system and I/O devices, depending on the relative speeds of the computer system and the I/O devices. The simplest strategy is to use the processor itself as the I/O controller, and to require that the device follow a strict order of events under direct program control, with the processor waiting for the I/O device at each step. Another strategy is to allow the processor to be ``interrupted'' by the I/O devices, and to have a (possibly different) ``interrupt handling routine'' for each device. This allows for more flexible scheduling of I/O events, as well as more efficient use of the processor. (Interrupt handling is an important component of the operating system.)

MC0070 -Set 2

Embed Size (px)

Citation preview

Page 1: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 1/22

1. Describe the following with respect to Deadlocks in Operating Systems:

a. Deadlock Avoidance

b. Deadlock Prevention

Answer

A) Deadlock avoidance

Deadlock avoidance is to avoid deadlock by only granting resources if granting them cannot result in adeadlock situation later. However, this works only if the system knows what requests for resources aprocess will be making in the future, and this is an unrealistic assumption. The text describes the bankersalgorithm but then points out that it is essentially impossible to implement because of this assumption.

B) Deadlock PreventionThe difference between deadlock avoidance and deadlock prevention is a little subtle. Deadlock avoidance refers to a strategy where whenever a resource is requested, it is only granted if it cannot resultin deadlock. Deadlock prevention strategies involve changing the rules so that processes will not makerequests that could result in deadlock.

Here is a simple example of such a strategy. Suppose every possible resource is numbered (easyenough in theory, but often hard in practice), and processes must make their requests in order; that is, they

cannot request a resource with a number lower than any of the resources that they have been granted sofar. Deadlock cannot occur in this situation.

As an example, consider the dining philosophers problem. Suppose each chopstick is numbered,and philosophers always have to pick up the lower numbered chopstick before the higher numberedchopstick. Philosopher five picks up chopstick 4, philosopher 4 picks up chopstick 3, philosopher 3 picksup chopstick 2, philosopher 2 picks up chopstick 1. Philosopher 1 is hungry, and without this assumption,would pick up chopstick 5, thus causing deadlock. However, if the lower number rule is in effect, he/shehas to pick up chopstick 1 first, and it is already in use, so he/she is blocked. Philosopher 5 picks upchopstick 5, eats, and puts both down, allows philosopher 4 to eat. Eventually everyone gets to eat.

An alternative strategy is to require all processes to request all of their resources at once, and either all aregranted or none are granted. Like the above strategy, this is conceptually easy but often hard to implement

in practice because it assumes that a process knows what resources it will need in advance.

2. Discuss the following with respect to I/O in Operating Systems:

a. I/O Control Strategies

b. Program - controlled I/O

c. Interrupt - controlled I/O

Answer

a. I/O Control Strategies

Several I/O strategies are used between the computer system and I/O devices, depending on the relativespeeds of the computer system and the I/O devices. The simplest strategy is to use the processor itself as

the I/O controller, and to require that the device follow a strict order of events under direct programcontrol, with the processor waiting for the I/O device at each step.

Another strategy is to allow the processor to be ``interrupted'' by the I/O devices, and to have a (possiblydifferent) ``interrupt handling routine'' for each device. This allows for more flexible scheduling of I/Oevents, as well as more efficient use of the processor. (Interrupt handling is an important component of the operating system.)

Page 2: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 2/22

A third general I/O strategy is to allow the I/O device, or the controller for the device, access to the mainmemory. The device would write a block of information in main memory, without intervention from theCPU, and then inform the CPU in some way that that block of memory had been overwritten or read. Thismight be done by leaving a message in memory, or by interrupting the processor. (This is generally theI/O strategy used by the highest speed devices -- hard disks and the video controller.)

b. Program - controlled I/O

One common I/O strategy is program-controlled I/O, (often called polled I/O). Here all I/O is performedunder control of an ``I/O handling procedure,'' and input or output is initiated by this procedure.

The I/O handling procedure will require some status information (handshaking information) from the I/Odevice (e.g., whether the device is ready to receive data). This information is usually obtained through asecond input from the device; a single bit is usually sufficient, so one input ``port'' can be used to collectstatus, or handshake, information from several I/O devices. (A  port is the name given to a connection toan I/O device; e.g., to the memory location into which an I/O device is mapped). An I/O port is usuallyimplemented as a register (possibly a set of D flip flops) which also acts as a buffer between the CPU andthe actual I/O device. The word port is often used to refer to the buffer itself.

Typically, there will be several I/O devices connected to the processor; the processor checks the ``status''input port periodically, under program control by the I/O handling procedure. If an I/O device requiresservice, it will signal this need by altering its input to the ``status'' port. When the I/O control programdetects that this has occurred (by reading the status port) then the appropriate operation will be performedon the I/O device which requested the service. A typical configuration might look somewhat as shown inFigure . The outputs labeled ``handshake in'' would be connected to bits in the ``status'' port. The inputlabeled ``handshake in'' would typically be generated by the appropriate decode logic when the I/O portcorresponding to the device was addressed.

Page 3: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 3/22

 Figure: Program controlled I/O

Program-controlled I/O has a number of advantages:

  All control is directly under the control of the program, so changes can be readily implemented.  The order in which devices are serviced is determined by the program, this order is not

necessarily fixed but can be altered by the program, as necessary. This means that the ``priority''of a device can be varied under program control. (The ``priority'' of a determines which of a set of devices which are simultaneously ready for servicing will actually be serviced first).

  It is relatively easy to add or delete devices.

Perhaps the chief disadvantage of program-controlled I/O is that a great deal of time may be spent testingthe status inputs of the I/O devices, when the devices do not need servicing. This ``busy wait'' or ``waitloop'' during which the I/O devices are polled but no I/O operations are performed is really time wastedby the processor, if there is other work which could be done at that time. Also, if a particular device hasits data available for only a short time, the data may be missed because the input was not tested at the

appropriate time

c. Interrupt - controlled I/O

Interrupt-controlled I/O reduces the severity of the two problems mentioned for program-controlled I/Oby allowing the I/O device itself to initiate the device service routine in the processor. This isaccomplished by having the I/O device generate an interrupt signal which is tested directly by thehardware of the CPU. When the interrupt input to the CPU is found to be active, the CPU itself initiates a

Page 4: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 4/22

subprogram call to somewhere in the memory of the processor; the particular address to which theprocessor branches on an interrupt depends on the interrupt facilities available in the processor.

The simplest type of interrupt facility is where the processor executes a subprogram branch to somespecific address whenever an interrupt input is detected by the CPU. The return address (the location of the next instruction in the program that was interrupted) is saved by the processor as part of the interrupt

process.

If there are several devices which are capable of interrupting the processor, then with this simple interruptscheme the interrupt handling routine must examine each device to determine which one caused theinterrupt. Also, since only one interrupt can be handled at a time, there is usually a hardware ``priorityencoder'' which allows the device with the highest priority to interrupt the processor, if several devicesattempt to interrupt the processor simultaneously. In Figure 4.8, the ``handshake out'' outputs would beconnected to a priority encoder to implement this type of I/O. the other connections remain the same.(Some systems use a ``daisy chain'' priority system to determine which of the interrupting devices isserviced first. ̀ `Daisy chain'' priority resolution is discused later.)

In most modern processors, interrupt return points are saved on a ``stack'' in memory, in the same way asreturn addresses for subprogram calls are saved. In fact, an interrupt can often be thought of as asubprogram which is invoked by an external device. If a stack is used to save the return address forinterrupts, it is then possible to allow one interrupt the interrupt handling routine of another interrupt. Inmodern computer systems, there are often several ``priority levels'' of interrupts, each of which can bedisabled, or ``masked.'' There is usually one type of interrupt input which cannot be disabled (a non-maskable interrupt) which has priority over all other interrupts. This interrupt input is used for warningthe processor of potentially catastrophic events such as an imminent power failure, to allow the processorto shut down in an orderly way and to save as much information as possible.

Most modern computers make use of ``vectored interrupts.'' With vectored interrupts, it is theresponsibility of the interrupting device to provide the address in main memory of the interrupt servicingroutine for that device. This means, of course, that the I/O device itself must have sufficient``intelligence'' to provide this address when requested by the CPU, and also to be initially ``programmed''with this address information by the processor. Although somewhat more complex than the simpleinterrupt system described earlier, vectored interrupts provide such a significant advantage in interrupthandling speed and ease of implementation (i.e., a separate routine for each device) that this method isalmost universally used on modern computer systems.

Some processors have a number of special inputs for vectored interrupts (each acting much like thesimple interrupt described earlier). Others require that the interrupting device itself provide the interruptaddress as part of the process of interrupting the processor.

Page 5: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 5/22

3. Discuss various conditions to be true for deadlock to occur.

Answer

In order for deadlock to occur, four conditions must be true.

  Mutual exclusion - Each resource is either currently allocated to exactly one process or it isavailable. (Two processes cannot simultaneously control the same resource or be in their criticalsection).

  Hold and Wait - processes currently holding resources can request new resources  No preemption - Once a process holds a resource, it cannot be taken away by another process or

the kernel.  Circular wait - Each process is waiting to obtain a resource which is held by another process.

Mutual exclusion

It is one of the characteristics of deadlock. When semaphores are used or mutual exclusion, thesemaphore has an initial value of 1, and P() is called before the critical section, and V() is called after thecritical section as shown below :

  semaphore-> P();  critical section  semaphore-> V();  remainder sectionlet us suppose that one process A is already executing its critical section then it implies that

semaphore value at that time is zero. If process B now tries to enter this critical section , it cannot enterthe critical section because it will have to wait before semaphore becomes greater than zero. This ispossible only when process A executes its signal operation; after executing its critical section.

Hold and Wait

  Attacking the Hold and Wait Condition; can a process hold a resource and ask for another? Canwe require processes to request all resources at once? Most processes do not statically knowabout the resources they need.

  To ensure that the hold-and-wait condition never occurs in the system, we must guarantee that,whenever a process requests a resource, it does not hold any other resources.

  One protocol that can be used requires each process to request and be allocated all its resourcesbefore it begins execution.

  An alternative protocol allows a process to request resources only when it has none. A processmay request some resources and use them. Before it can request any additional resources,however, it must release all the resources that it is currently allocated.

  To illustrate the difference between these two protocols, we consider a process that copies datafrom a DVD drive to a file on disk, sorts the file, and then prints the results to a printer.

  Both these protocols have two main disadvantages.o  First, resource utilization may be low, since resources may be allocated but unused for a

long period.o  Second, starvation is possible. A process that needs several popular resources may have

to wait indefinitely, because at least one of the resources that it needs is always allocatedto some other process.

Page 6: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 6/22

No Preemption

  Attacking the No Preemption Condition; Can resources be preempted? If a process' requests(holding certain resources) is denied, that process must release its unused resources and requestthem again, together with the additional resource.

  The third necessary condition for deadlocks is that there be no preemption of resources that have

already been allocated.  To ensure that this condition does not hold, we can use the following protocol.

o  If a process is holding some resources and requests another resource that cannot beimmediately allocated to it (that is, the process must wait), then all resources currentlybeing held are preempted.

o  The preempted resources are added to the list of resources for which the process iswaiting. The process will be restarted only when it can regain its old resources, as well asthe new ones that it is requesting.

Circular Wait

  Attacking the Circular Wait Condition; Can circular waits exist? Order resources by an index:

; requires that resources are always requested in order.  One way to ensure that this condition never holds is to impose a total ordering of all resource

types and to require that each process requests resources in an increasing order of enumeration.  Assign to each resource type a unique integer number, which allows us to compare two resources

and to determine whether one precedes another in our ordering.  Each process can request resources only in an increasing order of enumeration.  If these two protocols are used, then the circular-wait condition cannot hold.

4. Explain the following with respect to data files in UNIX:

a. Using Metacharacters When Referring to Filenames

b. File Substitution methods

Answer

A)  Using Metacharacters When Referring to Filenames

Sometimes it is useful to refer to several files without having to name each one of them. Likewise, if youcan remember only part of a filename, it is useful to list all the files whose names contain that part. UNIXprovides metacharacters, also known as wildcards, which enable you to refer to files in these ways.There are two metacharacters: the question mark (?) and the asterisk (*). In addition to metacharacters,filename substitution can be done on character sets.

Pattern Matching on a Single Character

In filename substitution, the question mark (?) stands for any single character. Consider the followingdirectory:$ls21x LINES.dat LINES.idx PAGES.dat PAGES.idxacct.pds marsha.pds p10 p101 p11

Page 7: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 7/22

t11 z11

You can use the question mark (?) in any position. For example,

$ls ?11p11 t11 z11

You can also use more than one question mark in a single substitution.For example,

$ls p??p10 p11

The following command gives you all three-character filenames:

$ls ???

21x p10 p11 t11 z11

Suppose that you wanted to list all of the files that begin with LINES. We could do this successfully with

$ls LINES ..??LINES.dat LINES.idx

Now suppose that you wanted to find the files that end in .pds. The following two commands illustratehow to do this:

$ ls ????.pdsacct.pds$$

ls ?????.pdsmarsha.pds

Pattern Matching on a Group of CharactersIn the previous example, to list all of the files ending in .pds using single character substitution, youwould have to know exactly how many characters precede the period. To overcome this problem, you usethe asterisk (*), which matches a character string of any length, including a length of zero. Consider thefollowing two examples:

$ls*.pdsacct.pds marsha.pds$ls p10*

p10 p101

As with single character substitution, more than one asterisk (*) can be used in a single substitution. Forexample,

$ls*.*LINES.dat LINES.idx PAGES.dat PAGES.idx  acct.pds 

Page 8: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 8/22

Pattern Matching on Character SetsYou have seen how you can access a group of files whose names are similar. What do you do, though, if you need to be more specific? Another way to do filename substitution is by matching on character sets.A character set is any number of single alphanumeric characters enclosed in square brackets  – [ and ].Suppose that you wanted a list of all the filenames that start with p or t followed by 11. You could use thefollowing command:

$ls [pt]11p11 t11

You can combine character sets with the metacharacters. To list the names of all the files thatbegin with p or t, you could use

$ls [pt]*P10 p101 p11 t11

Now suppose that you wanted a list of all the filenames that begin with an uppercase alphabeticcharacter. You could use

$ ls [ABCDEFGHIJKLMNOPQRSTUVWXYZ]*LINES.dat LINES.idx PAGES.dat PAGES.idx

If you’re guessing that there might be a better way to do this, you’re right. When the characters ina character set substitution are in sequence, you can use a hyphen (-) to denote all of the characters in thesequence. Therefore, you can abbreviate the previous command in this way:

$ ls [A-Z]*

If a character sequence is broken, you can still use the hyphen for the portion of the character set that is insequence. For example, the following command lists all the three-character filenames that begin with

p, q, r, s, t, and z:$ ls [p-tz]??p10 p11 t11 z11

B) File substitution methodsIt is important to understand how file substitution actually works. In the previous examples, the lscommand doesn’t do the work of file substitution – the shell does. Even though all the previous examplesemploy the ls command, any command that accepts filenames on the command line can use filesubstitution. In fact, using the simple echo command is a good way to experiment with file substitutionwithout having to worry about unexpected results. For example,

$ echo p*p10 p101 p11

When a metacharacter is encountered in a UNIX command, the shell looks for patterns in filenames thatmatch the metacharacter. When a match is found, the shell substitutes the actual filename in place of thestring containing the metacharacter so that the command sees only a list of valid filenames. If the shellfinds no filenames that match the pattern, it passes an empty string to the command.

Page 9: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 9/22

The shell can expand more than one pattern on a single line. Therefore, the shell interprets the command

$ ls LINES.* PAGES.*as$ ls LINES.dat LINES.idx PAGES.dat PAGES.idx

There are file substitution situations that you should be wary of. You should be careful about the use of whitespace (extra blanks) in a command line. If you enter the following command, for example, theresults might surprise you:

$ls LINES.*LINES.:not found21x LINES.dat LINES.idx PAGES.dat PAGES.idxacct.pds marsha.pds p10 p11t11 z11

What has happened is that the shell interpreted the first parameter as the filename LINES. with nometacharacters and passed it directly on to ls. Next, the shell saw the single asterisk (*), and matched it to

any character string, which matches every file in the directory. This is not a big problem if you are simplylisting the files, but it could mean disaster if you were using the command to delete data files! Unusualresults can also occur if you use the period (.) in a shell command. Suppose that you are using the

$ ls .*

command to view the hidden files. What the shell would see after it finishes interpreting themetacharacter is

$ ls . .. .profile

which gives you a complete directory listing of both the current and parent directories. When you think 

about how filename substitution works, you might assume that the default form of the ls command isactually

$ ls *

However, in this case the shell passes to ls the names of directories, which causes ls to list all the files inthe subdirectories. The actual form of the default ls command is

$ ls .

The find CommandOne of the wonderful things about UNIX is its unlimited path names. A directory can have a subdirectorythat itself has a subdirectory, and so on. This provides great flexibility in organizing your data.

Unlimited path names have a drawback, though. To perform any operation on a file that is not in yourcurrent working directory, you must have its complete path name. Disk files are a lot like flashlights: Youstore them in what seem to be perfectly logical places, but when you need them again, you can’tremember where you put them. Fortunately, UNIX has the find command.

Page 10: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 10/22

The find command begins at a specified point on a directory tree and searches all lower branches for filesthat meet some criteria. Since find searches by path name, the search crosses file systems, including thoseresiding on a network, unless you specifically instruct it otherwise. Once it finds a file, find can performoperations on it.

Suppose you have a file named urgent.todo, but you cannot remember the directory where you stored it.

You can use the find command to locate the file.

$ find / -name urgent.todo -print /usr/home/stuff/urgent.todo

The syntax of the find command is a little different, but the remainder of this section should clear up anyquestions.

The find command is different from most UNIX commands in that each of the argumentexpressions following the beginning path name is considered a Boolean expression. At any given stopalong a branch, the entire expression is true  – file found – if all of the expressions are true; or false – filenot found  –  if any one of the expressions is false. In other words, a file is found only if all the searchcriteria are met. For example,

$ find /usr/home -user marsha -size +50

is true for every file beginning at /usr/home that is owned by Marsha and is larger than 50 blocks. It is nottrue for Marsha’s files that are 50 or fewer blocks long, nor is it true for large files owned by someoneelse.

An important point to remember is that expressions are evaluated from left to right. Since the entireexpression is false if any one expression is false, the program stops evaluating a file as soon as it fails topass a test. In the previous example, a file that is not owned by Marsha is not evaluated for its size. If theorder of the expressions is reversed, each file is evaluated first for size, and then for ownership.

Another unusual thing about the find command is that it has no natural output. In the previous example,find dutifully searches all the paths and finds all of Marsha’s large files, but it takes no action. For the find command to be useful, you must specify an expression that causes an action to be taken. Forexample,

$ find /usr/home -user me -size +50 -print /usr/home/stuff/bigfile /usr/home/trash/bigfile.old

first finds all the files beginning at /usr/home that are owned by me and are larger than 50 blocks. Then itprints the full path name.

The argument expressions for the find command fall into three categories:

· Search criteria· Action expressions· Search qualifiers

Although the three types of expressions have different functions, each is still considered a Booleanexpression and must be found to be true before any further evaluation of the entire expression can takeplace. (The significance of this is discussed later.) Typically, a find operation consists of one or more

Page 11: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 11/22

search criteria, a single action expression, and perhaps a search qualifier. In other words, it finds a file andtakes some action, even if that action is simply to print the path name. The rest of this section describeseach of the categories of the find options.

Search Criteria

The first task of the find command is to locate files according to some user-specified criteria. You cansearch for files by name, file size, file ownership, and several other characteristics.

Finding Files with a Specific Name: -name fname

Often, the one thing that you know about a file for which you’re searching is its name. Suppose that youwanted to locate — and possibly take some action on — all the files named core. You might use thefollowing command:

$ find / -name core – print

This locates all the files on the system that exactly match the name core, and it prints their complete path

names.

The -name option makes filename substitutions. The command

$ find /usr/home -name "*.tmp" – print

prints the names of all the files that end in .tmp. Notice that when filename substitutions are used, thesubstitution string is enclosed in quotation marks. This is because the UNIX shell attempts to makefilename substitutions before it invokes the command. If the quotation marks were omitted from "*.tmp"and if the working directory contained more than one *.tmp file, the actual argument passed to the findcommand might look like this:

$ find /usr/home -name a.tmp b.tmp c.tmp – print

This would cause a syntax error to occur.

Locating Files of a Specific Size: -size n

Another useful feature of find is that it can locate files of a specific size. The -size n expression is a goodexample of a search criterion that is evaluated numerically. The numeric portion of the expression may beintegers in the form n, -n, or +n. An integer without a sign matches if the file is exactly n. An integerpreceded by a minus sign matches if the requested file is smaller than n. An integer preceded by a plussign matches if the file is larger than n. For example,

$ find / -size +100 – print

prints the names of all the files that are more than 100 blocks long.

In the -size expression, the integer may be suffixed with the character c. With the suffix, the match ismade on the file size in characters. Without the suffix, the match is made on the file size in blocks.Therefore, the command

$ find / -size -512c – print

Page 12: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 12/22

 prints the names of all the files that are less than 512 bytes long.

Once the find command has located a file, it must be told what to do with it. These are called actionexpressions.

As you know, it does little good to locate a file, and then take no action. One commonly used action is theprint expression, which causes the complete path name to be printed when a file is found. This is useful if you want to check for the existence of a file before deciding to take some other action.

Executing a UNIX Command on the Found Files: -exec cmd ;

Sometimes you know what action you want to take once you find a file. In those cases, you can use theexpression

exec cmd ;

where cmd is any UNIX command. ; tells the find command to take the action specified between exec

and. find then continues to evaluate argument expressions.

The most powerful aspect of the find command is the unique file substitution method found within theexec cmd expression. In any cmd statement, the argument {} is replaced with the name of the currentlymatched file. For example, suppose that the command

$ find /usr/home -name core -printgives the following results: /usr/home/dave/core /usr/home/marsha/core /usr/home/mike/core

The command

$ find /usr/home -name core -exec rm {} ;has the same effect as issuing these commands:$ rm /usr/home/dave/core$ rm /usr/home/mike/core$ rm /usr/home/marsha/core

Executing a UNIX Command on Found Files, But Querying First:

-ok cmd ;

The -ok expression works exactly like the -exec expression, except that the execution of the command isoptional. When it encounters an ok expression, the find program displays the generated command, withall substitutions made, and prints a question mark. If the user types y, the command is executed.

Writing Found Files to a Device: -cpio device

The -cpio device action expression causes a file to be written to a given device in cpio form. For example,the command

$ find /usr/home -cpio -o >/dev/rmt0

Page 13: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 13/22

 writes all the files in /usr/home and all its subdirectories to the magnetic tape device /dev/rmt0. This is agood way to back up data files. It is a shorthand equivalent of 

$ find /usr/home -print | cpio >/dev/rmt0

Search Qualifiers

There are times when you may want the find command to alter its normal search path. This isaccomplished by adding search qualifiers to the find command.

Searching for Files on Only the Current File System: -mount

The -mount search qualifier restricts the search to the file system named in the starting point. Forexample, the command

$ find / -mount -type d – print

prints the names of all the directories in only the root file system.

Altering the Search Path with –  depth

The -depth search qualifier alters the seek order to a depth-first search. The find command processes thefiles in a directory before it processes the directory itself. This helps in finding files to which the user hasaccess, even if his access to the directory is restricted. To see the difference, try the following twocommands. Remember that -print is always true.

$ find /usr -print$ find /usr -depth – print

Combining Search Criteria

You can combine search criteria in a single command. Because the expressions in a find command areevaluated from left to right and the search fails when any one expression fails, the effect is a logical AND.For example, the command

$ find /usr/home -name "*.tmp" -atime +7 -exec rm {} ;

removes all the files that end in .tmp and that have not been accessed inthe last 7 days. Suppose, though, that you wanted to locate files ending in either .tmp or .temp. You coulduse the expression -name "*mp", but you might find files that you didn’t expect. The solution is tocombine search criteria in a logical OR expression. The syntax is

( expression -o expression )

The in front of the parentheses is an escape character; it prevents the shell from misinterpreting theparentheses. The following command line, for example, finds files ending in either .tmp or .temp:

$ find /usr/home ( -name "*.tmp" -o -name "*.temp" )

Page 14: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 14/22

Negating Expressions to Find Files That Don’t Meet Criteria 

Suppose that Marsha wanted to see whether anyone was putting files into her personal directory. Shecould use the negation operator (!), as in

$ find /usr/home/marsha ! -user marsha -print

$ /usr/home/marsha/daves.todo

Specifying More Than One Path to Search

By specifying a directory in which the find command should begin searching, you can control the scopeof the search. The find command actually takes a list of directories to be searched, but you must specifyall paths before you supply any expression arguments. For example, the command

$ find /usr/home/mike /usr/home/dave

 produces a list of all the files in Mike’s and Dave’s directories and in your current working directory.

5. What do you mean by a Process? What are the various possible states of Process? Discuss.

AnswerProcess

In computing, a process is an instance of a computer program that is being executed. It contains theprogram code and its current activity. Depending on the operating system (OS), a process may be madeup of multiple threads of execution that execute instructions concurrently.

A computer program is a passive collection of instructions; a process is the actual execution of thoseinstructions. Several processes may be associated with the same program; for example, opening upseveral instances of the same program often means more than one process is being executed.

Multitasking is a method to allow multiple processes to share processors (CPUs) and other systemresources. Each CPU executes a single task at a time. However, multitasking allows each processor toswitch between tasks that are being executed without having to wait for each task to finish. Depending onthe operating system implementation, switches could be performed when tasks perform input/outputoperations, when a task indicates that it can be switched, or on hardware interrupts.

A common form of multitasking is time-sharing. Time-sharing is a method to allow fast response forinteractive user applications. In time-sharing systems, context switches are performed rapidly. This makes

it seem like multiple processes are being executed simultaneously on the same processor. The executionof multiple processes seemingly simultaneously is called concurrency.

For security and reliability reasons most modern operating systems prevent direct communicationbetween independent processes, providing strictly mediated and controlled inter-process communicationfunctionality.

Page 15: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 15/22

Process state

Created

(Also called New) When a process is first created, it occupies the "created" or "new" state. In this state,the process awaits admission to the "ready" state. This admission will be approved or delayed by a long-

term, or admission, scheduler. Typically in most desktop computer systems, this admission will beapproved automatically, however for real-time operating systems this admission may be delayed. In a realtime system, admitting too many processes to the "ready" state may lead to oversaturation and overcontention for the systems resources, leading to an inability to meet process deadlines.

Ready or waiting

(Also called waiting or runnable) A "ready" or "waiting" process has been loaded into main memory andis awaiting execution on a CPU (to be context switched onto the CPU by the dispatcher, or short-termscheduler). There may be many "ready" processes at any one point of the systems execution - forexample, in a one processor system, only one process can be executing at any one time, and all other"concurrently executing" processes will be waiting for execution.

A ready queue or run queue is used in computer scheduling. Modern computers are capable of runningmany different programs or processes at the same time. However, the CPU is only capable of handlingone process at a time. Processes that are ready for the CPU are kept in a queue for "ready" processes.Other processes that are waiting for an event to occur, such as loading information from a hard drive orwaiting on an internet connection, are not in the ready queue.

Running

A process moves into the running state when it is chosen for execution. The process's instructions areexecuted by one of the CPUs (or cores) of the system. There is at most one running process per CPU or

core.

Blocked

A process that is blocked on some event (such as I/O operation completion or a signal).

Terminated

A process may be terminated, either from the "running" state by completing its execution or by explicitlybeing killed. In either of these cases, the process moves to the "terminated" state. If a process is notremoved from memory after entering this state, it may become a Zombie process.

6. Explain the working of file substitution in UNIX. Also describe the usage of pipes in UNIX Operatingsystem.

Answer

Page 16: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 16/22

File substitution in UNIX

It is important to understand how file substitution actually works. In the previous examples, the lscommand doesn’t do the work of file substitution – the shell does. Even though all the previous examplesemploy the ls command, any command that accepts filenames on the command line can use filesubstitution. In fact, using the simple echo command is a good way to experiment with file substitution

without having to worry about unexpected results. For example,

$ echo p*p10 p101 p11

When a metacharacter is encountered in a UNIX command, the shell looks for patterns in filenames thatmatch the metacharacter. When a match is found, the shell substitutes the actual filename in place of thestring containing the metacharacter so that the command sees only a list of valid filenames. If the shellfinds no filenames that match the pattern, it passes an empty string to the command.

The shell can expand more than one pattern on a single line. Therefore, the shell interprets the command

$ ls LINES.* PAGES.*as$ ls LINES.dat LINES.idx PAGES.dat PAGES.idx

There are file substitution situations that you should be wary of. You should be careful about the use of whitespace (extra blanks) in a command line. If you enter the following command, for example, theresults might surprise you:

$ls LINES.*LINES.:not found21x LINES.dat LINES.idx PAGES.dat PAGES.idxacct.pds marsha.pds p10 p11

t11 z11

What has happened is that the shell interpreted the first parameter as the filename LINES. with nometacharacters and passed it directly on to ls. Next, the shell saw the single asterisk (*), and matched it toany character string, which matches every file in the directory. This is not a big problem if you are simplylisting the files, but it could mean disaster if you were using the command to delete data files! Unusualresults can also occur if you use the period (.) in a shell command. Suppose that you are using the

$ ls .*

command to view the hidden files. What the shell would see after it finishes interpreting themetacharacter is

$ ls . .. .profile

which gives you a complete directory listing of both the current and parent directories. When you think about how filename substitution works, you might assume that the default form of the ls command isactually

$ ls *

Page 17: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 17/22

However, in this case the shell passes to ls the names of directories, which causes ls to list all the files inthe subdirectories. The actual form of the default ls command is

$ ls .

The find CommandOne of the wonderful things about UNIX is its unlimited path names. A directory can have a subdirectorythat itself has a subdirectory, and so on. This provides great flexibility in organizing your data.

Unlimited path names have a drawback, though. To perform any operation on a file that is not in yourcurrent working directory, you must have its complete path name. Disk files are a lot like flashlights: Youstore them in what seem to be perfectly logical places, but when you need them again, you can’tremember where you put them. Fortunately, UNIX has the find command.

The find command begins at a specified point on a directory tree and searches all lower branches for filesthat meet some criteria. Since find searches by path name, the search crosses file systems, including those

residing on a network, unless you specifically instruct it otherwise. Once it finds a file, find can performoperations on it.

Suppose you have a file named urgent.todo, but you cannot remember the directory where you stored it.You can use the find command to locate the file.

$ find / -name urgent.todo -print /usr/home/stuff/urgent.todo

The syntax of the find command is a little different, but the remainder of this section should clear up anyquestions.

The find command is different from most UNIX commands in that each of the argument

expressions following the beginning path name is considered a Boolean expression. At any given stopalong a branch, the entire expression is true  – file found – if all of the expressions are true; or false – filenot found  –  if any one of the expressions is false. In other words, a file is found only if all the searchcriteria are met. For example,

$ find /usr/home -user marsha -size +50

is true for every file beginning at /usr/home that is owned by Marsha and is larger than 50 blocks. It is nottrue for Marsha’s files that are 50 or fewer blocks long, nor is it true for large files owned by someoneelse.

An important point to remember is that expressions are evaluated from left to right. Since the entireexpression is false if any one expression is false, the program stops evaluating a file as soon as it fails to

pass a test. In the previous example, a file that is not owned by Marsha is not evaluated for its size. If theorder of the expressions is reversed, each file is evaluated first for size, and then for ownership.

Another unusual thing about the find command is that it has no natural output. In the previous example,find dutifully searches all the paths and finds all of Marsha’s large files, but it takes no action. For the find command to be useful, you must specify an expression that causes an action to be taken. Forexample,

Page 18: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 18/22

$ find /usr/home -user me -size +50 -print /usr/home/stuff/bigfile /usr/home/trash/bigfile.old

first finds all the files beginning at /usr/home that are owned by me and are larger than 50 blocks. Then itprints the full path name.

The argument expressions for the find command fall into three categories:

· Search criteria· Action expressions· Search qualifiers

Although the three types of expressions have different functions, each is still considered a Booleanexpression and must be found to be true before any further evaluation of the entire expression can takeplace. (The significance of this is discussed later.) Typically, a find operation consists of one or moresearch criteria, a single action expression, and perhaps a search qualifier. In other words, it finds a file andtakes some action, even if that action is simply to print the path name. The rest of this section describes

each of the categories of the find options.

Search Criteria

The first task of the find command is to locate files according to some user-specified criteria. You cansearch for files by name, file size, file ownership, and several other characteristics.

Finding Files with a Specific Name: -name fname

Often, the one thing that you know about a file for which you’re searching is its name. Suppose that youwanted to locate — and possibly take some action on — all the files named core. You might use thefollowing command:

$ find / -name core – print

This locates all the files on the system that exactly match the name core, and it prints their complete pathnames.

The -name option makes filename substitutions. The command

$ find /usr/home -name "*.tmp" – print

prints the names of all the files that end in .tmp. Notice that when filename substitutions are used, thesubstitution string is enclosed in quotation marks. This is because the UNIX shell attempts to make

filename substitutions before it invokes the command. If the quotation marks were omitted from "*.tmp"and if the working directory contained more than one *.tmp file, the actual argument passed to the findcommand might look like this:

$ find /usr/home -name a.tmp b.tmp c.tmp – print

This would cause a syntax error to occur.

Locating Files of a Specific Size: -size n

Page 19: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 19/22

 Another useful feature of find is that it can locate files of a specific size. The -size n expression is a goodexample of a search criterion that is evaluated numerically. The numeric portion of the expression may beintegers in the form n, -n, or +n. An integer without a sign matches if the file is exactly n. An integerpreceded by a minus sign matches if the requested file is smaller than n. An integer preceded by a plussign matches if the file is larger than n. For example,

$ find / -size +100 – print

prints the names of all the files that are more than 100 blocks long.

In the -size expression, the integer may be suffixed with the character c. With the suffix, the match ismade on the file size in characters. Without the suffix, the match is made on the file size in blocks.Therefore, the command

$ find / -size -512c – print

prints the names of all the files that are less than 512 bytes long.

Once the find command has located a file, it must be told what to do with it. These are called actionexpressions.

As you know, it does little good to locate a file, and then take no action. One commonly used action is theprint expression, which causes the complete path name to be printed when a file is found. This is useful if you want to check for the existence of a file before deciding to take some other action.

Executing a UNIX Command on the Found Files: -exec cmd ;

Sometimes you know what action you want to take once you find a file. In those cases, you can use theexpression

exec cmd ;

where cmd is any UNIX command. ; tells the find command to take the action specified between execand. find then continues to evaluate argument expressions.

The most powerful aspect of the find command is the unique file substitution method found within theexec cmd expression. In any cmd statement, the argument {} is replaced with the name of the currentlymatched file. For example, suppose that the command

$ find /usr/home -name core -printgives the following results:

 /usr/home/dave/core /usr/home/marsha/core /usr/home/mike/core

The command

$ find /usr/home -name core -exec rm {} ;has the same effect as issuing these commands:$ rm /usr/home/dave/core

Page 20: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 20/22

$ rm /usr/home/mike/core$ rm /usr/home/marsha/core

Executing a UNIX Command on Found Files, But Querying First:

-ok cmd ;

The -ok expression works exactly like the -exec expression, except that the execution of the command isoptional. When it encounters an ok expression, the find program displays the generated command, withall substitutions made, and prints a question mark. If the user types y, the command is executed.

Writing Found Files to a Device: -cpio device

The -cpio device action expression causes a file to be written to a given device in cpio form. For example,the command

$ find /usr/home -cpio -o >/dev/rmt0

writes all the files in /usr/home and all its subdirectories to the magnetic tape device /dev/rmt0. This is a

good way to back up data files. It is a shorthand equivalent of 

$ find /usr/home -print | cpio >/dev/rmt0

Search Qualifiers

There are times when you may want the find command to alter its normal search path. This isaccomplished by adding search qualifiers to the find command.

Searching for Files on Only the Current File System: -mount

The -mount search qualifier restricts the search to the file system named in the starting point. For

example, the command

$ find / -mount -type d – print

prints the names of all the directories in only the root file system.

Altering the Search Path with –  depth

The -depth search qualifier alters the seek order to a depth-first search. The find command processes thefiles in a directory before it processes the directory itself. This helps in finding files to which the user hasaccess, even if his access to the directory is restricted. To see the difference, try the following twocommands. Remember that -print is always true.

$ find /usr -print$ find /usr -depth – print

Combining Search Criteria

You can combine search criteria in a single command. Because the expressions in a find command areevaluated from left to right and the search fails when any one expression fails, the effect is a logical AND.For example, the command

Page 21: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 21/22

 $ find /usr/home -name "*.tmp" -atime +7 -exec rm {} ;

removes all the files that end in .tmp and that have not been accessed inthe last 7 days. Suppose, though, that you wanted to locate files ending in either .tmp or .temp. You coulduse the expression -name "*mp", but you might find files that you didn’t expect. The solution is to

combine search criteria in a logical OR expression. The syntax is

( expression -o expression )

The in front of the parentheses is an escape character; it prevents the shell from misinterpreting theparentheses. The following command line, for example, finds files ending in either .tmp or .temp:

$ find /usr/home ( -name "*.tmp" -o -name "*.temp" )

Negating Expressions to Find Files That Don’t Meet Criteria 

Suppose that Marsha wanted to see whether anyone was putting files into her personal directory. Shecould use the negation operator (!), as in

$ find /usr/home/marsha ! -user marsha -print$ /usr/home/marsha/daves.todo

Specifying More Than One Path to Search

By specifying a directory in which the find command should begin searching, you can control the scopeof the search. The find command actually takes a list of directories to be searched, but you must specifyall paths before you supply any expression arguments. For example, the command

$ find /usr/home/mike /usr/home/dave

 produces a list of all the files in Mike’s and Dave’s directories and in your current working directory.

UNIX Pipes

Pipes, socketpairs and FIFOs are techniques that allow two processes to exchange data through a streamof bytes. Unlike files however, pipes and friends do not consume disk space but instead have a (usuallycircular) buffer within kernel. If the buffers become overloaded (e.g. if the consumer  is too slow), the producer(s) will be turned to waiting state by the system.

Pipes are usually one-way streams with a  producer side and a consumer side. Note that it is perfectlypossible to have multiple producers and/or multiple consumers (though multiple consumers tend to makethings hard to use.

Usage

Beside the usual shell-scripting use of pipes (where programs such as grep and less are linked together sothat grep's output become less's input with grep kernel | less), several unix programs use such techniques

Page 22: MC0070 -Set 2

8/3/2019 MC0070 -Set 2

http://slidepdf.com/reader/full/mc0070-set-2 22/22

to pass data around pre-build process, one of the most notable example of it being qmail. When runningGCC, for example, using the -pipe flag will use pipes instead of temporary files for intermediatecompilation results, thus speeding up compilation.

Implementation

There are two commands for creating pipes on a Unix system: pipe() and mkfifo(). Both act in similarways - the first creates an unnamed or anonymous pipe, the second creates a named pipe. Named pipesappear in the filesystem and exist until explicitly deleted; unnamed pipes do not and only exist while atleast one filedescriptor is open on it.

The standard way to create an (unnamed) pipe on Unix, is to call pipe() which "returns" two filedescriptors. One is used for writing to the pipe, the other for reading from the pipe.

Internally the pipe is (normally) a circular buffer/queue and two or more semaphores for locking. The size(on Linux) is typically around 4 kilobytes.

Such a pipe exists as long as either end of the pipe is open. It is cleaned after you close() both ends. Thereare a couple of ways to move file descriptors from a process to another on a typical unix system; the mostcommon and 'correct' way being to pass the file descriptor from a parent to a child over a fork() call.

When you write a pipe command line, it's the shell that calls pipe() and then when it fork()s the processesto do the work, it does some filedescriptor renaming (see dup()) to get the right descriptors at the rightplaces before it finally exec()s the programs. Remember fork()ed childs inherit the parents open filedescriptors by default.

Named pipes are similar, except they sit somewhere in the filesystem, and if you open the named piperepeatedly, you get the same pipe every time. Once created, the named pipe remains in the filesystemuntil deleted like any other file.