• C

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..
Who is Participating?
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.



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

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?
kuganeshAuthor Commented:
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
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

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
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;
      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
kuganeshAuthor Commented:
please help m,e... only one part left..the main()
>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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.