Sie sind auf Seite 1von 14

July 2011 Master of Computer Science (MSCCS) Semester 1 MC0070 Operating Systems with Unix 4 Credits (Book ID:

ID: B0682 & B0683) Assignment Set 2 (60 Marks)


Answer all Questions Book ID: B0682 Q.1) Describe the following with respect to Deadlocks in Operating Systems: a. Deadlock Avoidance Answer: 1(a) Deadlock Avoidance In general, resources allocated to a process are not preemptable; this means that once a resource has been allocated to a process, there is no simple mechanism by which the system can take the resource back from the process unless the process voluntarily gives it up or the system administrator kills the process. This can lead to a situation called deadlock. A set of processes or threads is deadlocked when each process or thread is waiting for a resource to be freed which is controlled by another process. When deadlock happens, then detected that deadlock had occurred and tried to fix the problem after the fact. 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 assumption. b. Deadlock Prevention Answer: 1(b) Deadlock Prevention 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 assumption, 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. Q.2) Discuss the following with respect to I/O in Operating Systems: a. I/O Control Strategies Answer: 2(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. Each question carries TEN marks

Page 1 of 14

Roll No. 521126647

July 2011 Master of Computer Science (MSCCS) Semester 1 MC0070 Operating Systems with Unix 4 Credits (Book ID: B0682 & B0683) Assignment Set 2 (60 Marks)
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.) A third general I/O strategy is to allow the I/O device, or the controller for the device, access to the main memory. The device would write a block of information in main memory, without intervention from the CPU, and then inform the CPU in some way that that block of memory had been overwritten or read. This might be done by leaving a message in memory, or by interrupting the processor. (This is generally the I/O strategy used by the highest speed devices -- hard disks and the video controller.) b. Program - controlled I/O Answer: 2(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 performed under 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/O device (e.g., whether the device is ready to receive data). This information is usually obtained through a second input from the device; a single bit is usually sufficient, so one input ``port'' can be used to collect status, or handshake, information from several I/O devices. (A port is the name given to a connection to an I/O device; e.g., to the memory location into which an I/O device is mapped). An I/O port is usually implemented as a register (possibly a set of D flip flops) which also acts as a buffer between the CPU and the 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 requires service, it will signal this need by altering its input to the ``status'' port. When the I/O control program detects that this has occurred (by reading the status port) then the appropriate operation will be performed on the I/O device which requested the service. A typical configuration might look somewhat as shown in Figure. The outputs labeled ``handshake in'' would be connected to bits in the ``status'' port. The input labeled ``handshake in'' would typically be generated by the appropriate decode logic when the I/O port corresponding to the device was addressed. 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 testing the status inputs of the I/O devices, when the devices do not need servicing. This ``busy wait'' or ``wait loop'' during which the I/O devices are polled but no I/O operations are performed is really time wasted by the processor, if there is other work which could be done at that time. Also, if a particular device has its data available for only a short time, the data may be missed because the input was not tested at the appropriate time.

Page 2 of 14

Roll No. 521126647

July 2011 Master of Computer Science (MSCCS) Semester 1 MC0070 Operating Systems with Unix 4 Credits (Book ID: B0682 & B0683) Assignment Set 2 (60 Marks)
Program controlled I/O is often used for simple operations which must be performed sequentially. For example, the following may be used to control the temperature in a room: DO forever INPUT temperature IF (temperature < setpoint) THEN turn heat ON ELSE turn heat OFF END IF Note here that the order of events is fixed in time, and that the program loops forever. (It is really waiting for a change in the temperature, but it is a ``busy wait.'') c. Interrupt - controlled I/O Answer: 2(c) Interrupt-controlled I/O Interrupt-controlled I/O reduces the severity of the two problems mentioned for program-controlled I/O by allowing the I/O device itself to initiate the device service routine in the processor. This is accomplished by having the I/O device generate an interrupt signal which is tested directly by the hardware of the CPU. When the interrupt input to the CPU is found to be active, the CPU itself initiates a subprogram call to somewhere in the memory of the processor; the particular address to which the processor 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 some specific 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 interrupt scheme the interrupt handling routine must examine each device to determine which one caused the interrupt. Also, since only one interrupt can be handled at a time, there is usually a hardware ``priority encoder'' which allows the device with the highest priority to interrupt the processor, if several devices attempt to interrupt the processor simultaneously. In Figure 4.8, the ``handshake out'' outputs would be connected 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 is serviced first. ``Daisy chain'' priority resolution is discussed later.) In most modern processors, interrupt return points are saved on a ``stack'' in memory, in the same way as return addresses for subprogram calls are saved. In fact, an interrupt can often be thought of as a subprogram which is invoked by an external device. If a stack is used to save the return address for interrupts, it is then possible to allow one interrupt the interrupt handling routine of another interrupt. In modern computer systems, there are often several ``priority levels'' of interrupts, each of which can be disabled, 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 warning the processor of potentially catastrophic events such as an imminent power failure, to allow the processor to 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 the responsibility of the interrupting device to provide the address in main memory of the interrupt servicing routine 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 simple interrupt system described earlier, vectored interrupts provide

