Today - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Today

Description:

How do you 'replace' a running process with a new process (new set of instructions) ... First, create a second version of your process. via fork ... – PowerPoint PPT presentation

Number of Views:75
Avg rating:3.0/5.0
Slides: 16
Provided by: janett
Category:
Tags: compile | today

less

Transcript and Presenter's Notes

Title: Today


1
Lecture 25
  • Today
  • exec() in Unix
  • CreateProcess in Windows
  • Announcements

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( )

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 ltiostream.hgt
  • include ltstdlib.hgt
  • include ltunistd.hgt
  • int main( )
  • cout ltlt "hello world" ltlt endl
  • int pid fork()
  • coutltlt"PID is "ltltpidltltendl
  • if (pid 0)
  • execl("/usr/bin/ls","ls",NULL)
  • else
  • coutltlt"original proc"ltltendl

4
What happens with exec ?
  • Before fork( )
  • After fork( )
  • After exec( )
  • One process still contains the original code we
    started with
  • Other process now has completely different code

int main() int pid fork( ) if (pid
0) exec( )
int main() int pid fork( ) if (pid
0) exec( )
int main() int pid fork( ) if (pid
0) exec( )
int main() // another program // starts
at beginning
int main() int pid fork( ) if (pid
0) exec( )
5
Another example with exec
  • Consider the print routine shown below
  • Prints a number N times
  • include ltiostreamgt
  • include ltstdlib.hgt
  • int main(int argc, char argv )
  • // argv0 is the program name
  • int num atoi(argv1)
  • int loops atoi(argv2)
  • for (int a0 altloops a)
  • cout ltlt num ltlt " "
  • Use print in this code
  • include ltiostream.hgt
  • include ltstdlib.hgt
  • include ltunistd.hgt
  • int main() cout ltlt "hello world" ltlt endlint
    pid fork()cout ltlt PID is " ltlt pid ltlt
    endlif (pid 0)
  • execl("./print", "print", "1", "100",
    NULL)else
  • execl("./print", "print", "2", "100",
    NULL)

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

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

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, numcout ltlt "Enter 1who,
    2ls, 3uptime -gt "cin gtgt cmdwhile (cmd ! 0)
    int pid fork() if (pid 0) if
    (cmd1) execl("/usr/bin/who", "who",
    NULL) if (cmd2) execl("/usr/bin/ls", "l
    s", NULL) if (cmd3) execl("/usr/bin/uptime
    ", "uptime",NULL) exit(1) cin gtgt
    cmd

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

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

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 ltwindows.hgt
  • More parameters than fork exec
  • Most can be NULL
  • Just need to specify program to run and a few
    flags

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 ltwindows.hgt
  • include ltiostream.hgt
  • void main( ) STARTUPINFO siPROCESS_INFORMATION
    piZeroMemory( 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 ltlt
    "CreateProcess failed." ltlt endlWaitForSingleObje
    ct( pi.hProcess, INFINITE )CloseHandle(
    pi.hProcess )CloseHandle( pi.hThread )

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
  • )

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)

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
  • Dont forget to double the \ in string literal
Write a Comment
User Comments (0)
About PowerShow.com