Run two CGIs simultaneously and Ajax to read updated value from CGI1

Flow of program:
C based CGI is used

CGI1:
-Gets called when user hits upload button(submit) ie form action = CGI1
-Does the file upload (copy to a directory etc)

JS, Ajax fucntion:
-A JS function is called when user hits upload button
-The JS function opens an Ajax request for CGI2
-Ajax keeps polling CGI2 for updated value

CGI2:
-Contains:
system ("some cmd to get directory size");

Issue:
Ajax is not able to read values from CGI2 as it polls to get updated values after every 1 sec. It is able to get last value.
In some cases it is able to get 1-2 other values but not EVERY TIME.

Logically my program is fine. I have tested running CGI2 separately, it works fine. I have tested Ajax, and no issues with it as well. The status which it receives is 0 or sometimes 500.
However, altogether, it is not working.

Possible cause:
-Environment issue
-Running 2 CGIs simultaneously
-System() command used in CGI2 is not the right way to get output for Ajax

Any suggestion/solution would be really appreciable.

Thanks

In other words, new thread, same problem.

I can't tell whether "some cmd to get directory size" is correct or not without seeing it, but it's not like you were getting sensible results from disk before. If you're not getting sensible results from the filesystem itself until CGI1 completes, no number of variations on reading it from the filesystem is going to be the "magic" way to do it, the information's just not there, yet -- or is being incorrectly cached somehow by your web server or CGI library.

I think you're going to have to create a way for CGI2 to communicate with CGI1 without storing the message on disk. Perhaps it could create a FIFO in /tmp/ which CGI1 would write to and CGI2 would read from, or a UNIX domain socket. I'll post pseudocode in a minute.

i am thinking it could be a couple of issues. either the form and how it is submitted and the ajax is called. you said CGI1 is being called by the normal form action and a submit button. or it is how CGI2 is updating ajax.

does the ajax that calls CGI2 work fine if you create a button that only calls the ajax and doesn't submit the form?

--nate


int percent;
mutex m;

void fifo_writer_thread(void)
{
    char fifoname[256];
    int fd;
    sprintf(fifoname, "/tmp/fifo-%d\n", file_unique_id);
    mkfifo(fifoname, 0006);

    while(1)
    {
      char buf[16];
      int fd;
      int local;
      lock(m);  local=percent; unlock(m);

      if(local < 0) break; // End thread when percent<0

      fd=open(fifoname, O_WRONLY|O_DIRECT);
      if(fd >= 0)
      {
        int len=sprintf(buf, "%d\n", local);
        write(fd, buf, len);
        close(fd);
      }

      sleep(1);
    }

    unlink(fifoname);
}

void upload_reader(void)
{
    while(1)
    {
       if(!transfer_block()) break;

       lock(m); percent++; unlock(m);
    }

    lock(m);  percent=-1; unlock(m);

    wait_for_thread();
}

I will check this case, but I am sure it will work fine then. However, along with CGI1 it doesn't.
Ya, form submit could be an issue. Ajax is just called by calling a JS function ie JS function gets called on button submit which further open(), send etc

---------- Post updated at 03:37 PM ---------- Previous update was at 03:36 PM ----------

@Corona688:But, how will Ajax read this?? It a JS function.

ok, then IF (definitely check this) ajax and CGI2 works by itself.... then i am thinking it is how the browser is handling form action with the submit button (and possibly the file input field is another issue) with the ajax.

have you tried not using a submit button and the form action tag but using a normal button with js that calls the ajax and then form.submit() ?

i assume the target of the form submission is in another iframe on the page that doesn't contain ajax js code. if not, then that is definitely the issue.

cheers,
--nate

Alright, I will try this.

Thanks Nate :wink:

Have the CGI2 script read from the fifo.

Oh ok . I will try this.Thanks