PROCESS CREATION (CONT.) o Address space Child duplicate of parent 。 Child has a program loaded into it o UNIX examples fork system call creates new process exec system call used after a fork to replace the process'memory space with a new program
PROCESS CREATION (CONT.) Address space Child duplicate of parent Child has a program loaded into it UNIX examples fork system call creates new process exec system call used after a fork to replace the process’ memory space with a new program
PROCESS CREATION parent resumes wait fork() child exec() exit()
PROCESS CREATION
PROCESS CREATION IN POSIX #include <sys/types.h> #include <stdio.h> #include <unistd.h> int main() L pid_t pid; /fork a child process * pid fork(); if (pid 0){/*error occurred * fprintf(stderr,"Fork Failed"); exit(-1); else if (pid ==0){/child process * execlp("/bin/1s","1s",NULL); else {/parent process * /parent will wait for the child to complete * wait(NULL) printf("Child Complete"); exit(O);
PROCESS CREATION IN POSIX
PROCESS CREATION IN WIN32 #include <stdio.h> #include <windows.h> int main(VOID) STARTUPINFO si; PROCESS_INFORMATION pi; /allocate merory 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 虚51, Epi)) fprintf(stderr,"Create Process Failed"); return -1; 7/parent will vait for the child to complete WaitForsingleObject(pi.hProcess,INFINITE); printf("Child Complete"); /close handles CloseHandle(pi.hProcess); CloseHandle(pi.hThread);
PROCESS CREATION IN WIN32
PROCESS CREATION IN JAVA import java.io.*i public class OSProcess public static void main(String[]args)throws IOException{ if (args.length !1){ System.err.println("Usage:java OSProcess <command>"); System.exit(0); } /args [o]is the command ProcessBuilder pb new ProcessBuilder(args[O]); Process proc pb.start(); /obtain the input stream InputStream is proc.getInputStream(); InputStreamReader isr nev InputStreamReader(is); BufferedReader br nev BufferedReader(isr); /read what is returned by the command String line; while (line br.readLine())!null) System.out.println(line); br.close();
PROCESS CREATION IN JAVA