You are on page 1of 56

Review Processes Practice!

Multi Processing

Tran Giang Son, tran-giang.son@usth.edu.vn

ICT Department, USTH

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 1 / 39


Review Processes Practice!

Review

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 2 / 39


Review Processes Practice!

Review

• Process
• Scheduling
• IO Redirection

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 3 / 39


Review Processes Practice!

Process

• What is process?
• Process vs program?

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 4 / 39


Review Processes Practice!

Process
• Process is a program in execution state

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 5 / 39


Review Processes Practice!

Process
• Process is a program in execution state (active)

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 5 / 39


Review Processes Practice!

Process
• Process is a program in execution state (active)
• Why process?
• Program is passive
• No execution → what’s running?

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 5 / 39


Review Processes Practice!

Process
• Process is a program in execution state (active)
• Why process?
• Program is passive
• No execution → what’s running?

• A process execution state contains

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 5 / 39


Review Processes Practice!

Process
• Process is a program in execution state (active)
• Why process?
• Program is passive
• No execution → what’s running?

• A process execution state contains


• Processor state (context)

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 5 / 39


Review Processes Practice!

Process
• Process is a program in execution state (active)
• Why process?
• Program is passive
• No execution → what’s running?

• A process execution state contains


• Processor state (context)
• File descriptors

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 5 / 39


Review Processes Practice!

Process
• Process is a program in execution state (active)
• Why process?
• Program is passive
• No execution → what’s running?

• A process execution state contains


• Processor state (context)
• File descriptors
• Memory allocation

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 5 / 39


Review Processes Practice!

Process
• Process is a program in execution state (active)
• Why process?
• Program is passive
• No execution → what’s running?

• A process execution state contains


• Processor state (context)
• File descriptors
• Memory allocation
• Process stack

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 5 / 39


Review Processes Practice!

Process
• Process is a program in execution state (active)
• Why process?
• Program is passive
• No execution → what’s running?

• A process execution state contains


• Processor state (context)
• File descriptors
• Memory allocation
• Process stack
• Data section

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 5 / 39


Review Processes Practice!

Process
• Process is a program in execution state (active)
• Why process?
• Program is passive
• No execution → what’s running?

• A process execution state contains


• Processor state (context)
• File descriptors
• Memory allocation
• Process stack
• Data section
• Heap

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 5 / 39


Review Processes Practice!

Process States
waiting

event finish event wait

interrupt

admitted exit
new ready running terminated

scheduled

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 6 / 39


Review Processes Practice!

Process States
waiting

event finish event wait

interrupt

admitted exit
new ready running terminated

scheduled

• new: process has just been created


• ready: waiting to be assigned (scheduled) to a processor
• running: it’s executing instructions
• waiting: waiting for some events to occur
• terminated: finished execution
Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 6 / 39
Review Processes Practice!

Process Creation

• Start a new process == Create a new process


• Create new child process
• Can create child process → grand child process

• Dependent on OS, parent and child can share


• All resources: opened files, devices, etc. . .
• Some resources: opened files only
• No resource

• A fully loaded system will have a process tree

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 7 / 39


Review Processes Practice!

Process Creation
$ pstree -A
init-+-acpid
|-cron
|-daemon---mpt-statusd---sleep
|-dbus-daemon
|-dovecot-+-anvil
| |-config
| `-log
|-master-+-pickup
| |-qmgr
| `-tlsmgr
|-mysqld_safe---mysqld---23*[{mysqld}]
|-php5-fpm---2*[php5-fpm]
|-proftpd
|-screen---bash---python2---{python2}
|-sshd-+-sshd---sshd---bash---pstree
| `-sshd---sshd
|-udevd---2*[udevd]
`-znc---{znc}
Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 8 / 39
Review Processes Practice!

Process Creation on Windows

BOOL WINAPI CreateProcess(


_In_opt_ LPCTSTR lpApplicationName,
_Inout_opt_ LPTSTR lpCommandLine,
_In_opt_ LPSECURITY_ATTRIBUTES lpProcessAttributes,
_In_opt_ LPSECURITY_ATTRIBUTES lpThreadAttributes,
_In_ BOOL bInheritHandles,
_In_ DWORD dwCreationFlags,
_In_opt_ LPVOID lpEnvironment,
_In_opt_ LPCTSTR lpCurrentDirectory,
_In_ LPSTARTUPINFO lpStartupInfo,
_Out_ LPPROCESS_INFORMATION lpProcessInformation
);
Source: MSDN

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 9 / 39


Review Processes Practice!

Process Creation on Windows

• A simplified WinAPI function:


UINT WINAPI WinExec(
_In_ LPCSTR lpCmdLine,
_In_ UINT uCmdShow
);

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 10 / 39


Review Processes Practice!

Process Creation on Windows

