Homework 1 1.4 What are the main differences between operating systems for mainframe computers and personal computers? Answer: The design goals of operating systems for those machines are quite different. PCs are inexpensive, so wasted resources like CPU cycles are inconsequential. Resources are wasted to improve usability and increase software user interface functionality. Main-frames are the opposite, so resource use is maximized, at the expensive of ease of use. 1.5 In a multiprogramming and time-sharing environment, several users share the system si-multaneously. This situation can result in various security problems. a. What are two such problems? b. Can we ensure the same degree of security in a time-shared machine as we have in a dedicated machine? Explain your answer. Answer: a. Stealing or copying one's programs or data; using system resources (CPU,memory, disk space, peripherals) without proper accounting. b. Probably not, since any protection scheme devised by humans can inevitably be bro-ken by a human, and the more complex the scheme, the more difficult it is to feel confident of its correct implementation. 2.3 What are the differences between a trap and an interrupt? What is the use of each function? Answer: An interrupt is a hardware-generated change-of-flow within the system. An interrupt handler is summoned to deal with the cause of the interrupt; control is then returned to the interrupted context and instruction. A trap is a software-generated interrupt. An interrupt can be used to signal the completion of an I/O to obviate the need for device polling. A trap can be used to call operating system routines or to catch arithmetic errors. 2.4 For what types of operations is DMA useful? Explain your answer. Answer: DMA is useful for transferring large quantities of data between memory and devices. It eliminates the need for the CPU to be involved in the transfer, allowing the transfer to complete more quickly and the CPU to perform other tasks concurrently. 2.5 Which of the following instructions should be privileged? a. Set value of timer. b. Read the clock. c. Clear memory. d. Turn off interrupts. e. Switch from user to monitor mode. Answer: The following instructions should be privileged: a. Set value of timer. b. Clear memory. c. Turn off interrupts. d. Switch from user to monitor mode. 3.7 What is the purpose of system calls? Answer: System calls allow user-level processes to request services of the operating system. 3.15 Why is the separation of mechanism and policy a desirable property? Answer: Mechanism and policy must be separate to ensure that systems are easy to modify. No two system installations are the same, so each installation may want to tune the operating system to suit its needs.With mechanism and policy separate, the policy may be changed at will while the mechanism stays unchanged. This arrangement provides a more flexible system. Cigarette Smoker ---------------- The shared data structures are Semaphore t_m = 0, t_p = 0, m_p = 0; // one per smoker Semaphore agent = 0; agent(){ int r while(1) { r = random(2); // random number 0-2 switch(r){ case 0: produce( matches, tobacco); signal(t_m); // signals the one that has paper break; case 1: produce( matches, paper); signal(m_p); // signals the one that has tobacco break; case 2: produce( paper, tobacco); signal(t_p); // signals the one that has matches break; } wait(agent); // waits until the ingredients are consumed } } smoker1(){ // has matches and paper; need tobacco wait(m_p); // waits until the ingredients are produced smoke(); signal(agent); // wakes up the agent } The other two smoker processes are similar. Another possible solution is to use only two semaphores ( agent and table ). The agent puts the ingredients on table, signal(table ), and then blocks. The smokers wait(table). One of them enters CS: if it needs both ingredients then it takes them and wakes the agent up. If not, just leaves the CS, signal( table ). You should be able to figure out the entire solution. Sleeping Barbers ( from Tannenbaum's book ) -------------------------------------------- #define CHAIRS 5 semaphore customers = 0; /* no. of customers waiting for service*/ semaphore barbers = 0; /* no. of barbers waiting for customers*/ semaphore mutex = 1; /* mutex access to shared */ int waiting = 0; /* no. of waiting customers */ void barber(){ while ( TRUE ) { wait(customers); /* go to sleep if no of customers==0*/ wait(mutex); /* access cs */ waiting--; /* decrement no. waiting */ signal(barbers); /* wakeup a barber */ signal(mutex); /* exit the cs */ cut_hair(); /* outside the cs */ } } void customer(){ wait(mutex); /* access the cs */ if ( waiting < CHAIRS ) { waiting++; signal(customers); /* wake up a barber if necessary */ signal(mutex); /* exit the cs */ wait(barbers); /* block if no of free barbers is 0 */ get_hair_cut(); /* receive hair cut */ } else { signal(mutex) } } Semaphores using locks and conditions ------------------------------------- Shared variables int count; Lock lock; Condition cond; initialize(int val){ count = val } P(){ lock.acquire(); count--; while( count < 0 ){ cond.wait(lock); /**** This operation is atomically: the current process is blocked and the lock is released. when signal occurs then the lock is acquired first and then the process is waken up ****/ } lock.release(); } V(){ lock.acquire(); count++; cond.signal(); // if somebody was blocked lock.release(); }