Sei sulla pagina 1di 11

Operating system: 1.

A) System Calls
When a computer is turned on, the program that gets executed first is called the ``operating system.'' It controls pretty much all activity in the computer. This includes who logs in, how disks are used, how memory is used, how the CPU is used, and how you talk with other computers. The operating system we use is called "Unix". The way that programs talk to the operating system is via ``system calls.'' A system call looks like a procedure call (see below), but it's different -- it is a request to the operating system to perform some activity.

System calls are expensive. While a procedure call can usually be performed in a few machine instructions, a system call requires the computer to save its state, let the operating system take control of the CPU, have the operating system perform some function, have the operating system save its state, and then have the operating system give control of the CPU back to you. This concept is important, and will be seen time and time again in this class. System calls are system dependent. Knowing this, it would not be a good idea to directly use system calls when portability cannot be neglected. System calls are also quite complex. Most often, it involves the duo of TRAP and RET (or some variations of those two). To implement system call, one would need specialized knowledge of I/O registers, the sequence of operations needed to use them; and most important of all, implement enough protection because I/O resources are generally shared among multiple users and/or processes.

System Calls for I/O


There are 5 basic system calls that Unix provides for file I/O.
1. 2. 3. 4. 5. int open(char *path, int flags [ , int mode ] ); int close(int fd); int read(int fd, char *buf, int size); int write(int fd, char *buf, int size); off_t lseek(int fd, off_t offset, int whence);

a system call is how a program requests a service from an operating system's kernel. This may include hardware related services (e.g. accessing the hard disk), creating and executing new processes, and communicating with integral kernel services (like scheduling). System calls provide an essential interface between a process and the operating system.

Types of System Calls The main types of system calls are as follows:

O Process Control: These types of system calls are used to control the processes. Some examples are end, abort, load, execute, create process, terminate process etc. O File Management: These types of system calls are used to manage files. Some examples are Create file, delete file, open, close, read, write etc. O Device Management: These types of system calls are used to manage devices. Some examples are Request device, release device, read, write, get device attributes etc. O Information Maintenance: These types of system calls are used to set system data and get process information. Some examples are time, OS parameters, id, time used etc. O Communications: These types of system calls are used to establish a connection. Some examples are send message, received messages, terminate etc

1. B) virtual machines

Definitions
A virtual machine (VM) is a software implementation of a machine (i.e. a computer) that executes programs like a physical machine. Virtual machines are separated into two major classifications, based on their use and degree of correspondence to any real machine: 1) A system virtual machine provides a complete system platform which supports the execution of a complete operating system (OS)[1] . These usually emulate an existing architecture, and are built with either the purpose of providing a platform to run programs where the real hardware is not available for use (for example, executing software on otherwise obsolete platforms), or of having multiple instances of virtual machines lead to more efficient use of computing resources, both in terms of energy consumption and cost effectiveness (known as hardware virtualization, the key to a cloud computing environment), or both. 2) A process virtual machine (also, language virtual machine) is designed to run a single program, which means that it supports a single process. Such virtual machines are usually closely suited to one or more programming languages and built with the purpose of providing program portability and flexibility (amongst other things). An essential characteristic of a virtual machine is that the software running inside is limited to the resources and abstractions provided by the virtual machineit cannot break out of its virtual environment. A virtual machine was originally defined by Popek and Goldberg as "an efficient, isolated duplicate of a real machine". Current use includes virtual machines which have no direct correspondence to any real hardware.[2] System virtual machine advantages:

multiple OS environments can co-exist on the same computer, in strong isolation from each other

the virtual machine can provide an instruction set architecture (ISA) that is somewhat different from that of the real machine application provisioning, maintenance, high availability and disaster recovery[3]

