kyanh wrote:

A`, /me ko^ mong cho+` ca^u tra? lo+`i the^' na`y :(( Bo+?i vi`, /me ddang vie^'t Winefish ( http://winefish.sf.net ) -- chu+o+ng tri`nh ddo^i khi go.i chu+o+ng tri`nh ngoa`i va` ddo^i khi pha?i "KILL" (force) chu+o+ng tri`nh ngoa`i ddo'. Tu'm la.i, Winefish pha?i qua?n ly' ID ca'c chu+o+ng tri`nh con no' go.i. Bi chu+` ba'c no'i nhu+ the^', thi`i /me so+. qua', lo+~ khi na`o gie^'t nha^`m thi` ...

/me theo do~i ca'ch la`m cu?a nhie^`u program. Tha^'y no' cu~ng ko^ co' ca'ch xu+? ly' na`o dda(.c bie^.t. Du+o+'i dda^y la` ca'ch em la`m....

A`, co' pha?i du`ng "fork()" se~ gia?i qhuyeest ddu+o+jc mo.i chuye^.n ko^?

,----
|  /* fork():
| create a child proccess the differs
| from the parent only in its PID and PPID; | the resouce ultilisation are set to 0 */
`----

[code]
/* Taken from SciTTEGTK.cxx
kyanh: everything emitted from `running' will be captured [ 2>&1 ]
*/
static gint xsystem( const gchar *command, const gchar *outfile )
{
gint pid = 0;
/* fork():
create a child proccess the differs from the parent only in its PID and PPID;
the resouce ultilisation are set to 0 */
if ( ( pid = fork() ) == 0 ) {
 close( 0 );
 gint fh = open( outfile, O_WRONLY );
 close( 1 );
 dup( fh );
 close( 2 );
 dup( fh );
 DEBUG_MSG( "xsystem: running now [%s]\n", command );
 execlp( "/bin/sh", "sh", "-c", command, NULL );
 exit( 127 );
}
return pid;
}
[/code]
Du`ng fork() nhu+ ha`m xsystem cu?a kyanh la` o^?n ro^`i, gia' tri. tra? ve^` cu?a ha`m xsystem tre^n la` pid cu?a child process. Kyanh cu+' go.i kill pid na`y la` ddu+o+.c.
Gia?i thi'ch:

Fork la` ha`m go.i mo^.t la^`n nhu+ng tra? ve^` hai la^`n. Mo^.t la^` o+? parent process va` 1 la^`n o+? child process (gia' tri. tra? ve^` -1 co' nghi~a la` lo^~i khi go.i ha`m fork) ---->-Parent Process---->pid = fork() |---------> parent process ne^'u pid >0 |--------->child process ne^'u pid =0 Tie^'p theo, khi kyanh go.i ha`m execlp trong child process (pid = 0) thi` image process trong bo^. nho+' cu?a child process *bi. thay the^'* hoa`n toa`n bo+?i image cu?a chu+o+ng tri`nh ddu+o+.c go.i bo+?i ha`m excelp, nhu+ng process id kho^ng thay ddo^?i (xem man page cua? ha`m excelp). Do ddo' chu+o+ng tri`nh ddu+o+.c go.i bo+?i ha`m excelp va^~n la` con cu?a parent process. Khi child process ke^'t thu'c, va^~n co`n tho^ng tin ve^` process trong bo^. nho+' (process id ... gia' tri. tra? ve^` cu?a child process) =>child tro+? tha`nh zombie process *cho dde^'n khi* parent process go.i ha`m wait dde^? gia?i pho'ng. Nhu+ va^.y, kyanh co' the^? handle hoa`n toa`n chu+o+ng tri`nh ddu+o+.c go.i bo+?i ha`m excelp, go.i kill ma` kho^ng so+. kill nha^`m sang process kha'c (sau khi go.i ha`m kill ne^n go.i wait dde^? cho+` process ke^'t thu'c). Kyanh cu~ng co' the^? la`m mo^.t so^' bu+o+'c dde^? kie^?m tra child process cu?a mi`nh co`n cha.y hay kho^ng (ba(`ng ca'ch go.i ha`m kill vo+'i signal = 0). Ngoa`i ra kyanh ne^n cha(.n signal SGN_CHILD dde^? bie^'t khi na`o thi` child process cu?a mi`nh ke^'t thu'c.

Ky~ thua^.t ma` /me du`ng la` cha(.n signal SGN_CHILD, trong ha`m xu+? li' signal na`y, go.i ha`m wait dde^? cho+` child process exit. The^m: Ne^'u parent process ke^'t thu'c tru+o+'c child process thi` child process se~ co' parent process la` 1- init process, va` init process se~ gia?i pho'ng tho^ng tin ve^` child process khi child process ke^'t thu'c => gia?i pho'ng process id cho tie^'n tri`nh kha'c.

[OT]
va(n vo+'i cha? vo+?, do+? qua' pha?i kho^ng ca'c ba'c? :(
[/OT]


-------------------------------------------------------
This SF.Net email is sponsored by: NEC IT Guy Games.  How far can you shotput
a projector? How fast can you ride your desk chair down the office luge track?
If you want to score the big prize, get to know the little guy. Play to win an NEC 61" plasma display: http://www.necitguy.com/?r=20
_______________________________________________
VietLUG-users mailing list
VietLUG-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vietlug-users

Trả lời cho