Linux Fork/Exec Example & Windows Process Creation

Linux Fork/Exec Example & Windows Process Creation
Slide Note
Embed
Share

In this context, learn about the process creation techniques using fork/exec in Linux and CreateProcess in Windows. Understand the concepts of threads, processes, and dispatching in the context of CS 111 Lecture Notes. Explore the differences between processes and threads, including their relationship, state-sharing capabilities, and context switching mechanisms.

  • Linux
  • Windows
  • Process Creation
  • Threads
  • CS 111 Lecture

Uploaded on Mar 03, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Linux Fork/Exec Example int child_pid_or_zero = fork(); if (child_pid_or_zero == 0) { execvp( ls , argv); } else { waitpid(child_pid_or_zero, &status, options); }; Child process Parent process CS 111 Lecture Notes: Threads, Processes, and Dispatching Slide 1

  2. Windows Process Creation BOOL CreateProcess( LPCTSTR lpApplicationName, LPTSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES lpThreadAttributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCTSTR lpCurrentDirectory, LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMATION lpProcessInformation ); WaitForSingleObject(lpProcessInformation->hProcess, INFINITE); CS 111 Lecture Notes: Threads, Processes, and Dispatching Slide 2

  3. Thread Creation #include <thread> ... std::thread t(func); ... t.join(); void func() { /* This code will run concurrently with the code to the left. */ ... } CS 111 Lecture Notes: Threads, Processes, and Dispatching Slide 3

  4. Processes vs. Threads Process Threads A thread is part of a process: A process can contain many threads A process also contains state x y z Variables and other state std:cin std:cout CS 111 Lecture Notes: Threads, Processes, and Dispatching Slide 4

  5. Create New Thread New thread shares state with existing threads Process Process new std::thread(...) x y z x y z std:cin std:cout std:cin std:cout CS 111 Lecture Notes: Threads, Processes, and Dispatching Slide 5

  6. Create New Thread Parent Process x y z fork() std:cin std:cout x y z std:cin std:cout Child Child starts with one thread x y z Child s state is a copy of parent s No sharing std:cin std:cout CS 111 Lecture Notes: Threads, Processes, and Dispatching Slide 6

  7. Context Switch Process A Control Block Process B Control Block thread state Hardware Registers Threads Threads Core thread state A1 B1 A2 A3 R0 R1 SP RN SP A3 Stack B1 Stack CS 111 Lecture Notes: Threads, Processes, and Dispatching Slide 7

  8. Context Switch Process A Control Block Process B Control Block thread state Hardware Registers Threads Threads Core thread state A1 B1 A2 A3 R0 R1 SP RN SP Saved Registers (all but SP) A3 Stack B1 Stack CS 111 Lecture Notes: Threads, Processes, and Dispatching Slide 8

  9. Context Switch Process A Control Block Process B Control Block thread state Hardware Registers Threads Threads Core thread state A1 B1 A2 A3 R0 R1 SP RN SP SP A3 Stack B1 Stack CS 111 Lecture Notes: Threads, Processes, and Dispatching Slide 9

  10. Context Switch Process A Control Block Process B Control Block thread state Hardware Registers Threads Threads Core thread state A1 B1 A2 A3 R0 R1 SP RN SP SP A3 Stack B1 Stack CS 111 Lecture Notes: Threads, Processes, and Dispatching Slide 10

  11. Context Switch thread state Hardware Registers Threads Threads Core thread state A1 B1 A2 A3 R0 R1 RN SP Process A Control Block Process B Control Block SP A3 Stack B1 Stack CS 111 Lecture Notes: Threads, Processes, and Dispatching Slide 11

More Related Content