Multiple VMs each running its own operating system (called guest operating system) are frequently used in server consolidation, where different services that used to run on individual machines to avoid interference are instead run in separate VMs on the same physical machine. The desire to run multiple operating systems was the original motivation for virtual machines, as it allowed time-sharing a single computer between several single-tasking Operation Systems. In some respects, a system virtual machine can be considered a generalization of the concept of virtual memory that historically preceded it. IBM's CP/CMS, the first systems to allow full virtualization, implemented time sharing by providing each user with a single-user operating system, the CMS. Unlike virtual memory, a system virtual machine allowed the user to use privileged instructions in his code. This approach had certain advantages, for instance it allowed users to add input/output devices not allowed by the standard system.[4] As technology evolves virtual memory, in respect to virtualization, will utilize the technologies of memory overcommitment to manage the memory sharing between multiple virtual machines on one physical computer. On a related note, sometimes it is possible to share those memory pages that have identical contents among multiple virtual machines running on the same physical machine, mapping them to the same physical page, by a technique known as Kernel SamePage Merging. This is particularly useful for read-only pages, such as those ones containing code segments, especially in the case of multiple virtual machines running the same or similar software, such as the Operating System, software libraries, web server, middleware components, etc.. The guest OSes do not have to be compliant with the hardware making it possible to run different OSes on the same computer (e.g., Microsoft Windows and Linux, or older versions of an OS to support software that has not yet been ported to the latest version). The use of virtual machines to support different guest OSes is becoming popular in embedded systems; a typical use is to support a real-time operating system at the same time as a high-level OS such as Linux or Windows. Another use is to sandbox an OS that is not trusted, possibly because it is a system under development. Virtual machines have other advantages for OS development, including better debugging access and faster reboots.[5] A process VM, sometimes called an application virtual machine, runs as a normal application inside a host OS and supports a single process. It is created when that process is started and destroyed when it exits. Its purpose is to provide a platform-independent programming environment that abstracts away details of the underlying hardware or operating system, and allows a program to execute in the same way on any platform. A process VM provides a high-level abstraction that of a high-level programming language (compared to the low-level ISA abstraction of the system VM). Process VMs are implemented

using an interpreter; performance comparable to compiled programming languages is achieved by the use of just-in-time compilation. This type of VM has become popular with the Java programming language, which is implemented using the Java virtual machine. Other examples include the Parrot virtual machine, which serves as an abstraction layer for several interpreted languages, and the .NET Framework, which runs on a VM called the Common Language Runtime. A special case of process VMs are systems that abstract over the communication mechanisms of a (potentially heterogeneous) computer cluster. Such a VM does not consist of a single process, but one process per physical machine in the cluster. They are designed to ease the task of programming parallel applications by letting the programmer focus on algorithms rather than the communication mechanisms provided by the interconnect and the OS. They do not hide the fact that communication takes place, and as such do not attempt to present the cluster as a single parallel machine. Unlike other process VMs, these systems do not provide a specific programming language, but are embedded in an existing language; typically such a system provides bindings for several languages (e.g., C and FORTRAN). Examples are PVM (Parallel Virtual Machine) and MPI (Message Passing Interface). They are not strictly virtual machines, as the applications running on top still have access to all OS services, and are therefore not confined to the system model provided by the "VM".

2. B) grep , egrep and fgrep :

grep (grep "some text") will grep some text. egrep (grep -E in linux) is extended grep where additional regular expression metacharacters have been added like +, ?, | and () fgrep (grep -F in linux) is fixed or fast grep and behaves as grep but does not recognise any regular expression metacharacters as being special.

grep COMMAND: grep command selects and prints the lines from a file which matches a given string or pattern. SYNTAX: The Syntax is grep [options] pattern [file] EXAMPLE: Lets assume that we have a file file1.txt and it has the following data.

hscripts has many valuable free scripts It is the parent site of www.forums.hscripts.com hscripts include free tutorials and free gif images Purchase scripts from us A webmaster/web master resource website

1.

To print all lines containing hscripts :

grep 'hscripts' file1.txt


The output will be. hscripts has many valuable free scripts It is the parent site of www.forums.hscripts.com hscripts include free tutorials and free gif images

2.

To print the count of line that matches hscripts.

grep -c 'hscripts' file1.txt


The output will be. 3

egrep COMMAND: egrep command is used to search and find one or more files for lines that match the given string or word. SYNTAX: The Syntax is egrep [options] pattern [file] EXAMPLE: Lets assume that we have a file file1.txt and it has the following data.

hscripts has many valuable free scripts It is the parent site of www.forums.hscripts.com hscripts include free tutorials and free gif images free DNS lookup tool Purchase scripts from us A webmaster/web master resource website
1. To search more words from a file :

egrep 'hscripts|forums|free' file1.txt


