31
PROGRAM INTERACTIONS ECE 650 - REZA BABAEE

ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROGRAM INTERACTIONSECE 650 - REZA BABAEE

Page 2: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

LEARNING OBJECTIVES

▸ Apply Unix-based system calls to interact with the execution environment

▸ Explain what a process and subprocess is and make programs that have multiple subprocesses

▸ Use Unix-based constructors to enable processes interact with one another

Today’s class

Page 3: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

REFERENCES

▸ Advanced Linux Programming

▸ Chapter 2.1 (Interacting with Execution Environment)

▸ Chapter 3 (Processes)

▸ Chapter 5.4 (Pipes)

Page 4: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

INTERACTING WITH THE EXECUTION ENVIRONMENT

https://scineadvisors.com/

Page 5: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

STANDARD INPUT, OUTPUT, AND ERROR

▸ Originally in C-family programming languages standard input, output and error

▸ Change them in the terminal

http

s://e

n.w

ikip

edia

.org

/wik

i/File

:Std

stre

ams-

notit

le.s

vg

Page 6: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

ARGUMENT TO THE main() FUNCTION

▸ If main() is a function what are the arguments for it:

▸ argc

▸ argv[]

▸ Who calls the main() function?

▸ Let’s define some arguments for our program!

$ foo –s –t 10 bar.txt baz.txt

switchswitch with

an argument

positional argument

Page 7: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROCESSING CLI OPTIONS

‣ getopt() is a library function to parse CLI arguments

‣ Usage:

‣ getopt(argc, argv, “st:”)

‣ input: arguments and a string describing the desired format

‣ output: returns the next argument and an option value

‣ see example in using_getopt.cpp

Page 8: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)
Page 9: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROCESS

http://www.pouet.net/prod.php?which=10782

Page 10: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROCESS

▸ What is a process:

▸ “A running instance of a program” (known to the OS)

▸ Examples:

▸ Each of the two running instances of Firefox

▸ 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

Page 11: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROCESS - COMPONENTS

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

Page 12: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROCESS - FIND THEM IN YOUR OS!

▸ Windows:

▸ Task manager

▸ Unix-like (Mac and Linux):

▸ In the terminal type:

- ps or top

- ps –f for full details

Page 13: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROCESS - HIERARCHY

▸ Each process (with some exceptions) has a parent process (indicated by ppid)

▸ Can we get this information within a program?

▸ YES!

▸ Use getpid() and getppid() from unistd.h

Page 14: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROCESS - KILL

▸ Run kill in the terminal (run kill with -KILL)

▸ What does kill really do?

http

s://w

ww

.redd

it.co

m/r/

linux

mas

terr

ace/

com

men

ts/3

y42q

z/ki

lling

_a_n

onre

spon

ding

_pro

cess

/

Page 15: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROCESS - CREATE

▸ Running a program will automatically create (at least) a process

▸ Creating process within your program:

▸ 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

Page 16: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROCESS - FORK

▸ Forks an execution of a process (creates a new copy 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() returns 0 to indicate that this is a child process

▸ The parent and child are independent

Page 17: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROCESS - EXEC

▸ 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

Page 18: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)
Page 19: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

https://toronto-employmentlawyer.com/break-workplaces/

Page 20: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

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

Page 21: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROCESS - RECEIVING/SENDING SIGNALS

▸ Receiving a signal:

▸ Default disposition

▸ Signal handler procedure

▸ Sending signal from one process to another process (SIGTERM, SIGKILL)

- Usually the parent process sends signals to its children

- int kill(pid_t pid, int sig)

- Send a signal sig to a process pid

‣ It’s a good practice to kill and wait for children to terminate before exiting

Page 22: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROCESS - TERMINATION

▸ What does it mean “a process terminates”?

▸ Exit codes:

▸ zero: successful termination

▸ non-zero: termination with an error

Page 23: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROCESS - ZOMBIE PROCESSES

▸ A zombie process is a process that exits and the parent is not handling the exit status!

Page 24: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PROCESS - WAITING FOR A CHILD!

▸ A parent process can wait for a child process to terminate

▸ pid_t waitpid(pid_t pid, int *stat_loc, int options)

▸ Blocks the parent until the process with the specified pid terminates

▸ The return code from the terminating process is placed in stat_loc

▸ options control whether the function blocks or not

▸ 0 is a good choice for options

Page 25: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)
Page 26: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

INTER-PROCESS COMMUNICATION

https://pxhere.com/en/photo/1449809

Page 27: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PIPELINE

▸ Unidirectional communication

▸ Serial devices: the read and write order are the same

▸ It synchronizes two processes

▸ In a shell use |

- Example: ls | less

https://en.wikipedia.org/wiki/Pipeline_(Unix)#/media/File:Pipeline.svg

Page 28: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

PIPE

▸ 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

Page 29: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

DUP

▸ 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

Page 30: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

POPEN/PCLOSE

▸ Combining pipe, fork, dup2, exec, and fdopen!

▸ The call to popen creates a child process executing the given command

▸ The second argument to peopen “w” or “r” indicates whether the process wants to read from or write to the child process

▸ Return value from popen is one end of a pipe

▸ The other end is connected to the standard input/output of the child process

▸ WARNING: popen uses shell to execute the given command!

Page 31: ECE 650 - REZA BABAEE PROGRAM INTERACTIONSrbabaeec/ece650/w20/assets/pdf/... · 2020. 2. 6. · PROCESS - FORK Forks an execution of a process (creates a new copy of the process)

FIFO

▸ For processes that are unrelated (no parent-child relationship)

▸ In the shell

▸ mkfifo

▸ In the program

▸ Open and use a fifo exactly like a file:

int fd = open (fifo_path, O_WRONLY);

write (fd, data, data_length);

close (fd);