Tải bản đầy đủ (.pdf) (16 trang)

báo cáo bài tập nhập môn hệ điều hành lab 3 2

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.11 MB, 16 trang )

<span class="text_page_counter">Trang 1</span><div class="page_container" data-page="1">

<b>TRƯỜNG ĐẠI HỌC TÔN ĐỨC THẮNG KHOA CÔNG NGHỆ THÔNG TIN </b>

<b>BÁO CÁO BÀI TẬP NMHĐH HK2, 2021-2022</b>

<b>Lab 3</b>

<b>Thành Viên: Nguyễn Ngô Đăng Khoa (521H0084)</b>

1. Viết chương trình để truyền đối vào số nguyên dương n vào và...10

2. Sử dụng lời gọi system() để viết chương trình thực thi các lệnh Linux đã học..11

3. Tạo ra cây tiến trình như sau, với mỗi tiến trình con, in ra ID của nó và ID của tiến trình cha...124. Viết một chương trình lặp vơ tận với lời gọi while(1); và thực thi nó. Đưa tiến trình này vào “background” thơng qua việc gửi tín hiệu SIGTSTP bằng cách nhấn Ctrl + Z. Sử dụng lệnh ps để xác định PID của nó và sử dụng kill để kết thúc nó.145. Viết chương trình mà khi chạy nó sinh ra tiến trình con, để tiến trình con trở thành zombie. Tiến trình zombie này cần tồn tại trong hệ thống tối thiểu 10 giây (bằng cách dùng lời gọi sleep(10)). Sau đó dùng lệnh ps –l để xem trạng thái các

</div><span class="text_page_counter">Trang 2</span><div class="page_container" data-page="2">

tiến trình. Kết liễu zombie này bằng cách xác định PID của tiến trình cha nó và sử dụng lệnh kill...15KẾT LUẬN...16

</div><span class="text_page_counter">Trang 3</span><div class="page_container" data-page="3">

A. PHẦN THỰC HÀNHVí Dụ 1: 1.1 Tạo tiến trình

<b>A: Code Chương Trình</b>

<small>Chúy ý: code phải được giải thích để thể hiện hiểu rõ chương trình mình biết</small>//P1.c

printf("Current process ID: %d\n", getpid()); printf("Parent process ID: %d\n", getppid()); return ;0

{ // make two process which run same

// program after this instruction

ộ // Thêm m t vài l nh printf và theo dõi.ệ

fork(); fork();

</div><span class="text_page_counter">Trang 4</span><div class="page_container" data-page="4">

// thêm m t vài l nh fork vào đây và xem thay đ i.

printf("Hello world! from Process ID: %d\n",

getpid()); return ;0

(pid ) if <0 printf("fork error! No child created\n"); elseif(pid ==0)

{

//sleep(2);

printf("Hello from Child!\n"); printf("Child ID is %d!\n", getpid()); }

else

{

printf("Hello from Parent!\n"); printf("Parent ID is %d!\n", getpid()); }

</div><span class="text_page_counter">Trang 5</span><div class="page_container" data-page="5">

// Fork returns process id

// Child process

else exit(0);

return ;0

</div><span class="text_page_counter">Trang 6</span><div class="page_container" data-page="6">

// A C program to demonstrate Orphan Process. // Parent process finishes execution while the// child process is running. The child process// becomes orphan.

// Create a child process

pid int =fork();

(pid )if >0

printf("in parent process\n");

// Note that pid is 0 in child process

// and negative if fork() fails

elseif (pid ==0) {

sleep( );6

printf("in child process\n"); }

return ;0

}

</div><span class="text_page_counter">Trang 8</span><div class="page_container" data-page="8">

intmain( intargc, char *argv[]){

pnum, count, retval, child_no;int

pnum =atoi(argv[ ]);1

child_no count;=

printf("Tien trinh %d, PID %d\n", child_no,

getpid()); } else

{

for(count ; count pnum; count=0 < ++) wait(NULL); printf("Tien trinh cha PID %d\n", getpid()); }

</div><span class="text_page_counter">Trang 9</span><div class="page_container" data-page="9">

{ i;int

pid int =fork(); (pid if == )0

for = (i ; i ; i0 <5 ++) printf("I am Child\n"); else

{

signal(SIGCHLD,SIG_IGN); printf("I am Parent\n"); while( );1

}}

<b>B: Kết Quả Demo</b>

</div><span class="text_page_counter">Trang 10</span><div class="page_container" data-page="10">

B. PHẦN BÀI TẬP

1. Viết chương trình để truyền đối vào số nguyên dương n vào và

a. Tiến trình cha tiếp tục tính rồi xuất ra tổng S = 1 + 2 + …. + n

b. Đồng thời tạo một tiến trình con tính tổng các ước số của n và in ra màn hình<small>.</small>

<b>A Code chương trình:</b>

intmain( intargc, char *argv[]){

pid_t pid =fork();

(pid ) if <0 printf("fork error! No child created\n"); for(int = i 1 1; < argc; i++)

{

k int =atoi(argv[ ]);1

(pid if == )0

{ t ;int =0

for(int = i 1; i <= k; i++) {

(k i if % == ) t 0 += i;

</div><span class="text_page_counter">Trang 11</span><div class="page_container" data-page="11">

wait(NULL);

for(int = i 0; i <= k; i++) s += i;

printf("Tong cac gia tri tu 1 den %d la: %d\n", k,s);

} return ;0

}}

</div><span class="text_page_counter">Trang 12</span><div class="page_container" data-page="12">

printf("Command pwd\n"); re =system("pwd"); printf("Command ls -a\n"); re =system("ls -a");

(re if !=-1) printf("System call is done!\n"); printf("Call system to execute ps -a\n"); re =system("ps -a");

(re if !=-1) printf("System call ps is done!\n"); return ;0

</div><span class="text_page_counter">Trang 13</span><div class="page_container" data-page="13">

n1 int =fork(); n2 int =fork();

(n1 if ==0) {

(n2 if ==0) {

printf("H ");

printf("my id is %d, my parentid is %d\n",

getpid(), getppid()); }

}

printf("B\n");

printf("my id is %d, my parentid is %d\n",

getpid(), getppid()); }

} return ;0

}

</div><span class="text_page_counter">Trang 14</span><div class="page_container" data-page="14">

{ i;int

pid int =fork(); (pid if == )0

for = (i ; i ; i0 <5 ++) printf("I am Child\n"); else

{

</div><span class="text_page_counter">Trang 15</span><div class="page_container" data-page="15">

signal SIGCHLD( ,SIG_IGN); printf("I am Parent\n"); while( );1

}}

pid_t child_pid =fork();

</div><span class="text_page_counter">Trang 16</span><div class="page_container" data-page="16">

// Child process

(child_pid if >0) printf("%d\n",getpid()); // Parent process

elseif(child_pid == ) 0

sleep( );10

return ;0

</div>

×