The output will be. hscripts has many valuable free scripts It is the parent site of www.forums.hscripts.com hscripts include free tutorials and free gif images

free DNS lookup tool 2. 3. To print the lines containing "fr ee" followed by images:

egrep 'free.*images' file1.txt


The output will be.

hscripts include free tutorials and free gif images fgrep COMMAND: fgrep command is used to search one or more files for lines that match the given string or word. fgrep is faster than grep search, but less flexible: it can only find fixed text, not regular expressions. SYNTAX: The Syntax is fgrep [options] pattern [file] EXAMPLE: Lets assume that we have a file file1.txt and it has the following data.

hscripts is the parent site of www.forums.hscripts.com ask your technical doubts in our forum our forums is free
1. To search and print the lines containing forum :

fgrep 'forum' file1.txt


fgrep command prints the output as.

hscripts is the parent site of www.forums.hscripts.com our forums is free

3)a)Operating system shells


Your interface to the operating system is called a

shell.

The shell is the outermost layer of the operating system. Shells incorporate a programming language to control processes and files, as well as to start and control other programs. The shell manages the interaction between you and the operating system by prompting you for input, interpreting that input for the operating system, and then handling any resulting output from the operating system. Shells provide a way for you to communicate with the operating system. This communication is carried out either interactively (input from the keyboard is acted upon

immediately) or as a shell script. A

shell script is a sequence of shell and operating

system commands that is stored in a file. When you log in to the system, the system locates the name of a shell program to execute. After it is executed, the shell displays a command prompt. This prompt is usually a $ (dollar sign). When you type a command at the prompt and press the Enter key, the shell evaluates the command and attempts to carry it out. Depending on your command instructions, the shell writes the command output to the screen or redirects the output. It then returns the command prompt and waits for you to type another command. A

command line is the line on which you type. It contains the shell prompt. The basic

format for each line is as follows: The shell considers the first word of a command line (up to the first blank space) as the command and all subsequent words as arguments. Note: When libc.a is moved or renamed, the Killed error message is displayed from the shell because there is no libc.a file available for the system to load and run the utilities. The recsh command invokes the recovery shell, which provides an ability to rename libc.a if it is accidently moved.

1. RESPONSIBILITIES OF SHELL : 1. The shell interpits the command line for the user 2. Porgram exicution the shell is responsible for excuting the command that we enter meaning it analyzes the command before it is exucted 3. The shell has a responsibility of the input output redirection of a file. 4.The shell provides commands that let you customize your environment also. 5.The shell has its own built-in programming language, meaning that the shell analyzes each statement in the language one line at a time and then executes it. 6.The shell the interface that is seen and used by users. i.e. Kernal and Shell. Kernal takes care of hardware interface and shell communicates with Kernal. That communication is called system calls Thats just a quick sum. 3)b) In addition to simple lists of commands, shell scripts can contain all the familiar control structures, such as conditionals and loops: Name if Syntax
if CONDITION; then COMMANDS...; fi

Example
if [ $test ]; then echo "test is true"; fi

for

for VARIABLE in LIST...; for i in 1 2 3; do echo $i iteration ; done do COMMANDS...; done [ EXPRESSION ] [ -e FILENAME ] [ $hippos -eq 5 ] [ $hippos = "five" ]

Check if EXPRESSION is true or false. See man test for a full explanation of valid expressions. True iff the give file exists True iff the variable hippos contains the number 5 True iff the variable hippos contains the string "five" True iff the file with a filename given in the variable filename is not executable. True when either FILE1 exists or FILE2 is a directory True when $compiler contains a string and the filename whose name is given by $objfile is older than that given by $cppfile

condition

[ ! -x $filename ] [ -e FILE1 -o -d FILE2 ] [ -n $compiler -a $objfile -ot $cppfile ]

4)a)Disk/Free Space Management:


Since there is only a limited amount of disk space, it is necessary to reuse the space from deleted files for new files. To keep track of free disk space, the system maintains a free-space list which records all disk blocks that are free. Free-space list can be implemented as: 1) Bit-Vector: Each block is represented by a 1 bit. If the block is free, the bit is 0; if the block is allocated, the bit is 1. 2) Linked List: This approach link all the free disk blocks together, keeping a pointer to the first free block. This block contains a pointer to the next free disk block, and so on. 3) Grouping: This approach stores the addresses of n free blocks in the first free block. The first n-1 of these are actually free. The last one is the disk address of another block containing addresses of other n free blocks. The importance of this implementation is that addresses of a large number of free blocks can be found quickly. 4) Counting: This approach takes advantage of the fact that several contiguous blocks may be allocated or freed simultaneously. Thus, rather than keeping a list of free disk addresses, the address of the first free block is kept and the number n of free contiguous blocks that follow