Page 3 of 14

Roll No. 521126647

July 2011 Master of Computer Science (MSCCS) Semester 1 MC0070 Operating Systems with Unix 4 Credits (Book ID: B0682 & B0683) Assignment Set 2 (60 Marks)
such a significant advantage in interrupt handling speed and ease of implementation (i.e., a separate routine for each device) that this method is almost universally used on modern computer systems. Some processors have a number of special inputs for vectored interrupts (each acting much like the simple interrupt described earlier). Others require that the interrupting device itself provide the interrupt address as part of the process of interrupting the processor. Q.3) Discuss various conditions to be true for deadlock to occur.

Answer:3) In general, resources allocated to a process are not preemptable; this means that once a resource has been allocated to a process, there is no simple mechanism by which the system can take the resource back from the process unless the process voluntarily gives it up or the system administrator kills the process. This can lead to a situation called deadlock. A set of processes or threads is deadlocked when each process or thread is waiting for a resource to be freed which is controlled by another process. 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 is available. (Two processes cannot simultaneously control the same resource or be in their critical section). 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.

Deadlock can be modeled with a directed graph. In a deadlock graph, vertices represent either processes (circles) or resources (squares). A process which has acquired a resource is show with an arrow (edge) from the resource to the process. A process which has requested a resource which has not yet been assigned to it is modeled with an arrow from the process to the resource. If these create a cycle, there is deadlock. The deadlock situation in the above code can be modeled like this.

This graph shows an extremely simple deadlock situation, but it is also possible for a more complex situation to create deadlock. Here is an example of deadlock with four processes and four resources.

Page 4 of 14

Roll No. 521126647

July 2011 Master of Computer Science (MSCCS) Semester 1 MC0070 Operating Systems with Unix 4 Credits (Book ID: B0682 & B0683) Assignment Set 2 (60 Marks)

There are a number of ways that deadlock can occur in an operating situation. We have seen some examples, here are two more. Two processes need to lock two files, the first process locks one file the second process locks the other, and each waits for the other to free up the locked file. Two processes want to write a file to a print spool area at the same time and both start writing. However, the print spool area is of fixed size, and it fills up before either process finishes writing its file, so both wait for more space to become available.

Book ID: B0683 Q.4) Explain the following with respect to data files in UNIX: a. Using Meta characters When Referring to Filenames

Answer: 4(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 you can remember only part of a filename, it is useful to list all the files whose names contain that part. UNIX provides 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 following directory: $ls 21x LINES.dat LINES.idx p10 PAGES.dat p101 p11 PAGES.idx

Acct.pds t11 z11

marsha.pds

You can use the question mark (?) in any position. For example, $ls ?11 p11 t11 z11

Page 5 of 14

Roll No. 521126647

July 2011 Master of Computer Science (MSCCS) Semester 1 MC0070 Operating Systems with Unix 4 Credits (Book ID: B0682 & B0683) Assignment Set 2 (60 Marks)
You can also use more than one question mark in a single substitution. For example, $ls p?? p11 p10

The following command gives you all three-character filenames: $ls p?? 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 illustrate how to do this: $ ls ????.pds acct.pds $ $ ls ?????.pds marsha.pds Pattern Matching on a Group of Characters In the previous example, to list all of the files ending in .pds using single character substitution, you would have to know exactly how many characters precede the period. To overcome this problem, you use the asterisk (*), which matches a character string of any length, including a length of zero. Consider the following two examples: $ls *.pds Acct.pds $ ls p10.* P10 p101 marsha.pds

As with single character substitution, more than one asterisk (*) can be used in a single substitution. For example, $ ls *.* LINES.dat marsha.pds Pattern Matching on Character Sets You 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 ]. LINES.idx PAGES.dat PAGES.idx acct.pds

