MC0070 Operating Systems With Unix

32
MC0070 Operating Systems with Unix ASSIGNME NT SET – 1 1. Define process. Explain the major components of a process ? Ans: Process :A process can be simply defined as a program in execution. Process along with program code, comprises of program counter value, Processor register contents, values of variables, stack and program data. A process is created and terminated, and it follows some or all of the states of process transition; such as New, Ready, Running, Waiting, and Exit. Process is not the same as program. A process is more than a program code. A process is an ‘active’ entity as oppose to program which considered being a ‘passive’ entity. As we all know that a program is an algorithm expressed in some programming language. Being a passive, a program is only a part of process. Process, on the other hand, includes: · Current value of Program Counter (PC) · Contents of the processors registers · Value of the variables · The process stack, which typically contains temporary data such as subroutine parameter, return address, and temporary variables. · A data section that contains global variables. · A process is the unit of work in a system. A process has certain attributes that directly affect execution, these include: PID - The PID stands for the process identification. This is a unique number that defines the process within the kernel. PPID - This is the processes Parent PID, the creator of the process. UID - The User ID number of the user that owns this process. EUID - The effective User ID of the process. 1 | Page

Transcript of MC0070 Operating Systems With Unix

Page 1: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

ASSIGNMENT SET – 1

1. Define process. Explain the major components of a process ?

Ans: Process:A process can be simply defined as a program in execution. Process along with program code, comprises of program counter value, Processor register contents, values of variables, stack and program data. A process is created and terminated, and it follows some or all of the states of process transition; such as New, Ready, Running, Waiting, and Exit. Process is not the same as program. A process is more than a program code. A process is an ‘active’ entity as oppose to program which considered being a ‘passive’ entity. As we all know that a program is an algorithm expressed in some programming language. Being a passive, a program is only a part of process. Process, on the other hand, includes:

· Current value of Program Counter (PC)· Contents of the processors registers· Value of the variables· The process stack, which typically contains temporary data such as subroutine parameter, return address, and temporary variables.· A data section that contains global variables.· A process is the unit of work in a system.

A process has certain attributes that directly affect execution, these include:

PID - The PID stands for the process identification. This is a unique number that defines the process within the kernel.

PPID - This is the processes Parent PID, the creator of the process.

UID - The User ID number of the user that owns this process.

EUID - The effective User ID of the process.

GID - The Group ID of the user that owns this process.

EGID - The effective Group User ID that owns this process.

Priority - The priority that this process runs at.

To view a process you use the ps command.

# ps -l

F S UID PID PPID C PRI NI P SZ:RSS WCHAN TTY TIME COMD

30 S 0 11660 145 1 26 20 * 66:20 88249f10 ttyq 6 0:00 rlogind

1 | P a g e

Page 2: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

The F field: This is the flag field. It uses hexadecimal values which are added to show the value of the flag bits for the process. For a normal user process this will be 30, meaning it is loaded into memory.

The S field: The S field is the state of the process, the two most common values are S for Sleeping and R for Running. An important value to look for is X, which means the process is waiting for memory to become available.

PID field: The PID shows the Process ID of each process. This value should be unique. Generally PID are allocated lowest to highest, but wrap at some point. This value is necessary for you to send a signal to a process such as the KILL signal.

PRI field: This stands for priority field. The lower the value the higher the value. This refers to the process NICE value. It will range from 0 to 39. The default is 20, as a process uses the CPU the system will raise the nice value.

P flag: This is the processor flag. On the SGI this refers to the processor the process is running on.

SZ field: This refers to the SIZE field. This is the total number of pages in the process. Each page is 4096 bytes.

TTY field: This is the terminal assigned to your process.

Time field: The cumulative execution time of the process in minutes and seconds.

COMD field: The command that was executed.

In Process model, all software on the computer is organized into a number of sequential processes. A process includes PC, registers, and variables. Conceptually, each process has its own virtual CPU. In reality, the CPU switches back and forth among processes.

2. Write Note on :

a. Mainframe Operating Systems

b. Embedded Operating Systems

c. Servers Operating Systems

d. Desktop Operating Systems

Ans:

A: Mainframe Operating System :

