recursion - Recursive multi-process program in c -
i working on simple programme should print out file tree given path. programme worked straight recursion when modified fork new kid process iterate on each new directory started getting weird output. appears processes beingness allowed move backwards parent directory.
i have been testing on little sample folder called test. test contains 2 files (testfile1 , testfile2) , folder (testsub). folder testsub contains 2 files (testsubfile1 , testsubfile2). far can tell, kid process supposed iterate through testsub folder doing , moving directory test folder , iterating through that.
#include <stdio.h> #include <stdlib.h> #include <sys/stat.h> #include <dirent.h> #include <string.h> #include <sys/wait.h> #include <sys/types.h> #include <unistd.h> int recfork(char *path){ /*int key = rand() % 113; printf("--------\n--------\n"); printf("new function\n"); printf("key: %d\n", key); printf("path: %s\npid: %d\n", path, getpid()); printf("--------\n--------\n");*/ int status; pid_t pid = 1; char name[1024]; struct stat statbuf; if(stat(path, &statbuf) == -1) homecoming -1; /* if item file */ if(s_isreg(statbuf.st_mode)){ printf("pid: %d ", getpid()); printf("%s\t%8ld\n", path, statbuf.st_size); } /* if item directory */ else if((statbuf.st_mode & s_ifmt) == s_ifdir){ pid = fork(); if(pid > 0){ //parent //printf("forked kid pid: %d\n", pid); waitpid(pid, &status, wuntraced); //printf("killed: %d\n", pid); } else if(pid == 0){ //child //printf("child: %d\n", getpid()); dir *dir; struct dirent *dp = null; if ((dir = opendir(path)) == null){ printf("cannot open %s\n", path); exit(exit_failure); } else{ printf("dir: %s/\n", path); while((dp = readdir(dir)) != null){ //printf("pid: %d key: %d dp = %s\n", getpid(), key, dp->d_name); if(strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) continue; sprintf(name, "%s/%s", path, dp->d_name); //printf("process: %d key: %d\n", getpid(), key); //printf("i passed: %s\n", path); //printf("calling recfork(%s)\n\n", name); recfork(name); } closedir(dir); } } else{ //failed fork printf("failed fork\n"); exit(exit_failure); } } //printf("returning : %d key: %d\n", getpid(), key); homecoming 0; } the code includes quite few commented out printf statements using seek , debug going on. i'm @ loss , don't know else ask. larn doing wrong if can point me in right direction much appreciated.
what's happening readdir() reading files in order come out of filesystem, has no relation how ls displays things. readdir() raw/unsorted output, while ls default sorts name.
e.g. on-disk file construction may this:
./test/. ./test/.. ./test/testfile1 ./test/testsub/. ./test/testsub/.. ./test/testfile2 since readdir returning in order it's stored in directory listing, you'll ., .., work on testfile, recursive testsub, go main test dir , work on testfile2.
and note you're not checking type-of-file. you'll attempting opendir() calls on actual files, fail, , cause particular process exit(). should checking filetypes returned stat , doing recursive calls when it's directory.
c recursion multiprocess
No comments:
Post a Comment