Page 6 of 14

Roll No. 521126647

July 2011 Master of Computer Science (MSCCS) Semester 1 MC0070 Operating Systems with Unix 4 Credits (Book ID: B0682 & B0683) Assignment Set 2 (60 Marks)
Suppose that you wanted a list of all the filenames that start with p or t followed by 11. You could use the following command: $ls [pt]11 p11 t11

You can combine character sets with the metacharacters. To list the names of all the files that begin 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 alphabetic character. You could use $ ls [ABCDEFGHIJKLMNOPQRSTUVWXYZ]* LINES.dat LINES.idx PAGES.dat PAGES.idx If youre guessing that there might be a better way to do this, youre right. When the characters in a character set substitution are in sequence, you can use a hyphen (-) to denote all of the characters in the sequence. 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 in sequence. 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 methods The find Command One of the wonderful things about UNIX is its unlimited path names. A directory can have a subdirectory that 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 your current working directory, you must have its complete path name. Disk files are a lot like flashlights: You store them in what seem to be perfectly logical places, but when you need them again, you cant remember 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 files that 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 perform operations 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

Page 7 of 14

Roll No. 521126647

July 2011 Master of Computer Science (MSCCS) Semester 1 MC0070 Operating Systems with Unix 4 Credits (Book ID: B0682 & B0683) Assignment Set 2 (60 Marks)
/usr/home/stuff/urgent.todo The syntax of the find command is a little different, but the remainder of this section should clear up any questions. 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 stop along a branch, the entire expression is true file found if all of the expressions are true; or false file not found if any one of the expressions is false. In other words, a file is found only if all the search criteria 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 not true for Marshas files that are 50 or fewer blocks long, nor is it true for large files owned by someone else. An important point to remember is that expressions are evaluated from left to right. Since the entire expression 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 the order 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 Marshas 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. For example, $ 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 it prints 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 Boolean expression and must be found to be true before any further evaluation of the entire expression can take place. (The significance of this is discussed later.) Typically, a find operation consists of one or more search criteria, a single action expression, and perhaps a search qualifier. In other words, it finds a file and takes 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 can search 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 youre searching is its name. Suppose that you wanted to locateand possibly take some action onall the files named core. You might use the following 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

Page 8 of 14

Roll No. 521126647

July 2011 Master of Computer Science (MSCCS) Semester 1 MC0070 Operating Systems with Unix 4 Credits (Book ID: B0682 & B0683) Assignment Set 2 (60 Marks)
prints the names of all the files that end in .tmp. Notice that when filename substitutions are used, the substitution 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 find command 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 good example of a search criterion that is evaluated numerically. The numeric portion of the expression may be integers in the form n, -n, or +n. An integer without a sign matches if the file is exactly n. An integer preceded by a minus sign matches if the requested file is smaller than n. An integer preceded by a plus sign 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 is made 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 action expressions. As you know, it does little good to locate a file, and then take no action. One commonly used action is the print 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 the expression 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 the exec cmd expression. In any cmd statement, the argument {} is replaced with the name of the currently matched file. For example, suppose that the command $ find /usr/home -name core -print gives 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

Page 9 of 14

Roll No. 521126647

July 2011 Master of Computer Science (MSCCS) Semester 1 MC0070 Operating Systems with Unix 4 Credits (Book ID: B0682 & B0683) Assignment Set 2 (60 Marks)
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 is optional. When it encounters an ok expression, the find program displays the generated command, with all 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 is accomplished 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 the files in a directory before it processes the directory itself. This helps in finding files to which the user has access, even if his access to the directory is restricted. To see the difference, try the following two commands. 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 are evaluated 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 in the last 7 days. Suppose, though, that you wanted to locate files ending in either .tmp or .temp. You could use the expression -name "*mp", but you might find files that you didnt 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 the parentheses. 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 Dont Meet Criteria Suppose that Marsha wanted to see whether anyone was putting files into her personal directory. She could use the negation operator (!), as in