Mainframe OS can be used by many users at the same time so it must need to service for many users. a mainframe OS is more powerful and expensive than pc OS. Mainframe OS designed to huge process from many users and it means, it manages lots of I/O for many users, Mainframe OS offers three kinds of service batch, transaction processing and timesharing.

2 | P a g e

Page 3: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

B: Embedded Operating System :

An embedded operating system is an operating system for U. These operating systems are designed to be compact, efficient, and reliable, forsaking many functions that non-embedded computer operating systems provide, and which may not be used by the specialized applications they run. They are frequently also real-time operating systems, and the term RTOS is often used as a synonym for embedded operating system.

An important difference between most embedded operating systems and desktop operating systems is that the application, including the operating system, is usually statically linked together into a single executable image. Unlike a desktop operating system, the embedded operating system does not load and execute applications. This means that the system is only able to run a single application.

C: Servers Operating Systems:

Server-oriented operating systems tend to have certain features in common that make them more suitable for the server environment, such as

GUI not available or optional ability to reconfigure and update both hardware and software to some extent without restart, advanced backup facilities to permit regular and frequent online backups of critical data, transparent data transfer between different volumes or devices, flexible and advanced networking capabilities, automation capabilities such as daemons in UNIX and services in Windows, and tight system security, with advanced user, resource, data, and memory protection.

Server-oriented operating systems can, in many cases, interact with hardware sensors to detect conditions such as overheating, processor and disk failure, and consequently alert an operator or take remedial measures itself.

Because servers must supply a restricted range of services to perhaps many users while a desktop computer must carry out a wide range of functions required by its user, the requirements of an operating system for a server are different from those of a desktop machine. While it is possible for an operating system to make a machine both provide services and respond quickly to the requirements of a user, it is usual to use different operating systems on servers and desktop machines. Some operating systems are supplied in both server and desktop versions with similar user interface.

The desktop versions of the Windows and Mac OS X operating systems are deployed on a minority of servers, as are some proprietary mainframe operating systems, such as z/OS. The dominant operating systems among servers are UNIX-based or open source kernel distributions, such as Linux (the kernel).

The rise of the microprocessor-based server was facilitated by the development of Unix to run on the x86 microprocessor architecture. The Microsoft Windows family of operating systems also runs on x86 hardware, and since Windows NT have been available in versions suitable for server use.

While the role of server and desktop operating systems remains distinct, improvements in the reliability of both hardware and operating systems have blurred the distinction between the two classes. Today, many desktop and server operating systems share similar code bases, differing

3 | P a g e

Page 4: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix mostly in configuration. The shift towards web applications and middleware platforms has also lessened the demand for specialist application servers.

D : Desktop Operating Systems :

A desktop operating system is one that is intended for a desktop computer (Unless you are a network administrator or something like that, you probably use a desktop computer.) These OSes usually come with things that one would probably use at a desk. For example, Windows sometimes comes with Microsoft Office pre-installed.

3.Memory management is important in operating systems. Discuss the main problems that can occur if memory is managed poorly ?

Ans: The part of the operating system which handles this responsibility is called the memory manager. Since every process must have some amount of primary memory in order to execute, the performance of the memory manager is crucial to the performance of the entire system. Virtual memory refers to the technology in which some space in hard disk is used as an extension of main memory so that a user program need not worry if its size extends the size of the main memory.

For paging memory management, each process is associated with a page table. Each entry in the table contains the frame number of the corresponding page in the virtual address space of the process. This same page table is also the central data structure for virtual memory mechanism based on paging, although more facilities are needed. It covers the Control bits, Multi-level page table etc. Segmentation is another popular method for both memory management and virtual memory

Basic Cache Structure : The idea of cache memories is similar to virtual memory in that some active portion of a low-speed memory is stored in duplicate in a higher-speed cache memory. When a memory request is generated, the request is first presented to the cache memory, and if the cache cannot respond, the request is then presented to main memory.

Content-Addressable Memory (CAM) is a special type of computer memory used in certain very high speed searching applications. It is also known as associative memory, associative storage, or associative array, although the last term is more often used for a programming data structure.

In addition to the responsibility of managing processes, the operating system must efficiently manage the primary memory of the computer. The part of the operating system which handles this responsibility is called the memory manager. Since every process must have some amount of primary memory in order to execute, the performance of the memory manager is crucial to the performance of the entire system. Nutt explains: “The memory manager is responsible for allocating primary memory to processes and for assisting the programmer in loading and storing the contents of the primary memory. Managing the sharing of primary memory and minimizing memory access time are the basic goals of the memory manager.”

