Showing posts with label Signals. Show all posts
Showing posts with label Signals. Show all posts

29 December 2017

Simple ProcMon - Putting all things together

I have written few posts on process management in Python - fork, exec, wait, and exit. These are exactly same Python APIs around system APIs provided by Linux/Unix/POSIX. In this post, I am going to talk about putting things together and create a simple process monitor.

Read how to fork, exec, wait and exit before proceeding with this post.

Functionality of Simple Process Monitor
  1. Have the list of processes that need to be monitored in the INI file. Let us call this configuration
  2. Load the configuration file
  3. Start the process
  4. Exec the new program
  5. Loop #3 and #4 until all the processes are loaded
  6. Monitor the process (wait). A simple call to "wait" would listen for a child to exit.
  7. When a child dies, handle it either by restarting the process again or do nothing (depending on how it is configured).
  8. Go back to #6 and continue the monitoring
It is going to tough to post all the code here. Refer my GITHub for the code. Feel free to extend or write tests

Experience: It is another personal experience on higher productivity claim of Python. I wrote the code in a couple of hours and spent around 2 hours on refactoring. Given my (lack of) experience in Python, Python seems to fulfill its promise.

25 December 2017

Process Management in Python - os._exit()

This is a continuation of series of posts on Process management in Python. Read thisthis and this before continuing further. In this post, we are going to see how os._exit works and how it is related to "wait".

os._exit when invoked, the process that is calling it terminates immediately without doing any clean-ups (however system.exit() can be handled by the Python program, a much safer/clean way). The parent process receives the exit status when the parent process calls "wait" or "waitpid. os._exit calls _exit of OS

The following code demonstrates it. Beware, os._exit exits the process abruptly.


For more information on exit and _exit refer - https://en.wikipedia.org/wiki/Exit_(system_call)

Refer the Code in GIT Hub

Here is the code in Python (being Python, it is self-explanatory :-))


24 December 2017

Process Management in Python - os.wait()

This is a continuation of series of posts on Process management in Python. Read this and this before continuing further. In this post, we are going to see
  1. How the parent process can get the status of its child after the child has exited using "os.wait"
  2. What happens to the child processes after they exit but the parent is still alive and did not get the status (defunct - meaning that userspace is cleared but a small about of information in kernel space is retained so as to give the information when the parent requests the status which may potentially lead to inability to create new processes)
Getting the status of the child processes is simple. Just call os.wait() to get status of any child processes or waitpid to get status of a specific process. Note, this is blocking call meaning that the parent (thread) waits here until the child is exited/terminated.

For more information on wait/waitpid, refer - https://en.wikipedia.org/wiki/Wait_(system_call)

Refer the Code in GIT Hub

Here is the code in Python (being Python, it is self-explanatory :-))


23 December 2017

Process Management in Python - os.exec

This is continuation of the previous post - Process Management in Python - os.fork()

In Linux, running a command from the shell is a two-step process. The shell first creates a process using fork (posted earlier) and overlays a new program using "exec". For example, when you want to run the command "ls" from the shell - the shell forks a copy of its own and immediately loads the binary of "ls" and starts the execution again.


Beware, you cannot find a function exec() but rather it is a family of functions with names such as - execl, execle, execlp, execv. The functions are almost same and they differ only in parameters that are being passed. I have used one such functions in the following code. When you run this code, you should see a directory listing - the current process upon the successful calling of exec, loads "ls" and executes it. Once "ls" is executed, it exits (like it normally used to do)

As an experiment, you can do fork to create a child process and in the child process execution flow, you can overlay a new program.

Refer the Code in GIT Hub

Try running this script from Linux shell and see the output. Do not forget to share, like, comment if you like this post :-)


22 December 2017

Process Management in Python - os.fork()

Inspired by https://pymotw.com/3/signal/index.html, I thought of exploring os package in Python. The next few posts would be on process management and how to do it in Python. I would be showing you the code that is tiny. The goal is to provide basic code and I leave the improvision to you.

There are four important system calls - fork, exec, wait and exit that are related to process management. Interestingly Python provides equivalent APIs as a part of os package so that you can do a lot of system programming (and use it in a cool way). In this post, let us see some code that uses "os.fork" to create a new process.

os.fork() - creates a new process (often called as parent-child processes). The following code has enough comments and some explanation embedded as comments. Suggest you read the code and run it in Linux box.

Refer the Code in GIT Hub

If you want to know more about fork, refer - https://en.wikipedia.org/wiki/Fork_(system_call)

Share, comment and contribute if you like this post :-)


21 December 2017

Alarm and Signal in Python

Continuation to my previous post in Signals in Python. Again the credits go to Python Module of the week. The following code shows how easier it is to work with Python. This simple script registers for an alarm using SIGALRM which is configured to fire after "n" seconds. After "n" seconds, the handler function gets called.

In this code, I set alarm once as a part of main control flow and register again in the alarm handler. This program does not exit unless one kills the process or stops the execution of the program. (Python code seems to be more beautiful)



20 December 2017

Signal Handling in Python

If you are system programmer, you would have heard about signals and probably would have done lots of coding using signal. It might also be a second nature to you. But when you started to get your hands dirty with signals especially in C, it might have been quite involved. Now moving to signal handling in Python

Recently (last week), there was a post from Python Module of the week on signals and I thought I would try it. Here is the code that does some signal handling. You might want to check the full post (BTW, it is very interesting one). I feel that it easy to write code (and beautiful too) and I was able to do C-equivalent code in less than 30 minutes (which means that Python makes us productive). Here is the script that I wrote. You can find in GIT Hub and feel free to play around.