SlideShare a Scribd company logo
1 of 20
Using Fork and Pipe
Methods & Tools for Software Engineering (MTSE)
Fall 2020
Prof. Arie Gurfinkel
2
2
Additional Information
Advanced Linux Programming
• Chapter 2.1 (Interacting with Execution Environment)
• Chapter 3 (Processes)
• Chapter 5.4 (Pipes)
The book is available from the links below
https://github.com/MentorEmbedded/advancedlinuxprogramming/blob/g
h-pages/alp-folder/advanced-linux-programming.pdf
https://github.com/MentorEmbedded/advancedlinuxprogramming/tree/gh
-pages
3
3
PROCESS
4
4
What is a “Process”?
• What is a process:
• “A running instance of a program”
• Examples:
• Each of the two instances of Chrome
• The shell and the ls command executed, each is a process
• Advanced programmers use multiple processes to
• Do several tasks at once
• Increase robustness (one process fails, other still running)
• Make use of already-existing processes
5
5
Standard input, output, and error
• Let’s change stdin, stdout, and stderr
https://en.wikipedia.org/wiki/File:Stdstreams-notitle.svg
6
6
The “Guts” of a Process!
• The main components of a
process:
- An executable piece of code (a
program)
- Data that is input or output by the
program
- Execution context (information
about the program needed by
OS)
https://gabrieletolomei.wordpress.com/miscellanea/operating-systems/in-memory-layout/
Process of a 32bit architecture
7
7
Life Cycle of a (Unix) Process
Idle state when the process is creating
Ready ready to run
Run executing
Wait waiting for resources (CPU, disk, network, etc.)
Zombie ended, waiting to be collected
https://www.cs.miami.edu/home/burt/learning/Csc521.111/notes/process-life-cycle.html
Idle Ready Run
Wait
Zombie
8
8
Let’s Dissect a Process!
• Windows:
• Task manager
• Unix-like (Mac and Linux):
• In the terminal type:
• ps or top or htop
• ps –f for full details
9
9
UNIX Process Management
p i d = f o r k ( ) ;
i f ( p i d = = 0 )
e x e c ( . . . ) ;
e l s e
w a i t ( p i d ) ;
p i d = f o r k ( ) ;
i f ( p i d = = 0 )
e x e c ( . . . ) ;
e l s e
w a i t ( p i d ) ;
m a i n ( ) {
. . .
}
p i d = f o r k ( ) ;
i f ( p i d = = 0 )
e x e c ( . . . ) ;
e l s e
w a i t ( p i d ) ;
e x e c
f o r k
w a i t
https://ece.uwaterloo.ca/~smzahedi/crs/se350/slides/03-management.pdf
10
10
UNIX Process Management System Calls
fork()
• Create a copy of current process and start it as a child
execv() / execl() / …
• Load an executable into the current process and run it
wait() / waitpid()
• Wait for a child process to finish
kill()
• Send a signal (e.g., SIGTERM, SIGKILL, SIGINT) to another process
11
11
The Parent of a Process
• Each process (with some exceptions) has a parent process
(indicated by ppid – parent process identifier)
• Can we get this information within a program?
• YES!
• Use getpid() and getppid() libc functions defined in unistd.h
// c++ -o main main.cpp
#include <unistd.h>
#include <iostream>
int main(void) {
std::cout << "my pid: " << getpid()
<< " ppid: " << getppid() << "n";
}
12
12
Creating a Process
• Using a system
• Runs a shell (as a subprocess) to run the given commands
• Why using system is not recommended:
• The call to system relies on the installed shell
• It brings the shell’s:
• Features
• limitations
• Security flaws
13
13
Creating a Process - fork() system call
Forks an execution of the process
• after a call to fork(), a new process is created (called child)
• the original process (called parent) continues to execute concurrently
• in the parent, fork() returns the process id of the child that was created
• in the child, fork() return 0 to indicate that this is a child process
• The parent and child are independent
Man(ual) Page
• man 2 fork
14
14
exec() – executing a program in a process
exec() series of functions are used to start another program in the
current process
• after a call to exec() the current process is replaced with the image of the
specified program
• different versions allow for different ways to pass command line arguments
and environment settings
• int execv(const char *file, char *const argv[ ])
– file is a path to an executable
– argv is an array of arguments. By convention, argv[0] is the name of the
program being executed
Man page
• man 3 exec
15
15
kill() – sending a signal
A process can send a signal to any other process
• usually the parent process sends signals to its children
• int kill(pid_t pid, int sig)
– send a signal sig to a process pid
• useful signal: SIGTERM
– asks a process to terminate
When a parent process exits, the children processes are terminated
It’s a good practice to kill and wait for children to terminate before exiting
Man page
• man 2 kill
16
16
Signals
• A special message sent to a process
• Signals are asynchronous
• Different types of signals (defined in signum.h)
• SIGTERM: Termination
• SIGINT: Terminal interrupt (Ctrl+C)
• SIGKILL: Kill (can't be caught or ignored)
• SIGBUS: BUS error
• SIGSEGV: Invalid memory segment access
• SIGPIPE: Write on a pipe with no reader, Broken pipe
• SIGSTOP: Stop executing (can't be caught or ignored)
• Handling a signal:
• Default disposition
• Signal handler procedure
• Sending signal from one process to another process (SIGTERM,
SIGKILL)
17
17
waitpid() – Waiting for a child
A parent process can wait for a child process to terminate
• pid_t waitpid(pid_t pid, int *status, int options)
– block until the process with the specified pid terminates
– the return code from the terminating process is placed in status
– options control whether the function blocks or not
• 0 is a good choice for options
Man page
• man 2 waitpid
18
18
pipe() and dup2() – Inter-Process
Communication
pipe() creates a ONE directional pipe
• two file descriptors: one to write to and one to read from the pipe
• a process can use the pipe by itself, but this is unusual
• typically, a parent process creates a pipe and shares it with a child, or
between multiple children
• some processes read from it, and some write to it
– there can be multiple writers and multiple readers
• although multiple writers is more common
dup2() duplicates a file descriptor
• used to redirect standard input, standard output, and standard error to a pipe
(or another file)
• STDOUT_FILENO is the number of the standard output
Man pages
• man 2 pipe
• man 2 dup2
19
19
getopt() – processing CLI options
CLI – command line interface
At a start of the program, main(argc, argv) is called, where
• argc is the number of CLI arguments
• argv is an array of 0 terminated strings for arguments
– e.g., argv[0] is “foo”, argv[1] is “-s”, argv[2] is “-t”, argv[2] is “10”, …
getopt() is a library function to parse CLI arguments
• getopt(argc, argv, “st:”)
• input: arguments and a string describing desired format
• output: returns the next argument and an option value
• see example in using_getopt.cpp
$ foo –s –t 10 bar.txt baz.txt
flag
flag with an
argument
positional
argument
20
20
/dev/urandom – Really Random Numbers
/dev/urandom is a special file (device)
that provides supply of “truly” random
numbers
”infinite size file” – every read returns a
new random value
To get a random value, read a byte/word
from the file
see using_rand.cpp for an example
Have to use it for Assignment 3!
https://www.2uo.de/myths-about-urandom/

More Related Content

Similar to 04_ForkPipe.pptx

6. processes and threads
6. processes and threads6. processes and threads
6. processes and threadsMarian Marinov
 
Lecture2 process structure and programming
Lecture2   process structure and programmingLecture2   process structure and programming
Lecture2 process structure and programmingMohammed Farrag
 
Week1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC BeginWeek1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC Begin敬倫 林
 
3.5 create, monitor and kill processes v2
3.5 create, monitor and kill processes v23.5 create, monitor and kill processes v2
3.5 create, monitor and kill processes v2Acácio Oliveira
 
101 3.5 create, monitor and kill processes v2
101 3.5 create, monitor and kill processes v2101 3.5 create, monitor and kill processes v2
101 3.5 create, monitor and kill processes v2Acácio Oliveira
 
Lecture_Slide_4.pptx
Lecture_Slide_4.pptxLecture_Slide_4.pptx
Lecture_Slide_4.pptxDiptoRoy21
 
Process management
Process managementProcess management
Process managementBirju Tank
 
Unix Shell and System Boot Process
Unix Shell and System Boot ProcessUnix Shell and System Boot Process
Unix Shell and System Boot ProcessArvind Krishnaa
 
Processes in Linux.ppt
Processes in Linux.pptProcesses in Linux.ppt
Processes in Linux.pptjguuhxxxfp
 
Process (OS),GNU,Introduction to Linux oS
Process (OS),GNU,Introduction to Linux oSProcess (OS),GNU,Introduction to Linux oS
Process (OS),GNU,Introduction to Linux oSHarrytoye2
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File UploadJoe Suh
 
Reproducibility and automation of machine learning process
Reproducibility and automation of machine learning processReproducibility and automation of machine learning process
Reproducibility and automation of machine learning processDenis Dus
 
Input and Output Devices and Systems
Input and Output Devices and SystemsInput and Output Devices and Systems
Input and Output Devices and SystemsNajma Alam
 
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017OpenEBS
 

Similar to 04_ForkPipe.pptx (20)

6. processes and threads
6. processes and threads6. processes and threads
6. processes and threads
 
process creation OS
process creation OSprocess creation OS
process creation OS
 
Systemcall1
Systemcall1Systemcall1
Systemcall1
 
Lecture2 process structure and programming
Lecture2   process structure and programmingLecture2   process structure and programming
Lecture2 process structure and programming
 
Week1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC BeginWeek1 Electronic System-level ESL Design and SystemC Begin
Week1 Electronic System-level ESL Design and SystemC Begin
 
3.5 create, monitor and kill processes v2
3.5 create, monitor and kill processes v23.5 create, monitor and kill processes v2
3.5 create, monitor and kill processes v2
 
Os lectures
Os lecturesOs lectures
Os lectures
 
3 process management
3 process management3 process management
3 process management
 
Process management
Process managementProcess management
Process management
 
101 3.5 create, monitor and kill processes v2
101 3.5 create, monitor and kill processes v2101 3.5 create, monitor and kill processes v2
101 3.5 create, monitor and kill processes v2
 
Lecture_Slide_4.pptx
Lecture_Slide_4.pptxLecture_Slide_4.pptx
Lecture_Slide_4.pptx
 
Process management
Process managementProcess management
Process management
 
Unix Shell and System Boot Process
Unix Shell and System Boot ProcessUnix Shell and System Boot Process
Unix Shell and System Boot Process
 
Processes in Linux.ppt
Processes in Linux.pptProcesses in Linux.ppt
Processes in Linux.ppt
 
Process (OS),GNU,Introduction to Linux oS
Process (OS),GNU,Introduction to Linux oSProcess (OS),GNU,Introduction to Linux oS
Process (OS),GNU,Introduction to Linux oS
 
Mychurch File Upload
Mychurch File UploadMychurch File Upload
Mychurch File Upload
 
Reproducibility and automation of machine learning process
Reproducibility and automation of machine learning processReproducibility and automation of machine learning process
Reproducibility and automation of machine learning process
 
Linux Programming
Linux ProgrammingLinux Programming
Linux Programming
 
Input and Output Devices and Systems
Input and Output Devices and SystemsInput and Output Devices and Systems
Input and Output Devices and Systems
 
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
Dynamic Instrumentation- OpenEBS Golang Meetup July 2017
 

Recently uploaded

complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...asadnawaz62
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .Satyam Kumar
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfROCENODodongVILLACER
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfAsst.prof M.Gokilavani
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxKartikeyaDwivedi3
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage examplePragyanshuParadkar1
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 

Recently uploaded (20)

complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...complete construction, environmental and economics information of biomass com...
complete construction, environmental and economics information of biomass com...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
Churning of Butter, Factors affecting .
Churning of Butter, Factors affecting  .Churning of Butter, Factors affecting  .
Churning of Butter, Factors affecting .
 
Risk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdfRisk Assessment For Installation of Drainage Pipes.pdf
Risk Assessment For Installation of Drainage Pipes.pdf
 
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdfCCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
CCS355 Neural Networks & Deep Learning Unit 1 PDF notes with Question bank .pdf
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Concrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptxConcrete Mix Design - IS 10262-2019 - .pptx
Concrete Mix Design - IS 10262-2019 - .pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
DATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage exampleDATA ANALYTICS PPT definition usage example
DATA ANALYTICS PPT definition usage example
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 

04_ForkPipe.pptx

  • 1. Using Fork and Pipe Methods & Tools for Software Engineering (MTSE) Fall 2020 Prof. Arie Gurfinkel
  • 2. 2 2 Additional Information Advanced Linux Programming • Chapter 2.1 (Interacting with Execution Environment) • Chapter 3 (Processes) • Chapter 5.4 (Pipes) The book is available from the links below https://github.com/MentorEmbedded/advancedlinuxprogramming/blob/g h-pages/alp-folder/advanced-linux-programming.pdf https://github.com/MentorEmbedded/advancedlinuxprogramming/tree/gh -pages
  • 4. 4 4 What is a “Process”? • What is a process: • “A running instance of a program” • Examples: • Each of the two instances of Chrome • The shell and the ls command executed, each is a process • Advanced programmers use multiple processes to • Do several tasks at once • Increase robustness (one process fails, other still running) • Make use of already-existing processes
  • 5. 5 5 Standard input, output, and error • Let’s change stdin, stdout, and stderr https://en.wikipedia.org/wiki/File:Stdstreams-notitle.svg
  • 6. 6 6 The “Guts” of a Process! • The main components of a process: - An executable piece of code (a program) - Data that is input or output by the program - Execution context (information about the program needed by OS) https://gabrieletolomei.wordpress.com/miscellanea/operating-systems/in-memory-layout/ Process of a 32bit architecture
  • 7. 7 7 Life Cycle of a (Unix) Process Idle state when the process is creating Ready ready to run Run executing Wait waiting for resources (CPU, disk, network, etc.) Zombie ended, waiting to be collected https://www.cs.miami.edu/home/burt/learning/Csc521.111/notes/process-life-cycle.html Idle Ready Run Wait Zombie
  • 8. 8 8 Let’s Dissect a Process! • Windows: • Task manager • Unix-like (Mac and Linux): • In the terminal type: • ps or top or htop • ps –f for full details
  • 9. 9 9 UNIX Process Management p i d = f o r k ( ) ; i f ( p i d = = 0 ) e x e c ( . . . ) ; e l s e w a i t ( p i d ) ; p i d = f o r k ( ) ; i f ( p i d = = 0 ) e x e c ( . . . ) ; e l s e w a i t ( p i d ) ; m a i n ( ) { . . . } p i d = f o r k ( ) ; i f ( p i d = = 0 ) e x e c ( . . . ) ; e l s e w a i t ( p i d ) ; e x e c f o r k w a i t https://ece.uwaterloo.ca/~smzahedi/crs/se350/slides/03-management.pdf
  • 10. 10 10 UNIX Process Management System Calls fork() • Create a copy of current process and start it as a child execv() / execl() / … • Load an executable into the current process and run it wait() / waitpid() • Wait for a child process to finish kill() • Send a signal (e.g., SIGTERM, SIGKILL, SIGINT) to another process
  • 11. 11 11 The Parent of a Process • Each process (with some exceptions) has a parent process (indicated by ppid – parent process identifier) • Can we get this information within a program? • YES! • Use getpid() and getppid() libc functions defined in unistd.h // c++ -o main main.cpp #include <unistd.h> #include <iostream> int main(void) { std::cout << "my pid: " << getpid() << " ppid: " << getppid() << "n"; }
  • 12. 12 12 Creating a Process • Using a system • Runs a shell (as a subprocess) to run the given commands • Why using system is not recommended: • The call to system relies on the installed shell • It brings the shell’s: • Features • limitations • Security flaws
  • 13. 13 13 Creating a Process - fork() system call Forks an execution of the process • after a call to fork(), a new process is created (called child) • the original process (called parent) continues to execute concurrently • in the parent, fork() returns the process id of the child that was created • in the child, fork() return 0 to indicate that this is a child process • The parent and child are independent Man(ual) Page • man 2 fork
  • 14. 14 14 exec() – executing a program in a process exec() series of functions are used to start another program in the current process • after a call to exec() the current process is replaced with the image of the specified program • different versions allow for different ways to pass command line arguments and environment settings • int execv(const char *file, char *const argv[ ]) – file is a path to an executable – argv is an array of arguments. By convention, argv[0] is the name of the program being executed Man page • man 3 exec
  • 15. 15 15 kill() – sending a signal A process can send a signal to any other process • usually the parent process sends signals to its children • int kill(pid_t pid, int sig) – send a signal sig to a process pid • useful signal: SIGTERM – asks a process to terminate When a parent process exits, the children processes are terminated It’s a good practice to kill and wait for children to terminate before exiting Man page • man 2 kill
  • 16. 16 16 Signals • A special message sent to a process • Signals are asynchronous • Different types of signals (defined in signum.h) • SIGTERM: Termination • SIGINT: Terminal interrupt (Ctrl+C) • SIGKILL: Kill (can't be caught or ignored) • SIGBUS: BUS error • SIGSEGV: Invalid memory segment access • SIGPIPE: Write on a pipe with no reader, Broken pipe • SIGSTOP: Stop executing (can't be caught or ignored) • Handling a signal: • Default disposition • Signal handler procedure • Sending signal from one process to another process (SIGTERM, SIGKILL)
  • 17. 17 17 waitpid() – Waiting for a child A parent process can wait for a child process to terminate • pid_t waitpid(pid_t pid, int *status, int options) – block until the process with the specified pid terminates – the return code from the terminating process is placed in status – options control whether the function blocks or not • 0 is a good choice for options Man page • man 2 waitpid
  • 18. 18 18 pipe() and dup2() – Inter-Process Communication pipe() creates a ONE directional pipe • two file descriptors: one to write to and one to read from the pipe • a process can use the pipe by itself, but this is unusual • typically, a parent process creates a pipe and shares it with a child, or between multiple children • some processes read from it, and some write to it – there can be multiple writers and multiple readers • although multiple writers is more common dup2() duplicates a file descriptor • used to redirect standard input, standard output, and standard error to a pipe (or another file) • STDOUT_FILENO is the number of the standard output Man pages • man 2 pipe • man 2 dup2
  • 19. 19 19 getopt() – processing CLI options CLI – command line interface At a start of the program, main(argc, argv) is called, where • argc is the number of CLI arguments • argv is an array of 0 terminated strings for arguments – e.g., argv[0] is “foo”, argv[1] is “-s”, argv[2] is “-t”, argv[2] is “10”, … getopt() is a library function to parse CLI arguments • getopt(argc, argv, “st:”) • input: arguments and a string describing desired format • output: returns the next argument and an option value • see example in using_getopt.cpp $ foo –s –t 10 bar.txt baz.txt flag flag with an argument positional argument
  • 20. 20 20 /dev/urandom – Really Random Numbers /dev/urandom is a special file (device) that provides supply of “truly” random numbers ”infinite size file” – every read returns a new random value To get a random value, read a byte/word from the file see using_rand.cpp for an example Have to use it for Assignment 3! https://www.2uo.de/myths-about-urandom/

Editor's Notes

  1. - Use > for output and < for input - 0< for stdin 1> for stdout and 2> for stderr
  2. How is this used – typically, fork a process, child and parent are now both running the same program. One sets up the child program, and runs exec – becoming the new program The parent, usually, waits for the child to finish
  3. UNIX splits creating a process into two steps, each of them a lot simpler.
  4. Asynchronous: when a process receives a signal, it processes the signal immediately, without finishing the current function or even the current line of code.