4 | P a g e

Page 5: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

The real challenge of efficiently managing memory is seen in the case of a system which has multiple processes running at the same time. Since primary memory can be space-multiplexed, the memory manager can allocate a portion of primary memory to each process for its own use. However, the memory manager must keep track of which processes are running in which memory locations, and it must also determine how to allocate and de-allocate available memory when new processes are created and when old processes complete execution. While various different strategies are used to allocate space to processes competing for memory, three of the most popular are Best fit, Worst fit, and First fit. Each of these strategies are described below:

Best fit: The allocator places a process in the smallest block of unallocated memory in which it will fit. For example, suppose a process requests 12KB of memory and the memory manager currently has a list of unallocated blocks of 6KB, 14KB, 19KB, 11KB, and 13KB blocks. The best-fit strategy will allocate 12KB of the 13KB block to the process.

Worst fit: The memory manager places a process in the largest block of unallocated memory available. The idea is that this placement will create the largest hold after the allocations, thus increasing the possibility that, compared to best fit, another process can use the remaining space. Using the same example as above, worst fit will allocate 12KB of the 19KB block to the process, leaving a 7KB block for future use.

First fit: There may be many holes in the memory, so the operating system, to reduce the amount of time it spends analyzing the available spaces, begins at the start of primary memory and allocates memory from the first hole it encounters large enough to satisfy the request. Using the same example as above, first fit will allocate 12KB of the 14KB block to the process.

Notice in the diagram above that the Best fit and First fit strategies both leave a tiny segment of memory unallocated just beyond the new process. Since the amount of memory is small, it is not likely that any new processes can be loaded here. This condition of splitting primary memory into segments as the memory is allocated and deallocated is known as fragmentation. The Worst fit strategy attempts to reduce the problem of fragmentation by allocating the largest fragments to new processes. Thus, a larger amount of space will be left as seen in the diagram above.

Another way in which the memory manager enhances the ability of the operating system to support multiple process running simultaneously is by the use of virtual memory. According the Nutt, “virtual memory strategies allow a process to use the CPU when only part of its address space is loaded in the primary memory. In this approach, each process’s address space is partitioned into parts that can be loaded into primary memory when they are needed and written back to secondary

5 | P a g e

Page 6: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

memory otherwise.” Another consequence of this approach is that the system can run programs which are actually larger than the primary memory of the system, hence the idea of “virtual memory.” Brookshear explains how this is accomplished:

“Suppose, for example, that a main memory of 64 megabytes is required but only 32 megabytes is actually available. To create the illusion of the larger memory space, the memory manager would divide the required space into units called pages and store the contents of these pages in mass storage. A typical page size is no more than four kilobytes. As different pages are actually required in main memory, the memory manager would exchange them for pages that are no longer required, and thus the other software units could execute as though there were actually 64 megabytes of main memory in the machine.”

In order for this system to work, the memory manager must keep track of all the pages that are currently loaded into the primary memory. This information is stored in a page table maintained by the memory manager. A page fault occurs whenever a process requests a page that is not currently loaded into primary memory. To handle page faults, the memory manager takes the following steps:

1. The memory manager locates the missing page in secondary memory.2. The page is loaded into primary memory, usually causing another page to be unloaded.3. The page table in the memory manager is adjusted to reflect the new state of the memory.4. The processor re-executes the instructions which caused the page fault.

4. Explain the following with suitable examples:

1. Copying Files

2. Moving Files

3 Removing Files

4. Creating Multiple Directories

5. Removing a Directory

6. Renaming Directories

Ans: Copying Files-

Copying files and directories is done with the cp command. A useful option is recursive copy (copy all underlying files and subdirectories), using the -R option to cp. The general syntax is

cp [-R] fromfile tofile

As an example the case of user newguy, who wants the same Gnome desktop settings user oldguy has. One way to solve the problem is to copy the settings of oldguy to the home directory of newguy:

victor:~> cp -R ../oldguy/.gnome/ .

This gives some errors involving file permissions, but all the errors have to do with private files that newguy doesn't need anyway. We will discuss in the next part how to change these permissions in case they really are a problem.

