logo
Author: n4n
Description: task.pl =))
Language: Perl
#!/usr/bin/perl
use strict;
use warnings;

use POSIX qw/:sys_wait_h setsid/;

our %PIDS;

main:
{
    print "shell# ";
    
    while( my $in = <STDIN> )
    {
        chomp $in;

        pid_check(\%PIDS);

        if( cmd_check($in) )
        {
            new_process($in);
        }
        else
        {
            print "\nCommand not found: $in\n\n" unless cmd_check($in);
        }
    }
}


###################################################
# --- SUB's --- #

## Создать потомок
sub new_process
{
    my $cmd = shift;
    
    die "fork() failed" unless defined( my $pid = fork() );
    
    # потомок
    if( $pid == 0 )
    {   
        setsid();
        exec($cmd);
    }
    # родитель
    else
    {
        $PIDS{$pid} = 1;
    }
}

## Проверить наличие запрашиваемой программы
sub cmd_check
{
    my $cmd = shift;
    
    foreach my $p ( split /:/, $ENV{PATH} )
    {
        return "$p/$cmd" if -f "$p/$cmd";
    }
    
    return 0;
}

## Перепроверить ЖИЗНЬ процесса
sub pid_check
{
    my $r = shift;
    
    foreach my $p ( keys %{$r} )
    {
        my $kid =  waitpid( $p, WNOHANG );
        
        if( $kid == 0 ) { print "PID: [$p] is alive\n"; }
        else { print "PID: [$p] is dead\n"; }
    }
}
Recent pastes:
123123123 (PHP)
ksurent (Perl)
ksurent (Bash)
guata (Plain Text)
biophreak (Plain Text)
Raik (PHP)
MUSbKA (Plain Text)
MUSbKA (Plain Text)
nekitozzz (C++)
nekitozzz (Plain Text)
nekitozzz (C++)
ksurent (Perl)
MUSbKA (C++)
stalin_alex (C++)
dzantiev (Plain Text)
unreal (C++)
eug2b (Plain Text)
We Are Not Responsible for User Content
netsago.paste.β © 2009—2012 Neunica