[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


A Simple Safe Programming syn..

Posted on 2004-09-09
Medium Priority
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 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..
Question by:kuganesh
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 46

Expert Comment

by:Kent Olsen
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 46

Accepted Solution

Kent Olsen earned 290 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);

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

656 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