1. Trang chủ
  2. » Công Nghệ Thông Tin

Tiến trình trong Linux và các hàm trong C potx

14 442 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 14
Dung lượng 70,5 KB

Nội dung

1. Khái quát  !" #$%&'()"'*+$,(-".% /0*112&#$%- ".%.3045& )645'()"'78.%91:6;<=>?7 9@94954A(68&=8.%#3"06 4BC'()"'&#$%D.C@E71F- DE08".%;<G4(045& =8(H.(A:-.304"@( GI8$.#J#!600*7#5K0/*@*27 081L(/127M=D#3N$..0A.%/O>=P O*>*11=2&)645.%7D1F%877 @Q.%7 -.%4.08.%45& RS@HT@UG#!.%#G4(-& VG#G#!$(7G#!(.%-AK W0&X.%D0GC8GN41Y 1#!I";<=>?8.%8CKZ!7( 6-AE&[..%D#3S@HG674\4S +7--*8.%8(6L084\/12 '()"'& )]^.%JH",(-A G]_7-.01AF7-T8G16G`*#3 C9.%4(-]@>Oa/>*11O@*`2@K (64!.%& [#DK7.%G0#$%/GD #$%$_E.%@K84U7#! 30%D-*.%4#$%2&bK".% -HA98459'()"'79#!@9 c "#!@908&dL@U1*1.%- H(10&eS170.%-8( 8& X7.%#3K0G]_50fghe&a( .%GN4K#3C8.%8&'.% 8GS0.&XA7.D61:@ (.%7-K0D49G]_ "& 2. Cách hoạt động của tiến trình Vc#$%Q@B(7D-K0L=ij @9#$%4#-40L#!/@2&X.% -88J irunning JSD.%.I";<=>?@9L8H8 G4(%& iwaiting J.%]'()"'#5I";<=>?74!.#3K008 8& isuspend J'()"'@Q.%&X.%#3#48 /1**02&VE.4-E7'()"'1F8T/k*020 ,(.%45&=K008A=>?.%.0U & XA@B(74 l @9(=ij7D-1;@U(# .%40L#!&=Dm-AEc.% "nD080o& dL@UJp1Pbo (`1F*.%C4"#A0L#5&XH.D 04(64#$8A@B(7mDDC .%1*1&VN(-^DAE1A .%H8&d"0%7D-@9(`/2 q S.%5Q.%m&'@9(11*/2(N. %'()"'&'**/2m-.%58& 3. Cấu trúc tiến trình =D*'()"'I<.%#.r [.-#!@9J1*c41*q904#$%*0  !7H.7'()"'1FI<40,#$%*04 49584N+0S49#4.%&'%1K8 0S#$%*0.%#!81;@U X%71*c#$%*0%+0`*c& p*0`*c 1*q#$%*04%+@*0`*q& p*0@*`*q =DEE5#!@91*c41*q-C8L8 0484N*0 !&'%A( G5'()"'#$%*00U4U#!@9& [.@9(017(61F(ADG4"8.%'( )"'187 dL@UJp01P` +.%#38]@@N>Oa/0*11 @*`2&>Oa#!16A@#$-8]Qqstquvw&V.%5 AEC7'()"'1FNK16/#].% .216AA4K008.%5&V.% K@T7(61F 16>OaK008.%8E1&>Oa xEQ8]qC4 l 8]c#3@.%EAN&X.% #34DC'()"'&.%I<4 N.%8&y4L@UA7DK(01P`1F]q. %*0C1*c41*q4516>OaE<3czc4czq& t ,(H(*0T0#$%nA^T #3'()"'04495&[#DKC#3 A7+. %#3'()"0S{J49T,(/@*2449T@ (/@2&,(#!64-1;@U&I< 0|0.%9#$%-1;@U,(& X#4(m4&XQ#4(9B%8#4(}1F#3 '()"'0|01:4@9CN.%(6&en 81:#4(7L#5#$%8&,(#$ %(6C@.%m1F\65$& XQ,(4#4(-1:7B@(%G1:C8 .%&+.%1C0S@(A&dL@U.%*0@1*c x#.1-8]~~7*0@1*qx-.145 8]~@*~& +.%m#3(6@AG`*/`* @*10*2&eT816G808`*#3C&V +.%C7#!'()"'1FC1fDt`*J1@/16 Gz271@/16Gc2741@*/16Gq2&=8`*#3#8. ]07K74G8+&=D-CA8`*8&dL@U1*cC `*`*c741*qC`*`*q&'()"'K00816G`*+.% 4#ADG`*.%-& [7+.%-A.01#.U488] 4"1!N&X.%m#3@GA#8 .G#!&=D1F@9(0*44**4A.G#! .%& a) Bảng thông tin tiến trình '()"'#KD@18A(6N. %/0*11*2&e.%I<K>Oa(6945G .4"8.%&dL@UDN(017#!N G.%4](A.%#3NJ! g .=>?.%7A#!1;@U.%7M b) Xem thông tin của tiến trình (01'()"'@9]G.4".%&X9* 167011F.G4".%#!@97.%(6K 8.%&dL@U011F#.n16s` X8G@014"7?OaA#!@9N.%7>Oa 16]@(6K0.%7>>Oa16]@.% /0*>Oa2&ySD1F016.%-]@>>Oac7] @.%7#3N(6C&[.D. %%'()"'1FK@T0A4(&•XO€!.% #3#41;@U&XO€!.@U=>?.%&=a @B(.%#3(N&XX•%*$NH .%&[#D.7#!@9-04(6QK "*8N.%&)(A8.%(67D1; @U(Jp01P 4. Tạo lập tiến trình a) Gọi tiến trình mới bằng hàm system() =D-N.%8A#$%H n11*/2&=-^D-.%5Q.% &'11*/2#38#1J #include <stdlib.h> int system( const char (cmdstr) ) 'N+(@1H4!(K@T5I4"$N &[-#$#$454(N1*H((6Jp1P@1 11*/21F4",+cqu.#GC#31*N(@1& ,+sc.08+8&=B7,4"11*/2,+@@11 (#3N4"& ‚ dL@U1;@U11*/2711*& #include <stdlib.h> #include <stdio.h> int main() { printf( "Thuc thi lenh ps voi system\n" ); system( "ps –ax" ); printf( "Thuc hien xong. \n" ); exit( 0 ); } '11*/2D#31;@UN(ƒ01P„'()"'& b) Thay thế tiến trình hiện hành với các hàm exec +.%#3'()"'K0cG58(.% H@&[..%…D(N#$%e/n 11*/2f27'()"'#!H(88#JK008 G5.%57"_@188.%70,( #$%eA^T4G54QK008.%&)#. %54@18E"06'()"'&[G4(#! K!84.AA(6& [..%…4.D6.%eC G5-1Y.%…%-1;@U8**#3 K05&=8**1F..%…/ ,(7 @(7G`*2.%e8&=_-16] @>Oa.%…B&X0** 81J #include <unistd.h> extern char **environ; int execl( const char *path, const char *arg, ); int execlp( const char *file, const char *arg, ); v int execle( const char *path, const char *arg, , char *const envp[] ); int exect( const char *path, char *const argv[] ); int execv( const char *path, char *const argv[] ); int execvp( const char *file, char *const argv[] ); )168"AED_6160`*#!@†. A#$%EHA^&8616E"#$%H 7616#$H8DN#$%Q@B(& dL@U1;@U**70**& #include <unistd.h> #include <stdio.h> int main() { printf( "Thuc thi lenh ps voi execlp\n" ); execlp( "ps", "ps", "–ax", 0 ); printf( "Thuc hien xong. Nhung chung ta se khong thay duoc dong nay.\n" ); exit( 0 ); } c) Nhân bản tiến trình với hàm fork() sX..%GK345D&)-.%5. G.%m4D1FG-18m#" .0.%(%1N**&=8$8 #$%#!@9-1;@U`/2S1 5.%&`/28(7H7-1F4"q8]8 EH7145%#!_4"c8]EH& V8`/2#1J #include <sys/types.h> #include <unistd.h> u pid_t fork() [.G7`/21F8.%(q.%/@^A'()" '0K008AG5.%52&X.% EN.%/0*0*112.%5N.% /@0*112&X.%1F-16]@>OaA(&7.% BA]@>>Oa16]@>Oa.%& •8.%7,(HC.%#31|0 6&=_-@K(D-@.%4. %7-]4"`/2&eA.%7`/21F4"] z&XA.%7`/21F4"]16A_>Oa .%4Q&X#!30G8#3.%7`/21F4"]sc&V 0@‡#384]^1@&16A/2& ),"41;@U`/2#!-@}1J pid_t new_pid; new_pid = fork(); // tách tiến trình switch (new_pid) { case -1: printf( "Khong the tao tien trinh moi" ); break; case 0: printf( "Day la tien trinh con" ); // mãlệnh dành cho tiến trình con đặt ở đây break; default: printf( "Day la tien trinh cha" ); // mãlệnh dành cho tiến trình cha đặt ở đây break; } sdL@U1;@U`/27`‡@*& w #include <sys/types.h> #include <unistd.h> #include <stdio.h> int main() { pid_t pid; char * message; int n; printf( "Bat dau.\n" ); pid = fork(); switch ( pid ) { case -1: printf( "Khong the tao tien trinh moi" ); exit(1); case 0: message = "Day la tien trinh con"; n = 0; for ( ; n < 5; n++ ) { printf( "%s", message ); sleep( 1 ); } break; default: message = "Day la tien trinh cha"; n = 0; for ( ; n < 3; n++ ) { printf( "%s", message ); sleep( 1 ); } break; } ˆ exit( 0 ); } eA@]4H#$%7D1FKnq.%  !4.I*&[.6*1HAI4">Oa4 >>Oaq.%4(`/20817D-H( #$%#1J p`‡@*o01P` d) Kiểm soát và đợi tiến trình con V`/28.%L.%47AH. .%†.%"0&)GD.%E03 .%H(84U%5.0UH&y4L@UA7H7 D1FKn.%.D.%4†G84 .%4.%"C.I%&=DG 6"7D6n.%.D%.%m K8-&'$7#$%EH(84U-% 5.#$%&)#34(7D1;@Uk/2 #include <sys/types.h> #include <sys/wait.h> pid_t wait(int &stat_loc); 'k#3N1FAE.%@Q!.%.D #5H(.08(".%&k/21HA( .%4.%CAEH&V.%.D71F 4"16>Oa#$T.%&[.D"A6161‡8 [?%k/2m1F4"8.%.D. 1‡&=D-1;@U881Y11‰k&#1J cz [...]... (stat_loc): Trả về trị kh c 0 nếu tiến trình con kết th c bình thường WEXITSTATUS (stat_loc): Nếu WIFEXITED trả về trị kh c 0, macro này sẽ trả về mãlỗi c a tiến trình con WIFSIGNALED (stat_loc): Trả về trị kh c 0 nếu tiến trình con kết th c bởi một tín hiệu gửi đến WTERMSIG(stat_loc) : Nếu WIFSIGNALED kh c 0, macro này sẽ cho biết số tín hiệu đă hủy tiến trình con WIFSTOPPED(stat_loc): WSTOPSIG(stat_loc)... Trả về trị kh c 0 nếu tiến trình con đă dừng Nếu WIFSTOPPED trả về trị kh c 0, macro này trả về số hiệu c a signal Ví dụ c ch sử dụng hàm wait() để chờ tiến trình con kết th c sau khi gọi fork(), wait_child .c #include #include #include int main() { pid_t pid; int child_status; int n; // nhân bản tiến trình, tạo bản sao mới pid = fork(); switch ( pid ) { case -1: //... đư c tiến trình mới printf( "Khong the tao tien trinh moi" ); exit( 1 ); case 0: // fork thành c ng, chúng ta đang ở trong tiến trình con printf( "Hello world from child\n" ); 11 n = 0; for ( ; n < 3; n++ ) { printf( "Tien trinh con" ); sleep( 1 ); } exit( 0 ); // Mãlỗi trả về c a tiến trình con default: // fork thành c ng, chúng ta đang ở trong tiến trình cha printf( "Tien trinh cha, cho tien trinh con... thanh.\n" ); // Chờ tiến trình con kết th c wait( &child_status ); printf( "Tien trinh cha – tien trinh con hoan thanh.\n" ); } return ( 0 ); } Trong ví dụ trên, chúng ta tuy rằng đă c sử dụng biến child_status, nhưng chưa dùng nó để xem xét mã lỗi trả về c a tiến trình con Ví dụ dưới đây sẽ th c hiện vi c này, wait_child2 .c #include #include #include #include ... // Mã lỗi trả về c a tiến trình con default: // fork thành c ng, chúng ta đang ở trong tiến trình cha n = 3; for ( ; n > 0; n ) { printf( "Tien trinh cha" ); sleep( 1 ); } // Chờ tiến trình con kết th c wait( &child_status ); // Kiểm tra và in mãlỗi trả về c a tiến trình con printf( "Tien trinh con hoan thanh: PID = %d\n", pid ); if ( WIFEXITED( child_status ) ) printf( "Tien trinh con thoat ra voi... int main() { char *message; int n; pid_t pid; int child_status; // nhân bản tiến trình, tạo bản sao mới pid = fork(); 12 switch ( pid ) { case -1: // fork không tạo đư c tiến trình mới printf( "Khong the tao tien trinh moi" ); exit( 1 ); case 0: // fork thành c ng, chúng ta đang ở trong tiến trình con printf( "Hello world from child\n" ); n = 5; for ( ; n > 0; n ) { printf( "Tien trinh con" ); sleep(... trả về c a tiến trình con printf( "Tien trinh con hoan thanh: PID = %d\n", pid ); if ( WIFEXITED( child_status ) ) printf( "Tien trinh con thoat ra voi ma %d\n", WEXITSTATUS( child_status ) ); else printf( "Tien trinh con ket thuc binh thuong\n" ); break; } 13 exit( 0 ); } 14 . 81J #include <unistd.h> extern char **environ; int execl( const char *path, const char *arg, ); int execlp( const char *file, const char *arg, ); v int execle( const char *path, const char. const char *arg, , char *const envp[] ); int exect( const char *path, char *const argv[] ); int execv( const char *path, char *const argv[] ); int execvp( const char *file, char *const argv[] ); )168"AED_6160`*#!@†. A#$%EHA^&8616E"#$%H 7616#$H8DN#$%Q@B(& dL@U1;@U**70**& #include. đang ở trong tiến trình cha printf( "Tien trinh cha, cho tien trinh con hoan thanh. " ); // Chờ tiến trình con kết th c wait( &child_status ); printf( "Tien trinh cha – tien

Ngày đăng: 01/08/2014, 21:20

TỪ KHÓA LIÊN QUAN

w