Articles by "Operating System"
Showing posts with label Operating System. Show all posts


The Purpose of The System Call


The interface between a process and an operating system is provided by system calls. In general, system calls are available as assembly language instructions. They are also included in the manuals used by the assembly level programmers. System calls are usually made when a process in user mode requires access to a resource. Then it requests the kernel to provide the resource via a system calls.
In general, system calls are required in the following situations:
  • If a file system requires the creation or deletion of files. Reading and writing from files also require a system call.
  • Creation and management of new processes.
  • Network connections also require system calls. This includes sending and receiving packets.
  • Access to a hardware device such as a printer, scanner etc. requires a system call.




Types of System Calls

There are mainly five types of system calls. These are explained in detail as follows:

Process Control

These system calls deal with processes such as process creation, process termination etc.

File Management

These system calls are responsible for file manipulation such as creating a file, reading a file, writing into a file etc.

Device Management

These system calls are responsible for device manipulation such as reading from device buffers, writing into device buffers etc.

Information Maintenance

These system calls handle information and its transfer between the operating system and the user program.

Communication

These system calls are useful for interprocess communication. They also deal with creating and deleting a communication connection.
Some of the examples of all the above types of system calls in Windows and Unix are given as follows:

Types of System Calls
Windows
Linux
Process Control
CreateProcess()
ExitProcess()
WaitForSingleObject()
fork()
exit()
wait()
File Management
CreateFile()
ReadFile()
WriteFile()
CloseHandle()
open()
read()
write()
close()
Device Management
SetConsoleMode()
ReadConsole()
WriteConsole()
ioctl()
read()
write()
Information Maintenance
GetCurrentProcessID()
SetTimer()
Sleep()
getpid()
alarm()
sleep()
Communication
CreatePipe()
CreateFileMapping()
MapViewOfFile()
pipe()
shmget()
mmap()


Purpose of System Calls


There are many different system calls as shown above. The purpose of some of those system calls is as follows:

open()

The open() system call is used to provide access to a file in a file system. This system call allocates resources to the file and provides a handle that the process uses to refer to the file. A file can be opened by multiple processes at the same time or be restricted to one process. It all depends on the file organization and file system.

read()

The read() system call is used to access data from a file that is stored in the file system. The file to read can be identified by its file descriptor and it should be opened using open() before it can be read. In general, the read() system calls takes three arguments i.e. the file descriptor, the buffer which stores read data and the number of bytes to be read from the file.

write()

The write() system call writes the data from a user buffer into a device such as a file. This system call is one of the ways to output data from a program. In general, the write() system calls takes three arguments i.e. the file descriptor,the pointer to the buffer where data is stored and the number of bytes to write from the buffer.

close()

The close() system call is used to terminate access to a file system. Using this system call means that the file is no longer required by the program and so the buffers are flushed, the file metadata is updated and the file resources are de-allocated.

wait()

In some systems, a process may wait for another process to complete its execution. This happens when a parent process creates a child process and the execution of the parent process is suspended until the child process executes. The suspending of the parent process occurs with a wait() system call. When the child process completes execution, the control is returned back to the parent process.
This system call runs an executable file in the context of an already running process. It replaces the previous executable file. This is known as an overlay. The original process identifier remains since a new process is not created but data, heap, stack etc. of the process are replaced by the new process.

fork()

Processes use the fork() system call to create processes that are a copy of themselves. This is one of the major methods of process creation in operating systems. When a parent process creates a child process and the execution of the parent process is suspended until the child process executes. When the child process completes execution, the control is returned back to the parent process.

exit()

The exit() system call is used by a program to terminate its execution. In a multithreaded environment, this means that the thread execution is complete. The operating system reclaims resources that were used by the process after the exit() system call.

kill()

The kill() system call is used by the operating system to send a termination signal to a process that urges the process to exit. However, kill() system call does not necessarily mean killing the process and can have various meanings.



C-LOOK Disk Scheduling Algorithm-

  • Circular-LOOK Algorithm is an improved version of the LOOK Algorithm.
  • Head starts from the first request at one end of the disk and moves towards the last request at  the other end servicing all the requests in between.
  • After reaching the last request at the other end, head reverses its direction.
  • It then returns to the first request at the starting end without servicing any request in between.
  • The same process repeats.