Page 10 of 14

Roll No. 521126647

July 2011 Master of Computer Science (MSCCS) Semester 1 MC0070 Operating Systems with Unix 4 Credits (Book ID: B0682 & B0683) Assignment Set 2 (60 Marks)
$ 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 scope of the search. The find command actually takes a list of directories to be searched, but you must specify all 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 Mikes and Daves directories and in your current working directory

Q.5)

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

Answer: 5) A process is a particular instance of a program which is executing. It includes the code for the program, the current value of the program counter, all internal registers, and the current value of all variables associated with the program (i.e., the memory state.) Different (executing) instances of the same program are different processes. Each process in the system is represented by a data structure called a Process Control Block (PCB), or Process Descriptor in Linux, which performs the same function as a travelers passport. The PCB contains the basic information about the job including: What it is Where it is going How much of its processing has been completed Where it is stored How much it has spent in using resources

Process Identification: Each process is uniquely identified by the users identification and a pointer connecting it to its descriptor. Process Status: This indicates the current status of the process; READY, RUNNING, BLOCKED, READY SUSPEND, BLOCKED SUSPEND. Process State: This contains all of the information needed to indicate the current state of the job. Accounting: This contains information used mainly for billing purposes and for performance measurement. It indicates what kind of resources the process has used and for how long. There are various process state management models such as

1) Two-state process management model: The operating systems principal responsibility is in controlling the
execution of processes. This includes determining the interleaving pattern for execution and allocation of resources to processes. One part of designing an OS is to describe the behavior that we would like each process to exhibit. The simplest model is based on the fact that a process is either being executed by a processor or it is not. Thus, a process may be considered to be in one of two states, RUNNING or NOT RUNNING

2) Three-state process management model: The three-state process management model is designed to
overcome this problem, by introducing a new state called the BLOCKED state. This state describes any process which is waiting for an I/O event to take place. In this case, an I/O event can mean the use of some device or a signal from another process. The three states in this model are:

RUNNING: The process that is currently being executed. Page 11 of 14


Roll No. 521126647

July 2011 Master of Computer Science (MSCCS) Semester 1 MC0070 Operating Systems with Unix 4 Credits (Book ID: B0682 & B0683) Assignment Set 2 (60 Marks) READY: A process that is queuing and prepared to execute when given the opportunity. BLOCKED: A process that cannot execute until some event occurs, such as the completion of an I/O
operation. At any instant, a process is in one and only one of the three states. For a single processor computer, only one process can be in the RUNNING state at any one instant. There can be many processes in the READY and BLOCKED states, and each of these states will have an associated queue for processes. Five-state process management model: While the three state model is sufficient to describe the behavior of processes with the given events, we have to extend the model to allow for other possible events, and for more sophisticated design. In particular, the use of a portion of the hard disk to emulate main memory (so called virtual memory) requires additional states to describe the state of processes which are suspended from main memory, and placed in virtual memory (on disk). Of course, such processes can, at a future time, be resumed by being transferred back into main memory. The Medium Level Scheduler controls these events. A process can be suspended from the RUNNING, READY or BLOCKED state, giving rise to two other states, namely, READY SUSPEND and BLOCKED SUSPEND. A RUNNING process that is suspended becomes READY SUSPEND, and a BLOCKED process that is suspended becomes BLOCKED SUSPEND. A process can be suspended for a number of reasons; the most significant of which arises from the process being swapped out of memory by the memory management system in order to free memory for other processes. Other common reasons for a process being suspended are when one suspends execution while debugging a program, or when the system is monitoring processes. For the five-state process management model, consider the following transitions described in the next sections. SUSPEND BLOCKED then BLOCKED If a process in the RUNNING state requires more memory, then at least one BLOCKED process can be swapped out of memory onto disk. The transition can also be made for the BLOCKED process if there are READY processes available, and the OS determines that the READY process that it would like to dispatch requires more main memory to maintain adequate performance. SUSPEND BLOCKED then SUSPEND READY A process in the SUSPEND BLOCKED state is moved to the SUSPEND READY state when the event for which it has been waiting occurs. Note that this requires that the state information concerning suspended processes be accessible to the OS. SUSPEND READY then READY When there are no READY processes in main memory, the OS will need to bring one in to continue execution. In addition, it might be the case that a process in the READY SUSPEND state has higher priority than any of the processes in the READY state. In that case, the OS designer may dictate that it is more important to get at the higher priority process than to minimise swapping. SUSPENDED but READY Normally, the OS would be designed so that the preference would be to suspend a BLOCKED process rather than a READY one. Q.6) Explain the working of file substitution in UNIX. Also describe the usage of pipes in UNIX Operating system.

