bài tập thực hành cơ bản- môn hệ điều hành - Pdf 16

Bài tập thực hành cơ bản- môn Hệ Điều Hành năm 2009
GVHD: Phạm Đức Thành SVTH:Trần Thị Thanh Trâm_Lớp 06CT3
Bài tập thực hành cơ bản – môn Hệ Điều Hành
Nhóm I :Các thao tác đọc /ghi nội dung file văn bản /nhị phân, khảo sát hình thức tổ chức dữ liệu trên file
( 2t TH )
Bài 1: Viết chương trình (VCT) nhập 1 số nguyên 2byte, 1 số nguyên 4byte, 1 chuỗi ký tự & lưu chúng vào 1 tập tin nhị phân
(theo đúng thứ tự trên). Sau đó dùng 1 công cụ xem nội dung tập tin dưới dạng DUMP (dạng hexa) để khảo sát giá trị từng
byte của tập tin & tìm hiểu lý do vì sao giá trị các byte là như vậy.
#include <stdio.h>
#include <conio.h>
#include <string.h>
int LuuFileNhiPhan(char *FileName,int n2,long n4,char *S)
{ FILE *f=fopen(FileName,"wb");
if(f==NULL) return 0;
fwrite(&n2,sizeof(n2),1,f);
fwrite(&n4,sizeof(n4),1,f);
fwrite(S,strlen(S),1,f);
fclose(f); return 1;
}
void main()
{ clrscr();
int n2; long n4; char S[256];
printf("\n Moi ban nhap n2 : "); scanf("%d",&n2);
printf("\n Moi ban nhap n4 : "); scanf("%d",&n4);
fflush(stdin);
printf("\n Moi ban nhap chuoi S : "); gets(S);
if(LuuFileNhiPhan("D:/Bai1.DAT",n2,n4,S)==0)
printf("\nLoi");
else printf("\n Thanh cong");
}
Bài 2: VCT nhập 1 số nguyên, 1 số thực, 1 chuỗi ký tự & lưu chúng vào 1 tập tin văn bản (trên 3 dòng khác nhau). Sau đó