• A simplified WinAPI function:


UINT WINAPI WinExec(
_In_ LPCSTR lpCmdLine,
_In_ UINT uCmdShow
);
• It’s deprecated.
Source: MSDN

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 10 / 39


Review Processes Practice!

Process Creation on UNIX/Linux

• New processes are not created from scratch


• Two steps
• fork()
• exec()

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 11 / 39


Review Processes Practice!

Process Creation on UNIX/Linux

• New processes are not created from scratch


• Two steps
• fork()
• exec()
parent
wait()

fork()

exec() exit()
child

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 11 / 39


Review Processes Practice!

Process Creation on UNIX/Linux

• fork()
• Perfectly «clone» current process to a new process

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 12 / 39


Review Processes Practice!

Process Creation on UNIX/Linux

• fork()
• Perfectly «clone» current process to a new process
• Open files
• Register states
• Memory allocations
• Except process id

• Who’s who?
• Parent?
• Child?

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 12 / 39


Review Processes Practice!

Process Creation on UNIX/Linux

• fork()
• Perfectly «clone» current process to a new process
• Open files
• Register states
• Memory allocations
• Except process id

• Who’s who?
• Parent?
• Child?

pid_t fork(void);

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 12 / 39


Review Processes Practice!

Process Creation on UNIX/Linux


• Parent: fork() returns process id of child
• Child: fork() returns 0
• Example
#include <unistd.h>
#include <stdio.h>
int main() {
printf("Main before fork()\n");
int pid = fork();
if (pid == 0) printf("I am child after fork()\n");
else printf("I am parent after fork(), child is %d\n", pid);
return 0;
}

$ ./dofork
Main before fork()
I am parent after fork(), child is 2378
I am child after fork()
Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 13 / 39
Review Processes Practice!

Process Creation on UNIX/Linux

• exec()
• Load an executable binary to replace current process image
• A family of functions.
• Ask man

int execl(...);
int execle(...);
int execlp(...);
int execv(...);
int execvp(const char *file, char *const argv[]);
int execvP(...);

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 14 / 39


Review Processes Practice!

Process Creation on UNIX/Linux

• exec() example

#include <stdio.h>
#include <unistd.h>
int main() {
printf("Going to launch ps -ef\n");
char *args[]= { "/bin/ps", "-ef" , NULL};
execvp("/bin/ps", args);
return 0;
}

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 15 / 39


Review Processes Practice!

Scheduling

• Multiple processes running at the same time


• Process scheduler is a part that decides which processes to
be executed at a certain time.

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 16 / 39


Review Processes Practice!

Scheduling

• Maximize CPU usage


• Responsiveness for User interface
• Provide computational power for heavy-workload processes
• «Multitasking»
• Different characteristics of processes
• CPU bound: spends more time on computation
• I/O bound: spends more time on I/O devices
(reading/writing disk, printing. . . )

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 17 / 39


Review Processes Practice!

Scheduling

• By the ability to pause running processes


• Preemption: OS forcely pauses running processes
• Non-preemption (also cooperation): processes willing to
pause itself

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 18 / 39


Review Processes Practice!

Scheduling

• By the ability to pause running processes


• Preemption: OS forcely pauses running processes
• Non-preemption (also cooperation): processes willing to
pause itself
• By duration between each «switch»
• Short term scheduler: milliseconds (fast, responsive)
• Long term scheduler: seconds/minutes (batch jobs)

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 18 / 39


Review Processes Practice!

Scheduling with Context Switch


Process A Operating System Process B

Executing

Interrupt save state into PCB0

reload state from PCB1

Executing

save state into PCB1

Interrupt
reload state from PCB0

Executing
Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 19 / 39
Review Processes Practice!

Scheduling with Context Switch

• Switch between processes


• Save data of old process
• Load previously saved data of new process

• Context switch is overhead


• No work done for processes during context switch
• Time slice (time between each switch) is hardware-limited

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 20 / 39


Review Processes Practice!

Scheduling with Context Switch

Operating System Process A Process B Process C Process D

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 21 / 39


Review Processes Practice!

Scheduler

• Knowns
• List of processes
• Process states
• Accounting information

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 22 / 39


Review Processes Practice!

Scheduler

• Knowns
• List of processes
• Process states
• Accounting information

• Constraints
• Process priority (if any)
• Processes have scheduling priority
• Indicates the importance of each process
• Higher priority: more likely to be scheduled

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 22 / 39


Review Processes Practice!

Scheduler

• Problems
• P1: What processes to run next?

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 23 / 39


Review Processes Practice!

Scheduler

• Problems
• P1: What processes to run next?
• P2: How long should it run?

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 23 / 39


Review Processes Practice!

Scheduler

• Problem 1: What processes to run next?


