LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2014, 11:00 AM   #1
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Rep: Reputation: 135Reputation: 135
Reading 2 files and process both at same time


I have a perl script which takes a file as input, read it and process it.

Can I give 2 files as input and process them at same time rather than process them one after another means when the perl script starts it forks itself and process the two files with different child at same time.
 
Old 03-18-2014, 11:11 AM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
does this demostration help you:
Code:
[schneidz@hyper ~]$ touch hello world
[schneidz@hyper ~]$ ll hello & ll world
[1] 22604
-rw-rw-r--. 1 schneidz schneidz 0 Mar 18 12:09 world
-rw-rw-r--. 1 schneidz schneidz 0 Mar 18 12:09 hello
you can put one instance of your perl program in the background and run against one input file and at the same time execute another instance with another input file.

Last edited by schneidz; 03-18-2014 at 11:13 AM.
 
Old 03-18-2014, 11:44 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
What do you really want to do? Merge two or more files?
 
Old 03-18-2014, 01:27 PM   #4
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by NevemTeve View Post
What do you really want to do? Merge two or more files?
Hi Nevemteve, thanks for the reply. I dont want to merge. When I said two files means they are two different category of files.
e.g. one file is storage related, one file is server related and each file has thousands of entry in it.

The perl script read each file line by line and process them.
What I want to do is, if I am merging the content, it will take huge time to process the other category of file as the processing is serial.
So I categorized two different files and want to process them in parallel manner. As I said earlier, in someway to fork the script for two child script and each child will process separate files paralelly.

And can this be done from inside the script or outside the script ??
 
Old 03-18-2014, 01:29 PM   #5
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by schneidz View Post
does this demostration help you:
Code:
[schneidz@hyper ~]$ touch hello world
[schneidz@hyper ~]$ ll hello & ll world
[1] 22604
-rw-rw-r--. 1 schneidz schneidz 0 Mar 18 12:09 world
-rw-rw-r--. 1 schneidz schneidz 0 Mar 18 12:09 hello
you can put one instance of your perl program in the background and run against one input file and at the same time execute another instance with another input file.
HI schneidz. Thanks for the reply. The problem is I am not allowed to run two instance of the script for two different file. I have to handle both files with a single instance of the script.
 
Old 03-18-2014, 01:29 PM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
^ my previous suggestion was to just run it twice at the same time (calling different input files).


edit: ^ thats a weird requirement... that limitation mite be in place so that 1 person doesnt spam the cpu. not knowing the source of your script it would be hard to edit it but maybe you can have a function that is called twice with 2 different inputs.

Last edited by schneidz; 03-18-2014 at 01:32 PM.
 
Old 03-18-2014, 01:43 PM   #7
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by schneidz View Post
^ my previous suggestion was to just run it twice at the same time (calling different input files).


edit: ^ thats a weird requirement... that limitation mite be in place so that 1 person doesnt spam the cpu. not knowing the source of your script it would be hard to edit it but maybe you can have a function that is called twice with 2 different inputs.
Dedicated cpu is there to process the perl script, as the processing is massive so spamming is not the concern for now, may be in future as the file size is growing and I am considering this threading approach for that.

So as per your idea the whole file processing programme in the script will be in one function and it will be called for each file ..right ???
 
Old 03-18-2014, 01:45 PM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
i got another idea... if your script can take input from a pipe, maybe you can feed your script like so:
Code:
(cat file.1 & cat file.2) | divya.pl
 
Old 03-19-2014, 12:38 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP: So your question is: How to fork in perl-script? Tried perldoc -f fork yet?

