Click here to Skip to main content
Click here to Skip to main content

Peer Process and its Significance in Improving Robustness of Software Application

, 30 Jun 2014
Rate this:
Please Sign up or sign in to vote.
How to counter crash issue in applications

Introduction

Softwares now a day are generally assumed to be user friendly, but there is one more thing that is now in demand for software of business class. That is 24 by 7 working by software, like services or continuously running processes, which required very little human assistance to watch over them. As hardware has grown over the past few decades, machines are available which need not be refreshed or restarted even for years. Softwares on the other hand, are fragile and more error prone. Software processes can be hanged, crashed, or become memory consumers. Here, we are discussing a scenario which deals with the issue of crashing in applications. It is not an unknown fact that most application crashes run for more than just a few hours. There are many standard practices that are followed to overcome this like
Controllers, which keep watch on them.
Triggers, notify users if application crashes.
And OS services, which come with built in function like auto-start, auto-restart, recovery, etc.
But leaving everything on OS or building a master controller may not be a suitable solution; it may be time consuming to be developed also. Peer process can be an effective solution. They are attached directly to a process and they are easy to develop. Developer can use them as required, as per their available information and privileges.

Assumption: You must have a basic knowledge of C#, and processes, EXE
Problem: Crash in applications
Solution: Peer architecture and peer processes

Using the Code

Let's first understand how it works. Suppose you have an application Sample.exe. It needs to be run 24x7 to do some task/processing. But it crashes due to some reasons. I am using another application to run along with it. Peer process will be called/initialized by Sample.exe with some information (I used Process ID and Process Name). Now it will check periodically whether its host application is running or not. If not running, it will restart it again. After restart, it will kill itself as another peer will be attached to new Sample.exe instance. Furthermore, if user stops Sample.exe gracefully, or terminates it after its work finishes, it dies along with the peer process attached with it.

Peer process architecture

Let’s work on a practical version. I have developed an application named App24x7.exe to show how it works. App24x7.exe is a form application displaying its process Id. Suppose it has something processing behind.

Host Application

When form will load, a second process called PeerProcess.exe will be launched. PeerProcess.exe has been passed Process Id and Process name of App24x7.exe. Here is the code of Form Load Event.

private void Form1_Load(object sender, EventArgs e)
{
  ProcessStartInfo pstrNew = new ProcessStartInfo("PeerProcess");
  pstrNew.Arguments =Process.GetCurrentProcess().ProcessName + " " +Process.GetCurrentProcess().Id;
  label1.Text += " With Process ID:" + Process.GetCurrentProcess().Id;
  Process.Start(pstrNew);
}

I have displayed the PeerProcess.exe for demo purposes. It will print status of Host application (from where it is launched) every single minute. If you see the screenshot, process ID is printed same as that of form.

Peer process

Peer process will relaunch App24x7.exe if it found it crashed/ not running. While starting the EXE, it will kill itself, because with new instance of App24x7.exe, a new peer process will be attached. Code sample is as follows.

static void Main(string[] args)
{
  string strProcessName=string.Empty;
  string strProcessID = string.Empty;
  if (args.Length == 0)
  {
    Console.WriteLine("No process attached, Exit in 5 Seconds");
    System.Threading.Thread.Sleep(5000);
    return;
  }
  else
  {
    strProcessName = args[0];
    strProcessID = args[1];
    Console.WriteLine("Process attached: " +strProcessName +" "+ strProcessID);
  }
  while (true)
  {
    try
    {
      Process pstrProcesses =Process.GetProcessById(int.Parse(strProcessID));
      Console.WriteLine("Attached Process is running");
      System.Threading.Thread.Sleep(60000);
    }
    catch(Exception ex)
    {
      Console.WriteLine("Process not running:Restarting");
      System.Threading.Thread.Sleep(2000);
      ProcessStartInfo pstrNew = new ProcessStartInfo(strProcessName);
      System.Diagnostics.Process.Start(pstrNew);
      break;
    }
  }
}

Now I forcefully killed my allocation using task manager. I did not close it gracefully; instead I use Kill command of OS. This can be done by task manager using End Process button. In the next screenshot, you can see I select the process and killed it.

Task manager kill operation(forceful kill)

After a time interval when PeerProcess.exe will check the status of host app, it will find it dead. So it will restart it.

Peer process

Hence next instance of App24x7.exe will be launched, and its peer process will also be launched.

Host Application App24x7

Peer process

There is one last case, when I close the application by myself, it will kill the peer process first, then kill itself. To find out what peer process to kill, I used the folder address. In the given scenario, PeerProcess.exe was present in the folder of App24x7.exe. So I kill the peer, which was launched from the same folder.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
  Process[] pstrProcesses =Process.GetProcessesByName("PeerProcess");
  string strBaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
  Process strCurrent = Process.GetCurrentProcess();
  for (int i = 0; i < pstrProcesses.Length; i++)
  {
    if (Path.GetDirectoryName(pstrProcesses[i].MainModule.FileName.ToUpper()) == 
            Path.GetDirectoryName(strCurrent.MainModule.FileName.ToUpper()))
    {
      pstrProcesses[i].Kill();
    }
  }
}

Points of Interest

There may be other ways to mark your peer. You can give unique name to your peer process as per EXE, or any other way can be used. The idea is that peer process can be used for any application. And multiple instances of it can be run for different services, but one service per peer will be suitable.

That’s all folks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Anant Beriwal
Software Developer
India India
No Biography provided

Comments and Discussions

 
QuestionThis was a tip... PinprotectorOriginalGriff30-Jun-14 6:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 30 Jun 2014
Article Copyright 2014 by Anant Beriwal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid