Simple fork examples

S

Simon

Hi guys!

I have read through the perldoc and must be really stupid.

I want to get my hands on some really basic example scripts of using fork,
and what it is used for.
Ive looked on the net and cant find too many examples of this anywhere, that
I can get a grip on.

Any information and/or scripts so I can start playing with this stuff and
learn it, instead of reading obscure doco would be great just to get a start
on it.

Thanku.
 
I

it_says_BALLS_on_your forehead

Hi guys!

I have read through the perldoc and must be really stupid.

I want to get my hands on some really basic example scripts of using fork,
and what it is used for.
Ive looked on the net and cant find too many examples of this anywhere, that
I can get a grip on.


use strict; use warnings;

$| = 1;

my $num_kids = shift;

my $pid;
for my $i ( 1.. $num_kids ) {
$pid = fork;
die "couldn't fork" if (!defined $pid);
if ( $pid == 0 ) {
print "$i about to sleep 3 secs...\n";
sleep 3;
print "$i done sleeping!\n";
exit;
}
print "pid '$pid'...i am parent\n";
}
do {
$pid = waitpid( -1, 0 );
} until $pid == -1;
 
S

Simon

Hi balls!
Mate thank u for that...I do feel dumb..probably am..just a newbie.
I ran your script, and got the following..


C:\>ballsonforehead.pl
Use of uninitialized value in foreach loop entry at C:\ballsonforehead.pl
line 9
..
Use of uninitialized value in foreach loop entry at C:\ballsonforehead.pl
line 9
..
 
Q

QoS

Simon said:
[snip]
I ran your script, and got the following..

C:\>ballsonforehead.pl
Use of uninitialized value in foreach loop entry at C:\ballsonforehead.pl
line 9
.
Use of uninitialized value in foreach loop entry at C:\ballsonforehead.pl
line 9
.

it_says_BALLS_on_your forehead said:
use strict; use warnings;

$| = 1;

my $num_kids = shift;

^
notice the shift does not supply a default.
you must start this script with a integer as an argument.


In addition, if you want the script to work on more platforms,
afaik it is better to use threads.
 
G

Gunnar Hjalmarsson

Simon said:
I want to get my hands on some really basic example scripts of using fork,
and what it is used for.

Check out the CPAN module Parallel::ForkManager. It's a useful tool, and
by reading its docs, including the examples, you may get a better
understanding of the concept as well.
 
M

Michele Dondi

I want to get my hands on some really basic example scripts of using fork,
and what it is used for.

It is used to fork() additional processes, which could be completely
different programs from your own, in which cases there are probably
easier means to do so, or "specialized versions" of the same program.
But seriously it's strange that you want to use this particular
function without knowing in advance what it is for. For example, I see
in

perldoc perlfunc

that there exists a shmctl function, but I do not wonder whether I
need it. In case I have a task for which someone will suggest the use
of it, I'll check it... but this has not happened yet...
Any information and/or scripts so I can start playing with this stuff and
learn it, instead of reading obscure doco would be great just to get a start
on it.

perl -e '{fork,redo}'

NO SERIOUSLY: DON'T DO IT!!

But then again, if you find the docs to be obscure you should ask
about why and where they are, because they seem rather clear to me.


Michele
 
M

Michele Dondi

Check out the CPAN module Parallel::ForkManager. It's a useful tool, and
by reading its docs, including the examples, you may get a better
understanding of the concept as well.

I'm not really sure... if he wants to understand the basic usage of
fork()... how a tool that hides and actually makes it more friendly
could help him. But worth mentioning, anyway.


Michele
 
S

Simon

Michele!!!

THANK YOU SO MUCH FOR THAT..IT WORKS NOW..

Thanks Michele!

Simon

Michele Dondi said:
C:\>ballsonforehead.pl
Use of uninitialized value in foreach loop entry at C:\ballsonforehead.pl
line 9 [...]
my $num_kids = shift;

Just make that

my $num_kids = shift || 5;


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
S

Simon

Hi Michele!

Again thank you so much for that.

The reason is is that Im learning perl from scratch at my place of work.

I may well have the modules misplaced, but a perl guru at work is running
perl scripts such as the following..

