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

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

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 (852.69 KB, 22 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ĐHHK1, 2023-2024</b>

<b>Lab 3</b>

<b>Nhóm:</b> 01 <b>Tổ:</b> 02

1: Nguyễn Thanh Sơn ( MSSV: 52200287)

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

<b>Mục lục……….</b>

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

<b>A. PHẦN THỰC HÀNH</b>

<b>Ví Dụ 1: Biên dịch chương trình hello.c</b>

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

<b>Ví Dụ 2: Biên dịch chương trình truyền đối số và in ra (các) đối số đó</b>

2.1: Code chương trìnhA. Code chương trình//main.c

int main(int argc, char ** argv) {

printf("so doi so truyen vao: %i\n", argc); // in ra số đối số truyền vàoprintf("gia tri cac doi so: ");

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

int i = 0;

for(int i=0; i < argc; i++) // tạo vòng lặp in ra các đối số truyền vàoprintf("%s\t", argv[i]);

B. Kết quả Demo

<b>Ví Dụ 3.1: Sử dụng thư viện tỉnh để biên dịch chương trìnhA. Code chương trình</b>

//hello1.c#include<stdio.h>void hello1(int i) {

printf("Hello parameter 1 = %d \n", i); //in ra màn hình tham số 1}

//hello2.c#include<stdio.h>void hello2(int i) {

printf("Hello parameter 2 = %d \n", i); //in ra màn hình tham số 2}

//main.c

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

int main(int argc, char ** argv) {

int i = atoi(argv[1]); // ép kiểu char về int bằng hàm atoiint k = atoi(argv[2]); // ép kiểu char về int bằng hàm atoihello1(i); // gọi hàm hello1

hello2(k); // gọi hàm hello2}

<b>B. Kết quả demo</b>

<b>Ví dụ 3.2: Sử dụng thư viện động để biên dịch chương trình</b>

//hello1.c#include<stdio.h>void hello1(int i) {

printf("Hello parameter 1 = %d \n", i); //in ra màn hình tham số 1}

//hello2.c#include<stdio.h>void hello2(int i) {

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

printf("Hello parameter 2 = %d \n", i); //in ra màn hình tham số 2}

int main(int argc, char ** argv) {

int i = atoi(argv[1]); // ép kiểu char về int bằng hàm atoiint k = atoi(argv[2]); // ép kiểu char về int bằng hàm atoihello1(i); // gọi hàm hello1

hello2(k); // gọi hàm hello2}

<b>Ví dụ 4: Tạo tiến trìnhA. Code chương trình</b>

#include<stdio.h>#include<unistd.h>int main() {

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

}

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

#include<stdio.h>#include<unistd.h>int main() {

pid_t pid;

if( (pid=fork()) == 0) {

printf("child process: child ID = %d\n", getpid());printf("My parent id: Parent ID = %d\n", getppid());} else if(pid > 0) {

printf("parent process: parent ID = %d\n", getpid());printf("child id = %d\n", pid);

} else {

printf("fork error!\n");}

return 0;}

#include<stdio.h>#include<unistd.h>int main() {

int pid, pid1;

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

if( (pid = fork()) == 0) {printf("---B---\n");

printf("This is B: ID = %d\n", getpid());printf("Parent: A ID = %d\n", getppid());printf("---end B---\n");

} else if(pid > 0) {

if( (pid1 = fork()) == 0) {printf("---C---\n");

printf("This is C: ID = %d\n", getpid());printf("Parent: A ID = %d\n", getppid());printf("---end C---\n");

return 0;}

<b>B. Kết quả demo</b>

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

<small>int main(int argc, char ** argv) {</small>

<small> int a = atoi(argv[1]); // ép kiểu char về int thông qua ham atoi</small>

<small> if(argc >2) // kiem tra dieu kien doi so vao thoa man =2 hay khong</small>

<small>printf("Doi so truyen vao qua nhieu \n");</small>

<small> else if(a<=0) //kiểm tra điều kiện a có thỏa mản <= 0 hay khơng</small>

<small>printf("Doi so nhap vao khong phai la so nguyen duong\n"); else { </small>

<small> int sum =0;</small>

<small> for(int i=0; i<=a; i++) // chạy vòng lặp để tính tổng từ 1 tới n</small>

<small> sum = sum + i;printf("S = %i\n",sum); } </small>

<small> return 0;}</small>

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

<small>a. Báo lỗi nếu lời gọi có đối số khơng phải là một số nguyên dương.</small>

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

<small>b. Báo lỗi nếu có nhiều hơn 2 đối số (là main.out và n).</small>

<small>Bài 2: Viết chương trình nhận một đối số từ mơi trường là số phần tử trong mảng, nhập giá trị của tất cả các phần tử trong mảng, xuất tổng của mảng đó ra khỏi màn hình.</small>

<small>a. Báo lỗi nếu n không phải là số nguyên dương .</small>

<small>b. Báo lỗi nếu khác n+2 đối số (là ex2.out, n và n phần tử trong mảng).</small>

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

<small>int main(int argc, char ** argv) {</small>

<small>int n = atoi(argv[1]); //ép kiểu char về int thông qua hàm atoi</small>

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

<small>double check = atof(argv[1]); //ép kiểu char về double thông qua hàm atof</small>

<small>int arr[n]; //khai báo mảng arr gồm n phần tử</small>

<small>if(n <= 0 || n != check) //kiểm tra điều kiện n có là số ngun dương khơng</small>

<small>printf("So luong phan tu phai la so nguyen duong\n");else if( argc != n + 2) //kiểm tra điều kiện đối số vào thỏa mản = n+2</small>

<small>printf("Doi so truyen vao khong hop le\n");else {</small>

<small>int i; // khởi tạo i</small>

<small>double sum = 0; // khởi tạo sum</small>

<small>for(i = 0; i < n; i++) { // chạy vòng lặp để:</small>

<small>arr[i] = atof(argv[i+2]); // lưu giá trị vào mảng</small>

<small>sum += atof(argv[i+2]); // tính tổng các phần tử trong mảng</small>

<small>printf("Tong cac phan tu trong mang: %f\n", sum);}</small>

<small>return 0;}</small>

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

<small>a. Báo lỗi nếu n không phải là số nguyên dương .</small>

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

<small>b. Báo lỗi nếu khác n+2 đối số (là ex2.out, n và n phần tử trong mảng).</small>

<small>Ví Dụ 3: Tạo tệp lib1.c tính tổng các số từ 1 đến n; lib2.c có hàm giai thừa có giá trị nguyên từ 1 đến n, trong đó n là đối số đầu vào, n là số nguyên dương.</small>

<small>1). Tạo thư viện liên kết tĩnh libs.s; Tạo một tệp select.c với chức năng chính. Trong hàm main, nếu argv [1] = 1 thực hiện tính tổng các số từ 1 đến argv [2] thì hàm cịn lại thực hiện tính giai thừa các số từ 1 đến argv [2]</small>

<small>2). Tạo thư viện liên kết động libd.d; Tạo file selectd.c với hàm main. Trong hàm main, argv [1] = 1 thực hiện tính tổng các số từ 1 đến argv [2], hàm cịn lại thực hiện phân tích nhân tử các số từ 1 đến argv [2] – nhớ sử dụng liên kết thư viện.</small>

<small>a. Báo lỗi nếu số tham số truyền vào khác 3</small>

<small>b. Báo lỗi nếu n nhập vào không phải là số nguyên dươngA: Code chương trình</small>

#include<stdio.h>#include<stdlib.h>

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

int lib1(int n) {int i = 0, sum = 0;

for(i; i <= n; i++) // tạo vịng lặp để tính tổngsum += i;

printf("Tong cac so tu 1 den %d la: %d\n", n, sum);return 0;

}//lib2.c

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

int main(int agrc, char ** agrv) {int n1 = atoi(agrv[1]);int n2 = atoi(agrv[2]);

if(agrc != 3) // kiểm tra số tham số có khác 3 khơngprintf("So tham so khong hop le\n");

else if(n1 <= 0 || n2 <= 0 || n1 != atof(agrv[1]) || n2 != atof(agrv[2]))// kiểm tra n có phải là số nguyên dương không

printf("n phai la so nguyen duong\n");else if(n1 == 1) { // kiểm tra argv[1] có = 1 không

lib1(n2);lib2(n2);} else {

return 0;}

<b>B: Kết quả demo</b>

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

<small>a. Báo lỗi nếu số tham số truyền vào khác 3</small>

<small>b. Báo lỗi nếu n nhập vào không phải là số ngun dương</small>

<b>Ví Dụ 4:</b>

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

#include <stdio.h>#include <unistd.h>

int main(){

pid_t pid1, pid2, pid3, pid4, pid5;

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

pid1 = fork();if(pid1 < 0){

printf("Fork error! No C created!\n");} else if(pid1 == 0){

printf("Hello from C\n");printf("C ID is %d\n", getpid());

printf("My parent A PID is %d\n", getppid());pid2 = fork();

if(pid2 < 0){

printf("Fork error! No H created\n");} else if(pid2 == 0){

printf("Hello from H\n");printf("H ID is %d\n", getpid());

printf("My parent C PID is %d\n", getppid());}

else {sleep(3);

printf("Hello from C\n");printf("C ID is %d\n", getpid());printf("My child H PID is %d\n", pid2);}

}else{

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

printf("Hello from A\n");printf("A ID is %d\n", getpid());printf("My child C PID is %d\n", pid1);pid3 = fork();

if(pid3 < 0){

printf("Fork error! No B created\n");} else if(pid3 == 0){

printf("Hello from B\n");printf("B ID is %d\n", getpid());

printf("My parent A PID is %d\n", getppid());pid4 = fork();

if(pid4 <0){

printf("Fork error! No D created\n");} else if(pid4 == 0){

printf("Hello from D\n");printf("D ID is %d\n", getpid());

printf("My parent B PID is %d\n", getppid());}

else {sleep(3);

printf("Hello from B\n");printf("B ID is %d\n", getpid());

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

printf("My child D PID is %d\n", pid4);pid5 = fork();

} else {sleep(3);

printf("Hello from B\n");printf("B ID is %d\n", getpid());printf("My parent E PID is %d\n", pid5);}

}}else {

printf("Hello from A\n");printf("A ID is %d\n", getpid());printf("My parent B PID is %d\n", pid3);}

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

B: Kết quả demo

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

<b>KẾT LUẬN </b>

<b>Sau khi học và hoàn thành phần lab 3 nhóm thu được kết sau:- Em học được cách tìm ID cha và con của process- Em học được cách sử thư viện động</b>

<b>- Em học được cách sử thư viện tĩnh</b>

<small>Chú ý bài làm giống nhau sẽ bị trừ điểm.</small>

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

<b>KẾT QUẢ LÀM VIỆC CÁC THÀNH VIÊN TRONG NHĨM</b>

<b><small>Nguyễn Văn A</small>12345678</b>

<b>CHÚ THÍCH:TN: TRƯỞNG NHĨMTV: THÀNH VIÊN</b>

<b>HT: MỨC ĐỘ HỒN THÀNH CƠNG VIỆC ĐƯỢC GIAO</b>

</div>

×