6 | P a g e

Page 7: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

Moving Files-

The mv command (stands for move) allows you to rename a file in the same directory or move a file from one directory to another. If you move a file to a different directory, the file can be renamed or it can retain its original name. mv can also be used to move and rename directories.

% mv [<options>] <source1> [<source2> ...] <target>

Depending on whether the <source(s)> and <target> are files or directories, different actions are taken. These are described in the table below. If <target> is a filename, only one <source> file may be specified.

Source Target Result

file new file name Rename file to new name

file existing file nameOverwrite existing file with source file contents; keep existing file name

directory new directory name Rename directory to new name

directoryexisting directory name

Move directory such that it becomes a subdirectory of existing directory

one or more files

existing directory name

Move files to existing directory

An important option is:

-i If <target> exists, the user is prompted for confirmation before overwriting.

Creating Multiple Directories-

The mkdir command (for make directory) is used to create a directory. The command format is:

% mkdir <dirname> ...

If a pathname is not specified, the directory is created as a subdirectory of the current working directory. Directory creation requires write access to the parent directory. The owner ID and group ID of the new directory are set to those of the creating process.

Examples:

create a subdirectory in the current working directory

% mkdir progs

create one with a full pathname (the directory Tools must already exist)

% mkdir /usr/nicholls/Tools/Less

7 | P a g e

Page 8: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

The mkdir command can even create a directory and its subdirectories if we use its –p option:

$ mkdir –p journal/97

Removing a Directory-

To remove an empty directory, use rmdir. Suppose that we made a typing mistake while creating a directory and want to remove it so that we can create the right one. Enter these commands:

$ mkdir jornal

$rmdir jornal

$ mkdir journal

The rmdir command removes only empty directories. If a directory still has files, we must remove them befor using rmdir:

$ rmdir journal

Rmdir:journa:Directory not empty

