Showing posts with label Operating System. Show all posts

Why thread is necessary? In which circumstances user-level thread is better than kernel level thread?



Thread is necessary due to the following reasons:
  •   Will by default share memory
  •   Will share file descriptors
  •   Will share file system context
  •   Will share signal handling
    There are two distinct models of thread controls, and they are user-level threads and kernel-level threads. The thread function library to implement user-level threads usually runs on top of the system in user mode. Thus, these threads within a process are invisible to the operating system. User-level threads have extremely low overhead, and can achieve high performance in computation. However, using the blocking system calls like read(), the entire process would block. Also, the scheduling control by the thread runtime system may cause some threads to gain exclusive access to the CPU and prevent other threads from obtaining the CPU. Finally, access to multiple processors is not guaranteed since the operating system is not aware of existence of these types of threads. On the other hand, kernel-level threads will guarantee multiple processor access but the computing performance is lower than user-level threads due to load on the system. The synchronization and sharing resources among threads are still less expensive than multiple- process model, but more expensive than user-level threads. Thus, user-level thread is better than kernel level thread.
Learn more »

What do you mean by file systems? What are the major difference between file system interfaces and file system implementation? Explain




We have three essential requirements for long-term information storage:
  •   It must be possible to store a very large amount of information.
  •   The information must survive the termination of the process using it.
  •   Multiple processes must be able to access the information concurrently.
    The usual solution to all these problems is to store information on disks and other external media in units called files. Files are managed by the operating system. How they are structured, named, accessed, used, protected, and implemented are major topics in operating system design. As a whole, that part of the operating system dealing with files is known as the file system.
    File system implementation deals with:
  •   How files and directories are stored?
  •   How disk space is managed?
  •   How to make everything work efficiently and reliably?
    The main objective of file system implementation is:
  •   To describe the details of implementing local file systems and directory structures
  •   To describe the implementation of remote file systems
  •   To discuss block allocation and free-block algorithms and trade-offs
    Different allocation methods are used in file system implementation. Some of the allocation methods are:
  •   Contiguous allocation
  •   Linked allocation

Indexed allocation
File system interface provides applications with various system calls and commands such as open, write, read, seek, etc. Since main memory is usually too small, the computer system must provide secondary storage to back up main memory. The file system provides the mechanism for storage of and (multiple) access to both data and programs residing on the disks. Under this we describe following topics:
  •   Access method
  •   Directory structure
  •   File system mounting
  •   File sharing
  •   Protection
Learn more »

Write a C++ program to implement LRU algorithm.


C++Program to implement LRU algorithm
//  main.cpp
//  quicksort
//

#include<iostream>
#define max 100

class lrupagereplacement

{
   
   private:
   
    int frame[10], count[10], cstr[max];
   
    int tot,nof,fault;
   
   public:
   
 lrupagereplacement (){fault=0;}
   
    void getdata();
   
    void push();
   
    void dis();
   
};

void lrupagereplacement::getdata()

{
   
    int pno,i=0;
   
   
    std::cout<<"\n Enter No. of Page Frames:";
   
    std::cin>>nof;
   
    std::cout<<"\n Enter the Context String:(Press -1 to end)\n";
   
    do
       
    {
       
        std::cin>>pno;
       
        cstr[i++]=pno;
       
    }while(pno!=-1);
   
    tot=i-1;
   
}

void lrupagereplacement::push()

{
   
    int x,i,flag=0,nc=0,maximum,maxpos=-1,mark=0;
   
    for(i=0;i<nof;i++)
       
    {
       
        frame[i]=-1;
       
        count[i]=mark--;
       
    }
   
    for(i=0;i<tot;i++)
       
    {
       
        flag=0;
       
        x=cstr[i];
       
        nc++;
       
        for(int j=0; j<nof; j++)
           
        {
           
            for(int k=0; k<nof;k++)
               
                count[k]++;
           
            if(frame[j]==x)
               
            {
               
                flag=1;
               
                count[j]=1;
               
                break;
               
            }
           
        }
       
        if(flag==0)
           
        {
           
            maximum = 0;
           
            for(int k=0;k<nof;k++)
               
            {
               
                if(count[k]>maximum && nc>nof)
                   
                {
                   
                    maximum=count[k];
                   
                    maxpos = k;
                   
                }
                
            }
           
            if(nc>nof)
               
            {
               
                frame[maxpos]=x;
               
                count[maxpos]=1;
               
            }
           
            else
               
                frame[nc-1]=x;
           
            fault++;
           
            std::cout<<"\nThe Page Fault No:"<<fault;
           
            dis();
           
        }
       
    }
   
    std::cout<<"\nTotal Page Faults :"<<fault;
   
}

void lrupagereplacement::dis()

{
   
    int i=0;
   
    std::cout<<"\n-\n";
   
    while(i<nof)
       
    {
       
        std::cout<<"|   "<<frame[i]<<"   ";
       
        i++;
       
    }
   
    std::cout<<"|";
}

int main()

{
   
 lrupagereplacement lru;
   
 lrupagereplacement.getdata();
   
 lrupagereplacement.push();
   
 return 0;
}

SAMPLE OUTPUT
Enter No. of Page Frames:4

Enter the Context String:(Press -1 to end)
1 2 3 4 2 5 9 -1

The Page Fault No1
-
|   1   |   -1   |   -1   |   -1   |
The Page Fault No2
-
|   1   |   2   |   -1   |   -1   |
The Page Fault No3
-
|   1   |   2   |   3   |   -1   |
The Page Fault No4
-
|   1   |   2   |   3   |   4   |
The Page Fault No5
-
|   5   |   2   |   3   |   4   |
The Page Fault No6
-
|   5   |   2   |   9   |   4   |
Total Page Faults :6Program ended with exit code: 0
Learn more »