the first block.

4)b) The Banker's algorithm is run by the operating system whenever a process requests

resources.[2] The algorithm avoids deadlock by denying or postponing the request if it determines that accepting the request could put the system in an unsafe state (one where deadlock could occur). When a new process enters a system, it must declare the maximum number of instances of each resource type that may not exceed the total number of resources in the system. Also, when a process gets all its requested resources it must return them in a finite amount of time.
[edit] Resources For the Banker's algorithm to work, it needs to know three things:

How much of each resource each process could possibly request How much of each resource each process is currently holding How much of each resource the system currently has available

Resources may be allocated to a process only if it satisfies the following conditions:


1. request max, else set error condition as process has crossed maximum claim made by it. 2. request available, else process waits until resources are available.

Some of the resources that are tracked in real systems are memory, semaphores and interface access. The Banker's Algorithm derives its name from the fact that this algorithm could be used in a banking system to ensure that the bank does not run out of resources, because the bank would never allocate its money in such a way that it can no longer satisfy the needs of all its customers. By using the Banker's algorithm, the bank ensures that when customers request money the bank never leaves a safe state. If the customer's request does not cause the bank to leave a safe state, the cash will be allocated, otherwise the customer must wait until some other customer deposits enough. Basic data structures to be maintained to implement the Banker's Algorithm:

Let n be the number of processes in the system and m be the number of resource types. Then we need the following data structures:

Available: A vector of length m indicates the number of available resources of each type. If Available[j] = k, there are k instances of resource type Rj available. Max: An nm matrix defines the maximum demand of each process. If Max[i,j] = k, then Pi may request at most k instances of resource type Rj. Allocation: An nm matrix defines the number of resources of each type currently allocated to each process. If Allocation[i,j] = k, then process Pi is currently allocated k instance of resource type Rj. Need: An nm matrix indicates the remaining resource need of each process. If Need[i,j] = k, then Pi may need k more instances of resource type Rj to complete task.

Note: Need = Max - Allocation.


Safe and Unsafe States

A state (as in the above example) is considered safe if it is possible for all processes to finish executing (terminate). Since the system cannot know when a process will terminate, or how many resources it will have requested by then, the system assumes that all processes will eventually attempt to acquire their stated maximum resources and terminate soon afterward. This is a reasonable assumption in most cases since the system is not particularly concerned with how long each process runs (at least not from a deadlock avoidance perspective). Also, if a process terminates without acquiring its maximum resources, it only makes it easier on the system. A safe state is considered to be the decision maker if it is going to process ready queue. Safe State ensures the Security. Given that assumption, the algorithm determines if a state is safe by trying to find a hypothetical set of requests by the processes that would allow each to acquire its maximum resources and then terminate (returning its resources to the system). Any state where no such set exists is an unsafe state.
Requests

When the system receives a request for resources, it runs the Banker's algorithm to determine if it is safe to grant the request. The algorithm is fairly straight forward once the distinction between safe and unsafe states is understood.
1. Can the request be granted? o If not, the request is impossible and must either be denied or put on a waiting list 2. Assume that the request is granted 3. Is the new state safe? o If so grant the request o If not, either deny the request or put it on a waiting list

Whether the system denies or postpones an impossible or unsafe request is a decision specific to the operating system.

Limitations
Like other algorithms, the Banker's algorithm has some limitations when implemented. Specifically, it needs to know how much of each resource a process could possibly request. In most systems, this information is unavailable, making it impossible to implement the Banker's algorithm. Also, it is unrealistic to assume that the number of processes is static since in most systems the number of processes varies dynamically. Moreover, the requirement that a process will eventually release all its resources (when the process terminates) is sufficient for the correctness of the algorithm, however it is not sufficient for a practical system. Waiting for hours (or even days) for resources to be released is usually not acceptable.

Potrebbero piacerti anche