$rm journal/*

$ rmdir journal

Actually, rm can remove directories if we use its -r option.

Renaming Directories:

To change the name of a file, use the following command format (where thirdfile and file3 are

sample file names):

mv thirdfile file3

The result of this command is that there is no longer a file called thirdfile, but a new file called file3

contains what was previously in thirdfile.

Like cp, the mv command also overwrites existing files. For example, if you have two files,

fourthfile and secondfile, and you type the command

mv fourthfile secondfile

mv will remove the original contents of secondfile and replace them with the contents of fourthfile. The effect is that fourthfile is renamed secondfile, but in the process secondfile is deleted.

8 | P a g e

Page 9: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

5. Discuss the concept of File substitution with respect to managing data files in UNIX ?

Ans: File Substitution Works

It is important to understand how file substitution actually works. In the previous examples, the ls command doesn’t 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

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:

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!

9 | P a g e

Page 10: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

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 .

6. Describe the use of following special Variables in the Bourne Shel:l

1. IFS 2. MAIL 3. MAILCHECK

4. MAILPATH 5. PS1 6. PS2.

Ans:

IFS - Internal Field Seperator The "IFS" variable lists the characters used to terminate a word. I

discussed this briefly earlier. Normally, whitespace separates words, and this variable contains a

space, a tab and a new line. Hackers find this variable interesting, because it can be used to break

into computer systems. A poorly written program may carelessly execute "/bin/ps." A hacker may

redefine the PATH variable, and define IFS to be "/." When the program executes "/bin/ps," the shell

will treat this as "bin ps." In other words, the program "bin" is executed with "ps" as an argument. If

the hacker has placed a program called "bin" in the searchpath, then the hacker gains privileged

access.

MAIL - Incoming mail The "MAIL" variable specifies where your mailbox is located. It is set by the

login process.

10 | P a g e

Page 11: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

MAILCHECK - How often to check for mail The "MAILCHECK" variable specifies how often to

check for mail, in seconds. The default value is 600 seconds (10 minutes). If you set it to zero, every

time the shell types a prompt, it will check for mail.

MAILPATH - searchpath for mail folders The "MAILPATH" variable lists colon-separated

filenames. You can add a "%" after the filename, and specify a special prompt for each mailbox. In

addition, several environment variables are specified by the login process. "TERM" defines the

terminal type, and "USER" or "LOGNAME" defines your user ID. "SHELL" defines your default

shell, and "TZ" specifies your time zone. Check the manual pages, and test your own environment to

find out for sure. The external program "env" prints all current environment variables.

PS1 - Normal Prompt The "PS1" variable specifies the prompt printed before each command. It is

normally "$ ." The current directory cannot be placed inside this prompt. Well, some people make a

joke, and tell a new user to place a period inside this variable. A "." does signifies the current

directory, however, most users prefer the actual name.

PS2 - Secondary Prompt The "PS2" environment variable defines the secondary prompt, This is the prompt you see when you execute a multi-line command, such as "for" or "if." You also see it when you forget to terminate a quote. The default value is "> ."

11 | P a g e

Page 12: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

ASSIGNMENT SET – 2

1. What is difference between multi-programming and timesharing operating systems?

Ans : Multiprogramming is the allocation of more than one concurrent program on a computer system and its resources. Multiprogramming allows using the CPU effectively by allowing various users to use the CPU and I/O devices effectively. Multiprogramming makes sure that the CPU always has something to execute, thus increases the CPU utilization. On the other hand, Time sharing is the sharing of computing resources among several users at the same time. Since this will allow a large number of users to work in a single computer system at the same time, it would lower the cost of providing computing capabilities

What is Multiprogramming System?

Multiprogramming is the fast switching of CPU between several programs. A program is generally made up of several tasks. A task usually ends with some request to move data which would require some I/O operations to be executed. Multitasking was commonly done to keep the CPU busy, while the currently running program is doing I/O operations. Compared to other executing instructions, I/O operations are extremely slow. Even if a program contains a very small number of I/O operations, most of the time taken for the program is spent on those I/O operations. Therefore, using this idle time and allowing another program to utilize the CPU at that time will increase the CPU utilization. Multiprogramming was initially developed in late 1950s as a feature of operating systems and was first used in mainframe computing. With the introduction of virtual memory and virtual machine technologies, the use of multiprogramming was enhanced.

What is Time Sharing System?

Time sharing, which was introduced in 1960s, is the sharing of computing resources among several users at the same time. In time sharing systems, several terminals are attached to a single dedicated server having its own CPU. Actions/commands executed by the operating system of a time sharing system have a very short time span. Therefore the CPU is assigned to users at the terminals for a short time period, thus a user in a terminal gets the feeling that she has a CPU dedicated to her behind her terminal. The short time period that a command is executed on a time sharing system is called a time slice or a time quantum. With the development of internet, time sharing systems have become more popular since expensive server farms could host a very large number of customers sharing the same resources. Since the websites operate mainly in bursts of activity followed by periods of idle time, the idling time of one customer could be used effectively by the other, without any of them noticing the delay.

What is the difference between Multiprogramming System and Time Sharing System?

Main difference between multiprogramming and time sharing is that multiprogramming is the effective utilization of CPU time, by allowing several programs to use the CPU at the same time but time sharing is the sharing of a computing facility by several users that want to use the same facility at the same time. Each user on a time sharing system gets her own terminal and gets the feeling that

12 | P a g e

Page 13: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

she is using the CPU alone. Actually, time sharing systems use the concept of multiprogramming to share the CPU time between multiple users at the same time

2. Discuss the file structure? Explain the various access modes ?

Ans: File Structure Unix hides the “chunkiness” of tracks, sectors, etc. and presents each file as a “smooth” array of bytes with no internal structure. Application programs can, if they wish, use the bytes in the file to represent structures. For example, a wide-spread convention in Unix is to use the newline character (the character with bit pattern 00001010) to break text files into lines. Some other systems provide a variety of other types of files. The most common are files that consist of an array of fixed or variable size records and files that form an index mapping keys to values. Indexed files are usually implemented as B-trees.

File Types Most systems divide files into various “types.” The concept of “type” is a confusing one, partially because the term “type” can mean different things in different contexts. Unix initially supported only four types of files: directories, two kinds of special files (discussed later), and “regular” files. Just about any type of file is considered a “regular” file by Unix. Within this category, however, it is useful to distinguish text files from binary files; within binary files there are executable files (which contain machine-language code) and data files; text files might be source files in a particular programming language (e.g. C or Java) or they may be human-readable text in some mark-up language such as html (hypertext markup language). Data files may be classified according to the program that created them or is able to interpret them, e.g., a file may be a Microsoft Word document or Excel spreadsheet or the output of TeX. The possibilities are endless.

In general (not just in Unix) there are three ways of indicating the type of a file:

1. The operating system may record the type of a file in meta-data stored separately from the file, but associated with it. Unix only provides enough meta-data to distinguish a regular file from a directory (or special file), but other systems support more types.

2. The type of a file may be indicated by part of its contents, such as a header made up of the first few bytes of the file. In Unix, files that store executable programs start with a two byte magic number that identifies them as executable and selects one of a variety of executable formats. In the original Unix executable format, called the a.out format, the magic number is the octal number 0407, which happens to be the machine code for a branch instruction on the PDP-11 computer, one of the first computers to implement Unix. The operating system could run a file by loading it into memory and jumping to the beginning of it. The 0407 code, interpreted as an instruction, jumps to the word following the 16-byte header, which is the beginning of the executable code in this format. The PDP-11 computer is extinct by now, but it lives on through the 0407 code!

3. The type of a file may be indicated by its name. Sometimes this is just a convention, and sometimes it's enforced by the OS or by certain programs. For example, the Unix Java compiler refuses to believe that a file contains Java source unless its name ends with .java.

13 | P a g e

Page 14: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

Some systems enforce the types of files more vigorously than others. File types may be enforced

Not at all, Only by convention, By certain programs (e.g. the Java compiler), or By the operating system itself.

Unix tends to be very lax in enforcing types.

Access Modes

Systems support various access modes for operations on a file.

Sequential. Read or write the next record or next n bytes of the file. Usually, sequential access also allows a rewind operation.

Random. Read or write the nth record or bytes i through j. Unix provides an equivalent facility by adding a seek operation to the sequential operations listed above. This packaging of operations allows random access but encourages sequential access.

Indexed. Read or write the record with a given key. In some cases, the “key” need not be unique--there can be more than one record with the same key. In this case, programs use a combination of indexed and sequential operations: Get the first record with a given key, then get other records with the same key by doing sequential reads.

Note that access modes are distinct from from file structure--e.g., a record-structured file can be accessed either sequentially or randomly--but the two concepts are not entirely unrelated. For example, indexed access mode only makes sense for indexed files.

File Attributes This is the area where there is the most variation among file systems. Attributes can also be grouped by general category.

Name.

Ownership and Protection.

Owner, owner's “group,” creator, access-control list (information about who can to what to this file, for example, perhaps the owner can read or modify it, other members of his group can only read it, and others have no access).

Time stamps.

Time created, time last modified, time last accessed, time the attributes were last changed, etc. Unix maintains the last three of these. Some systems record not only when the file was last modified, but by whom.

Sizes. Current size, size limit, “high-water mark”, space consumed (which may be larger than size because of internal fragmentation or smaller because of various compression techniques).

14 | P a g e

Page 15: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

Type Information.

As described above: File is ASCII, is executable, is a “system” file, is an Excel spread sheet, etc.

Misc.

Some systems have attributes describing how the file should be displayed when a directly is listed. For example MacOS records an icon to represent the file and the screen coordinates where it was last displayed. DOS has a “hidden” attribute meaning that the file is not normally shown. Unix achieves a similar effect by convention: The ls program that is usually used to list files does not show files with names that start with a period unless you explicit request it to (with the -a option).

Unix records a fixed set of attributes in the meta-data associated with a file. If you want to record some fact about the file that is not included among the supported attributes, you have to use one of the tricks listed above for recording type information: encode it in the name of the file, put it into the body of the file itself, or store it in a file with a related name (e.g. “foo.attributes”). Other systems (notably MacOS and Windows NT) allow new attributes to be invented on the fly. In MacOS, each file has a resource fork, which is a list of (attribute-name, attribute-value) pairs. The attribute name can be any four-character string, and the attribute value can be anything at all. Indeed, some kinds of files put the entire “contents” of the file in an attribute and leave the “body” of the file (called the data fork) empty.

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

Ans: There are many resources that can be allocated to only one process at a time, and we have seen

several operating system features that allow this, such as mutexes, semaphores or file locks.

Sometimes a process has to reserve more than one resource. For example, a process which copies files from one tape to another generally requires two tape drives. A process which deals with databases may need to lock multiple records in a database.

A deadlock is a situation in which two computer programs sharing the same resource are effectively preventing each other from accessing the resource, resulting in both programs ceasing to function.

The earliest computer operating systems ran only one program at a time. All of the resources of the system were available to this one program. Later, operating systems ran multiple programs at once, interleaving them. Programs were required to specify in advance what resources they needed so that they could avoid conflicts with other programs running at the same time. Eventually some operating systems offered dynamic allocation of resources. Programs could request further allocations of resources after they had begun running. This led to the problem of the deadlock. Here is the simplest example:

Program 1 requests resource A and receives it.Program 2 requests resource B and receives it.Program 1 requests resource B and is queued up, pending the release of B.Program 2 requests resource A and is queued up, pending the release of A.

15 | P a g e

Page 16: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

Now neither program can proceed until the other program releases a resource. The operating system cannot know what action to take. At this point the only alternative is to abort (stop) one of the programs.

Learning to deal with deadlocks had a major impact on the development of operating systems and the structure of databases. Data was structured and the order of requests was constrained in order to avoid creating deadlocks.

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. Here is an example of a situation where deadlock can occur.

Mutex M1, M2;/* Thread 1 */while (1) { NonCriticalSection() Mutex_lock(&M1); Mutex_lock(&M2); CriticalSection(); Mutex_unlock(&M2); Mutex_unlock(&M1);}

 /* Thread 2 */

