Sunday, January 12, 2014

Advanced : processes, sub-processes & how to call them in python

Hello.... Friends , Today we are going to study about processes, how to create a process ? how we can call processes in our local system & how python shell interact with *nix shell ( Linux & Unix ) ? So if we talk about processes that what is a process ?

Process : any running program is called a process. Each process has its own system state, Which includes memory, Lists of open files, a program counter that keeps track of instruction being executed, and a call stack used to hold the local variables of function,
Normally, a process executes statements one after the other in a single sequence of control flow, which is sometimes called the main thread of the process.

Now let's understand
How to create a process : we can say every program create there process using library functions which are founds in os 
So let's see practically how we can create a process in this small demo i am using fork method to create a process
Forking : forking is nothing much just a cloning a process is known as forking, by using method fork() we can create a process as the parent process.
if we explore more about process then we can say every process have there own PID Process ID ) there memory use etc.

So here we can see that we create a new process a child process now we can assign different task to this process.

So here we study what is a process in os & how to create a new process using python.
now we are going to study about the sub processes

Sub Processes : To run Unix Commands we need to create a sub process that runs the command Let's see how sub processes works
few sub process commands :
os.system()
os.popen()
subprocess.call()

                        

this is simple we importing a os class from the python built-in classes


in next image we can see that we are importing subprocess along with importing os now in subprocess i want to execute the subprocess unix commands in python so let's what we did
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False) in privious command we call the arg along with the command.


In this image i am not importing os class so we need complete command that shell =True so that we can execute unix command


No comments:

Post a Comment