[ marko85 @ 05.01.2007. 03:47 ] @
Pozdrav,
Kako koristeci exec da napravim program u perl-u koji ce uraditi sledece:

create a program shell.pl that will repeatedly read a line of user input (containing a Unix/Linux command to be executed) until the user enters CTRL-D (end of file). For each line entered,
your program should fork a process that will execute the provided command using exec.
Your parent process should use wait to await the completion of the child process. Your program should handle invalid commands by printing a suitable error message.

Ima li neko ideju?

Jednostavno da program radi u terminalu bi bilo najlakse.

[ marko85 @ 05.01.2007. 03:49 ] @
Jednostavno program mora da radi u Unix-u.
[ alex @ 05.01.2007. 14:36 ] @
Pogledaj man stranu za perlipc, recimo.

Evo ti pocetni skeleton programa. Ugradi pozive komandi sâm.

Code:

while (<>) {
    # ukucana linija je u $_ varijabli
    # razradi dalje ovde
}
print "Job well done.."


Eto, to ti je dovoljno za pocetak da sam resis problem.
[ marko85 @ 06.01.2007. 17:38 ] @
#!/usr/bin/perl

#
#fork.pl
#------
#Simple example of some code that creates processes using the fork() system call.
#A number of other system calls are also made. They are getuid() which returns the
#numeric userid of the user executing the process, getpid() which returns the
#numeric process id (PID) of the process, sleep() which causes the process to sleep for
#designated number of seconds, and exit() which terminates the program with the
#specified completion status (zero is normal completion. For those unfamiliar with C,
#the %d format code used in the print() call causes an integer variable to be formatted
#for output as a decimal ('d') number.

main:
{
my ($i, $pid);

for ($i=0;$i<3;$i++) {
printf("Parent process before the fork, uid= %d, pid=%dn",$>,$$);
$pid=fork();
if ($pid!=0) {
# /* parent process executes here */
printf(„Parent process after the fork, uid= %d, pid=%dn”,$>,$$);
}
else {
# /* child process execute here */
printf(„Child process after the fork, uid= %d, pid=%dn”,$>, $$);
sleep(1);
printf(„Child process exiting, uid= %d, pid=%dn”,$>, $$);
exit(0);
}
} #/* end for */
printf(„Parent process exiting, uid= %d, pid=%dn”,$>, $$);
} #/* end main */

#!/usr/bin/perl
#exec.c
#------
#Simple example of some code that causes a running process to execute a different
#program (in this case, the 'date' program which prints the current date and time).
#Handling of an error situation is also illustrated.

main:
{
my( $i,$rc);

for ($i=0;$i<3;$i++) {
if (($i % 2) == 0) { #/* if i is an even number try to invoke an unknown program */
$rc=system(„unknownprogram”);
}
else {# /* i is odd so invoke the date program */
$rc=system(„/bin/date”);
}
if ($rc!=0) {
printf(„Error - unknown program.n”);
}
} #/* end for */
printf(„About to exit the 'exec' program.n”);
} #/* end exec */

Treba da uradim kombinaciju ova dva programam ali gledao sam one primere ali nije nije sličan onome sto mi treba.
Iskreno ne poznajem perl toliko dobro ali ovo mi je vrlo hitno inače puce mi jedan module..
Kako da editujem ova dva programar da rade ono sto želim?
Pozdrav

P.S. Alex molim te pomogni mi.Treba da dam ovaj rad u ponedeljak ali nemam ideju kako to da uradim.
[ VRider @ 06.01.2007. 23:26 ] @
Kad se poziva fork, pravi se identicna kopija programa, sa svim promenljivim, i onda bi oba instance programa nastavile da se izvrsavaju paralelno. Jedino kako mozes da razlikujes originalni proces kopiju je na osnovu PIDa, koji ti vraca fork(). Ako si u parent procesu, onda ti vraca PID childa, a ako si u childu, onda ti vraca 0. To je sve sto treba da znas iz prvog programa.

U drugom programu je koriscena funkcija system, iako ti kazes da treba da se koristi exec. Razlika izmedju system i exec je u tome sto system sam po sebi ceka da se child izvrsi, i nema potrebe da se koristi wait (ako uopste i moze). Tako ti da drugi program ili ne valja ili nisi lepo napisao u prvoj poruci.

Osim toga, treba da i postavis jedan signal handler, koji ce u slucaju da je Ctrl-D pritisnuto sve da pobije i ugasi program.

Nije tesko, kreni, pa vici ako zapne.
[ alex @ 08.01.2007. 17:03 ] @
VRider: specijalan handler za Ctrl-D uopste nije potreban, jer ce program da "iskoci" iz while() ako se pritisne Ctrl-D..
[ VRider @ 08.01.2007. 17:23 ] @
Mislio sam na neko obavestenje, tipa "Kraj izvrsavanja, dovidjenja". Znam da bi umro i ovako.