Showing posts with label Operating Systems. Show all posts
Showing posts with label Operating Systems. Show all posts

Sunday, March 14, 2010

OS lab expts

click here for download

Hope it will be useful for yu ppl..!!:)

Friday, February 12, 2010

SJF - NON PREEMPTIVE SCHEDULING ALGORITHM

#include<stdio.h>

main()

{

    int b[10],p[10],t[10],w[10],n,i,j,tot=0,tot_turn=0;

    float avg;

    printf("\n Enter the number of Process:");

    scanf("%d",&n);

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

    {

        printf("\n Enter the process no:");

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

        printf("\n Enter the burst time:");

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

    }

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

    {

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

        {

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

            {

                int temp;

                temp=p[i];

                p[i]=p[j];

                p[j] = temp;

                temp = b[i];

                b[i] = b[j];

                b[j] = temp;

            }

        }

    }

    w[0] = 0;

    t[0] = b[0];

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

    {

        w[i]=t[i-1];

        t[i] = w[i] + b[i];

    }

    printf("\n\n Gantt Chart:");

    printf("\n\n..........................................");

    printf("\n|p%d\t|",p[0]);

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

        printf("p%d\t|",p[i]);

    printf("\n\n..........................................");

    printf("%d",w[0]);

    printf("\t%d",t[0]);

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

        printf("\t%d",t[i]);

    printf("\n\n|");

    printf("\n Process \t Burst time \t Waiting time \t Turnaround time");

    printf("\n...........\t.............\t...........\t.........\n");

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

    {

        printf("\n p%d    \t %d      \t%d    \t%d",p[i],b[i],w[i],t[i]);

        tot = tot + w[i];

        tot_turn = tot_turn + t[i];

    }

    avg=(float)tot/n;

    printf("\n\n The average waiting time is: %f",avg);

    avg = (float)tot_turn/n;

    printf("\n\n The average turn around time is %f",avg);

}

Monday, February 1, 2010

FCFS Implementation in C


Hi friends, below is a code to implement FCFS in C language...

(code given may not be clear, you can however download the code itself by clicking the link below)