• Job queue - set of all processes entering the system, stored
on disk
• Ready queue - set of all processes residing in main
memory, ready and waiting to execute
• Device queues - set of processes waiting for an I/O device
• Lists of PCBs
• Processes change state → they migrate among the various
queues

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 24 / 39


Review Processes Practice!

Scheduler

• Problem 2: How long should it run?


• First In First Served
• Earliest Deadline First
• Shortest Remaining Time
• Round Robin
• ...

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 25 / 39


Review Processes Practice!

Scheduler

Algorithm Preempt? Priority? Note

First Come, First Served No No Depends on arrival time


Shortest-Job-First No Yes Low waiting time ω
Shortest-Remaining-Time-First Yes Yes Preemptive SJF, low ω
Round Robin Yes No Low response time ρ
Multilevel Queue Depends Depends Several subqueues, permanent
Multilevel Feedback Queue Depends Depends Several subqueues, migrate

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 26 / 39


Review Processes Practice!

IO Redirection

esc

stdout (1)
F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12

~
`
!
1
@
2
#
3
$
4
%
5
^
6
&
7
*
8
(
9
)
0
_
-
+
= delete
stdin (0) Process
{ } |
Q W E R T Y U I O P [ ] \
tab

: enter

caps lock
A S D F G H J K L ; ‘ return

< > ?
Z X C V B N M , . /
shift shift

alt ⌘ ⌘ alt

fn control option command command option

stderr (2)

Default: input from keyboard and output to terminal

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 27 / 39


Review Processes Practice!

IO Redirection

esc

stdout (1)
F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12

~
`
!
1
@
2
#
3
$
4
%
5
^
6
&
7
*
8
(
9
)
0
_
-
+
= delete
stdin (0) Process file
{ } |
Q W E R T Y U I O P [ ] \
tab

: enter

caps lock
A S D F G H J K L ; ‘ return

< > ?
Z X C V B N M , . /
shift shift

alt ⌘ ⌘ alt

fn control option command command option

stderr (2)

Input from keyboard and output to file

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 28 / 39


Review Processes Practice!

IO Redirection

file stdin (0) Process stdout (1)

stderr (2)

Input from file and output to terminal

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 29 / 39


Review Processes Practice!

IO Redirection

file stdin (0) Process stdout (1) file

stderr (2)

Input from file and output to another file

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 30 / 39


Review Processes Practice!

IO Redirection

file stdin (0) Process 1 stdout (1) stdin (0) Process 2 stdout (1) file

stderr (2) stderr (2)

Input from file, pipe output of Process 1 to Process 2, output to


another file

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 31 / 39


Review Processes Practice!

Processes

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 32 / 39


Review Processes Practice!

Modules

• os
• subprocess

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 33 / 39


Review Processes Practice!

Task

• Create a process
• Run and wait for finish
• Run in background
• Run with timeout

• IO redirection
• Redirect input
• Redirect output
• Redirect with pipe

• Terminate
• Get return code

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 34 / 39


Review Processes Practice!

os module
• os module is deprecated in Python 3
• This is for references only.

Task How

Run and wait os.system("ps aux")


Run in background os.system("long_command.sh &")
Timeout N/A
Redirect input os.popen("bc", "w").write("1+2")
Redirect output print(os.popen("ps aux", "r").readlines())
Redirect with pipe os.pipe(), os.fork()
Terminate os.kill(pid, signal.SIGTERM)
Get return code return value of os.system()

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 35 / 39


Review Processes Practice!

subprocess module

Task How

Run and wait subprocess.run(["ps", "aux"])

Run in background subprocess.Popen("long_command.sh")

Timeout subprocess.run("long_command.sh", timeout = 10)

Redirect input subprocess.Popen("bc", stdin=subprocess.PIPE).communicate(b"3+4\n")

Redirect output subprocess.Popen(["ps", "aux"], stdout=subprocess.PIPE).communicate()

Redirect with pipe subprocess.Popen("bc", stdin=anotherProcess.stdout)

Terminate anotherProcess.terminate(), anotherProcess.kill()

Get return code subprocess.check_output(), catch CalledProcessError

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 36 / 39


Review Processes Practice!

Practice!

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 37 / 39


Review Processes Practice!

Practical work 7: Python shell

• Create a new python program, name it «7.shell.py»


• Make a shell
• User inputs command
• Shell executes the command, print output
• Support IO redirection
• input from file to process
• output from process to file
• e.g. input from one process being output of another

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 38 / 39


Review Processes Practice!

Practical work 7: Python shell

• Run it and test some commands


• ls -la
• ls -la > out.txt
• bc < input.txt
• ps aux | grep term

• Push your work to corresponding forked Github repository

Multi Processing Tran Giang Son, tran-giang.son@usth.edu.vn 39 / 39

You might also like