Showing posts with label Interview-Operating System. Show all posts
Showing posts with label Interview-Operating System. Show all posts

Monday, August 24, 2015

[Design] How is Pipe implemented in Unix/Linux - Shuatiblog.com



[Design] How is Pipe implemented in Unix/Linux - Shuatiblog.com
In Unix-like OS, a pipeline is a set of processes chained by their standard streams, so that the output of each process (stdout) feeds directly as input (stdin) to the next one.
Pipes are unidirectional byte streams which connect the standard output from one process into the standard input of another process. Neither process is aware of this redirection and behaves just as it would normally. It is the shell which sets up these temporary pipes between the processes.

  1. Linux has a VFS called pipefs that is mounted in the kernel (not in user space)
    PipeFS is a unique virtual filesystemThis filesystem is mounted inside the kernel rather than in the userspace. While most filesystems are mounted under “/”, PipeFS is mounted on “pipe:”, making PipeFS its own root (yes, a second root filesystem).
    This filesystem is one superblock and cannot exceed that amount system-wide. The entry point of this filesystem/second-root is the system-call “pipe()”. Unlike the other virtual/pseudo filesystems, this one cannot be viewed.
    Many of you may be wondering what purpose this PipeFS filesystem serves. Unix pipes use this filesystem. When a pipe is used (eg. ls | less), the pipe() system-call makes a new pipe object on this filesystem. Without this filesystem, pipes cannot be made.
    Also, threads and forks communicate together via pipes. Without PipeFS, processes could not fork and threads could not communicate.
    Network pipes also rely on this virtual/pseudo filesystem.
  2. pipefs has a single super block and is mounted at it’s own root (pipe:), alongside /
  3. pipefs cannot be viewed directly unlike most file systems
  4. The entry to pipefs is via the pipe(2) syscall
  5. The pipe(2) syscall used by shells for piping with the | operator (or manually from any other process) creates a new file in pipefs which behaves pretty much like a normal file
  6. The file on the left hand side of the pipe operator has its stdout redirected to the temporary file created in pipefs
  7. The file on the right hand side of the pipe operator has its stdin set to the file on pipefs
  8. pipefs is stored in memory and through some kernel magic
The way a shell might set the stdin of a process to a pipe descriptor could be (pseudocode):
pipe(p) // create a new pipe with two handles p[0] and p[1]
fork() // spawn a child process
    close(p[0]) // close the write end of the pipe in the child
    dup2(p[1], 0) // duplicate the pipe descriptor on top of fd 0 (stdin)
    close(p[1]) // close the other pipe descriptor
    exec() // run a new process with the new descriptors in place
Read full article from [Design] How is Pipe implemented in Unix/Linux - Shuatiblog.com

Friday, July 3, 2015

Avoiding Deadlock: Bankers Algorithm



http://geeksquiz.com/deadlock-prevention/
Deadlock Avoidance
Deadlock avoidance can be done with Banker’s Algorithm.
http://rosettacode.org/wiki/Banker's_algorithm
The Banker's algorithm is a resource allocation and deadlock avoidance algorithm developed by Edsger Dijkstra that tests for safety by simulating the allocation of predetermined maximum possible amounts of all resources, and then makes a "s-state" check to test for possible deadlock conditions for all other pending activities, before deciding whether allocation should be allowed to continue, if their is no safe state it don’t allow the request made by the process.
One reason this algorithm is not widely used in the real world is because to use it the operating system must know the maximum amount of resources that every process is going to need at all times. 

Therefore, for example, a just-executed program must declare up-front that it will be needing no more than, say, 400K of memory. The operating system would then store the limit of 400K and use it in the deadlock avoidance calculations.
Avoiding Deadlock: Bankers Algorithm
The system is said to be in a safe state if there exists a sequence of other valid system states that leads to the successful completion of all processes.
  • Processes request only 1 resource at a time.
  • Request is granted only it results in a safe state.
  • If request results in an unsafe state, the request is denied and the process continues to hold resources it has until such time as it's request can be met.
  • All requests will be granted in a finite amount of time.
  • Algorithm can be extended for multiple resource types.
  • Advantage: Avoids deadlock and it is less restrictive than deadlock prevention.
  • Disadvantage: Only works with fixed number of resources and processes.
  • Guarantees finite time - not reasonable response time
  • Needs advanced knowledge of maximum needs
  • Not suitable for multi-access systems
  • Unnecessary delays in avoiding unsafe states which may not lead to deadlock
Inputs to Banker’s Algorithm
1. Max need of resources by each process.
2. Currently allocated resources by each process.
3. Max free available resources in the system.
Request will only be granted under below condition.
1. If request made by process is less than equal to max need to that process.
2. If request made by process is less than equal to freely availbale resource in the system.