int DocFileVB(char *FileName,char *S)
{ FILE *f=fopen(FileName,"rt");
if(f==NULL) return 0;
char S1[256];
S[0]=0;
while(1)
{ char *P=fgets(S1,256,f);
if(P==NULL) break;
strcat(S,S1);
}
fclose(f); return 1;
}
int GhiFileVB(char *FileName,char *S)
{ FILE *f=fopen(FileName,"wt");
if(f==NULL) return 0;
fprintf(f,"%s",S);
fclose(f); return 1;
}
void MaHoa(char *P, char *C)
{ int l=strlen(P);
for(int i=0;i<l;i++)
C[i]=P[i]^3;
C[l]=0;//Ket thuc chuoi
}
void main()
{ clrscr();
char S[256],P[256];
if(DocFileVB("D:/bai3.txt",S)==0) printf("\n Loi");
else
{ printf("\n Chuoi S = %s",S);

}
void GiaiMa(char *C, char *P)
{ int l=strlen(C);
for(int i=0;i<l;i++)
P[i]=C[i]^3;
P[l]=0;
}
void main()
{ clrscr();
char S[256],P[256];
if(DocFileVB("D:/mahoa1.txt",S)==0)
printf("\n Loi");
else
{ printf("\n Chuoi S = %s",S);
GiaiMa(S,P);
if(GhiFileVB("D:/giaima2.txt",P)==0)
printf("\n Loi");
else printf("\n Thanhcong");
}
getch();
}
Nhóm II: Xây dựng CT Shell – thao tác với các hàm ngắt ( 3t TH )
Bài 1: VCT giả lập các lệnh của DOS: MD,CD,RD,DEL,REN,TYPE, bằng cách dùng các hàm của C.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>
#include <dir.h>
#include <dos.h>
char CacLenh[][10]={"MD","CD","RD","DEL","REN","TYPE"};

exit(1);
}
}
void ChuyenDoiThuMuc(char *DIRNAME)
{ if (chdir("\\")) //chuyen ve thu muc goc
{ perror("chdir()"); exit(1);
}
if (chdir(DIRNAME))
{ perror("chdir()");
exit(1);
}
}
void XoaThuMuc(char *DIRNAME)
{ int stat;
stat = rmdir(DIRNAME);
if (!stat) printf("\nDirectory deleted\n");
else
{ perror("\nUnable to delete directory\n");
exit(1);
}
}
void XoaTapTin(char *file)
{ if (remove(file) == 0)
printf("Removed %s.\n",file);
else perror("\nLoi");
}
void DoiTenTapTin(char oldname[],char newname[])
{ if (rename(oldname, newname) == 0)
printf("Renamed %s to %s.\n", oldname, newname);
else perror("rename");

case 1: strcpy(DIRNAME,M[1]);
ChuyenDoiThuMuc(DIRNAME);
break;
case 2: strcpy(DIRNAME,Curdir);
strcat(DIRNAME,M[1]);
XoaThuMuc(DIRNAME);
break;
case 3: strcpy(DIRNAME,M[1]);
XoaTapTin(DIRNAME);
break;
case 4: DoiTenTapTin(M[1],M[2]);
break;
case 5: XemNoiDungTapTin(M[1]);
break;
default:printf("\n Chua co lenh nay ");
}
}
char *Current_directory(char *path)
{ strcpy(path,"D:\\"); path[0]='A' + getdisk();
getcurdir(0,path+3); return (path);
}
void main()
{ clrscr();
char MangLenh[6][50]; char ChuoiLenh[256];
while(1)
{ Curdir[0]=0;
Current_directory(Curdir);
fflush(stdin);//rua bo dem ban phim
printf("%s>",Curdir); gets(ChuoiLenh);
strupr(ChuoiLenh);

result = inportb(port);
printf("Ky tu read from port 0x%X = 0x%X la %c\n",port, result,result);
}
int Get_Ascii_Code()
{ union REGS r; //b1: khai bao bien thanh ghi
r.h.ah=0; //b2: cho biet chuc nang cua ham
int86(0x16,&r,&r); //b3: goi ham ngat ban phim
return r.h.al; //b4: tra ve ket qua
}
int Get_Scan_Code()
{ union REGS r;
r.h.ah=0;
int86(0x16,&r,&r);
return r.h.ah;
}
unsigned Get_Code()
{ union REGS r;
r.h.ah=0;
int86(0x16,&r,&r);
return r.x.ax;
}
void main()
{ unsigned kq=Get_Code();
printf("\nKet qua ascii=0x%X cua phim %c", kq & 0x00FF , kq & 0x00FF );
printf("\nKet qua scan=0x%X",( kq & 0xFF00 ) >> 8 ); getch();
}
Bài 3 : Giả lập các lệnh MD, DEL, TYPE với các phần công việc chính được thực hiện bằng cách gọi ngắt.
#include<stdio.h>
#include<conio.h>
#include<dos.h>

for(int i=0;i<100;i++)
printf("%4.2X",p+i);
getch();
}
Bài 2: Tương tự như bài trên nhưng địa chỉ ô nhớ nhập vào là địa chỉ vật lý.
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{ unsigned char AddPhysic;
unsigned char seg,off;
printf("\n\n Moi nhap dia chi vat ly= "); scanf("%d",&AddPhysic);
seg=AddPhysic/10; off=AddPhysic%10;
unsigned *p=(unsigned *)MK_FP(seg,off);
for(int i=0;i<100;i++)
printf("%4.2X",p+i);
getch();
}
Nhóm IV: thao tác với sector ( 2t TH )
Bài 1: VCT nhập vào địa chỉ logic của 1 sector, đọc & hiển thị ra màn hình (dưới dạng Hex) nội dung của sector đó.
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<process.h>
#include <graphics.h>
int DocMotSector(int Sector_LG,unsigned char Buf[] )
{ if(absread( 0 , 1 ,Sector_LG,Buf)!=0) // 0: đĩa A ; 1: đọc
{ perror("\n Loi "); return 0;
}
return 1;

printf("%3.2X",buf[i]);
}
void main()
{ int sector,track,head;
unsigned char buf[512];
printf("\n Moi nhap dia chi sector vat ly ");
printf("\n\t Sector= "); scanf("%d",&sector);
printf("\n\t Track= "); scanf("%d",&track);
printf("\n\t Head= "); scanf("%d",&head);
if(DocMotSectorVL(sector,track,head,buf)==1)
XuatNoiDungMotSector(buf);
else printf("\n That bai");
getch();
}
Bài 3: VCT nhập vào các thông số của 1 đĩa vật lý: số sector/track, số track/side, số side; sau đó nhập tiếp địa chỉ logic hoặc
vật lý của 1 sector, tính & xuất ra địa chỉ tương ứng còn lại của sector đó.
#include<stdio.h>
#include<conio.h>
#include<bios.h>
class cSector
{ int SectorPerTrack,TrackPerSide,Sides;
int SecNo,TrackNo,SideNo; int SectorLG;
public:
void NhapThongSoVL()
{ printf("\n Moi nhap thong so vat ly cua Disk");
printf("\n\t So Sector tren 1 track= ");scanf("%d",&SectorPerTrack);
printf("\n\t So track tren 1 side= "); scanf("%d",&TrackPerSide);
printf("\n\t So Sides= "); scanf("%d",&Sides);
}
void NhapSectorLG()

objSec.NhapThongSoVL(); objSec.NhapSectorVL();
objSec.TinhSectorLG(); objSec.XuatSectorLG();
objSec.NhapSectorLG(); objSec.TinhSectorVL();
objSec.XuatSectorVL(); getch();
}
BÀI 4: VCT nhập vào địa chỉ logic hoặc vật lý (tùy theo sự lựa chọn của người dùng) của 1 sector, đọc và lưu nội
dung sector đó vào 1 file. Và CT thực hiện công việc tương ứng ngược lại (đọc nội dung file & lưu vào sector).
BÀI 4A: (Nhập vào địa chỉ của 1 Sector,đọc và ghi nội dung Sector đó vào 1 file)
Bài 4A1: (Nhập địa chỉ logic)
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<dos.h>
#include<bios.h>
class cSector
{ int SectorLG;
unsigned char buf[512];
public:
void NhapSectorLG()
{ do
{ printf("\n Moi nhap Sector logic thu= "); scanf("%d",&SectorLG);
if(SectorLG<0||SectorLG>2879)
printf("\n Ban nhap sai ");
}while(SectorLG<0||SectorLG>2879) ;
}
int DocMotSectorLG()
{ if(absread( 0 ,1,SectorLG,&buf)!=0) // 0:O dia A;
{ perror("Loi"); return 0;
}
return 1;

unsigned char S[512];
public:
void NhapSectorVL()
{ printf("\n Moi nhap Sector vat ly thu= "); scanf("%d",&SecNo);
printf("\n Moi nhap track thu= "); scanf("%d",&TrackNo);
printf("\n Moi nhap Side thu= "); scanf("%d",&SideNo);
}
int DocMotSectorVL()
{ int kq=biosdisk( 2 , 0 ,SideNo,TrackNo,SecNo,1,&buf); // 2: Đọc; 0:O dia A
kq=kq&0x00;
if(kq==0) return 1;
perror("Loi");return 0;
}
int LuuFileVB(char *FileName)
{ FILE *f=fopen(FileName,"wt");
if(f==NULL) return 0;
for(int i=0;i<512;i++)
fprintf(f,"%3.2X",buf[i]);
fclose(f); return 1;
}
void XuatNoiDung()
{ for(int i=0;i<512;i++)
printf("%3.2X",buf[i]);
}
};
void main()
{ clrscr();
cSector objSec; objSec.NhapSectorVL();
objSec.DocMotSectorVL(); objSec.XuatNoiDung();
objSec.LuuFileVB("e:/test1.txt"); getch();

printf("%3.2X",S[i]);
}
};
void main()
{ clrscr();
cSector objSec; objSec.DocFileVB("e:/test.txt");
objSec.GhiMotSectorVL(); objSec.XuatNoiDung();
getch();
}
Bài 4B2(Ghi một Sector logic)
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<bios.h>
#include<string.h>
class cSector
{ int SectorLG;
unsigned char S[512];
public:
int DocFileVB(char *FileName)
{ FILE *f=fopen(FileName,"rt");
if(f==NULL) return 0;
for(int i=0;i<512;i++)
fscanf(f,"%X",&S[i]);
fclose(f); return 1;
}
int GhiMotSectorLG()
{ if(abswrite( 0 ,1,SectorLG,&S)!=0) // 0: O đĩa A
{ perror("Loi"); return 0;
}

return 1;
}
int XuatBangBootSector(unsigned char *buf)
{ printf("\n Ten cong ty= ");
for(int i=0;i<8;i++)
printf("%c",buf[i+3]);
printf("\n So Byte cua sector=%d ",(buf[12]<<8)+buf[11]);
printf("\n So Sector cua cluster(SC)=%d",buf[13]);
printf("\n So Sector truoc bang FAT(SB)=%d",(buf[15]<<8)+buf[14]);//do uu tien + truoc << sau;
printf("\n So luong bang FAT(NF)=%d",buf[16]);
printf("\n So Entry cua RDET(SR)=%d",(buf[18]<<8)+buf[17]);
printf("\n So Sector cua volume=%d",(buf[20]<<8)+buf[19]);
printf("\n So sector cua FAT=%d",(buf[23]<<8)+buf[22]);
}
void XuatNoiDungMotSector(unsigned char *buf)
{ for(int i=0;i<512;i++)
printf("%3.2X",buf[i]);
}
void main()
{ int sector_LG=0; unsigned char buf[512];
clrscr();
if(DocMotSector(sector_LG,buf)==1)
{ XuatNoiDungMotSector(buf);
XuatBangBootSector(buf);
}
else printf("\n That bai");
getch();
}
Bài 5_Cách2:
#include<stdio.h>

printf("\n Sector per FAT=%d ",(buf[23]<<8)+buf[22]);
printf("\n Heads=%d ",(buf[25]<<8)+buf[24]);
printf("\n Hidden Sector=%ld",((long)buf[31]<<24)+((long)buf[30]<<16)
+(buf[29]<<8)+buf[28]);
printf("\n File System= ");
for(i=0;i<8;i++)
printf("%c",buf[i+54]);
}
};
void main()
{ clrscr();
cSector objSec; objSec.DocBootSector();
objSec.XuatNoiDungBootSector(); getch();
}
Nhóm VI: thao tác với các thành phần quản lý Volume ( 3t TH )
Bài 1: VCT đọc bảng thư mục gốc đĩa mềm & hiển thị tên của các tập tin trên đó.
#include<stdio.h>
#include<conio.h>
#include<bios.h>
#include<dos.h>
int Sector=19; unsigned char *Buf=new unsigned char[512*14];
int DocRD()
{ if(absread(0,14,Sector,Buf)!=0)//ta khong su dung &buf vi da khai bao Buf la kieu *Buf
{ perror("Loi");return 0; }
return 1;
}
void XuatChuoi(unsigned char A[],int n)
{ for(int i=0;i<n;i++)
printf("%c",A[i]);
}

}
int LuuFileVanBan(char *FileName)
{ FILE *f=fopen(FileName,"wt");
if(f==NULL) return 0;
for(int i=0;i<224*32;i++)
fprintf(f,"%3.2X",Buf[i]);
fclose(f); return 1;
}
void main()
{ clrscr(); DocRD();
LuuFileVanBan("D:/rootdir.txt"); getch();
}
Bài 2B: Đọc FAT
#include<stdio.h>
#include<conio.h>
#include<bios.h>
#include<dos.h>
int Sector=1; unsigned char *Buf=new unsigned char[512*9];
int DocFAT()
{ if(absread(0,9,Sector,Buf)!=0)
{ perror("Loi");return 0; }
return 1;
}
int LuuFileVanBan(char *FileName)
{ FILE *f=fopen(FileName,"wt");
if(f==NULL) return 0;
for(int i=0;i<512*9;i++)
fprintf(f,"%3.2X",Buf[i]);
fclose(f); return 1;
}

