HARBIN INSTITUTE OF TECHNOLOGY CHINA BSC Computer Science YEAR 1 SEMESTER 2 EXAMINATIONS 2008 OPERATING SYSTEMS SOLUTIONS Dr. F.J Mtenzi Duration 2 hours Attempt THREE questions. All questions carry equal marks. 1. (a) What is a process in a computer system A process is an instance of a program in execution. Also a process is an ‘environment’ in which a program executes. A process behaves like a complete self-contained computer executing independently of other processes. A process is like an emulation of a self-contained computer. It can have a window to act as its screen. When a window is active, the corresponding process uses the keyboard and mouse as its input devices. It has its own separate memory, its own set of processor registers. It processes instructions from the program in its own memory area. Although processes can’t be implemented this way as the machine would be slowed down unacceptably, however, thinking of a process as an emulation of a separate computer is still a useful mental model... Marking: 5 marks for all of the above (b) Describe briefly the three main stages in the evolution of operating system designs. (6 marks) Early Operating systems were designed in an unstructured way as a collection of procedures. Any procedure can be called by any other procedure. Maintenance was very difficult. The second generation were designed using structured design methods. An inner layer (the kernel) communicates with hardware.No other layer can access hardware directly. The inner layer was only accessible by a second layer via Application Programming Interfaces. The second layer consisted of utilities and a shell. The user could interact with the computer via the shell. The third layer was application programs. Which could interact with the hardware via APIs. The third generation Used object-oriented design principles. Where the operating system is designed as a set of interacting objects. Each object belongs to a class which defines specific behaviour. Marking: 6 marks for all of the above including diagram (c) Explain what is a process? Briefly explain four reasons for process termination. (6 marks) Any good explanation of any of the following reasons for process termination Normal completion Time limit exceeded Memory unavailable Bounds violation Protection error example write to read-only file Arithmetic error Time overrun process waited longer than a specified maximum for an event I/O failure 2 Invalid instruction happens when try to execute data Privileged instruction Data misuse Operating system intervention such as when deadlock occurs Parent terminates so child processes terminate Parent request Marking: 5 marks for all of the above (d) Describe the main components of an operating system. Operating system structure: Two main components: Kernel and Shell Kernel: The inner part of the system responsible for dealing with processes and allocating system resources. Resources include: processor time, memory, disk space, network bandwidth, access to unshareable devices (e.g. sound card, mouse, keyboard) Processor management - Processes run for a short period of time (a ‘quantum’ or ‘timeslice’) and are then interrupted. The OS kernel regains control and can switch to another process. If the timeslice is small enough, the processes appear to run in parallel with each other. Memory management - Each process has its own address space, the range of addresses from 0 upwards that it can refer to. Memory management hardware maps (translates) process addresses into corresponding addresses in physical memory. Operating system can provide a simplified, idealised view of a resource example: file system provides named files they may be located on disks, CDs, remote machines... access is the same regardless of how or where the data is actually stored. Device drivers: Kernel components that interact with specific hardware devices (e.g. the sound card) Shell: the ‘desktop’ program that lets you interact with the operating system. GUI programs are event driven. Events arrive in an unpredictable sequence from various sources (mouse, keyboard, ...) Programs must be prepared to deal with events in any sequence. Shells can also be character based like the bash shell in Linux Marking: 10 marks for all of the above 2. (a) What contribution does a filesystem make to the overall operation of an operating system? A filesystem is a mechanism which allows a user to organize information by storing it in files and allows the user to organize the files into some convenient arrangement so that they can be referred to by name. It normally allows files to be grouped into directories to make it easy to find files. It also acts to conceal the differences between the various hardware devices which may be used to store files. Marking: 5 marks for the above answer While sophisticated file managers implement file sharing by allowing several users to access a single copy of a file at the same time, others implement file sharing by (b) 3 providing a copy of the file to each user. List the advantages and disadvantages of each method. The advantage of having just one copy of a file to be shared by several users is the saving of space in both secondary storage and memory. The disadvantage is that the operating system has to implement a concurrency control algorithm, similar to that of the readers and writers, to allow many users to access that file without compromising its integrity. The main disadvantage of having multiple copies of the same file is the amount of space used both in secondary storage and main memory. An advantage is that the operating system is not complicated by having to support mutual exclusion when several users access the same file. Marking 10 marks for the above answer. (c) Explain how are read, write and execute file permissions controlled on file in a UNIX filesystem. (5 marks) Every file has wrx permissions set for 3 categories of user , user, goup and other e.g a file might be marked rw_r__r__ This indicates the privilege for user, group and other in that order. The field is either marked with w, r, x, or _ . r means that that category if user can read a file, w means that that category if user can write to a file and x means that that category if user can execute a file. Marking 5 marks for the above answer. (d) When is compaction of secondary storage beneficial from the file manager’s perspective? Give several examples. List some problems that could be presented as a result of compaction and explain how they might be avoided. Compared to memory compaction, which may take seconds, disk compaction may take several hours; therefore it should be done sparingly. Usually compaction is dictated by users’ complaints because they experience long waits while retrieving data from files that are dispersed throughout the disk forming a long chain whose links must be accessed sequentially. Data files and database that grow during their life time are prime examples of files that need to be compacted periodically. If the system crashes during compaction, the files that were in transit may be destroyed. This could be solved by doing a complete backup of the disk to be compacted before initiating compaction. However, this would add to the overhead. Marking 5 marks for the above answer 4 3. (a) Explain the operation of a fixed-size memory allocation scheme. (5 marks) FIXED-SIZE ALLOCATION Process view of address space System view of address space 512M 384M 128M 306M (256M + 50M) 50M 256M 0 128M 0 • As shown on the diagram an example using 512Mb of memory. – 512M of physical memory is divided into four blocks of 128M each – each process gets one 128M block and thinks it has memory allocated from address 0 to address 128M–1 in its 4GB address space – address translation adds the physical base address of the block to the addresses used by the process to get the real physical address • Advantages: – simple to implement (not much more than an adder connected between processor and memory to do the address translation) • Disadvantages – wasteful (small processes may not need 128M of memory) – limits number of concurrent processes to number of blocks available – can’t run processes needing more than 128M Marking 5 marks for the above answer. (b) Describe the problem of fragmentation which can occur in a variable-size memory allocation scheme. (5 marks) Fragmentation: After a number of small processes have finished running, the memory that they have used will be marked a free for other new processes to use. However these may not be in contiguous address spaces. This will leave small scattered blocks of free memory located around memory. This is called fragmentation. If a large process is to run, there may not be enough contiguous free memory for it to run. Marking 5 marks for the above answer. (c) Most modern systems use virtual memory management. How does this system operate? (10 marks) 5 VIRTUAL MEMORY • Allocate memory in fixed-size blocks called pages (e.g. 4K each) • Need to keep track of physical location of each page (using a page table) – process can have as many pages as it needs – process can allocate more memory as it needs it by requesting more blocks – no fragmentation: use any block anywhere VIRTUAL ADDRESS TRANSLATION • Imagine 1000-byte pages: • Page number is first digit of address – page 0 is addresses 0000 to 0999 – page 1 is addresses 1000 to 1999 – page 2 is addresses 2000 to 2999 – last three digits give address within the page VIRTUAL ADDRESS TRANSLATION • Now consider 4K pages with binary addresses (4K in binary is 0x1000) • Page number is first 20 bits (5 hex digits) – page 0 is 0x00000000 to 0x00000FFF – page 1 is 0x00001000 to 0x00001FFF – page 2 is 0x00002000 to 0x00002FFF – last 12 bits (3 hex digits) give address within the page THE PAGE TABLE • OS maintains a page table in its own memory area for each process – n-th entry contains physical address of start of page n To Translating a virtual address: – take page number (first 20 bits of address) and get corresponding physical address – add on last 12 bits of address (offset into 4K page) Marking 10 marks for the above answer. (d) What is meant by thrashing and how can it be remedied? • (5 marks) Referencing a page table entry for a page which is not present generates a page fault exception – OS exception handler needs to load page (if reserved) or report error (if unused) • OS needs to have some spare page frames available at all times • If page faults occur too often, thrashing occurs • Each process needs a working set of pages – a kernel thread frees pages to maintain a minimum number of free page frames – OS spends all its time swapping pages – average rate of faults must be lower than time needed to read a page from disk – enough to let the process run for long enough before faulting 6 • Reasons for thrashing – too many processes, not enough physical memory to allow them to all establish working sets – bad choices when freeing pages (pages freed which are almost immediately needed again) • Usual solution: add more memory run fewer processes! Marking 5 marks for the above answer. 4. (a) Explain each of the following commands as used in UNIX giving examples, where appropriate to support your answer: (i) ls ~ –a (ii) cat kibena.txt (iii) ps aux | more (iv) grep ^[A-F] students (b) What is meant when we say that a file is structureless in UNIX? (5 marks) We mean that a file consists of a stream of bytes with no other defined structure. Other filesystems structure files into records or blocks. In UNIX any such structure has to be imposed on the data by the application program. This simplifies the treatment of data and allows data from or to any source to be treated the same by an application program. Marking 5 marks for the above answer. (c) What information does a superblock contain in an ext2 filesystem? (5 marks) Superblock contains: Size of the filesystem, number of free blocks, list of free blocks, Index of next free block, Number of free inodes, Index of next free inode, Filesystem clean flag (d) Name three text editors used in Linux. Briefly describe the three ‘modes’ of the vi editor. vi, pico, nano, emacs, joe Command mode - When you invoke vi, the edit session starts in command mode. In Linux you will know that you are in command mode if you do not see the word “insert” or a colon at the bottom of your editing window. Text-Input (Insert mode) mode - Your keyboard behaves like a normal typewriter allowing you to enter text. You will know that you are in text-input mode when you see the word “insert” at the bottom of your editing window. Pressing ESC returns you to command mode. 7 Last Line Mode / Colon mode - While in the command mode, if you enter a “: “ the cursor will move to the last line on the screen and you can enter global commands such as wq or q!. You will need to be in Last Line mode to carry out some complicated operations on your file which cannot be done with simple key strokes. (e) Write a shell script which will take the input from a sentence containing at least three words. Use the read command and three variables to divide the sentence into three parts. Display the output of the three variables. #!bin/bash echo –n “Enter input: ” read line echo “You entered: $line” echo –n “Enter another line: ” read word1 word 2 word3 echo “The first word is: $word1” echo “The second word is: $word2” echo “The rest of the line is: $word3” exit 1 8