ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
What if you have one webpage that is a cgi script and it uses the find command to dump a lot of info into a file. Then a couple lines later it reads from that file.
What if more than one person is on that same webpage? Could multiple instances of that cgi script be ran near simultaneously? I'm assuming if one instance was writing to the file then it would have a lock on it and the other instance wouldn't be able to open or write to it until the other was done. But what if one instance writes to it and during the split second between writing to and and then reading from it the other instance opens it and writes to it? Is that possible?
No. I mean like right there between your "find" and your "process" Where the "..." is. If it was a cgi script on a webpage or on a computer that multiple people were telneting into. So more than one person could be executing the script at the same time. After one instance of the script runs find whatnot >"$TempFile" could another instane of the script run find whatnot >"$TempFile" BEFORE the other instance of the script runs process $TempFile?
After reading around I think there's actually a thing called a "temporary file" and he used one in his pseudo code? It would get it's own unique name and no other instance of mycgi.cgi would mess with it. (sorry I'm not a programmer, maybe I shouldn't be posting here) I think he's telling me how a programmer worth his salt would avoid the issue in the first place.
But IF someone used a regular file could the issue of multiple instances of whatever.cgi messing with it at the same time arise? Like if you just had a file called temp.txt in the same directory as your script. Couldn't a cgi script open and close a file twice, the same as streams in a C++ program, and then maybe another instance of the same script opens the the file and alters it after the other instance closes the file but before it opens it the 2nd time?
$$ returns the pid (process ID) of your CGI script which should be unique if more then one person accesses the web page at the same time. It is not a random character generator.
There are many ways to create a random file name such as mktemp
Ordinarily, CGI processes don't use temporary files at all. When they do, there are established "correct" methods (such as mktemp()) for doing so without conflict. It is inadvisable to "roll your own."
Usually, a CGI process is "fast and quick," and it isn't responsible for any sort of "heavy-duty work" such as what might involve a temporary file. More time-consuming work is usually handled by background processes which receive work-requests and send results by some kind of a queue, e.g. a database table. (SQL "transactions" are then used to assure atomicity.)
So-called "race conditions" are always a concern.
Last edited by sundialsvcs; 02-27-2018 at 08:04 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.