Qiang has asked for the wisdom of the Perl Monks concerning the following question:

Hello, Monks I am having this weird problem with forking.

what i am doing during each child process is to chown directories with corresponding uid and gid and few other external commands.

to my surprise, the chown command in one child process changed the directory in other child process. I did `ls` to the working dir and you can see the directories' owner ship keep chaning. here is the demostration.

sunsplash:~# ls -l /home/grad/c total 28 drwx------ 3 angelop employee 4096 Jan 10 17:23 abunting drwx------ 3 angelop employee 4096 Jan 10 17:23 aer drwx------ 3 angelop employee 4096 Jan 10 17:23 alison3 drwx------ 3 angelop employee 4096 Jan 10 17:23 alitvak drwx------ 3 angelop employee 4096 Jan 10 17:23 angelad drwx------ 3 angelop employee 4096 Jan 10 17:23 angelop drwxr-xr-x 3 root root 4096 Jan 10 17:23 anistasi sunsplash:~# ls -l /home/grad/c total 48 drwx------ 3 bookoff misc 4096 Jan 10 17:23 abunting drwx------ 3 anistasi employee 4096 Jan 10 17:23 aer drwx------ 3 anistasi employee 4096 Jan 10 17:23 alison3 drwx------ 3 anistasi employee 4096 Jan 10 17:23 alitvak drwx------ 3 bookoff misc 4096 Jan 10 17:23 angelad drwx------ 3 anistasi employee 4096 Jan 10 17:23 angelop drwx------ 3 anistasi employee 4096 Jan 10 17:23 anistasi drwx------ 3 anistasi employee 4096 Jan 10 17:24 asantili drwx------ 3 anistasi employee 4096 Jan 10 17:24 banfield drwxr-xr-x 3 root root 4096 Jan 10 17:24 bbatke drwxr-xr-x 3 root root 4096 Jan 10 17:24 bigdaddy drwxr-xr-x 3 root root 4096 Jan 10 17:25 bookoff sunsplash:~# ls -l /home/grad/c total 48 drwx------ 3 bookoff misc 4096 Jan 10 17:23 abunting drwx------ 3 bigdaddy employee 4096 Jan 10 17:23 aer drwx------ 3 bigdaddy employee 4096 Jan 10 17:23 alison3 drwx------ 3 bigdaddy employee 4096 Jan 10 17:23 alitvak drwx------ 3 bookoff misc 4096 Jan 10 17:23 angelad drwx------ 3 anistasi employee 4096 Jan 10 17:23 angelop drwx------ 3 anistasi employee 4096 Jan 10 17:23 anistasi drwx------ 3 anistasi employee 4096 Jan 10 17:24 asantili drwx------ 3 anistasi employee 4096 Jan 10 17:24 banfield drwxr-xr-x 3 root root 4096 Jan 10 17:24 bbatke drwxr-xr-x 3 root root 4096 Jan 10 17:24 bigdaddy drwxr-xr-x 3 root root 4096 Jan 10 17:25 bookoff
#!/usr/bin/perl -w use strict; use lib "/home/qiang/source"; use Parallel::ForkManager; my $source = "/home/sta/"; my $target = "/home/grad/"; my @source_topdirs = map {$source . $_} ('a'..'z'); my @target_topdirs = map {$target . $_} ('a'..'z'); my $maildir = "Maildir"; my $convert_script = "/home/qiang/m.pl"; my $max_proc = 5; my $pm = new Parallel::ForkManager($max_proc); my $start = scalar localtime(time); foreach my $source_topdir ($source_topdirs[2]) { # usr names under /home/sta/[a-z] my @usr_names = map {chomp;$_} `ls $source_topdir`; # /home/grad/[a-z] my $target_topdir = $target_topdirs[2]; foreach my $usr_name (@usr_names) { my $source_usrdir = $source_topdir . "/" . $usr_name; my $target_usr_maildir = $target_topdir . "/" . $usr_name . "/ +" . $maildir; print "working on $source_usrdir \n"; $pm->start and next; # do the fork # prepare new user mail dir and convert old user dir into it. # &mkdirs_and_convert($target_topdir,$source_usrdir,$target_us +r_maildir); my ($uid,$gid) = (stat $source_usrdir)[4,5]; #print "uid: $uid gid: $gid source: $source_usrdir target: $targe +t_topdir\n"; `/bin/mkdir -p $target_usr_maildir && $convert_script -s $source_usrdir -R -d $target_usr_maildir & +& /bin/chown -R $uid:$gid $target_topdir && /bin/chmod -R 0700 $target_topdir`; $pm->finish; } } $pm->wait_all_children;
before I put all commands in seperate backtick and then I switched , put them in one in hope that would spawn one child and hence nothing would be shared externaly. any ideas ?

Replies are listed 'Best First'.
Re: weird kids process, why ?
by bluto (Curate) on Jan 10, 2005 at 23:31 UTC
    Perhaps I'm missing something, but shouldn't you chown/chmod $target_usr_maildir instead?

    FWIW, if you use this script more than once you really should check the results from backticks and system calls like 'stat'. Since recursive chowning is a pretty big hammer, you should probably make extra sure the values you are passing to it are computed properly.

      I finally look carefully at my code after I was being told second time about this.. I am shocked that I made this stupid mistake.

      sorry wasting everyone time. It was my prejudgement that forking caused this.

      because $target_usr_maildir is under $target_topdir, and $target_topdir is the user home dir.
      It should be set with the user ownership all the way down by doing
      /bin/chown -R $uid:$gid $target_topdir
Re: weird kids process, why ?
by jonadab (Parson) on Jan 10, 2005 at 23:53 UTC
    to my surprise, the chown command in one child process changed the directory in other child process. I did `ls` to the working dir and you can see the directories' owner ship keep chaning.

    Yes, that's what happens. File (and directory) permissions and ownerships are properties of the filesystem, which is shared by all processes. If you change the permissions or ownerships on a file (or on a directory), they do not just change for your process(es) only, but for everyone (assuming they change at all -- i.e., assuming you have the permissions needed to make the change). If you change them and then shut the computer down and turn it back on the next day, they'll *still* be whatever you changed them to, even though all of the processes from the previous day are gone, because the file permissions and ownerships are stored on disk, the same as the actual contents of the files are stored on disk.


    "In adjectives, with the addition of inflectional endings, a changeable long vowel (Qamets or Tsere) in an open, propretonic syllable will reduce to Vocal Shewa. This type of change occurs when the open, pretonic syllable of the masculine singular adjective becomes propretonic with the addition of inflectional endings."  — Pratico & Van Pelt, BBHG, p68
      If you change the permissions or ownerships on a file (or on a directory), they do not just change for your process(es) only, but for everyone
      I don't really follow this. I am trying to change ownership on the target directory recursively. but instead the target directory seems to be exist longer and shared into other process.

      why I am seeing the follwing result within 3 different 'ls' is baffling me.

      drwx------ 3 angelop employee 4096 Jan 10 17:23 aer drwx------ 3 anistasi employee 4096 Jan 10 17:23 aer drwx------ 3 bigdaddy employee 4096 Jan 10 17:23 aer
      thanks for the headup on stat and backtick, thought i am not sure the weridness may result by those but i know i should check the command status.