How can I separate a process from CGI so that I can store and read files from memory?

Is it possible that I can create a separate process like a daemon, from a CGI script that stores reading text files in memory, then re-accessing memory in the next cgi run, reading data using a pipe?

Will most hosting providers allow separate processes? Are memory channels fast and easy to code / work on a unix / linux system?

Is there a solution that can be made without using any additional CPAN modules? This is a CGI process, so I want to keep it to a minimum.

+3
source share
3 answers

, resource.cgi:

#! /usr/bin/perl

use warnings;
use strict;

use Reader;
use CGI qw/ :standard /;

print header("text/plain"),
      "Contents:\n",
      Reader::data,
      "-" x 40, "\n";

Content-Type: text/plain; charset=ISO-8859-1

Contents:
This is a data file
with some very interesting
bits.
----------------------------------------

Reader.pm, :

package Reader;

use warnings;
use strict;

use Fcntl qw/ :DEFAULT :flock :seek /;
use POSIX qw/ setsid /;    

:

my $PIDFILE = "/tmp/reader.pid";
my $DATA    = "/tmp/file.dat";
my $PIPE    = "/tmp/reader.pipe";

Sub import use Module. , . $PIDFILE.

sub import {
  return unless my $fh = take_lock();

  my $child = fork;
  die "$0: fork: $!" unless defined $child;

  if ($child) {
    print $fh  "$child\n" or die "$0: write $PIDFILE: $!";
    close $fh             or die "$0: close $PIDFILE: $!";
    return;
  }

  # daemonize
  close $fh;
  chdir "/";
  open STDIN,  "<", "/dev/null";
  open STDOUT, ">", "/dev/null";
  open STDERR, ">", "/dev/null";
  setsid;

  open $fh, "<", $DATA or die;
  undef $/;
  my $data = <$fh>;
  close $fh;

  while (1) {
    open my $fh, ">", $PIPE or die;
    print $fh $data         or die;
    close $fh;
  }
}

, $PIDFILE. , , , , .

sub take_lock {
  sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT or die "$0: open $PIDFILE: $!";
  flock $fh => LOCK_EX                       or die "$0: flock $PIDFILE: $!";

  my $pid = <$fh>;

  if (defined $pid) {
    chomp $pid;

    if (kill 0 => $pid) {
      close $fh;
      return;
    }
  }
  else {
    die "$0: readline $PIDFILE: $!" if $!;
  }

  sysseek  $fh, 0, SEEK_SET or die "$0: sysseek $PIDFILE: $!";
  truncate $fh, 0           or die "$0: truncate $PIDFILE: $!";

  unless (-p $PIPE) {
    system("mknod", $PIPE, "p") == 0
                            or die "$0: mknod exited " . ($? >> 8);
  }

  $fh;
}

, :

sub data {
  open my $fh, "<", $DATA or die "$0: open $DATA: $!";
  local $/;
  scalar <$fh>;
}

:

1;

, . - , .

, , , , .

0

, , RAM- . cgi-.

+1

Why would you want to do that? What problem are you trying to solve? Will something like File :: Map work? These are mmap files, so the files are not in memory, but they act the way they are. I wrote a little about it in the memory card files instead of sticking them .

0
source

Source: https://habr.com/ru/post/1730236/


All Articles