[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

A Simple Safe Programming syn..

The safe state algorithm is used by Operating Systems to avoid deadlock situations. The algorithm works by starting with a safe state, accepting a request for resources by a process, determining if the new state is safe, and then granting the request only if the new state is safe.

    I am trying to write a program to implement the safe state algorithm under the following conditions. The program must handle up to 10 processes at any one time. There are four resource groups that can be requested. These are disk drives, tape drives, printers, and graphical plotters.

    The current "state" of the system needs to be read into the program from a data file. It can be assumed that the current state is a safe one, however it will be simple enough for the program to verify this. Once the current state is read in, a new resource request can be read in from a data file or from the keyboard.

    The program should accept this resource request and determine if the new state is also safe. The process of accepting new resource requests can be continued if desired.

    The output from the program will be a display of the current state, the resource request entered, a message indicating whether the state is safe or not, and the sequence of processes that was used to verify the safe state. The output to the screen gotta be readable so i can determine what has occurred.

    I need help on uncluding the code..it can either bein c or c++ as the compiler and runon  MS-dos environment. PLease help me construct coding.


Thhank you  in advance..
0
kuganesh
Asked:
kuganesh
  • 4
  • 2
1 Solution
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi kuganesh,

The first thing that you're going to have to do is decide how to structure communications between the different components.  You might build your demo using a thread model, in which case all of the threads can have access to a common memory region, you might build this on a parent/child model so that the child can simply signal the parent, or you may come up with any number of other inter-process communication methods.

The simplest is simply to define the state as free/reserved via zero/non-zero.  If the state is zero, the item is free for assignment.  Reserving an object involves simply setting the state to the task ID or thread ID of the requesting process.

Does this start you on your way?
Kent
0
 
kuganeshAuthor Commented:
SAFESTATE
BEGIN
WORK [M]: initialized with contents of AVAILABLE;
FINISH [N]: each element is set FASLE;
SAFE = TRUE;
FOR i = 1 to N DO
IF ((FINISH[i]   FALSE) and (NEED [i] <= WORK [i])) THEN
WORK [i] = WORK [i] + ALLOCATION [i];
FINISH[i] = TRUE;
ENDIF
ENDFOR
FOR i = 1 to N DO
IF (FINISH[i]   FALSE) THEN
SAFE = FALSE;
BREAK;
ENDIF
ENDFOR
END SAFESTATE
 
yes that certainly helps me... but i need to get an example coding.. i know the basics which is what i posted in this message but i have no clue on how to construct a program which uses this alog. i need help in the coding sequence and displaying the output.
pls do help me further. thanx
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Ok.  For "sake of argument" let's assume that your example is using pthreads.  (Multiple execution points within the same program.)  What you'll need to do is ensure that the locking/unlocking mechanism always occurs in the same thread.  

There's quite a bit more to things than this simple code shows, but it should get you started.

Kent




#define NUMBER_OF_CONTROLLED_RESOURCES 20
#define NUMBER_OF_PROCESSES 10

#define RQ_NONE 0
#define RQ_HD0   1
#define RQ_HD1   2
#define RQ_CD0   3
#define RQ_CD1   4
....

 
pthread * Processes[NUMBER_OF_PROCESSES];  /*  Process control for each of the (up to) 10 active processes.  */

int ResourceState[NUMBER_OF_CONTROLLED_RESOURCES + 1];  // Resource number 0 isn't used

/*  The index into the Request Queue is the process number.
      A process requests a resource by placing the index of the resource into the

int RequestQueue[NUMBER_OF_PROCESSES];  
int ReleaseQueue[NUMBER_OF_PROCESSES];

MainThread ()
{
  int idx;
  while (1)
  {
    for (idx = 0; idx < NUMBER_OF_CONTROLLED_RESOURCES + 1; idx++)
      if (RequestQueue[idx] != 0)
        ProcessRequest ();
      if (ReleaseQueue[idx] != 0)
        ProcessRelease ();
    sleep (10);  /*  Wait 10 milliseconds before checking again  */
}

RequestResource (int Resource)
{
  RequestQueue[ProcessNumber] = Resource;
}

ReleaseResource (int Resource)
{
  ReleaseQueue[ProcessNumber] = Resource);
}



0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
kuganeshAuthor Commented:
wow.. thanks a lot for that
i managed tomake and call one of  the processes but it would not run sucessfully and the attemp to output the status would not happen
Could  some one Please put an example out..bacause it is quite confusing to make all of the safe state alog apply.
This would be great and i can dosome modification to rectify the display.
Thank You
0
 
kuganeshAuthor Commented:
#define MAXN 10             /* maximum number of processes                   */
#define MAXM 4             /* maximum number of resource types              */
int Available[MAXM];        /* Available[j] = current # of unused resource j */
int Max[MAXN][MAXM];        /* Max[i][j] = max demand of i for resource j    */
int Allocation[MAXN][MAXM]; /* Allocation[i][j] = i's current allocation of j*/
int Need[MAXN][MAXM];       /* Need[i][j] = i's potential for more j         */
                            /* Need[i][j] = Max[i][j] - Allocation[i][j]     */
int Finish[MAXN];

int is_safe_state()
{
  int work;
  int res;
  int proc;
 
  for(res=0;res<MAXM;res++)
  {
    work=Available[res];
    for(proc=0;proc<MAXN;proc++)
       Finish[proc]=0;
    for(proc=0;proc<MAXN;proc++)
    {
      if(Finish[proc]==0 && Need[proc,res]<=work)
      {
           work+=Allocation[proc,res];
           Finish[proc]=1;
      }else{
           return(0);
      }
    }
  }
  return(1);
}

//still need to have main() to populate the initial state and call the algorithm and print stuff
0
 
kuganeshAuthor Commented:
please help m,e... only one part left..the main()
0
 
sunnycoderCommented:
>please help m,e... only one part left..the main()
Decide on what all functionality you want in main and in what order ...

Programming is nothing but converting your thoughts from one language to another ;o)

You may need to use some library functions ... look into
fopen
fclose
fgets
printf
scanf

Since this a homework assignment, experts here cannot give you the entire code ... Make a "genuine" attempt, post it here and we will try to help get it working
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now