paulj
paulj

Reputation: 337

How does system command behave under Perl and CGI

I have a Perl script under cgi with the following:

system ("echo mystuff > myfile.txt")

If I hit this with two requests simultaneously, do both requests try to write to myfile, or does it block since system causes a fork and pauses parent process until child finishes?

Upvotes: 1

Views: 109

Answers (2)

redneb
redneb

Reputation: 23870

If the script is executed twice in parallel, then both instance of it will call the system command and it is possible that the output of the echo will get garbled. This happen under CGI or if you run the script directly in the command line. If you don't like that, you can use something like flock to ensure that only one process is ran at a time, although you should be very careful if you do that.

Upvotes: 2

hobbs
hobbs

Reputation: 240049

With conventional CGI, your two requests will be run in two different processes. Neither will block the other. If you are unlucky (or have enough traffic), race conditions might garble the contents of myfile.txt.

Upvotes: 1

Related Questions