while (1) { NonCriticalSection() Mutex_lock(&M2); Mutex_lock(&M1); CriticalSection(); Mutex_unlock(&M1); Mutex_unlock(&M2);}

Suppose thread 1 is running and locks M1, but before it can lock M2, it is interrupted. Thread 2 starts running; it locks M2, when it tries to obtain and lock M1, it is blocked because M1 is already locked (by thread 1). Eventually thread 1 starts running again, and it tries to obtain and lock M2, but it is blocked because M2 is already locked by thread 2. Both threads are blocked; each is waiting for an event which will never occur.

16 | P a g e

Page 17: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

Traffic gridlock is an everyday example of a deadlock situation.

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.

The dining philosophers problem discussed in an earlier section is a classic example of deadlock. Each philosopher picks up his or her left fork and waits for the right fork to become available, but it never does.

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.

17 | P a g e

Page 18: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

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.

4. What are various File Systems supported in UNIX? Discuss any three of them ?

Ans: File System Types

Initially, there were only two types of file systems-the ones from AT&T and Berkeley. Following are some file systems types:

s5

Before SVR4, this was the only file system used by System V, but today it is offered by SVR4 by this name for backward compatibility only. This file system uses a logical block size of 512 or 1024 bytes and a single super block. It also can’t handle filenames longer than 14 characters.

