Search code examples
cprocessforkls

Scanning a directory and processing specific files simultaneously with fork in C


My goal is to implement an "ls" function so that I can scan through a specified directory and look for .csv files to process. My thinking right now is to create a child process using fork() each time a csv file is detected using the ls function but I am very confused on how to do this. I have read the fork() page on geeksforgeeks and if I call fork for every time a csv file is detected, the child processes would keep multiplying by 2? And would I have to wrap my entire processing code in the fork statement? If I waited for the each child process to complete before creating another child, that wouldn't be multiprocessing. I'm still learning C and I'm finding it hard to grasp how the fork function actually works. Any help would be greatly appreciated, thank you.


Solution

  • The basic structure should look something like this:

    while(d = readdir(fd) {
        if (iscsv(d->d_name)) {
            int pid = fork();
            switch(pid) {
            case 0: // Child process
                process_csv(d->d_name);
                exit(0);
                break;
            case -1: // Error
                perror("fork");
                exit(1);
                break;
            default: // Parent
                printf("Forked PID %d to process %s\n", pid, d->d_name);
        }
    }
    

    The return value of pid is used to distinguish whether you're continuing in the parent or child. The child processes the file and exits, the parent continues looping. So you don't keep doubling processes (known as a fork bomb), because only the original parent keeps looping and forking.