He kicks of a script that reads a text file containing 100s of machines.
The script might do the following...
Connect to each machine
Retrieve information about each machine
The script connects to around 20 machines at a time, so he has on his screen
20 command dos windows open simultaneously.
Id like to be able to do the same thing.
I have seen in his code either fork, or Spawn process or something similar,
but want to learn to do the same thing.

Really appreciate your help.

:)

Michele Dondi said:
I want to get my hands on some really basic example scripts of using fork,
and what it is used for.

It is used to fork() additional processes, which could be completely
different programs from your own, in which cases there are probably
easier means to do so, or "specialized versions" of the same program.
But seriously it's strange that you want to use this particular
function without knowing in advance what it is for. For example, I see
in

perldoc perlfunc

that there exists a shmctl function, but I do not wonder whether I
need it. In case I have a task for which someone will suggest the use
of it, I'll check it... but this has not happened yet...
Any information and/or scripts so I can start playing with this stuff and
learn it, instead of reading obscure doco would be great just to get a
start
on it.

perl -e '{fork,redo}'

NO SERIOUSLY: DON'T DO IT!!

But then again, if you find the docs to be obscure you should ask
about why and where they are, because they seem rather clear to me.


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
.'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
G

Gunnar Hjalmarsson

Michele said:
I'm not really sure... if he wants to understand the basic usage of
fork()... how a tool that hides and actually makes it more friendly
could help him.

Even if the code is 'hidden', sort of, the examples illustrate the use
of fork() in a context.
 
J

J. Gleixner

Simon said:
Hi Michele!

Again thank you so much for that.

The reason is is that Im learning perl from scratch at my place of work.

I may well have the modules misplaced, but a perl guru at work is running
perl scripts such as the following.. [...]
Id like to be able to do the same thing.
I have seen in his code either fork, or Spawn process or something similar,
but want to learn to do the same thing.

Why not ask your local perl guru for help? You'd probably learn things
much quicker.
 
S

Simon

Hi J!

Thanks for your input.

Logically this would be the first step....and yes logically I have tried
that.

But I dont know if you have, indeed I have, experienced my time in IT, that
some people are helpful, while others arent?

If you have experienced that scenario, then you would know why I chose to
come on here, just like everyone else, trying to ask some kind helpful
people out there to share the great knowledge they have, and indeed, in
time, to return that favour down the track.

Thanks for your comments.

S


J. Gleixner said:
Simon said:
Hi Michele!

Again thank you so much for that.

The reason is is that Im learning perl from scratch at my place of work.

I may well have the modules misplaced, but a perl guru at work is running
perl scripts such as the following.. [...]
Id like to be able to do the same thing.
I have seen in his code either fork, or Spawn process or something
similar, but want to learn to do the same thing.

Why not ask your local perl guru for help? You'd probably learn things
much quicker.
 
M

Martijn Lievaart

In addition, if you want the script to work on more platforms, afaik it
is better to use threads.

No, threads on Perl are different from threads in all other languages.
Unless you know the severe restrictions on threads (for starters, no
signals) you may think about using them. I decided against threads in all
programs I wrote when the question came up. Even started a thread about
it a while ago, which has lots of answers you never wanted to know, but
must know if you contemplate using threads.

M4
 
M

Michele Dondi

No, threads on Perl are different from threads in all other languages.
^^^
^^^

Hindi and Armenian included? :)
Unless you know the severe restrictions on threads (for starters, no
signals) you may think about using them. I decided against threads in all
programs I wrote when the question came up. Even started a thread about

Pun intended? :)


Michele
 
M

Michele Dondi

http://www.perlmonks.org/?node_id=580004
Randal: "If the answer is "threads", you asked the wrong question.".

: --
: A computer is a state machine.
: Threads are for people who can't program state machines.
: - Alan Cox
: --
: Threads are for people who have better things to do
: than program state machines.
: - renodino's amendment of Alan Cox's claim about threads...
: --
: Threads are for people who have better things to do than program state
: machines but instead prefer to waste time debugging concurrency and
: verification issues.
: - tirwhans's gloss of renodino's amendment.


Michele
 
R

rajendra

Hello All,

THis fork example is too good . can somebody explain me how this
works???....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top