Port Windows IPC Apps to Linux, Part 1: Processes and Threads
By Srinivasan S. Muthuswamy, Kavitha Varadarajan
2005-06-16
Creating a process
In Windows, you can use CreateProcess() to create a new process. The CreateProcess() function creates a new process and its main thread as follows:
BOOL CreateProcess(
LPCTSTR lpApplicationName, // name of executable module
LPTSTR lpCommandLine, // command line string
LPSECURITY_ATTRIBUTES lpProcessAttributes, // SD
LPSECURITY_ATTRIBUTES lpThreadAttributes, // SD
BOOL bInheritHandles, // handle inheritance option
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // new environment block
LPCTSTR lpCurrentDirectory, // current directory name
LPSTARTUPINFO lpStartupInfo, // startup information
LPPROCESS_INFORMATION lpProcessInformation // process information
)
|
bInheritHandles determines whether the handles have to be inherited to the child from the parent. lpApplicationName and lpCommandLine give the name and path of the process to be started. lpEnvironment defines the environment that has to be visible for the process.
In Linux, the exec* family of functions replace the current process image with a new process image (as shown in the following):
int execl(const char *path, const char *arg, ...);
int execlp(const char *file, const char *arg, ...);
int execle(const char *path, const char *arg , ..., char * const envp[]);
int execv(const char *path, char *const argv[]);
int execvp(const char *file, char *const argv[]);
|
These versions of exec* are just various calling interfaces for core function execve(): int execve(const char *filename, char *const argv [], char *const envp[]). Here argv is the pointer containing arguments list and envp is the pointer containing list of environment variables which are basically key=value pairs. This must be used along with the fork() command so both the parent and child processes are running: pid_t fork(void). fork() creates a child process that differs from the parent process only in its PID and PPID; in fact, the resource utilizations are set to 0. By default, the exec() function inherits the group and user IDs from the parent process, which makes it dependent on the parent process. This can be changed by: - Setting the
set-uid and set-gid bit on the program file pointed - Using the
setpgid() and setuid() system call
The CreateProcessAsUser() function is similar to CreateProcess() except that the new process runs in the security context of the user represented by the hToken parameter. There is no one-to-one equivalent for this function in Linux, but it can be replicated using the following logic: fork() to create a new child process with new PID setuid() to switch to the new PID exec() to change the existing process image with the process to execute
Tutorial Pages:
»
A mapping guide for complex, multithreaded, multiprocess applications
»
Processes
» Creating a process
»
Terminating a process
»
Using wait functions
»
Exiting a process
»
Environment variables
»
Examples
»
Threads
»
Examples of processes and threads
»
Next in the series
»
Resources
First published by IBM DeveloperWorks
|

|