LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-24-2018, 06:14 AM   #1
icecubeflower
Member
 
Registered: Mar 2008
Location: USA
Distribution: Slackware 13.1
Posts: 313

Rep: Reputation: 34
cgi


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?
 
Old 02-24-2018, 09:13 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Here comes magic:
Code:
$ cat mycgi.cgi
#!/bin/sh

TempFile="/tmp/cgitmp.$$"

find whatnot >"$TempFile"

...

process $TempFile

rm -- "$TempFile"
 
2 members found this post helpful.
Old 02-24-2018, 08:37 PM   #3
icecubeflower
Member
 
Registered: Mar 2008
Location: USA
Distribution: Slackware 13.1
Posts: 313

Original Poster
Rep: Reputation: 34
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?
 
Old 02-25-2018, 01:42 AM   #4
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,264
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
Look closely at the pseudo code NevemTeve has provided.

Hint: Different users will not be working with the same temporary file.
 
1 members found this post helpful.
Old 02-25-2018, 08:27 AM   #5
icecubeflower
Member
 
Registered: Mar 2008
Location: USA
Distribution: Slackware 13.1
Posts: 313

Original Poster
Rep: Reputation: 34
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?
 
Old 02-25-2018, 12:15 PM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Well, $$ in the filename will be replaced with a (quite) uniq number.

If the questions are 'Can two or more process write the same file? Can it be source of problems?' then both answer is 'Yes'
 
1 members found this post helpful.
Old 02-25-2018, 06:46 PM   #7
icecubeflower
Member
 
Registered: Mar 2008
Location: USA
Distribution: Slackware 13.1
Posts: 313

Original Poster
Rep: Reputation: 34
thx.
 
Old 02-26-2018, 06:16 AM   #8
icecubeflower
Member
 
Registered: Mar 2008
Location: USA
Distribution: Slackware 13.1
Posts: 313

Original Poster
Rep: Reputation: 34
I experimented with this. I tried:
Code:
TempFile1="/tmp/cgitmp.$$"
TempFile2="/tmp/cgitmp.$$"
TempKeep3="/tmp/cgitmp.$$"
TempBlah4="/tmp/cgitmp.$$"
But it made all 4 variables point to the same temp file. To make it work I had to do:
Code:
TempFile1="/tmp/cgitmp1.$$"
TempFile2="/tmp/cgitmp2.$$"
TempKeep3="/tmp/cgitmp3.$$"
TempBlah4="/tmp/cgitmp4.$$"
I thought the top one would have worked, that each $$ would be replaced with a unique number.
 
1 members found this post helpful.
Old 02-26-2018, 06:34 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Yes, I was not informative enough: it is unique to your script, actually, it is the PID of your script.
 
1 members found this post helpful.
Old 02-26-2018, 06:38 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896Reputation: 5896
$$ 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

https://www.tutorialspoint.com/unix_commands/mktemp.htm
 
1 members found this post helpful.
Old 02-27-2018, 05:45 AM   #11
icecubeflower
Member
 
Registered: Mar 2008
Location: USA
Distribution: Slackware 13.1
Posts: 313

Original Poster
Rep: Reputation: 34
thx all
 
Old 02-27-2018, 08:03 AM   #12
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
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.
 
  


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] Site redirecting to /cgi-sys/defaultwebpage.cgi sanjay87 Linux - Server 3 05-21-2012 02:05 PM
Cant seem to run any cgi scripts in cgi-bin folder, confused? j.smith1981 Linux - Server 5 02-14-2011 05:38 AM
Nagios, Statusmap.cgi and Trend.cgi NOT FOUND edwardcode Linux - Software 24 06-10-2010 07:45 AM
Nagios: statusmap.cgi & Trends.cgi files missing wlchak Linux - Software 6 10-30-2009 06:47 AM
http://www.burstnet.com/cgi-bin/ads/ad7954a.cgi/3980/RETURN-CODE rverlander LQ Suggestions & Feedback 1 06-07-2002 07:35 AM

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

All times are GMT -5. The time now is 05:46 AM.

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