fork() - will fork a new process with separate memory block, open file descriptors from parent process.
exec*() - it will replace current process with the program it is going to execute.
Above combination can be used to create several process in background. exec*() in detail :
There are various confusing forms in python.
1. execl(<program_path>,<arg1>,<arg2>,..,<argN>)
where,
program_path : either input absolute path of program file or set PATH variable and specify only program name
arg1,arg2,..,argN : These are arguments to program. Always remember to set arg1 to program name because in python sys.argv[0] is name of program. Actual program arguments are starting from arg2.
2. execv(<program_path>,args)
where,
program_path : either input absolute path of program file or set PATH variable and specify only program name
args : Create a tuple of arguments where first element is program name because in python sys.argv[0] is name of program. Actual program arguments are starting from second element in tuple.
3. execlpe(<program_path>,arg1,arg2,..,..,env)
- everything is similar to execl. Only difference is 'p' means python will look into PATH if program path is not specified. 'e' means we can pass dictionary which contains environment variable name and its value which will be set before executing program.
5. execvpe(<program_path>,args,env)
- everything is similar to execv. Only difference is 'p' means python will look into PATH if program path is not specified. 'e' means we can pass dictionary which contains environment variable name and its value which will be set before executing program.
Try below program:
[manas_khandeshe@static-61 ~/Documents/sandbox]cat test.sh
#!/bin/bash
script_name=$0
arg1=$1
current=`pwd`
echo "Script name : $script_name, $arg1"
echo "XYZ: "$XYZ
echo "PATH: "$PATH
echo "current directory: $current"
#!/usr/bin/env python3
import os
import sys
def _execl():
arg1 = "test"
arg2 = "abc"
os.environ["XYZ"] = 'MyXYZ'
os.execlp("/Users/manas_khandeshe/Documents/test.sh",arg1,arg2)
return
def _execlpe():
arg1 = "test"
arg2 = "abc"
env = {
"XYZ" : 'MyXYZ'
}
os.execlpe("/Users/manas_khandeshe/Documents/sandbox/test.sh",arg1,arg2,env)
return
def _execv():
args = ("test","abc")
os.environ["XYZ"] = 'MyXYZ'
os.execv("/Users/manas_khandeshe/Documents/sandbox/test.sh",args)
return
def _execvpe():
env = {
"PATH":"/Users/manas_khandeshe/Documents/sandbox",
"XYZ":"BlaBla"
}
args = ("test","abc")
os.execvpe("test.sh",args,env)
return
def main():
_execl()
#_execlpe()
#_execv()
#_execvpe()
return
main()
exit(0)
exec*() - it will replace current process with the program it is going to execute.
Above combination can be used to create several process in background. exec*() in detail :
There are various confusing forms in python.
1. execl(<program_path>,<arg1>,<arg2>,..,<argN>)
where,
program_path : either input absolute path of program file or set PATH variable and specify only program name
arg1,arg2,..,argN : These are arguments to program. Always remember to set arg1 to program name because in python sys.argv[0] is name of program. Actual program arguments are starting from arg2.
2. execv(<program_path>,args)
where,
program_path : either input absolute path of program file or set PATH variable and specify only program name
args : Create a tuple of arguments where first element is program name because in python sys.argv[0] is name of program. Actual program arguments are starting from second element in tuple.
3. execlpe(<program_path>,arg1,arg2,..,..,env)
- everything is similar to execl. Only difference is 'p' means python will look into PATH if program path is not specified. 'e' means we can pass dictionary which contains environment variable name and its value which will be set before executing program.
5. execvpe(<program_path>,args,env)
- everything is similar to execv. Only difference is 'p' means python will look into PATH if program path is not specified. 'e' means we can pass dictionary which contains environment variable name and its value which will be set before executing program.
Try below program:
[manas_khandeshe@static-61 ~/Documents/sandbox]cat test.sh
#!/bin/bash
script_name=$0
arg1=$1
current=`pwd`
echo "Script name : $script_name, $arg1"
echo "XYZ: "$XYZ
echo "PATH: "$PATH
echo "current directory: $current"
#!/usr/bin/env python3
import os
import sys
def _execl():
arg1 = "test"
arg2 = "abc"
os.environ["XYZ"] = 'MyXYZ'
os.execlp("/Users/manas_khandeshe/Documents/test.sh",arg1,arg2)
return
def _execlpe():
arg1 = "test"
arg2 = "abc"
env = {
"XYZ" : 'MyXYZ'
}
os.execlpe("/Users/manas_khandeshe/Documents/sandbox/test.sh",arg1,arg2,env)
return
def _execv():
args = ("test","abc")
os.environ["XYZ"] = 'MyXYZ'
os.execv("/Users/manas_khandeshe/Documents/sandbox/test.sh",args)
return
def _execvpe():
env = {
"PATH":"/Users/manas_khandeshe/Documents/sandbox",
"XYZ":"BlaBla"
}
args = ("test","abc")
os.execvpe("test.sh",args,env)
return
def main():
_execl()
#_execlpe()
#_execv()
#_execvpe()
return
main()
exit(0)
No comments:
Post a Comment