Coding
http://akbar.marlboro.edu/~mahoney/support/alg/alg/node145.html
http://rosettacode.org/wiki/Banker's_algorithm
Quiz/practices
A system contains three programs and each requires three tape units for its operation. The minimum number of tape units which the system must have such that deadlocks never arise is _________.
(A) 6
(B) 7
(C) 8
(D) 9
Answer: (B) 
if you use 5 then it may be possible that p1 have 2 p2 have 2 and p3 have 1 resource so all of the three process will wail infinitely ..if 6 resources then it may be possible that all three process have 2 resources each so they will wait infinitely,if 7 resources use then atleast one must have 3 resources so never deadlock can occur.


    http://geeksquiz.com/gate-gate-cs-2010-question-46/
    A system has n resources R0,…,Rn-1,and k processes P0,….Pk-1.The implementation of the resource request logic of each process Pis as follows: 
     if (i % 2 == 0) {
          if (i < n) request Ri
          if (i+2 < n) request Ri+2
    }
    else {
          if (i < n) request Rn-i
          if (i+2 < n) request Rn-i-2
    }
    In which one of the following situations is a deadlock possible?
    (A) n=40, k=26
    (B) n=21, k=12
    (C) n=20, k=10
    (D) n=41, k=19
    Answer: (B) 
    Option B is answer
    
    No. of resources, n = 21
    No. of processes, k = 12
    
    Processes {P0, P1....P11}  make the following Resource requests:
    {R0, R20, R2, R18, R4, R16, R6, R14, R8, R12, R10, R10}
    
    For example P0 will request R0 (0%2 is = 0 and 0< n=21). 
    
    Similarly, P10 will request R10.
    
    P11 will request R10 as n - i = 21 - 11 = 10.
    
    As different processes are requesting the same resource, deadlock
    may occur. 
    Read full article from Avoiding Deadlock: Bankers Algorithm

    Interview - Deadlock Misc



    https://mp.weixin.qq.com/s/BVGtDDCa7yjtfJJPNKOC_g
    当使用synchronized关键词提供的内置锁时,只要线程没有获得锁,那么就会永远等待下去,然而Lock接口提供了boolean tryLock(long time, TimeUnit unit) throws InterruptedException方法,该方法可以按照固定时长等待锁,因此线程可以在获取锁超时以后,主动释放之前已经获得的所有的锁。通过这种方式,也可以很有效地避免死锁。
    我们再来回顾一下死锁的定义,“死锁是指两个或两个以上的进程在执行过程中,由于竞争资源或者由于彼此通信而造成的一种阻塞的现象,若无外力作用,它们都将无法推进下去。”
    死锁条件里面的竞争资源,可以是线程池里的线程、网络连接池的连接,数据库中数据引擎提供的锁,等等一切可以被称作竞争资源的东西。

    1、线程池死锁

    用个例子来看看这个死锁的特征:
    final ExecutorService executorService = 
            Executors.newSingleThreadExecutor();
    Future<Long> f1 = executorService.submit(new Callable<Long>() {
    
        public Long call() throws Exception {
            System.out.println("start f1");
            Thread.sleep(1000);//延时
            Future<Long> f2 = 
               executorService.submit(new Callable<Long>() {
    
                public Long call() throws Exception {
                    System.out.println("start f2");
                    return -1L;
                }
            });
            System.out.println("result" + f2.get());
            System.out.println("end f1");
            return -1L;
        }
    });
    在这个例子中,线程池的任务1依赖任务2的执行结果,但是线程池是单线程的,也就是说任务1不执行完,任务2永远得不到执行,那么因此造成了死锁
    解决办法:扩大线程池线程数 or 任务结果之间不再互相依赖。

    2、网络连接池死锁

    同样的,在网络连接池也会发生死锁,假设此时有两个线程A和B,两个数据库连接池N1和N2,连接池大小都只有1,如果线程A按照先N1后N2的顺序获得网络连接,而线程B按照先N2后N1的顺序获得网络连接,并且两个线程在完成执行之前都不释放自己已经持有的链接,因此也造成了死锁。
    
    
    5. How to prevent deadlocks?
    Lock Ordering

    Deadlock occurs when multiple threads need the same locks but obtain them in different order.

    If you make sure that all locks are always taken in the same order by any thread, deadlocks cannot occur. 

    If a thread, like Thread 3, needs several locks, it must take them in the decided order. It cannot take a lock later in the sequence until it has obtained the earlier locks.


    Lock ordering is a simple yet effective deadlock prevention mechanism. However, it can only be used if you know about all locks needed ahead of taking any of the locks. This is not always the case.


    Lock Timeout
    Another deadlock prevention mechanism is to put a timeout on lock attempts meaning a thread trying to obtain a lock will only try for so long before giving up. If a thread does not succeed in taking all necessary locks within the given timeout, it will backup, free all locks taken, wait for a random amount of time and then retry.

    Operating System | Process Management | Deadlock Introduction
    Deadlock
     is a situation where a set of processes are blocked because each process is holding a resource and waiting for another resource acquired by some other process.
     in operating systems when there are two or more processes hold some resources and wait for resources held by other(s). For example, in the below diagram, Process 1 is holding Resource 1 and waiting for resource 2 which is acquired by process 2, and process 2 is waiting for resource 1.
    Deadlock can arise if following four conditions hold simultaneously (Necessary Conditions) 
    Coffman conditions
    Mutual Exclusion: One or more than one resource are non-sharable (Only one process can use at a time)
    Hold and Wait: A process is holding at least one resource and waiting for resources.
    No Preemption: A resource cannot be taken from a process unless the process releases the resource.
    Circular Wait: A set of processes are waiting for each other in circular form.

    Methods for handling deadlock
    There are three ways to handle deadlock
    1) Deadlock prevention or avoidance: The idea is to not let the system into deadlock state.
    2) Deadlock detection and recovery: Let deadlock occur, then do preemption to handle it once occurred.
    3) Ignore the problem all together: If deadlock is very rare, then let it happen and reboot the system. This is the approach that both Windows and UNIX take.

    http://geeksquiz.com/deadlock-prevention/
    Deadlock Prevention And Avoidance
    Eliminate Mutual Exclusion 
    It is not possible to dis-satisfy the mutual exclusion because some resources, such as the tap drive and printer, are inherently non-shareable.
    Eliminate Hold and wait
    1. Allocate all required resources to the process before start of its execution, this way hold and wait condition is eliminated but it will lead to low device utilization.
    Eliminate Circular Wait
    Each resource will be assigned with a numerical number. A process can request for the resources only in increasing order of numbering.
    For Example, if P1 process is allocated R5 resources, now next time if P1 ask for R4, R3 lesser than R5 such request will not be granted, only request for resources more than R5 will be granted.

    http://buttercola.blogspot.com/2014/11/interview-knowledge-based-questions-1.html
    Every time a thread takes a lock it is noted in a data structure (map, graph etc.) of threads and locks. Additionally, whenever a thread requests a lock this is also noted in this data structure.

    When a thread requests a lock but the request is denied, the thread can traverse the lock graph to check for deadlocks.

    One possible action is to release all locks, backup, wait a random amount of time and then retry. This is similar to the simpler lock timeout mechanism except threads only backup when a deadlock has actually occurred. Not just because their lock requests timed out. However, if a lot of threads are competing for the same locks they may repeatedly end up in a deadlock even if they back up and wait.

    A better option is to determine or assign a priority of the threads so that only one (or a few) thread backs up. The rest of the threads continue taking the locks they need as if no deadlock had occurred. If the priority assigned to the threads is fixed, the same threads will always be given higher priority. To avoid this you may assign the priority randomly whenever a deadlock is detected.
    http://www.shuatiblog.com/blog/2014/09/01/Multithreading-deadlock-prevention/
    Preventing one of the 4 conditions will prevent deadlock:
    Removing the mutual exclusion condition, but not very possible.

    The hold and wait conditions may be removed by requiring processes to request all the resources they will need before starting up.

    The no preemption condition may also be difficult or impossible to avoid as a process has to be able to have a resource for a certain amount of time, or the processing outcome may be inconsistent or thrashing may occur.

    The final condition is the circular wait condition.
    Answer
    Assign an order to our locks (require that the locks always acquired in order).
    This prevent 2 thread waiting to get the resource in each other’s hand.
    https://hellosmallworld123.wordpress.com/2014/05/21/threads-and-locks/
    inorder to prevent deadlock, we can usually focus on preemption and circular wait. For preemption, we can set a timeout and when hit, release the lock. For circular wait, we can try to enforce an order of acquiring the locks so that each thread must acquire the locks in a specific order, in that way there will be no thread holding one lock and waiting another.
    https://en.wikipedia.org/wiki/Deadlock
    Avoiding database deadlocks
    An effective way to avoid database deadlocks is to follow this approach from the Oracle Locking Survival Guide:
    Application developers can eliminate all risk of enqueue deadlocks by ensuring that transactions requiring multiple resources always lock them in the same order.

    Livelock
    A livelock is similar to a deadlock, except that the states of the processes involved in the livelock constantly change with regard to one another, none progressing.
    Livelock is a special case of resource starvation; the general definition only states that a specific process is not progressing.
    A real-world example of livelock occurs when two people meet in a narrow corridor, and each tries to be polite by moving aside to let the other pass, but they end up swaying from side to side without making any progress because they both repeatedly move the same way at the same time.
    Livelock is a risk with some algorithms that detect and recover from deadlock. If more than one process takes action, the deadlock detection algorithm can be repeatedly triggered. This can be avoided by ensuring that only one process (chosen arbitrarily or by priority) takes action.

    Distributed deadlock
    Distributed deadlocks can occur in distributed systems when distributed transactions or concurrency control is being used. Distributed deadlocks can be detected either by constructing a global wait-for graph from local wait-for graphs at a deadlock detector or by adistributed algorithm like edge chasing.
    Phantom deadlocks are deadlocks that are falsely detected in a distributed system due to system internal delays but don't actually exist.
    http://geeksquiz.com/deadlock-detection-recovery/
    Deadlock Detection
    1. If resources have single instance:
    In this case for Deadlock detection we can run an algorithm to check for cycle in the Resource Allocation Graph. Presence of cycle in the graph is the sufficient condition for deadlock.
    deadlock
    In the above diagram, resource 1 and resource 2 have single instances. There is a cycle R1–>P1–>R2–>P2. So Deadlock is Confirmed.
    2. If there are multiple instances of resources:
    Detection of cycle is necessary but not sufficient condition for deadlock detection, in this case system may or may not be in deadlock varies according to different situations.
    Deadlock Recovery
    Traditional operating system such as Windows doesn’t deal with deadlock recovery as it is time and space consuming process. Real time operating systems use Deadlock recovery.
    Recovery method
    1. Killing the process.
         killing all the process involved in deadlock.
         
         Killing process one by one. After killing each 
         process check for deadlock again keep repeating 
         process till system recover from deadlock.
    2. Resource Preemption
    Resources are preempted from the processes involved in deadlock, preempted resources are allocated to other processes, so that their is a possibility of recovering the system from deadlock. In this case system go into starvation.

    Quiz: http://geeksquiz.com/deadlock/
    http://www.geeksforgeeks.org/operating-systems-set-16/
    http://www2.latech.edu/~box/os/ch07.pdf
    http://www.cs.uic.edu/~jbell/CourseNotes/OperatingSystems/7_Deadlocks.html
    http://users.cs.cf.ac.uk/O.F.Rana/os/lectureos12/lectureos12.html

    Interview - Operating System - geeksquiz



    http://www.geeksforgeeks.org/operating-systems-set-16/
    1) Normally user programs are prevented from handling I/O directly by I/O instructions in them. For CPUs having explicit I/O instructions, such I/O protection is ensured by having the I/O instructions privileged. In a CPU with memory mapped I/O, there is no explicit I/O instruction. Which one of the following is true for a CPU with memory mapped I/O?
    (a) I/O protection is ensured by operating system routine(s)
    Memory mapped I/O means, accessing I/O via general memory access as opposed to specialized IO instructions. An example,
      unsigned int volatile const *pMappedAddress const = (unsigned int *)0x100;
    
    So, the programmer can directly access any memory location directly. To prevent such an access, the OS (kernel) will divide the address space into kernel space and user space. An user application can easily access user application. To access kernel space, we need system calls (traps).
    2) What is the swap space in the disk used for?
    (b) Saving process data
    3) Increasing the RAM of a computer typically improves performance because:
    (c) Fewer page faults occur
    4) Suppose n processes, P1, …. Pn share m identical resource units, which can be reserved and released one at a time. The maximum resource requirement of process Pi is Si, where Si > 0. Which one of the following is a sufficient condition for ensuring that deadlock does not occur?

    Answer (c)
    In the extreme condition, all processes acquire Si-1 resources and need 1 more resource. So following condition must be true to make sure that deadlock never occurs.
    sum1 < m
    The above expression can be written as following.
    sum2 < (m + n)
    5) Consider the following code fragment:
      if (fork() == 0)
      { a = a + 5; printf(“%d,%d\n”, a, &a); }
      else { a = a –5; printf(“%d, %d\n”, a, &a); } 
    Let u, v be the values printed by the parent process, and x, y be the values printed by the child process. Which one of the following is TRUE?
    (c) u + 10 = x and v = y
    fork() returns 0 in child process and process ID of child process in parent process.
    In Child (x), a = a + 5
    In Parent (u), a = a – 5;
    Therefore x = u + 10.
    The physical addresses of ‘a’ in parent and child must be different. But our program accesses virtual addresses (assuming we are running on an OS that uses virtual memory). The child process gets an exact copy of parent process and virtual address of ‘a’ doesn’t change in child process. Therefore, we get same addresses in both parent and child. 
    Operating System | User Level thread Vs Kernel Level thread
    USER LEVEL THREAD KERNEL LEVEL THREAD
    User thread are implemented by users. kernel threads are implemented by OS.
    OS doesn’t recognized user level threads. Kernel threads are recognized by OS.
    Implementation of User threads is easy. Implementation of Kernel thread is complicated.
    Context switch time is less. Context switch time is more.
    Context switch requires no hardware support. Hardware support is needed.
    If one user level thread perform blocking operation then entire process will be blocked. If one kernel thread perform blocking operation then another thread can continue execution.
    Example : Java thread, POSIX threads.

    Monitor is one of the ways to achieve Process synchronization. Monitor is supported by programming languages to achieve mutual exclusion between processes. For example Java Synchronized methods. Java provides wait() and notify() constructs.
    1. It is the collection of condition variables and procedures combined together in a special kind of module or a package.
    2. The processes running outside the monitor can’t access the internal variable of monitor but can call procedures of the monitor.
    3. Only one process at a time can execute code inside monitors.
    Syntax of Monitor
    monitors
    Condition Variables
    Two different operations are performed on the condition variables of the monitor.
    Wait.
    signal.
    let say we have 2 condition variables
    condition x, y; //Declaring variable
    Wait operation
    x.wait() : Process performing wait operation on any condition variable are suspended. The suspended processes are placed in block queue of that condition variable.
    Note: Each condition variable has its unique block queue.
    Signal operation
    x.signal(): When a process performs signal operation on condition variable, one of the blocked processes is given chance.
    If (x block queue empty)
      // Ignore signal
    else
    What protocol is used for communicating with a DNS?
    Domain Name System (DNS) is a hierarchical distributed naming system for computers, services, or any resource connected to the Internet or a private network. It associates various information with domain names assigned to each of the participating entities. Most prominently, it translates easily memorized domain names to the numerical IP addresses needed for the purpose of locating computer services and devices worldwide. The Domain Name System is an essential component of the functionality of the Internet.

    DNS primarily uses User Datagram Protocol (UDP) on port number 53 to serve requests.

    DNS queries consist of a single UDP request from the client followed by a single UDP reply from the server.

    Wednesday, January 21, 2015

    Java Examples | Files | Memory Mapped File



    Why use Memory Mapped File or MapppedByteBuffer
    Memory mapped file allows you to directly read from memory and write into memory by using direct and non direct Byte buffers. 

    Key advantage of  Memory Mapped File is that operating system takes care of reading and  writing and even if your program crashed just after writing into memory. OS will take care of writing content to File.
    One more notable advantage is shared memory, memory mapped files can be accessed by more than one process and can be act as shared memory with extremely low latency.

    Memory mapped files are special files in Java which allows Java program to access contents  directly from memory, this is achieved by mapping whole file or portion of file into memory and operating system takes care of loading page requested and writing into file while application only deals with memory which results in very fast IO operations.
    Memory used to load Memory mapped file is outside of Java heap Space. Java programming language supports memory mapped file with java.nio package and has MappedByteBuffer to read and write from memory.


    Advantage and Disadvantage of Memory Mapped file
    Possibly main advantage of Memory Mapped IO is performance. Memory Mapped Files are way faster than standard file access via normal IO.

    Another big advantage of memory mapped IO is that it allows you to load potentially larger file which is not otherwise accessible. Experiments shows that memory mapped IO performs better with large files.

    Though it has disadvantage in terms of increasing number of page faults. Since operating system only loads a portion of file into memory if a page requested is not present in memory than it would result in page fault.

    Most of major operating system like Windows platform, UNIX, Solaris and other UNIX like operating system supports memory mapped IO and with 64 bit architecture you can map almost any file into memory and access it directly using Java programming language.

    Another advantages is that the file can be shared, giving you shared memory between processes and can be more than 10x lower latency than using a Socket over loopback.


    3) By using memory mapped IO you can load portion of large files in memory.

    4) Memory mapped file can result in page fault if requested page is not in memory.

    5) Ability to map a region of file in memory depends on addressable size of memory. In a 32 bit machine you can not access beyond 4GB or 2^32.

    6) Memory mapped IO is much faster than Stream IO in Java.

    7) Memory used to load File is outside of Java heap and reside on shared memory which allow two different process to access File. By the way this depends upon, whether you are using direct or non-direct byte buffer.

    8) Reading and writing on memory mapped file is done by operating system, so even if your Java Program crash after putting content into memory it will make to disk, until OS is fine.
    9) Prefer Direct Byte buffer over Non Direct Buffer for higher performance.

    10) Don't call MappedByteBuffer.force() method to often, this method is meant to force operating system to write content of memory into disk, So if you call force() method each time you write into memory mapped file, you will not see true benefit of using mapped byte buffer, instead it will be similar to disk IO.

    11) In case of power failure or host failure, there is slim chance that content of memory mapped file is not written into disk, which means you could lose critical data.

    12) MappedByteBuffer and file mapping remains valid until buffer is garbage collected. sun.misc.Cleaner is probably the only option available to clear memory mapped file.

    Java Examples | Files | Memory Mapped File
    Loading large files into jvm may take up a lot of time and may also fail becuase the jvm memory may become full. 

    It is now possible to directly map the huge files without loading them into memory. A buffer is created around the file in the file system without loading the file into jvm. The file can be directly read or written using the mapped buffer. This functionality now enables java to now handle large files.

    A FileChannel represents a connection to a file which can be used for reading and writing.
    http://www.javacodegeeks.com/2013/05/power-of-java-memorymapped-file.html
    A memory-mapped file is a segment of virtual memory which has been assigned a direct byte-for-byte correlation with some portion of a file or file-like resource. This resource is typically a file that is physically present on-disk, but can also be a device, shared memory object, or other resource that the operating system can reference through a file descriptor. Once present, this correlation between the file and the memory space permits applications to treat the mapped portion as if it were primary memory.

    MappedByteBuffer mem =fc.map(FileChannel.MapMode.READ_WRITE, 0, bufferSize);
    http://www.linuxtopia.org/online_books/programming_books/thinking_in_java/TIJ314_029.htm
    Memory-mapped files allow you to create and modify files that are too big to bring into memory. With a memory-mapped file, you can pretend that the entire file is in memory and that you can access it by simply treating it as a very large array.
    MappedByteBuffer, which is a particular kind of direct buffer. Note that you must specify the starting point and the length of the region that you want to map in the file; this means that you have the option to map smaller regions of a large file.

    Although the performance of “old” stream I/O has been improved by implementing it with nio, mapped file access tends to be dramatically faster.

    Read full article from Java Examples | Files | Memory Mapped File

    Thursday, January 8, 2015

    操作系统



    操作系统
    请问死锁的条件是什么?以及如何处理死锁问题?
    解答:互斥条件(Mutual exclusion):
    • 1、资源不能被共享,只能由一个进程使用。
    • 2、请求与保持条件(Hold and wait):已经得到资源的进程可以再次申请新的资源。
    • 3、非剥夺条件(No pre-emption):已经分配的资源不能从相应的进程中被强制地剥夺。
    • 4、循环等待条件(Circular wait):系统中若干进程组成环路,该环路中每个进程都在等待相邻进程正占用的资源。
    如何处理死锁问题:
    • 1、忽略该问题。例如鸵鸟算法,该算法可以应用在极少发生死锁的的情况下。为什么叫鸵鸟算法呢,因为传说中鸵鸟看到危险就把头埋在地底下,可能鸵鸟觉得看不到危险也就没危险了吧。跟掩耳盗铃有点像。
    • 2、检测死锁并且恢复。
    • 3、仔细地对资源进行动态分配,以避免死锁。
    • 4、通过破除死锁四个必要条件之一,来防止死锁产生。
    2
    请阐述动态链接库与静态链接库的区别。
    解答:静态链接库是.lib格式的文件,一般在工程的设置界面加入工程中,程序编译时会把lib文件的代码加入你的程序中因此会增加代码大小,你的程序一运行lib代码强制被装入你程序的运行空间,不能手动移除lib代码。
    动态链接库是程序运行时动态装入内存的模块,格式*.dll,在程序运行时可以随意加载和移除,节省内存空间。
    在大型的软件项目中一般要实现很多功能,如果把所有单独的功能写成一个个lib文件的话,程序运行的时候要占用很大的内存空间,导致运行缓慢;但是如果将功能写成dll文件,就可以在用到该功能的时候调用功能对应的dll文件,不用这个功能时将dll文件移除内存,这样可以节省内存空间。
    3
    请阐述进程与线程的区别。
    解答:
    • ①从概念上:
      • 进程:一个程序对一个数据集的动态执行过程,是分配资源的基本单位。
      • 线程:一个进程内的基本调度单位。线程的划分尺度小于进程,一个进程包含一个或者更多的线程。
    • ②从执行过程中来看:
      • 进程:拥有独立的内存单元,而多个线程共享内存,从而提高了应用程序的运行效率。
      • 线程:每一个独立的线程,都有一个程序运行的入口、顺序执行序列、和程序的出口。但是线程不能够独立的执行,必须依存在应用程序中,由应用程序提供多个线程执行控制。
    • ③从逻辑角度来看(重要区别):
      • 多线程的意义在于一个应用程序中,有多个执行部分可以同时执行。但是,操作系统并没有将多个线程看做多个独立的应用,来实现进程的调度和管理及资源分配。
    4
    用户进程间通信主要哪几种方式?
    解答:主要有以下6种:
    • 1、管道:管道是单向的、先进先出的、无结构的、固定大小的字节流,它把一个进程的标准输出和另一个进程的标准输入连接在一起。写进程在管道的尾端写入数据,读进程在管道的道端读出数据。数据读出后将从管道中移走,其它读进程都不能再读到这些数据。管道提供了简单的流控制机制。进程试图读空管道时,在有数据写入管道前,进程将一直阻塞。同样地,管道已经满时,进程再试图写管道,在其它进程从管道中移走数据之前,写进程将一直阻塞。
      • 无名管道:管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系(通常是指父子进程关系)的进程间使用。
      • 命名管道:命名管道也是半双工的通信方式,在文件系统中作为一个特殊的设备文件而存在,但是它允许无亲缘关系进程间的通信。当共享管道的进程执行完所有的I/O操作以后,命名管道将继续保存在文件系统中以便以后使用。
    • 2、信号量:信号量是一个计数器,可以用来控制多个进程对共享资源的访问。它常作为一种锁机制,防止某进程正在访问共享资源时,其它进程也访问该资源。因此,主要作为进程间以及同一进程内不同线程之间的同步手段。
    • 3、消息队列:消息队列是由消息的链表,存放在内核中并由消息队列标识符标识。消息队列克服了信号传递信息少、管道只能承载无格式字节流以及缓冲区大小受限等缺点。
    • 4、信号:信号是一种比较复杂的通信方式,用于通知接收进程某个事件已经发生。
    • 5、共享内存:共享内存就是映射一段能被其它进程所访问的内存,这段共享内存由一个进程创建,但多个进程都可以访问。共享内存是最快的IPC方式,它是针对其它进程间通信方式运行效率低而专门设计的。它往往与其它通信机制(如信号量)配合使用,来实现进程间的同步和通信。
    • 6、套接字:套接字也是一种进程间通信机制,与其它通信机制不同的是,它可用于不同机器间的进程通信

    Read full article from 操作系统

    Saturday, July 26, 2014

    Operating Systems - Interview Questions and Answers




    What is a binary semaphore? What is its use?

    A binary semaphore is one, which takes only 0 and 1 as values. They are used to implement mutual exclusion and synchronize concurrent processes.


    What is thrashing?

    It is a phenomenon in virtual memory schemes when the processor spends most of its time swapping pages, rather than executing instructions. This is due to an inordinate number of page faults.

    List the Coffman's conditions that lead to a deadlock.

    Mutual Exclusion: Only one process may use a critical resource at a time.
    Hold & Wait: A process may be allocated some resources while waiting for others.
    No Pre-emption: No resource can be forcible removed from a process holding it.
    Circular Wait: A closed chain of processes exist such that each process holds at least one resource needed by another process in the chain.


    What is the resident set and working set of a process?
    Resident set is that portion of the process image that is actually in real-memory at a particular instant. 
    Working set is that subset of resident set that is actually needed for execution.
    When is a system in safe state?
    The set of dispatchable processes is in a safe state if there exists at least one temporal order in which all processes can be run to completion without resulting in a deadlock.
    What is cycle stealing?
    We encounter cycle stealing in the context of Direct Memory Access (DMA). Either the DMA controller can use the data bus when the CPU does not need it, or it may force the CPU to temporarily suspend operation. The latter technique is called cycle stealing. Note that cycle stealing can be done only at specific break points in an instruction cycle.
    What is meant by arm-stickiness?
    If one or a few processes have a high access rate to data on one track of a storage disk, then they may monopolize the device by repeated requests to that track. This generally happens with most common device scheduling algorithms (LIFO, SSTF, C-SCAN, etc). High-density multisurface disks are more likely to be affected by this than low density ones.
    What is busy waiting?
    The repeated execution of a loop of code while waiting for an event to occur is called busy-waiting. The CPU is not engaged in any real productive activity during this period, and the process does not progress toward completion.
    When does the condition 'rendezvous' arise?
    In message passing, it is the condition in which, both, the sender and receiver are blocked until the message is delivered.

    What is a trap and trapdoor?

    Trapdoor is a secret undocumented entry point into a program used to grant access without normal methods of access authentication. 
    A trap is a software interrupt, usually the result of an error condition.

    What are local and global page replacements?

    Local replacement means that an incoming page is brought in only to the relevant process address space. Global replacement policy allows any page frame from any process to be replaced. The latter is applicable to variable partitions model only.

    Define latency, transfer and seek time with respect to disk I/O.


    Seek time is the time required to move the disk arm to the required track. Rotational delay or latency is the time it takes for the beginning of the required sector to reach the head. Sum of seek time (if any) and latency is the access time. Time taken to actually transfer a span of data is transfer time.

    In the context of memory management, what are placement and replacement algorithms?


    Placement algorithms determine where in available real-memory to load a program. Common methods are first-fit, next-fit, best-fit. Replacement algorithms are used when memory is full, and one process (or part of a process) needs to be swapped out to accommodate a new program. The replacement algorithm determines which are the partitions to be swapped out.

    How are the wait/signal operations for monitor different from those for semaphores?


    If a process in a monitor signal and no task is waiting on the condition variable, the signal is lost. So this allows easier program design. Whereas in semaphores, every operation affects the value of the semaphore, so the wait and signal operations should be perfectly balanced in the program.

    What are demand-paging and pre-paging?


    With demand paging, a page is brought into memory only when a location on that page is actually referenced during execution. With pre-paging, pages other than the one demanded by a page fault are brought in. The selection of such pages is done based on common access patterns, especially for secondary memory devices.

    In loading programs into memory, what is the difference between load-time dynamic linking and run-time dynamic linking?

    For load-time dynamic linking: Load module to be loaded is read into memory. Any reference to a target external module causes that module to be loaded and the references are updated to a relative address from the start base address of the application module.


    With run-time dynamic loading: Some of the linking is postponed until actual reference during execution. Then the correct module is loaded and linked.

    What are the four layers that Windows NT have in order to achieve independence?
    Hardware abstraction layer
    Kernel
    Subsystems
    System Services.

    What is an idle thread?


    The special thread a dispatcher will execute when no ready thread is found.

    What is SMP?


    To achieve maximum efficiency and reliability a mode of operation known as symmetric multiprocessing is used. In essence, with SMP any process or threads can be assigned to any processor.

    What is process spawning?


    When the OS at the explicit request of another process creates a process, this action is called process spawning.



    What are the typical elements of a process image?
    User data: Modifiable part of user space. May include program data, user stack area, and programs that may be modified.
    User program: The instructions to be executed.
    System Stack: Each process has one or more LIFO stacks associated with it. Used to store parameters and calling addresses for procedure and system calls.
    Process control Block (PCB): Info needed by the OS to control processes.

    Explain the concept of Reentrancy?

    It is a useful, memory-saving technique for multiprogrammed timesharing systems. A Reentrant Procedure is one in which multiple users can share a single copy of a program during the same period. Reentrancy has 2 key aspects: The program code cannot modify itself, and the local data for each user process must be stored separately. Thus, the permanent part is the code, and the temporary part is the pointer back to the calling program and local variables used by that program. Each execution instance is called activation. It executes the code in the permanent part, but has its own copy of local variables/parameters. The temporary part associated with each activation is the activation record. Generally, the activation record is kept on the stack.
    Note: A reentrant procedure can be interrupted and called by an interrupting program, and still execute correctly on returning to the procedure.

    What are the key object oriented concepts used by Windows NT?

    Encapsulation, Object class and instance.

    Is Windows NT a full blown object oriented operating system? Give reasons.


    No Windows NT is not so, because its not implemented in object oriented language and the data structures reside within one executive component and are not represented as objects and it does not support object oriented capabilities.

    What are rings in Windows NT?


    Windows NT uses protection mechanism called rings provides by the process to implement separation between the user mode and kernel mode.

    What is mutant?

    In Windows NT a mutant provides kernel mode or user mode mutual exclusion with the notion of ownership.

    What are the sub-components of I/O manager in Windows NT?

    Network redirector/ Server
    Cache manager.
    File systems
    Network driver
    Device driver

    What are DDks? Name an operating system that includes this feature.

    DDks are device driver kits, which are equivalent to SDKs for writing device drivers. Windows NT includes DDks.

    What level of security does Windows NT meets?


    C2 level security.

    What are the reasons for process suspension?

    swapping
    interactive user request
    timing
    parent process request


    Read full article from Operating Systems - Interview Questions and Answers

    Labels

    Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

    Popular Posts