ufs

This is how the Berkeley fast file systems is known to SVR4 and adopted by most UNIX systems. Because the block size here can go up to 64 KB, performance of this file system is considerably better than s5. It uses multiple super blocks with each cylinder group storing a superblock. Unlike s5, ufs supports 255-character filenames, symbolic links and disk quotas.

Ext2

This is the standard file system of Linux, It uses a block size of 1024 bytes and, like ufs, uses multiple superblocks and symbolic links.

18 | P a g e

Page 19: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

Iso9660 or hsfs

This is the standard file system used by CD-ROMs and uses DOS-style 8+3 filenames, Since UNIX uses longer filenames, hsfs also provides Rock Ridge extensions to accommodate them.

msdos or pcfs

Most UNIX systems also support DOS file systems. You can create this file system on a floppy diskette and transfer files to it for use on a windows system. Linux and Solaris can also directly access a DOS file system in the hard disk.

swap

bfs The boot file system

This is used by SVR4 to host the boot programs and the UNIX kernel. Users are not meant to use this file system.

proc or procfs

This can be considered a pseudo-file system maintained in memory. It stores data of each running process and appears to contain files. But actually contains none. Users can obtain most process information including their PIDs. directly from here.

Fdisk

Creating Partitions

Both Linux and SCO UNIX allow a user to have multiple operating systems on Intel Machines. It’s no wonder then that both offer the Windows-type fdisk command to create, delete and activate partitions. fdisk in Linux , however, operates differently from windows. The fdisk m command shows you all its internal commands of which the following subset should serve our purpose:

