Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C# 3.5
Tip/Trick

Peer Process and its Significance in Improving Robustness of Software Application

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
30 Jun 2014CPOL4 min read 7.8K   4   1
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.

Image 1

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.

Image 2

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.

C#
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.

Image 3

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.

C#
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.

Image 4

Image 5

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.

Image 6

Peer process

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

Image 7

Host Application App24x7

Image 8

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.

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


Written By
Engineer adobe
India India
Software developer and product designer, writer (both technical and non-technical)
Researcher at heart, engineer by mind Smile | :)

Comments and Discussions

 
QuestionThis was a tip... Pin
OriginalGriff30-Jun-14 6:14
mveOriginalGriff30-Jun-14 6:14 

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

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