#include
#include
struct proc
{
char id[6];
int burst_time; /*Holds the burst time of the process*/
int wait_time; /*Holds the waiting time of the process*/
int turn_time; /*Holds the turnaround time of the process*/
}process[10];
main()
{
int no,i,tot_wait=0; //total waiting time
float avg_wait; //average waiting time
clrscr();
printf("\n Enter the Number of Processes:");
scanf("%d",&no);
for(i=0;i {
printf("\n Enter the process id:");
scanf("%s",process[i].id);
printf("\n Enter the burst time:");
scanf("%d",&process[i].burst_time);
process[i].wait_time = 0;
}
// Gantt chart display
printf("\n\n Gantt Chart:\n\n");
printf("\n %s - %d to %d",process[0].id,process[0].wait_time,process[0].burst_time);
process[0].turn_time = process[0].wait_time + process[0].burst_time;
for(i=1;i {
process[i].wait_time = process[i-1].wait_time + process[i-1].burst_time;
process[i].turn_time = process[i].wait_time + process[i].burst_time;
printf("\n %s - %d to %d",process[i].id,process[i].wait_time,process[i].turn_time);
}


//Process,waiting time, turnaround time display
printf("\n\n Process \t Waiting Time \t Turnaround time");
for(i=0;i {
printf("\n%s\t\t%d\t\t%d",process[i].id,process[i].wait_time,process[i].turn_time);
tot_wait = tot_wait + process[i].wait_time;
}
avg_wait = tot_wait / no;
printf("\n\n The average waiting time is %f",avg_wait);
getch();
}




Download Source Code

By G.Vivek Venkatesh

the code has been written so that there is no error...in case u find any error pls let me know...

Wednesday, January 20, 2010

Simulation of "ls" and "grep" commands

Simulation of ls command



#include <stdio.h>

#include <sys/types.h>

#include <dirent.h>

main(int age, char *argv[])

{

DIR *dir;

struct dirent *rddir;

printf(“/n Enter directory with outoput command”);

dir=opeindir(argv[1]);

while((rddir=readdir(dir))!=NULL)

{

printf(“%s\t”,rddir->d_name);

}

closedir(dir);

}


Simulation of GREP command

#include <stdio.h>

#include <stdlib.h>

main()

{

FILE *fp;

char c[100], pat[10];

int l,i,j=0,count=0,len,k,flag=0;

printf("\n Enter the pattern");

scanf("%s", pat);

len=strlen(pat);

fp=fopen("nn.txt","r");

while(!feof(fp))

{

fscanf(fp,"%s",c);

l=strlen(c);

count++;

for(i=0;i<l;i++)

{

if(c[i]==pat[j])

{

flag=0;

for(k=1;k<len;k++)

{

if(c[i|k]!=pat[k])

{

flag=1;

}

if(flag==0)

printf("\n The Pattern %s is present in word %d",pat,count");

}

}

}

}

Sunday, January 3, 2010

Interprocess Communication, Shared Memory, Process State


Interprocess Communication (IPC)


  1. Mechanism for processes to communicate and to synchronize their actions.
  2. Message system – processes communicate with each other without resorting to shared variables.
  3. IPC facility provides two operations:
    1. send(message) – message size fixed or variable
    2. receive(message)
  4. If P and Q wish to communicate, they need to:
    1. establish a communication link between them
    2. exchange messages via send/receive
  5. Implementation of communication link
    1. physical (e.g., shared memory, hardware bus)
    2. logical (e.g., logical properties)

Direct Communication

I. Processes must name each other explicitly:


a. send (P, message) – send a message to process P

b. receive(Q, message) – receive a message from process Q


II. Properties of communication link

a. Links are established automatically

b. A link is associated with exactly one pair of communicating processes

c. Between each pair there exists exactly one link

d. The link may be unidirectional, but is usually bi-directional

Indirect Communication

  1. Messages are directed and received from mailboxes (also referred to as ports)
    1. Each mailbox has a unique id
    2. Processes can communicate only if they share a mailbox.
  2. Properties of communication link
    1. Link established only if processes share a common mailbox
    2. A link may be associated with many processes
    3. Each pair of processes may share several communication links
    4. Link may be unidirectional or bi-directional
  3. Operations
    1. create a new mailbox
    2. send and receive messages through mailbox
    3. destroy a mailbox
  4. Primitives are defined as:

send(A, message) – send a message to mailbox A

receive(A, message) – receive a message from mailbox A

  1. Mailbox sharing
    1. P1, P2, and P3 share mailbox A
    2. P1, sends; P2 and P3 receive
    3. Who gets the message?
  2. Solutions
    1. Allow a link to be associated with at most two processes
    2. Allow only one process at a time to execute a receive operation
    3. Allow the system to select arbitrarily the receiver. Sender is notified who the receiver was.

Synchronization

  1. Message passing may be either blocking or non-blocking
  2. Blocking is considered synchronous
    1. Blocking send has the sender block until the message is received
    2. Blocking receive has the receiver block until a message is available
  3. Non-blocking is considered asynchronous
    1. Non-blocking send has the sender send the message and continue
    2. Non-blocking receive has the receiver receive a valid message or null

Buffering

Queue of messages attached to the link; implemented in one of three ways

1. Zero capacity – 0 messages
Sender must wait for receiver (rendezvous)

2. Bounded capacity – finite length of n messages
Sender must wait if link full

3. Unbounded capacity – infinite length
Sender never waits


Shared Memory



In computing, shared memory is memory that may be simultaneously accessed by multiple programs with an intent to provide communication among them or avoid redundant copies. Depending on context, programs may run on a single processor or on multiple separate processors. Using memory for communication inside a single program, for example among its multiple threads, is generally not referred to as shared memory.


In computer hardware, shared memory refers to a (typically) large block of random access memory that can be accessed by several different central processing units (CPUs) in a multiple-processor computer system.

A shared memory system is relatively easy to program since all processors share a single view of data and the communication between processors can be as fast as memory accesses to a same location.

The issue with shared memory systems is that many CPUs need fast access to memory and will likely cache memory, which has two complications:



* CPU-to-memory connection becomes a bottleneck. Shared memory computers cannot scale very well. Most of them have ten or fewer processors.


* Cache coherence: Whenever one cache is updated with information that may be used by other processors, the change needs to be reflected to the other processors, otherwise the different processors will be working with incoherent data (see cache coherence and memory coherence). Such coherence protocols can, when they work well, provide extremely high-performance access to shared information between multiple processors. On the other hand they can sometimes become overloaded and become a bottleneck to performance.


The alternatives to shared memory are distributed memory and distributed shared memory, each having a similar set of issues.

Process State

1. As a process executes, it changes state

a. new: The process is being created

b. running: Instructions are being executed

c. waiting: The process is waiting for some event to occur

d. ready: The process is waiting to be assigned to a processor

e. terminated: The process has finished execution



I hope this is useful for your assignment.