Command Action

A toggle a bootable flag N add a new partition

D delete a partition P Print partition table

L list known partition types Q Quit without saving

M print this menu W Write table to disk & exit.

mkfs : creating file systems

Now that you have created a partition, you need to create a file system on this partition to make it usable. mkfs is used to build a Linux file system on a device, usually a hard disk partition. The exit code returned by mkfs is 0 on success and 1 on failure.

19 | P a g e

Page 20: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

The file system-specific builder is searched for in a number of directories like perhaps /sbin, /sbin/fs, /sbin/fs.d, /etc/fs, /etc (the precise list is defined at compile time but at least contains /sbin and /sbin/fs), and finally in the directories listed in the PATH environment variable.

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

Ans: A process under unix consists of an address space and a set of data structures in the kernel to keep track of that process. The address space is a section of memory that contains the code to execute as well as the process stack. The kernel must keep track of the following data for each process on the system:

the address space map,

the current status of the process,

the execution priority of the process,

the resource usage of the process,

the current signal mask,

the owner of the process.

A process has certain attributes that directly affect execution, these include:

PID - The PID stands for the process identification. This is a unique number that defines the process within the kernel.

PPID - This is the processes Parent PID, the creator of the process.

UID - The User ID number of the user that owns this process.

EUID - The effective User ID of the process.

GID - The Group ID of the user that owns this process.

EGID - The effective Group User ID that owns this process.

riority - The priority that this process runs at.

To view a process you use the ps command.

# ps -l

F S UID PID PPID C PRI NI P SZ:RSS WCHAN TTY TIME COMD

30 S 0 11660 145 1 26 20 * 66:20 88249f10 ttyq 6 0:00 rlogind

20 | P a g e

Page 21: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

The F field: This is the flag field. It uses hexadecimal values which are added to show the value of the flag bits for the process. For a normal user process this will be 30, meaning it is loaded into memory.

The S field: The S field is the state of the process, the two most common values are S for Sleeping and R for Running. An important value to look for is X, which means the process is waiting for memory to become available.

PID field: The PID shows the Process ID of each process. This value should be unique. Generally PID are allocated lowest to highest, but wrap at some point. This value is necessary for you to send a signal to a process such as the KILL signal.

PRI field: This stands for priority field. The lower the value the higher the value. This refers to the process NICE value. It will range form 0 to 39. The default is 20, as a process uses the CPU the system will raise the nice value.

P flag: This is the processor flag. On the SGI this refers to the processor the process is running on.

SZ field: This refers to the SIZE field. This is the total number of pages in the process. Each page is 4096 bytes.

TTY field: This is the terminal assigned to your process.

Time field: The cumulative execution time of the process in minutes and seconds.

COMD field: The command that was executed.

The fork() System Call

The fork() system call is the basic way to create a new process. It is also a very unique system call, since it returns twice(!) to the caller. This system call causes the current process to be split into two processes - a parent process, and a child process. All of the memory pages used by the original process get duplicated during the fork() call, so both parent and child process see the exact same image. The only distinction is when the call returns. When it returns in the parent process, its return value is the process ID (PID) of the child process. When it returns inside the child process, its return value is ‘0′. If for some reason this call failed (not enough memory, too many processes, etc.), no new process is created, and the return value of the call is ‘-1′. In case the process was created successfully, both child process and parent process continue from the same place in the code where the fork() call was used.

#include <unistd.h> /* defines fork(), and pid_t. */

#include <sys/wait.h> /* defines the wait() system call. */

//storage place for the pid of the child process, and its exit status.

21 | P a g e

Page 22: MC0070 Operating Systems With Unix

MC0070 Operating Systems with Unix

spid_t child_pid; int child_status;

child_pid = fork(); /* lets fork off a child process… */

switch (child_pid) /* check what the fork() call actually did */

{ case -1: /* fork() failed */

perror("fork"); /* print a system-defined error message */ exit(1);

case 0: /* fork() succeeded, we’re inside the child process */ printf("hello worldn");

exit(0); //here the CHILD process exits, not the parent.

default: /* fork() succeeded, we’re inside the parent process */ wait(&child_status); /* wait till the child process exits */ }

/* parent’s process code may continue here… */

22 | P a g e