C Question: Using fork() to parallelize a function I\'ve been tasked with creati
ID: 3606398 • Letter: C
Question
C Question: Using fork() to parallelize a function
I've been tasked with creating a sorter that sorts multiple csv files in either a given directory or the current directory of the executable (depending on if a directory is specified) and outputs them to either a given directory or current one if none is specified.
The goal of this assignment is to induce parallelization through fork(), such that a process is created for every csv file that needs to be sorted.
However, I'm unsure fundamentally how to go about doing this, timing-wise. For example, searching the current directory for files, if I find a file, do I fork() and have the child process do the sorting while the parent process moves on to the next file (also to verify who's the parent and who's the child, do I simply do a comparison of the PID, where 0 is a child and a number >0 is a parent?), or will the child processes also have to launch their own child processes while inducing a recursive search?
Explanation / Answer
As far as the approach is concerned , following can be done:
Perform in the loop, looking for the csv files in the
current directory. As soon as a csv file is encoutered
fork a child and sort the file in that child and let the
parent continue the search. So the sorting activity or function
should be called in the child with filename as the argument.Yes
PID can be used to put the relevant codes in child (if pid == 0) for
child.No child process or fork needs ti be called from child
processes.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.