Advantages-

  • It does not causes the head to move till the ends of the disk when there are no requests to be serviced.
  • It reduces the waiting time for the cylinders just visited by the head.
  • It provides better performance as compared to LOOK Algorithm.
  • It does not lead to starvation.
  • It provides low variance in response time and waiting time.

Disadvantages-

  • There is an overhead of finding the end requests.


C++ program for C-LOOK Disk Scheduling Algorithm

#include <iostream>

#include <stdlib.h>

using namespace std;

class clook_disk

{

    int ref[100];

    int ttrk, cur, size, prev;

    int sort();

public:
    void getdata();

    void total_move();
};

int clook_disk::sort()

{

    int temp;

    for (int i = 0; i < size - 1; i++)

        for (int y = 0; y < size - 1; y++)

            if (ref[y] > ref[y + 1])

            {

                temp = ref[y];

                ref[y] = ref[y + 1];

                ref[y + 1] = temp;
            }

    for (int i = 0; i < size; i++)

        if (ref[i] > cur)

            return i;

    return size;
}

void clook_disk::getdata()

{

    cout << "Enter total number of tracks : ";

    cin >> ttrk;

    ttrk--;

    cout << "Enter the current position of head : ";

    cin >> cur;

    cout << "Enter previous position of head : ";

    cin >> prev;

    cout << "Enter the size of queue : ";

    cin >> size;

    cout << "Enter the request for tracks : ";

    for (int i = 0; i < size; i++)

        cin >> ref[i];
}

void clook_disk::total_move()

{

    int num = cur, move = 0, ind, dir = cur - prev;

    ind = sort();

    if (dir > 0)

    {

        for (int i = ind; i < size; i++)

        {

            move += ref[i] - num;

            num = ref[i];
        }

        if (ind != 0)

        {

            num = ref[0];

            cout << move << " ";

            for (int i = 0; i <= ind - 1; i++)

            {

                move += ref[i] - num;

                num = ref[i];
            }
        }
    }

    else

    {

        for (int i = ind - 1; i >= 0; i--)

        {

            move += num - ref[i];

            num = ref[i];
        }

        num = ref[size - 1];

        for (int i = size - 2; i >= ind; i--)

        {

            move += num - ref[i];

            num = ref[i];
        }
    }

    cout << "Total head movements : " << move;
}

int main()

{

    clook_disk clook;

    clook.getdata();

    clook.total_move();

    return 0;
}

You May Also Like:

C Program for SCAN disk scheduling algorithm 

C program for First Come First Serve (FCFS) disk scheduling algorithm | C Programming


C program for Shortest Seek Time Next Disk Scheduling Algorithm | C Programming


C++ program for LOOK Disk Scheduling Algorithm | C++ Programming


C Program for C-SCAN Disk Scheduling Algorithm | C Programming


C++ Program For C-LOOK Disk Scheduling Algorithm | C++ Programming

Look Disk Scheduling Algorithm

  • LOOK Algorithm is an improved version of the SCAN Algorithm.
  • Head starts from the first request at one end of the disk and moves towards the last request at the other end servicing all the requests in between.
  • After reaching the last request at the other end, the head reverses its direction.
  • It then returns to the first request at the starting end servicing all the requests in between.
  • The same process repeats.


C++ program for LOOK Disk Scheduling Algorithm

#include <iostream>

#include <stdlib.h>

using namespace std;

class look_disk

{

    int ref[100];

    int ttrk, cur, size, prev;

    int sort();

public:
    void getdata();

    void total_move();
};

int look_disk::sort()

{

    int temp;

    for (int i = 0; i < size - 1; i++)

        for (int y = 0; y < size - 1; y++)

            if (ref[y] > ref[y + 1])

            {

                temp = ref[y + 1];

                ref[y + 1] = ref[y];

                ref[y] = temp;
            }

    for (int i = 0; i < size; i++)

        if (ref[i] > cur)

            return i;

    return size;
}

void look_disk::getdata()

{

    cout << "Enter total number of tracks : ";

    cin >> ttrk;

    ttrk--;

    cout << "Enter the current position of head : ";

    cin >> cur;

    cout << "Enter previous position of head : ";

    cin >> prev;

    cout << "Enter the size of queue : ";

    cin >> size;

    cout << "Enter the request for tracks : ";

    for (int i = 0; i < size; i++)

        cin >> ref[i];
}

void look_disk::total_move()