textcolor(WHITE);
else if(value==0xFF7) //hu
textcolor(RED);
else
textcolor(YELLOW); //su dung
cprintf("%c",178);
delay(20);
}
}
void main()
{ clrscr(); DocFAT();
HienThiTrangThai(); getch();
}
Bài 4: VCT xóa toàn bộ đĩa (giống như lệnh “format /q” của DOS).
#include<stdio.h>
#include<conio.h>
#include<bios.h>
#include<dos.h>
int Sector_F1=1,Sector_F2=10,Sector_RD=19;
unsigned char *Buf_FAT= new unsigned char [512*9];
unsigned char *Buf_RD= new unsigned char [512*14];
int GhiFAT(int Sector)
{ if(abswrite(0,9,Sector,Buf_FAT)!=0)
{ perror("\n Loi"); return 0;
}
return 1;
}
int GhiRD(int Sector)
{ if(abswrite(0,14,Sector,Buf_RD)!=0)
{ perror("\n Loi"); return 0;

}
unsigned GetValueFat(int cluster)
{ int i= cluster * 3/2;
unsigned value=(Buf[i+1]<<8)+Buf[i];
if(cluster % 2==0)
value=value&0x0FFF;
else value=value>>4;
return value;
}
void ChuoiCluster(int cluster)
{ do
{ unsigned value=GetValueFat(cluster) ;
if(value==0xFFF) printf(" EOF");
else printf(" %8.2X ",value);
cluster=value;
}while(cluster!=0xFFF);
}
void main()
{ clrscr(); DocFAT(); int cluster=2;
printf("\n Chuoi cluster bat dau tu:\n %8.2X",cluster);
ChuoiCluster(cluster); getch();
}
Nhóm VII: truy xuất trực tiếp dữ liệu ở thư mục gốc ( 3t TH )
Bài 1: VCT thực hiện công việc kiểm tra 1 tập tin ở thư mục gốc có bị phân mảnh và nếu có thì bao nhiêu mảnh.
#include<stdio.h>
#include<conio.h>
#include<bios.h>
#include<dos.h>
#include<math.h>
int Sector=1;

else printf("\n Phan lam %d manh",kq);
getch();
}
Bài 2: VCT truy xuất đĩa ở mức trực tiếp (đọc/ ghi sector) thực hiện công việc hiển thị nội dung của một tập tin văn bản ở
thư mục gốc.
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<bios.h>
#include<dos.h>
int SectorRD=19;
unsigned char *BufRD=new unsigned char [512*14];
int SectorFAT=1;
unsigned char *BufFAT= new unsigned char[512*9];
unsigned char *Buf= new unsigned char[512];
int DocRD()
{ if(absread(0,14,SectorRD,BufRD)!=0)
{ perror("Loi"); return 0;
}
return 1;
}
void XuatChuoi(unsigned char A[],int n)
{ for(int i=0;i<n;i++)
printf("%c",A[i]);
}
int TimTapTinTrongRD(char *filename)
{ for(int i=0;i<224;i++)
{ if(BufRD[i*32]==0) //Never used
break;
if(BufRD[i*32]==0xE5) //Erased

}
return 1;
}
void XuatNoiDungTapTin(int cluster)
{ do
{ DocMotSector(cluster+32,Buf);
XuatChuoi(Buf,512); getch();
unsigned value=GetValueFAT(cluster);
cluster=value;
}while(cluster!=0xFFF);
}
void main()
{ clrscr(); DocRD();
int kq=TimTapTinTrongRD("Bai1N7.cpp");
if(kq==-1)
printf("\n Khong tim thay ");
else
{ printf("First cluster = %5d",KQ);
DocFAT(); XuatNoiDungTapTin(KQ);
}
getch();
}
Trang 18


Nhờ tải bản gốc

Tài liệu, ebook tham khảo khác

Music ♫

Copyright: Tài liệu đại học © DMCA.com Protection Status