Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 569813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:20:06+00:00 2026-05-13T13:20:06+00:00

Is it possible that I would be able to spawn a detached daemon like

  • 0

Is it possible that I would be able to spawn a detached daemon like process, from a CGI script
that stores read text files in memory, then re-access the memory in the next cgi execution, reading the data using a pipe?

Would most hosting ISP’s, allow detached processes? Are memory pipes fast, and easy to code/work with on a unix/linux system?

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:20:07+00:00Added an answer on May 13, 2026 at 1:20 pm

    Say you have a simple 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";
    

    Its output is

    Content-Type: text/plain; charset=ISO-8859-1
    
    Contents:
    This is a data file
    with some very interesting
    bits.
    ----------------------------------------

    The fun part is in Reader.pm, which begins with familiar-looking boilerplate:

    package Reader;
    
    use warnings;
    use strict;
    
    use Fcntl qw/ :DEFAULT :flock :seek /;
    use POSIX qw/ setsid /;    
    

    Next it defined rendezvous points:

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

    The sub import is called as part of use Module. If the daemon is already running, then there’s nothing to do. Otherwise, we fork off the daemon and write its process ID to $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;
      }
    }
    

    Every client needs to wait its turn getting a lock on $PIDFILE. Once we have the lock, we then check that the process identified is still running and create the named pipe if necessary.

    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;
    }
    

    Finally, reading the pipe is trivial:

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

    Don’t forget to return a true value from the module:

    1;
    

    You’ll note that operations can still fail in the daemon. For your sanity, you’ll want to log events somehow rather than choking silently.

    As to whether hosts will permit long-running processes, that will vary from provider to provider, but even if your daemon is killed off from time to time, the code above will restart it on demand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 342k
  • Answers 342k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You don't usually initialize specific memory locations; each section (including… May 14, 2026 at 5:11 am
  • Editorial Team
    Editorial Team added an answer script This sounds like a homework problem. Anyway, it was… May 14, 2026 at 5:11 am
  • Editorial Team
    Editorial Team added an answer E8 is greater than the maximum usable character 7F in… May 14, 2026 at 5:11 am

Related Questions

I would like to be able to spawn a process in python and have
I may have missed something obvious with WPF, but is it possible to create
I'd like to create a dynamic reporting webpage using JSP. Basically it should contain
I'm trying to create a syntax file and I want to create some keyword
I am using asp.net 3.5 and want to render the contents of a static

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.