{

    int num = cur, move = 0, ind, dir = cur - prev;

    ind = sort();

    if (dir > 0)

    {

        for (int i = ind; i < size; i++)

        {

            move += ref[i] - num;

            num = ref[i];
        }

        if (ind != 0)

        {

            if (ind == size)

                move += num - ref[ind - 1];

            else

                move += ref[size - 1] - ref[ind - 1];

            num = ref[ind - 1];

            for (int i = ind - 2; i >= 0; i--)

            {

                move += num - ref[i];

                num = ref[i];
            }
        }
    }

    else

    {

        for (int i = ind - 1; i >= 0; i--)

        {

            move += num - ref[i];

            num = ref[i];
        }

        if (ind == 0)

            move += ref[ind] - num;

        else if (ind != size)

            move += ref[ind] - ref[0];

        num = ref[ind];

        for (int i = ind + 1; i < size; i++)

        {

            move += ref[i] - num;

            num = ref[i];
        }
    }

    cout << "Total head movements : " << move;
}

int main()

{

    look_disk look;

    look.getdata();

    look.total_move();

    return 0;
}

You May Also Like:

C Program for SCAN disk scheduling algorithm 

C program for First Come First Serve (FCFS) disk scheduling algorithm | C Programming


C program for Shortest Seek Time Next Disk Scheduling Algorithm | C Programming


C++ program for LOOK Disk Scheduling Algorithm | C++ Programming


C Program for C-SCAN Disk Scheduling Algorithm | C Programming


C++ Program For C-LOOK Disk Scheduling Algorithm | C++ Programming

Introduction to SSTF disk scheduling :

SSTF stands for Shortest Time First which very uses full of learning about how the disk drive manages the data having the shortest seek time.



Algorithm of the SSTF is given below:-

  • Find the positive distance of all tracks in the request array from the head.
  • Find a track from the requested array which has not been accessed/serviced yet and has a minimum distance from the head.
  • Increment the total seek count with this distance.
  • Currently serviced track position now becomes the new head position.
  • Go to step 2 until all tracks in request array have not been serviced.

C program for Shortest Seek Time Next Disk Scheduling Algorithm 



#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
    int queue[100], t[100], head, seek = 0, n, i, j, temp;
    float avg;
    // clrscr();
    printf("*** SSTF Disk Scheduling Algorithm ***\n");
    printf("Enter the size of Queue\t");
    scanf("%d", &n);
    printf("Enter the Queue\t");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &queue[i]);
    }
    printf("Enter the initial head position\t");
    scanf("%d", &head);
    for (i = 1; i < n; i++)
        t[i] = abs(head - queue[i]);
    for (i = 0; i < n; i++)
    {
        for (j = i + 1; j < n; j++)
        {
            if (t[i] > t[j])
            {
                temp = t[i];
                t[i] = t[j];
                t[j] = temp;
                temp = queue[i];
                queue[i] = queue[j];
                queue[j] = temp;
            }
        }
    }
    for (i = 1; i < n - 1; i++)
    {
        seek = seek + abs(head - queue[i]);
        head = queue[i];
    }
    printf("\nTotal Seek Time is%d\t", seek);
    avg = seek / (float)n;
    printf("\nAverage Seek Time is %f\t", avg);
    return 0;
}

You May Also Like:

C Program for SCAN disk scheduling algorithm 

C program for First Come First Serve (FCFS) disk scheduling algorithm | C Programming


C program for Shortest Seek Time Next Disk Scheduling Algorithm | C Programming


C++ program for LOOK Disk Scheduling Algorithm | C++ Programming


C Program for C-SCAN Disk Scheduling Algorithm | C Programming


C++ Program For C-LOOK Disk Scheduling Algorithm | C++ Programming


Introduction to First Come First Serve (FCFS) disk scheduling :

                    The simplest form of disk scheduling is, of course, the first-come, first-served (FCFS) algorithm. This algorithm is intrinsically fair, but it generally does not provide the fastest service. Consider, for example, a disk queue with requests for I/O to blocks on cylinders 98, 183, 37, 122, 14, 124, 65, 67. If the disk head is initially at cylinder 53, it will first move from 53 to 98, then to 183, 37, 122, 14, 124, 65, and finally to 67, for a total head movement of 640 cylinders.



C program for First Come First Serve (FCFS) disk scheduling algorithm


#include <stdio.h>

#include <math.h>

int main()

