A Simple Safe Programming syn..

Posted on 2004-09-09
Last Modified: 2010-04-15
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 can either bein c or c++ as the compiler and runon  MS-dos environment. PLease help me construct coding.

Thhank you  in advance..
Question by:kuganesh
  • 4
  • 2
LVL 45

Expert Comment

ID: 12019439
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?

Author Comment

ID: 12019535
WORK [M]: initialized with contents of AVAILABLE;
FINISH [N]: each element is set FASLE;
FOR i = 1 to N DO
IF ((FINISH[i]   FALSE) and (NEED [i] <= WORK [i])) THEN
WORK [i] = WORK [i] + ALLOCATION [i];
FOR i = 1 to N DO
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
LVL 45

Accepted Solution

Kdo earned 145 total points
ID: 12020098
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.



#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);

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails


Author Comment

ID: 12023122
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

Author Comment

ID: 12023744
#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;
      if(Finish[proc]==0 && Need[proc,res]<=work)

//still need to have main() to populate the initial state and call the algorithm and print stuff

Author Comment

ID: 12023748
please help m,e... only one part left..the main()
LVL 45

Expert Comment

ID: 12024738
>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

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now