IPC USING PIPE ET TU, BRUTE
• • • •
Pipe.
Forking with Pipes (IPC). Read/write pipe. Process-level parallelism.
LEARNING OBJECTIVES
• • • •
You are given an encrypted file.
You are given an function that can decrypt a line(string). Decrypt the whole file.
Decrypt the whole file really fast.
MISSION
SINGLE-PROCESS VERSION
FILE
Read
Traverse each line
call function
Line 1 Line 2 Line 3 Line 4 Line 5 ….
… Line n
get_most_likely_printout(line x)
Process
Line 1 Line 2 Line 3 Line 4 Line 5 ….
… Line n
Read
Create Children
pid1 pid2
pidn
pid1 pid2
pidn
Children Decrypt
get…(line x) get…(line x)
get…(line x)
MULTI-PROCESS VERSION STEP I
FILE
Parent
MULTI-PROCESS VERSION STEP 2
Write to pipe
Children Decrypt
get…(line x) get…(line x)
Read from Pipe
Line 1 Line 2 Line 3 Line 4 Line 5 ….
… Line n
pid1 pid2
pidn
pipe 1 pipe 2
Print Output
Parent
get…(line x)
pipe n
WHAT MAY BE A PROBLEM
pid1 pid2
get…(line x)
get…(line x)
get…(line x)
pipe 1
pipe 2
pipe n
waitpid read
Won’t return!
Wait for parent
Wont read! Waitpid for child 2
Children Return
pidn
Parent
•
•
•
•
SOLUTION
In order to make writing to pipes never blocked… Adjust the size of pipe by using fcntl
How do I know the size of longest line You need argparse
Get #lines in file
Get #bytes of longest line —> Where you can adjust your pipe
Get ptr to input/output file.
• •
•
HANDLE ERROR CASES
• Try to make your code more robust.
What if …
•
• Input file is not readable
Pipe get destroyed during the write/read How do you test your program?
•
•
• Make sure you read lines correctly
•
Make sure you collect (waitpid) all children properly
•
• Make sure you close pipe when you have to
Make sure there is no deadlock in you program • And many other things.
Writing test cases to test your own program is critical to make yourself a good developer!
•