{

    int queue[20], n, head, i, j, k, seek = 0, max, diff;

    float avg;

    printf("Enter the max range of disk\n");

    scanf("%d", &max);

    printf("Enter the size of queue request\n");

    scanf("%d", &n);

    printf("Enter the queue of disk positions to be read\n");

    for (i = 1; i <= n; i++)

        scanf("%d", &queue[i]);

    printf("Enter the initial head position\n");

    scanf("%d", &head);

    queue[0] = head;

    for (j = 0; j <= n - 1; j++)

    {

        diff = abs(queue[j + 1] - queue[j]);

        seek += diff;

        printf("Disk head moves from %d to %d with seek %d\n", queue[j],
        queue[j + 1], diff);
    }

    printf("Total seek time is %d\n", seek);

    avg = seek / (float)n;

    printf("Average seek time is %f\n", avg);

    return 0;
}

You May Also Like:

C Program for SCAN disk scheduling algorithm 

C program for First Come First Serve (FCFS) disk scheduling algorithm | C Programming


C program for Shortest Seek Time Next Disk Scheduling Algorithm | C Programming


C++ program for LOOK Disk Scheduling Algorithm | C++ Programming


C Program for C-SCAN Disk Scheduling Algorithm | C Programming


C++ Program For C-LOOK Disk Scheduling Algorithm | C++ Programming



Introduction to SCAN disk scheduling :

                   In the SCAN algorithm, the disk arm starts at one end of the disk and moves toward the other end, servicing requests as it reaches each cylinder, until it gets to the other end of the disk. At the other end, the direction of the head movement is reversed, and servicing continues. The head continuously scans back and forth across the disk. The SCAN algorithm is sometimes called the elevator algorithm, since the disk arm behaves just like an elevator in a building, first servicing all the requests going up and then reversing to service requests the other way.



Different Types of Disk Scheduling Algorithm:

Different types of Disk Scheduling Algorithm

C Program for SCAN disk scheduling algorithm


#include <stdio.h>

#include <math.h>

int main()

{

    int queue[20], n, head, i, j, k, seek = 0, max, diff, temp, queue1[20],
    queue2[20], temp1 = 0, temp2 = 0;

    float avg;

    printf("Enter the max range of disk\n");

    scanf("%d", &max);

    printf("Enter the initial head position\n");

    scanf("%d", &head);

    printf("Enter the size of queue request\n");

    scanf("%d", &n);

    printf("Enter the queue of disk positions to be read\n");

    for (i = 1; i <= n; i++)

    {

        scanf("%d", &temp);

        if (temp >= head)

        {

            queue1[temp1] = temp;

            temp1++;
        }

        else

        {

            queue2[temp2] = temp;

            temp2++;
        }
    }

    for (i = 0; i < temp1 - 1; i++)

    {

        for (j = i + 1; j < temp1; j++)

        {

            if (queue1[i] > queue1[j])

            {

                temp = queue1[i];

                queue1[i] = queue1[j];

                queue1[j] = temp;
            }
        }
    }

    for (i = 0; i < temp2 - 1; i++)

    {

        for (j = i + 1; j < temp2; j++)

        {

            if (queue2[i] < queue2[j])

            {

                temp = queue2[i];

                queue2[i] = queue2[j];

                queue2[j] = temp;
            }
        }
    }

    for (i = 1, j = 0; j < temp1; i++, j++)

        queue[i] = queue1[j];

    queue[i] = max;

    for (i = temp1 + 2, j = 0; j < temp2; i++, j++)

        queue[i] = queue2[j];

    queue[i] = 0;

    queue[0] = head;

    for (j = 0; j <= n + 1; j++)

    {

        diff = abs(queue[j + 1] - queue[j]);

        seek += diff;

        printf("Disk head moves from %d to %d with seek %d\n", queue[j],
        queue[j + 1], diff);
    }

    printf("Total seek time is %d\n", seek);

    avg = seek / (float)n;

    printf("Average seek time is %f\n", avg);

    return 0;
}

You May Also Like:

C Program for SCAN disk scheduling algorithm 

C program for First Come First Serve (FCFS) disk scheduling algorithm | C Programming


C program for Shortest Seek Time Next Disk Scheduling Algorithm | C Programming


C++ program for LOOK Disk Scheduling Algorithm | C++ Programming


C Program for C-SCAN Disk Scheduling Algorithm | C Programming


C++ Program For C-LOOK Disk Scheduling Algorithm | C++ Programming