[英]File locking between C++ and PHP
我正在创建一种有点愚蠢但有效的方法来在PHP中创建作业,然后C ++开始使用。 PHP创建的作业文件是简单的文本文件,C ++逐行读取该文件,并将每一行添加到向量中。
资料夹:
问题:PHP假定文件不在“作业保持”中,但C ++并未处理该文件,但C ++实际上仅将其移至该位置以防止作业执行两次(如下所示)。 C ++仍然需要读取文件以将其内容放入向量中,因此在发生这种情况时,我需要防止PHP访问此文件。
另外,我需要禁止C ++在PHP编辑“ jobhold”文件夹中的作业文件时对其进行处理。
C ++函数,用于检查自上次完成此工作以来是否经过了足够的时间 :
-> 如果PHP对该文件具有锁定,则需要取消这两个步骤(3和4) 。 这意味着文件将被完全忽略,直到PHP完成它为止。
处理“ jobs”文件夹中文件的C ++函数:
-> 我需要确保PHP仍无法处理该文件,即使该文件位于“ jobhold”中也是如此 。 C ++需要立即锁定它(之后,期间, 之前? ),因此PHP绝不可能拾取该文件,并且直到线程内的某个地方才释放它。
处理作业文件的C ++线程:
-> 在此步骤之后,可以释放文件锁定 ,这时PHP可以锁定文件,因此C ++将不再处理该文件。
PHP (当前仅检查“ jobhold”中的文件是否存在 ):
<?php
if(isset($_GET['edit']))
{
$filename = $startdir.'jobhold/'.$_GET['edit'];
while (!file_exists($filename))
{
while(file_exists($startdir.'jobs/'.$_GET['edit']))
{
//Wait until C++ moves the file from "jobs" to "jobhold" by doing nothing
}
if(file_exists(filename)
{
break 1; //The file exists now, we may break the while.
}
//If we reached this point, the job simply doesn't exist.. so die.
echo "The job $_GET[edit] doesn't exist. <br /><br />";
die();
}
}
/* Other code here, such as HTML form so user can edit the txt file */
?>
->需要在PHP中添加另一项检查,以检查C ++是否在“ jobhold”中的作业文件上锁定了锁定。 如果是,请等到锁定被删除后再立即锁定文件,以使C ++无法再处理该文件。 像这样(愚蠢的例子):
<?php
while(file_locked_by_c++($startdir.'jobshold/'.$_GET['edit']))
{
//Wait until C++ releases the file by doing nothing
}
/* Lock file NOW */
?>
我确实找到了这个线程 ,尽管我不介意在C ++代码中使用C,但是如果没有适当的例子,我也没有设法弄清楚如何将这些问题应用于我的情况。
PS :之所以没有发布任何实际的C ++代码,是因为我对C ++的使用(有时是凌乱但大多是多余的)方式感到恼火,因为我是C ++的新手,这只会使其他新手和/或混淆摆脱我在这里想要实现的目标。
如果您坚持要看一些代码,那很好,但是请不要提及混乱/冗余,因为我已经知道了 ,它们只是便宜的解决方法,我将在以后的阶段中进行“修复”。 如果您是新手,请确保不要复制这些错误。
抱歉,这么长的篇幅太简单了,但我需要100%正确,否则整个应用程序可能会崩溃。
更新:
我目前正在这样做:
添加了一个名为“ lock”的新文件夹,其中将包含php-jobnameX.txt
和c-jobnameX.txt
,其中jobnameX是当前作业的名称。
C ++:
- 当C ++检查自上次以来是否经过了足够的时间时,我们在“ jobhold”中执行了jobfile,在将文件从“ jobhold”移至“ jobs”之前,它首先检查php是否已在“ lock”中创建了文件。 如果是这样,我们将跳过文件。
//This function will check if PHP has created a lock-file
bool checkifphplock(char* filename)
{
ifstream fin(filename);
if (fin)
{
fin.close();
return true;
}
return false;
}
int cont = 1;
if(checkifphplock(phplockfile))
{
cont = 0; //php lock found, so we go byebye
}
if(cont == 1)
{
/* Keep doing stuff */
}
- 当php开始在“作业”中的作业文件上工作时,在将作业文件从“作业”移回“作业持有”之前,它将在“锁”中创建一个锁定文件以通知php它仍在处理该文件。
ofstream myfileclock;
myfileclock.open (clockfile, fstream::in | fstream::out | fstream::app);
myfileclock << " ";
myfileclock.close();
- 一旦c ++线程完成了将文件内容复制到向量中,锁定文件将被删除以通知PHP它准备好再次进行编辑。
system("rm /path/to/program/lock/c-jobnameX.txt");
PHP:
- 编辑后,通过在“ lock”中创建文件来立即获得锁定。
//Obtain lock immediately:
$file = $startdir."lock/php-$_GET[edit]";
if(!is_file($file)){ file_put_contents_atomic($file," "); chmod($file,0777);} //Make file if there is none
if(!is_writable($file)){chmod($file.'/',0777);} //Chmod file if not writable
if(!is_writable($file)){die("File \"$file\" not writable.");} //Die if not writable
- 获取锁定后,再次检查c ++是否也没有锁定文件(以防C ++和PHP都在同一时间请求了作业文件)
while (file_exists($startdir."lock/c-$_GET[edit]"))
{
//Do nothing while c/c++ has a lock
}
- 如果c ++已将文件从“ jobhold”移到“ jobs”,我们将等到文件移回。
//Wait for filename to exist in jobhold. Or die if it doesn't exist in any folder.
$filename = $startdir.'jobhold/'.$_GET['edit'];
while (!file_exists($filename))
{
while(file_exists($startdir.'jobs/'.$_GET['edit']))
{
//Wait until C++ moves the file from "jobs" to "jobhold" by doing nothing
}
if(file_exists($filename))
{
break 1; //The file exists now, we may break the while.
}
//If we reached this point, the job simply doesn't exist.. so die.
echo "The job $_GET[edit] doesn't exist. <br /><br />";
unlink($file); //Remove lock for this non-existing job
die();
}
/* Continue creating form to edit the file etc. */
从技术上讲,最后一步永远是不可能的,因为如果C / C ++没有锁,则文件不能(不应该)处于“作业”中,但是检查不会受到损害。
这样是否可以100%确保PHP和C / C ++不能同时处理文件?
一种非常简单的文件锁定方法是在文件顶部有一行,显示读取的锁定内容或您喜欢的内容。 在执行任何操作之前,每段代码都会检查该行是否存在。 文件的第一个也是最后一个动作是通过添加“已锁定”一词来锁定/解锁文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.