Edit: I have just found and old example-code of mine. (Be vareful, it's quite dusty.)
Code:
#!/usr/local/bin/perl -w

use strict;

sub child_process {
    printf STDERR "child %s started param=%s\n", $$, $_[0];
    sleep (2);
    printf STDERR "child %s exiting\n", $$;
}

sub main {
    my $pid;

    $pid = fork;
    if (!$pid) {
	child_process ("ChildParam #1");
	exit (0);
    }

    $pid = fork;
    if (!$pid) {
	child_process ("ChildParam #2");
	exit (0);
    }
    wait;
}

main;

Last edited by NevemTeve; 03-19-2014 at 06:12 AM.
 
2 members found this post helpful.
Old 03-19-2014, 12:08 PM   #10
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
NevemTeve, thanks for the nice example. I will try that in my perl script and post the response.
 
Old 03-19-2014, 01:02 PM   #11
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by NevemTeve View Post
@OP: So your question is: How to fork in perl-script? Tried perldoc -f fork yet?

Edit: I have just found and old example-code of mine. (Be vareful, it's quite dusty.)
NevemTeve, I have one simple query, what is the role of sleep and wait here with fork.

Because the script is behaving differently with presence and absence of both.

Sometimes both childs started at same time and ended at same time, sometimes one child start & exit and then other child start & exit.
On which basis this behaviour is changing ?
 
Old 03-19-2014, 01:25 PM   #12
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
The children run as they want (can), the only thing you can do for them is waiting for them to terminate before exiting the parent.

Let's not forget that the child-processes (or the child and the parent) are supposed to do unrelated jobs, otherwise there is no point in using them.

PS: I should have used two wait's for the two children.
Code:
    $pid= wait;
    printf STDERR "wait#1 returned %d\n", $pid;

    $pid= wait;
    printf STDERR "wait#2 returned %d\n", $pid;
 
Old 03-19-2014, 11:53 PM   #13
divyashree
Senior Member
 
Registered: Apr 2007
Location: Bangalore, India
Distribution: RHEL,SuSE,CentOS,Fedora,Ubuntu
Posts: 1,386

Original Poster
Rep: Reputation: 135Reputation: 135
I modified the childs to read files from argument as below:

Code:
#!/usr/bin/perl

use warnings;
use strict;

my $REQPARAM = 4;
$#ARGV += 1;
unless ($#ARGV == 4) {
        printf "$0 requires minimum 4  arguments \n";
        printf "Usage: $0 -F1 <File 1> -F2 <File 2>\n";
        exit 100;
        }
else {
        main();

        }

#sub child_process {
#    printf STDERR "child %s started param=%s\n", $$, $_[0];
#    sleep (1);
#    printf STDERR "child %s exiting\n", $$;
#}

sub main {
    my $pid;

    $pid = fork;
    if (!$pid) {
        readfile ("$ARGV[1]");
        exit (0);
    }

    $pid = fork;
    if (!$pid) {
        readfile ("$ARGV[3]");
        exit (0);
    }
    wait;
}



sub readfile {

printf STDERR "child %s started param=%s\n", $$, $_[0];
foreach my $arg (@_){
        if (-e $_[0]){
        open FILE , '<'.$_[0]  or die $!;
        while (<FILE>){
                print "$_";
                }
            }
        }
sleep(2);
printf STDERR "child %s exiting\n", $$;
}
But here what is happening is, when readfile is called, if wrong arguments are provided its not throwing error of file not found and childs are simple starting and exiting.
 
Old 03-20-2014, 03:49 AM   #14
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,864
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
That's what debugging is good for...
Code:
#!/usr/bin/perl

use warnings;
use strict;

my $REQPARAM = 4;
$#ARGV += 1;
unless ($#ARGV == 4) {
        printf "$0 requires minimum 4  arguments \n";
        printf "Usage: $0 -F1 <File 1> -F2 <File 2>\n";
        exit 100;
        }
else {
        main();

        }

sub main {
    my ($pid, $childret);

    $pid = fork;
    if (!$pid) {
        readfile ("$ARGV[1]");
        exit (0);
    }

    $pid = fork;
    if (!$pid) {
        readfile ("$ARGV[3]");
        exit (0);
    }
    $pid= wait; $childret= $?; 
    printf STDERR "wait returned %d (exit-status=%d)\n", $pid, $childret;

    $pid= wait; $childret= $?; 
    printf STDERR "wait returned %d (exit-status=%d)\n", $pid, $childret;
}

sub readfile {
    printf STDERR "child %s started param=%s\n", $$, $_[0];
    foreach my $arg (@_) {
        open FILE , '<'.$_[0]  or die "*** Open error in $_[0]";
        while (<FILE>){
            print "$_";
        }
    }
    printf STDERR "child %s exiting\n", $$;
    exit (0);
}
 
Old 03-20-2014, 07:32 AM   #15
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
In any shell:

Code:
perl script-one.pl &
perl script-two.pl &
The shell-prompt returns immediately as the two programs are launched, as what are called "jobs," in the background of your terminal session. (So, they are not true "batch jobs.") Then use the jobs command to watch the parallel completion of the two commands that you launched as independent children of the shell by means of the "&" suffix. Use fg and bg to reconnect to either one. Also see nohup.

If you have many files to process, check out the -p numprocs argument of the xargs command.

A general notion of Unix-ish systems is that commands ought to be simple, and fairly self-centered. Then, you get extra mileage out of them by simple shell features like these, and by "piping" multiple commands together so that the output of one becomes the input to another.

It's a "disarmingly simple" idea, but it greatly reduces the complexity. Yes, "Perl can do anything you want." But maybe you can remove the complexity of "parallelism" from the program (regardless of language used), and move it up to the shell.

Last edited by sundialsvcs; 03-20-2014 at 07:36 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] total process time and time in present state(sleep/running/ uninterruptible). satyadev75 Linux - Server 1 05-16-2013 06:03 AM
[SOLVED] How to save the pid of a process at the time of process startup jhonnappier2007 Linux - Newbie 13 11-20-2011 11:54 PM
scanf reading newline into char array while reading 1 char at a time austinium Programming 6 09-26-2010 11:27 PM
Shell script for reading a particular process contineously from process table vinaykori Linux - General 2 05-29-2009 06:52 AM
Reading Files in USB during boot time pistolguy Programming 2 03-02-2006 11:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:14 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration