4_Processes

Describe what a process is in your own words Describe the role of the Operating System kernel in process creation Describe the differences between processes and threads, and list some advantages/disadvantages of each Understand that there are differences in process creation between different operating systems (you don’t need to know how to create them on each platform) Create a child process with the help of the course library
展开查看详情

1.Processes 1 Lecture 4: Processes Learning Goals Describe what a process is in your own words Describe the role of the Operating System kernel in process creation Describe the differences between processes and threads, and list some advantages/disadvantages of each Understand that there are differences in process creation between different operating systems (you don’t need to know how to create them on each platform) Create a child process with the help of the course library © Paul Davies, C. Antonio Sanchez. Not to be copied, used, or revised without explicit written permission from the copyright owner.

2.Process Processes 2 A complete program, consisting of one or more threads of execution, and an environment . Executable machine code Allocated memory block of virtually addressable memory (stack and heap) Operating-system-specific descriptors of resources (e.g. file descriptors, handles, data sources/sinks) Security attributes (e.g. process owner and permissions) A process can have child processes, which are spawned and run in parallel, each with its own environment.

3.Processes 3 Monolithic System Process Process Process Thread Thread Thread Process Process Process Thread Thread Thread Monolithic System Process Process Process Thread Thread Thread Process Process Process Thread Thread Thread System broken down into processes : Coarse grained or Process Granularity Processes broken down into threads : Fine grained or Thread Granularity No Granularity High Granularity Tending towards highly parallel programming with increasing data dependencies Towards Crude, single tasking systems running on 1 CPU/Core Multi-tasking systems Multi-threaded systems Application/Process/Thread Decomposition System broken down into processes Processes broken down into smaller processes Smaller Processes broken down into threads Threads broken down into smaller threads

4.Process Decomposition Processes 4 Scheduler Big Monolithic Single Tasking System Scheduler requests commands Process 1 Process 2 Process 3 Process 4 Big Monolithic Single Tasking System requests To : Multi-Tasking System From : Single-Tasking System Process Decomposition main() { } main() { } main() { } main() { } What guidelines should the programmer consider when decomposing a system? How many processes and threads should we create ?

5.Challenges with Processes Processes 5 Unlike threads within a single process, separate processes do not share address space . Process 1 Memory: Process 2 Memory: 0x10008000: FE 85 23 11 … 0x10008000: E5 64 32 F9 …

6.Challenges with Processes Processes 6 Unlike threads within a single process, separate processes do not share address space . Multi-tasking operating system kernel Support for process creation Mechanisms for inter-process communication , inter-process synchronization

7.Processes vs Threads Processes 7 If one process fails, others continue New process instances can be started manually, or outside of a main program Processes can be run on separate machines Advantages of Processes: Less efficient than threads due to additional overhead (more memory, state info) Communication/synchronization happens across process boundaries Disadvantages of Processes:

8.Processes 8 lpApplicationName needs to be full path to executable (or NULL) lpCommandLine will search the system PATH environment variable Needs to be non-const (is modified) Creating Processes (Windows)

9.Creating Processes (Windows) Processes 9 #include <windows.h> #include <iostream> int main() { STARTUPINFO si; PROCESS_INFORMATION pi; // initialize structures ZeroMemory ( &si, sizeof (si) ); si. cb = sizeof (si); ZeroMemory ( &pi, sizeof (pi) ); // create a variable string command-line const char cmd[] = "notepad.exe test.txt" ; char vcmd[ 80 ]; strcpy(vcmd, cmd); // Start the child process. CreateProcess ( NULL , // No module name (use command line) vcmd, // Command line NULL , // Process handle not inheritable NULL , // Thread handle not inheritable FALSE , // Set handle inheritance to FALSE 0 , // No creation flags NULL , // Use parents environment block NULL , // Use parents starting directory &si, // Pointer to STARTUPINFO structure &pi // Pointer to PROCESS_INFORMATION structure ); // Wait until child process exits. WaitForSingleObject( pi. hProcess , INFINITE ); // Close process and thread handles. CloseHandle( pi. hThread ); CloseHandle( pi. hProcess ); std ::cout << "done" << std ::endl; return 0 ; }

10.Creating Processes (OSX, Linux) Processes 10 fork : creates a new process by duplicating the calling process exec : replaces the current process with a new one waitpid : waits for a child process to receive an event (like terminating)

11.Creating Processes (OSX, Linux) Processes 11 #include <unistd.h> #include <sys/wait.h> #include <iostream> int main() { char * cmd[] = { "notepad.exe" , "test.txt" , NULL}; pid_t pid = fork(); if (pid == 0 ) { // child process execvp(cmd[ 0 ], cmd); // nothing beyond here gets executed } // wait for child process to complete int status; waitpid(pid, &status, 0 ); std::cout << "done" << std::endl; return 0 ; }

12.Creating Child Processes Processes 12 OS Kernel e.g. Windows, Linux , OSX + Multi Core CPU int main () { Statement A Statement B } int main () { Statement C Statement D } int main () { Statement E Statement F } int main () { CreateProcess( … ) ; CreateProcess( … ) ; CreateProcess( … ) ; } Program begins here Three calls to the OS Kernel requesting it to create and schedule 3 processes Scheduling

13.Library Abstractions PROCESSES 13 Calling OS kernel functions explicitly is undesirable. It ties our programs to a specific OS, making our code less portable . Some of the details can be abstracted away, wrapped in a library that provides a consistent interface . Child process creation, inter-process synchronization, inter-process communication is NOT part of any C++ standard. For ease-of-use and cross-platform portability, a CPEN333-specific library is provided. https://github.com/cpen333/library

14.Processes 14 OS Kernel e.g. Windows, Linux , OSX + Multi Core CPU int main () { Statement A Statement B } int main () { Statement C Statement D } int main () { Statement E Statement F } int main () { CreateProcess( … ) ; CreateProcess( … ) ; CreateProcess( … ) ; } Program begins here Scheduling CPEN333 Library Translation Layer Library Abstractions

15.Creating Child Process (CPEN333 Library) Processes 15 class subprocess { public : // Constructors subprocess( const std::string &cmd, bool start = true , bool detached = false ); subprocess( const std::vector<std::string> &exec, bool start = true , bool detached = false ); bool start(); // start subprocess bool join(); // wait for subprocess to complete bool terminate(); // force termination of process }; The course library introduces a subprocess class

16.Creating Child Process (CPEN333 Library) Processes 16 #include <cpen333/process/subprocess.h> #include <iostream> int main() { cpen333::process::subprocess proc( "notepad.exe test.txt" ); proc.join(); std::cout << "done" << std::endl; }

17.Creating Child Process (CPEN333 Library) Processes 17 #include <cpen333/process/subprocess.h> #include <vector> #include <string> #include <iostream> int main() { std :: vector < std :: string > exec = { "notepad.exe" , "filename with spaces.txt" }; cpen333 :: process :: subprocess proc2(exec); proc2.join(); std ::cout << "done" << std ::endl; }

18.Homework Processes 18 Download the course library and read/compile/run the examples in: examples/q0_basics examples/q1_threads examples/q2_processes https://github.com/cpen333/library Look up and learn about Resource acquisition is initialization (RAII)