Question: DEBUG HELP: fork() not creating child processes within helper method, but works fine in main? I'm trying to simulate a fork() step for a later

DEBUG HELP: fork() not creating child processes within helper method, but works fine in main?

I'm trying to simulate a fork() step for a later development stage in this program. However, when I try to fork() child processes, none of them created (evident by return of negative PIDs).

const char *get_filename_ext(const char *filename){ const char *dot = strrchr(filename, '.'); if(!dot | dot == filename) return ""; return dot+1; } void listdir(const char *name, int indent) { // CURRENTLY WORKS FOR DEFAULT DIRECTORY AND SUBSEQUENTIAL CHILD DIRECTORIES DIR *dir; struct dirent *entry; if(!(dir=opendir(name))) return; int pid; int i = 0; while((entry=readdir(dir))!=NULL) { if(entry->d_type==DT_DIR) { char path[1024]; if(strcmp(entry->d_name, ".")==0 || strcmp(entry->d_name, "..")==0){ continue; } pid = fork(); if(pid = 0){ printf(" DIRECTORY CHILD CREATED OF ID ", getpid()); snprintf(path, sizeof(path), "%s/%s", name, entry->d_name); printf("%*s[%s] ", indent, "", entry->d_name); listdir(path, indent+2); //printf(" EXACT PATH: %s ", path);mong }else{ printf(" Parent Communicating ID: %d "); } }else { if(strcmp(get_filename_ext(entry->d_name), "csv")==0) { printf( "CSV FOUND: %s ", entry->d_name); pid = fork(); if(pid = 0){ printf(" FILE CHILD CREATED OF ID ", getpid()); printf("%*s-%s ", indent, "", entry->d_name); }else{ printf(" Parent Communicating ID: %d "); } } } } closedir(dir); }

However, whenever I test fork() in main, it works perfectly fine. Why is this?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!