written 2.7 years ago by |
Solution:
Process creation:
A process may create several new processes, via a create-process system call, during the course of execution.
The creating process is called a parent process, and the new processes are called the children of that process. Each of these new processes may in turn create other processes, forming a tree of processes.
When a process creates a subprocess, that subprocess may be able to obtain its resources directly from the operatiiig system, or it may be constrained to a subset of the resources of the parent process.
The parent may have to partition its resources among its children, or it may be able to share some resources (such as memory or files) among several of its children.
It is easy to construct a process tree similar to what is shown in Figure by recursively tracing parent processes all the way to the in it process.
In general, a process will need certain resources (CPU time, memory, files, I/O devices) to accomplish its task.
Restricting a child process to a subset of the parent's resources prevents any process from overloading the system by creating too many sub-processes.
In addition to the various physical and logical resources that a process obtains when it is created, initialization data (input) may be passed along by the parent process to the child process.
Creating a separate process using the Win32 API:
# include <stdio.h> i # include <windows.h> int main(VOID) { STARTUPINFO si; PROCESS_INFORMATION pi; // allocate memory ZeroMemory(&si, sizeof (si)) ; si.cb = sizeof (si) ; ZeroMemory(&pi, sizeof(pi)); // create child process if (!CreateProcess(NULL, // use command line "C:\\WINDOWS\\system32\\mspaint.exe", // command line NULL, // don't inherit process handle NULL, // don't inherit thread handle FALSE, // disable handle inheritance 0, //no creation flags NULL, // use parent's environment block NULL, // use parent's existing directory &si, fprintf(stderr, "Create Process Failed"); return -1; } // parent will wait for the child to complete WaitForSingleObject(pi.hProcess, INFINITE); printf("Child Complete"); // close handles CloseHandle(pi.hProcess); CloseHandle(pi.hThread); }
The first two parameters passed to CreateProcess () are the application name and command line parameters.
If the application name is NULL, the command line parameter specifies the application to load. In this instance we are loading the Microsoft Windows mspaint.exe application.
Beyond these two initial parameters, we use the default parameters for inheriting process and thread handles as well as specifying no creation flags.