Answer: It is important to understand how file substitution actually works. ls command doesnt do the work of file substitution the shell does. Even though all the previous examples employ the ls command, any command that accepts filenames on the command line can use file substitution. 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

Page 12 of 14

Roll No. 521126647

July 2011 Master of Computer Science (MSCCS) Semester 1 MC0070 Operating Systems with Unix 4 Credits (Book ID: B0682 & B0683) Assignment Set 2 (60 Marks)
When a metacharacter is encountered in a UNIX command, the shell looks for patterns in filenames that match the metacharacter. When a match is found, the shell substitutes the actual filename in place of the string containing the metacharacter so that the command sees only a list of valid filenames. If the shell finds 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, the results might surprise you: $ls LINES.* LINES.: not found 21x LINES.dat LINES.idx Acct.pds marsha.pds p10 t11 z11

PAGES.dat p101 p11

PAGES.idx

What has happened is that the shell interpreted the first parameter as the filename LINES. With no metacharacters 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 simply listing the files, but it could mean disaster if you were using the command to delete data files! Unusual results 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 the metacharacter 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 is actually $ ls * However, in this case the shell passes to ls the names of directories, which causes ls to list all the files in the subdirectories. The actual form of the default ls command is $ ls . Pipes A second form of redirection is a pipe. A pipe is a connection between two processes in which one process writes data to the pipe and the other reads from the pipe. Thus, it allows one process to pass data to another process. The Unix system call to create a pipe is int pipe(int fd[2]) This function takes an array of two ints (file descriptors) as an argument. It creates a pipe with fd[0] at one end and fd[1] at the other. Reading from the pipe and writing to the pipe are done with the read and write calls that you have seen and used before. Although both ends are opened for both reading and writing, by convention a process writes to fd[1] and reads from fd[0]. Pipes only make sense if the process calls fork after creating the pipe. Each process should close the end of the pipe that it is not using. Here is a simple example in which a child sends a message to its parent through a pipe. #include <unistd.h>

Page 13 of 14

Roll No. 521126647

July 2011 Master of Computer Science (MSCCS) Semester 1 MC0070 Operating Systems with Unix 4 Credits (Book ID: B0682 & B0683) Assignment Set 2 (60 Marks)
#include <stdio.h> int main() { pid_t pid; int retval; int fd[2]; int n; retval = pipe(fd); if (retval < 0) { printf("Pipe failed\n"); /* pipe is unlikely to fail */ exit(0); } pid = fork(); if (pid == 0) { /* child */ close(fd[0]); n = write (fd[1],"Hello from the child",20); exit(0); } else if (pid > 0) { /* parent */ char buffer[64]; close(fd[1]); n = read(fd[0],buffer,64); buffer[n]='\0'; printf("I got your message: %s\n",buffer); } return 0; } There is no need for the parent to wait for the child to finish because reading from a pipe will block until there is something in the pipe to read. If the parent runs first, it will try to execute the read statement, and will immediately block because there is nothing in the pipe. After the child writes a message to the pipe, the parent will wake up. Pipes have a fixed size (often 4096 bytes) and if a process tries to write to a pipe which is full, the write will block until a process reads some data from the pipe.

Page 14 of 14

Roll No. 521126647

Das könnte Ihnen auch gefallen