This is the html version of the file http://cs.ua.edu/325/Fall2002/lectures/lec24thruFinal/cs325Fall02lecture-25.ppt.
G o o g l e automatically generates html versions of documents as we crawl the web.
To link to or bookmark this page, use the following url: http://www.google.com/search?q=cache:JCR9v1XUCeEC:cs.ua.edu/325/Fall2002/lectures/lec24thruFinal/cs325Fall02lecture-25.ppt+CreateProcess+simple+example&hl=en&ie=UTF-8
Google is not affiliated with the authors of this page nor responsible for its content.
These search terms have been highlighted: createprocess simple example
Page 1
Lecture 25
Today
exec() in Unix
CreateProcess in Windows
Announcements
Page 2
Creating new processes
How do you ?replace? a running process with a new process (new set of instructions)
Use the exec function
First, create a second version of your process
via fork( )
Next, ?replace? the contents of this new process with another program
via exec( ?)
Page 3
Exec function
Exec comes in many flavors (see man exec)
We will use execl
Specify location of new process (1st arg)
Specify name of new process (2nd arg)
Specify any arguments passed to new process
Specify NULL to end parameter list (3rd arg here)
#include
#include
#include
int main( ) {
cout << "hello world" << endl;
int pid = fork();
cout<<"PID is "<
#include
int main(int argc, char *argv[ ]) {
// argv[0] is the program name
int num = atoi(argv[1]);
int loops = atoi(argv[2]);
for (int a=0; a
#include
#include
int main() {
cout << "hello world" << endl;
int pid = fork();
cout << ?PID is " << pid << endl;
if (pid == 0)
execl("./print", "print",
"1", "100", NULL);
else
execl("./print", "print",
"2", "100", NULL);
}
Page 6
Class Exercises
Run the example shown on the previous page
Compile print.C (from previous slide) into an executable ?print?
Compile the main program and run it
Try it printing each number
100 times
1000 times
10,000 times
100,000 times
Page 7
Creating new processes
Lots of applications where the ability to generate a new process comes in handy
Simple example: command shell in Unix
The ?shell? is a C++ program
Basic algorithm
Get a command from user
Interpret the command
Invoke a process to execute this command
Page 8
Implementing a simple shell
Overview
Prompt user
Get command
If not time-to-exit
Fork new process
Replace new process with either who, ls or uptime
Read next command
int main() {
int cmd, num;
cout << "Enter 1=who, 2=ls, 3=uptime -> ";
cin >> cmd;
while (cmd != 0) {
int pid = fork();
if (pid == 0) {
if (cmd==1) execl("/usr/bin/who", "who", NULL);
if (cmd==2) execl("/usr/bin/ls",
"ls", NULL);
if (cmd==3) execl("/usr/bin/uptime", "uptime",NULL);
exit(1);
}
cin >> cmd;
}
}
Page 9
Class Exercises
Implement the program shown on the previous slide. Run it, and watch its execution.
What happens when you give an invalid number?
Modify the program (add two more commands to the ?shell? we are writing), consider:
date
hostname
Page 10
Programs & Processes
We have seen how to write a program that
When executed, could invoke additional instances of itself (fork)
When executed, could then replace these additional instances with other programs ? replace the code in the executing process (exec)
Unix and Windows handle these basic tasks differently
Page 11
Creating processes in Windows
No exact Windows equivalent of fork and exec
Windows has CreateProcess method
Creates a new process and loads the specified program into that process (one step)
Requires #include
More parameters than fork & exec
Most can be NULL
Just need to specify program to run and a few flags
Page 12
Example using CreateProcess
Need two variables to handle basic info ? must initialize correctly
Specify the location of the new process (our ?print? process from last time)
Wait for it to finish
#include
#include
void main( ) {
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
if ( ! CreateProcess( NULL,
?..\\print\\debug\\print.exe 5 10",
NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi) )
cerr << "CreateProcess failed." << endl;
WaitForSingleObject( pi.hProcess, INFINITE );
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
Page 13
CreateProcess syntax
BOOL CreateProcess (
LPCTSTR lpApplicationName, // pointer to executable module
LPTSTR lpCommandLine, // pointer to command line string
LPSECURITY_ATTRIBUTES lpProcessAttrib, // process security
LPSECURITY_ATTRIBUTES lpThreadAttrib, // thread security
BOOL bInheritHandles, // handle inheritance flag
DWORD dwCreationFlags, // creation flags
LPVOID lpEnvironment, // pointer to new environment block
LPCTSTR lpCurrentDirectory, // pointer to current dir name
LPSTARTUPINFO lpStartupInfo, // pointer to STARTUPINFO
LPPROCESS_INFORMATION lpProcessInformation // pointer to PROCESS_INFORMATION
);
Page 14
Comments on CreateProcess
Can specify program in either 1st or 2nd args
If in first, give location of program to run
If in second, give the command line to execute
Creation flags
If 0, runs in existing window
Also have other flags (combine with | )
CREATE_NEW_CONSOLE probably most useful
Specify priority, linkage to parent, etc.
Structures pi and si used for process communication (how to start, basic info)
Page 15
Class Exercises
Write a program (in the Windows VC++ environment) that starts up a copy of notepad as part of its execution
Notepad is located in c:\winnt\ directory
Don?t forget to double the ?\? in string literal