kỹ thuật điện hệ điều hành 3 bài thực hành os sinhvienzone com

7 44 0
kỹ thuật điện hệ điều hành 3 bài thực hành os sinhvienzone com

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

Thông tin tài liệu

Bai thưc Môn hoc: Hê điêu hanh C đ : quan l tiên trinh (process) Chu ky sông cua process Viêt chương trin h in cac chuôi môi trương thông qua khai bao extern char **environ chay chuong trinh /tenfile.c #include #include extern char **environ; // environment array int main(int argc, char **argv) { /* this is an array of strings */ char ** env = environ; /* this while loop checks each *env (the pointer is moved forward to the next memory location with *env++) it loops, until *env points to null */ while (*env) printf("%s\n",*env++); // step through environment exit(0); } Viêt chương trin h tao tâp cac process Bai thuc hanh he dieu hanh sô – GV Phi Loan SinhVienZone.com https://fb.com/sinhvienzonevn Dung ham for kiêm tra xem đa process cha hay Dùng hàm wait() hay waitpid process cha đ trạ thái tr từ process #include #include int main() { pid_t fork_retval ; printf ( "Start process : %d\n", getpid() ) ; fork_retval = fork() ; if ( fork_retval > ) { // parent printf ( "This is the parent : %d, starting child %d\n", getpid(), fork_retval ) ; wait() ; } else if ( fork_retval == ) { printf ( "This is the child : %d\n", getpid() ) ; } else { printf ( "Some error occurred\n" ); } fork_retval = fork() ; if ( fork_retval > ) { // parent printf ( "This is the parent : %d, starting child %d\n", getpid(), fork_retval ) ; wait() ; } else if ( fork_retval == ) { printf ( "This is the child : %d\n", getpid() ) ; } else { printf ( "Some error occurred\n" ); } return getpid() ; } Qua n l process Bai thuc hanh he dieu hanh sô – GV Phi Loan SinhVienZone.com https://fb.com/sinhvienzonevn Người dùng có t hậu (background) hạ chương trình theo hai cách: Mặt tiên (foreground) Mặt a Chạy foreground process: gõ lệnh tương ứng với tên chương trình theo cách thơng thường; Tương tác đượ với người dùng qua thiết bị nhập chuẩn (standard input) bàn phím Kết xu t cua chương trình c yếu hình (standard output) Trình thơng dịch lệnh bị chặn chương trình kết thúc - Châm dưt chươ trinh foreground: Ctrl-C - Tạm dừng chươ trinh: Ctrl –Z b Chạy background process: thêm d u & (ampersand) vào ci dòng lệnh Ví dụ: $find /usr/man -name *.1 –print > sect1 & Trinh thông dịch tạo process thưc hiệ chương trinh đồ g thời in job number ([n]) PID (Process IDentifier) cua process tạo ra, sau trinh thơng dịch sẵn sàng nhận lệnh mơi - Process background vẫ xu t standard output hình lúc thưc thi  ầ tái đị h hướng standard output đ tránh m t liệu xu t - Người dùng không th tương tác với chương trình qua standard input bàn phím với chương trình chạy mặt hậu  cầ phai tái định hướng standard input quan file chương trình ần nhập liệu - Liệt kê job tích cưc: dùng lệnh jobs $jobs -l - Dùng lệnh bg (background) đ chuy process tư foreground sang chạ background mặt hậu - Dùng lệnh f đê chuyên process tư background sang chay foreground fg job_number (Nếu có q trình chạ mặt hậu khơng cầ job_number) Thưc hanh cac lênh sau: find / -name “cron*” > output1.txt & find / -name “*.* > output2.txt & ls –l > output3.txt & Bai thuc hanh he dieu hanh sô – GV Phi Loan SinhVienZone.com https://fb.com/sinhvienzonevn jobs fg ls –l Sư dun g m execlp C pháp: int execlp(const char *file, const char *arg, ); Các ham exe ẽ thay process gọi h chương trình tương ứng tham s nhập cua h m Vung text, data, stack bị thay thế, vung user (user area) khơng bị thay ● Ham có chứa kí t l (execl, execlp, execle) danh sách tham l pointer đến string, kết thuc danh sách n NULL pointer ● Ham có chứa kí t p (execlp, execvp) ch p nhận tên cua chương tr ầ chạy, chạ ương trinh đó, tim execution path hiệ tại; h khơng có kí t p ph i cung c p đầ đ đường dẫ đến chương trinh cần chạy Thưc hiên chươ C ươ trinh sau: trinh #include #include #include main() { char *temp,*temp1,*temp2; temp1="Funny"; temp2="world"; execlp("echo","echo",temp1,temp2,NULL); printf("Error"); } C ươ trinh /* Example of use of fork system call */ #include #include #include main() { int pid; Bai thuc hanh he dieu hanh sô – GV Phi Loan SinhVienZone.com https://fb.com/sinhvienzonevn pid = fork(); if (pid < 0) { /* error occurred */ fprintf(stderr, "Fork failed!\n"); exit(-1); } else if (pid==0) { /* child process */ printf("I am the child, return from fork=%d\n", pid); execlp("/bin/ps", "ps", NULL); } else { /* parent process */ printf("I am the parent, return from fork, child pid=%d\n", pid); wait(NULL); printf("Parent exiting!\n"); exit(0); } } Tao pipe Thông thường, process tạo pipe trươc no fork hay nhiêu process Pipe đươc du đê giao tiêp giưa process cha va hay process anh em Ham pipe tạo pipe va đăt file descriptor c o đâu đoc va viêt cua pipe vao filedes[0] filedes[1] int pipe (int filedes[2]) C ươ trinh sau sư dun g ham fork đ tạo process Process cha viêt dư liêu vao pipe va process co đoc tư pipe #include #include #include #include /* Read characters from the pipe and echo them to stdout */ void read_from_pipe (int file) { FILE *stream; int c; stream = fdopen (file, "r"); Bai thuc hanh he dieu hanh sô – GV Phi Loan SinhVienZone.com https://fb.com/sinhvienzonevn while ((c = fgetc (stream)) != EOF) putchar (c); fclose (stream); } /* Write some random text to the pipe */ void write_to_pipe (int file) { FILE *stream; stream = fdopen (file, "w"); fprintf (stream, "hello, world!\n"); fprintf (stream, "goodbye, world!\n"); fclose (stream); } int main (void) { pid_t pid; int mypipe[2]; /* Create the pipe */ if (pipe (mypipe)) { fprintf (stderr, "Pipe failed.\n"); return EXIT_FAILURE; } /* Create the child process */ pid = fork (); if (pid == (pid_t) 0) { /* This is the child process Close other end first */ close (mypipe[1]); read_from_pipe (mypipe[0]); return EXIT_SUCCESS; } else if (pid < (pid_t) 0) { /* The fork failed */ fprintf (stderr, "Fork failed.\n"); return EXIT_FAILURE; } Bai thuc hanh he dieu hanh sô – GV Phi Loan SinhVienZone.com https://fb.com/sinhvienzonevn else { /* This is the parent process Close other end first */ close (mypipe[0]); write_to_pipe (mypipe[1]); return EXIT_SUCCESS; } } Them doan vaob chuong trinh #include Void main() { Whiale () Printf (“acb”); } Chay: /test1 >b1.txt & /test2>b2.txt & Jobs [1] Jobs [2] fg1 Bai thuc hanh he dieu hanh sô – GV Phi Loan SinhVienZone.com https://fb.com/sinhvienzonevn ... child process */ pid = fork (); if (pid == (pid_t) 0) { /* This is the child process Close other end first */ close (mypipe[1]); read_from_pipe (mypipe[0]); return EXIT_SUCCESS; } else if (pid

Ngày đăng: 28/01/2020, 22:10

Từ khóa liên quan

Tài liệu cùng người dùng

Tài liệu liên quan