?? synch.c
字號(hào):
# include <stdio.h>
# include <dos.h>
# include <bios.h>
# include <string.h>
# include <stdio.h>
# include <fcntl.h>
# include <sys\stat.h>
union REGS ipregs;
union REGS opregs;
struct SREGS spregs;
struct REGPACK reg;
struct DoubleWord
{
unsigned int HiWord;
unsigned int LoWord;
};
union LongToWord
{
long dw;
struct DoubleWord w;
};
int GetLockMode();
int SetLockMode();
int LogFile();
int LockFileSet();
int ReleaseFile();
int ReleaseFileSet();
int ClearFile();
int ClearFileSet();
int LockPhysicalRecord();
int LockPhysicalRecordSet();
int ReleasePhysicalRecord();
int ReleasePhysicalRecordSet();
int ClearPhysicalRecord();
int ClearPhysicalRecordSet();
void main()
{
int return_code;
char ch;
int fp0,fp1,fp2;
long recordStartOffset=0;
long recordLength=10;
char lockDirective=0;
int timeoutLimit=72;
char msg[] = "Hello world\n";
int lockMode;
clrscr();
printf("\n The example for Synchronous DOS call\n\n");
lockMode=GetLockMode();
printf("The lockmode is %d now .",lockMode);
printf("\nYou can change the lockmode to :");
scanf("%d",&lockMode);
return_code=SetLockMode(lockMode);
fp0 = creat("file0", S_IREAD |S_IWRITE);
return_code=LogPhysicalRecord (fp0, recordStartOffset, recordLength,
lockDirective, timeoutLimit);
if (return_code==0)
{printf("\nThe first 10 bytes of file 'file0' is logged,press any key to continue!");
ch=getch();}
return_code=LogPhysicalRecord (fp0, recordStartOffset+10, recordLength,
lockDirective+1, timeoutLimit);
if (return_code==0)
{printf("\nThe next 10 bytes of file 'file0' is locked,press any key to continue!");
ch=getch();}
LockPhysicalRecordSet(lockDirective, timeoutLimit);
if (return_code==0)
{printf("\nAll logged record of file 'file0' are locked,press any key to continue!");
ch=getch();
write(fp0, msg, strlen(msg));
}
ReleasePhysicalRecord(fp0, recordStartOffset, recordLength);
if (return_code==0)
{printf("\n\nThe first 10 bytes of file 'file0' is released,press any key to continue!");
ch=getch();}
ClearPhysicalRecord(fp0, recordStartOffset, recordLength);
if (return_code==0)
{printf("\nThe first 10 bytes of file 'file0' is cleared,press any key to continue!");
ch=getch();}
ReleasePhysicalRecordSet();
printf("\nAll locked record of file 'file0' are released,press any key to continue!");
ch=getch();
ClearPhysicalRecordSet();
printf("\nAll logged record of file 'file0' are cleared,press any key to continue!");
ch=getch();
close(fp0);
fp1 = creat("file1", S_IREAD |S_IWRITE);
fp2 = creat("file2", S_IREAD |S_IWRITE);
return_code=LogFile("file1", lockDirective, timeoutLimit);
if (return_code==0)
{printf("\n\nFile 'file1' is logged,press any key to continue!");
ch=getch();}
return_code=LogFile("file2", lockDirective+1, timeoutLimit);
if (return_code==0)
{printf("\nFile 'file2' is locked,press any key to continue!");
ch=getch();}
return_code=LockFileSet(timeoutLimit);
if (return_code==0)
{printf("\nAll logged file are locked,press any key to continue!");
ch=getch();
write(fp1,msg,strlen(msg));
close(fp1);
write(fp2,msg,strlen(msg));
close(fp2);
}
return_code=ReleaseFile("file1");
if (return_code==0)
{printf("\n\nFile 'file1' is released,press any key to continue!");
ch=getch();}
return_code=ClearFile("file1");
if (return_code==0)
{printf("\nFile 'file1' is cleared,press any key to continue!");
ch=getch();}
ReleaseFileSet();
printf("\nAll locked file are released,press any key to continue!");
ch=getch();
ClearFileSet();
printf("\nAll logged file are cleared,press any key to quit!");
ch=getch();
}
int GetLockMode()
{
ipregs.h.al = 0x02;
ipregs.h.ah = 0xC6;
intdosx(&ipregs,&opregs,&spregs);
return(opregs.h.al);
}
int SetLockMode(char lockMode)
{
ipregs.h.al = lockMode;
ipregs.h.ah = 0xC6;
intdosx(&ipregs,&opregs,&spregs);
return(opregs.h.al);
}
int LogFile(char *fileName, char lockDirective, int timeoutLimit)
{
reg.r_ds = FP_SEG(fileName);
reg.r_dx = FP_OFF(fileName);
reg.r_ax = 0xEB<<8 | lockDirective;
reg.r_bp = timeoutLimit;
intr(0x21, ®);
return _AL;
}
int LockFileSet(int timeoutLimit)
{
reg.r_ax = 0xCB<<8 ;
reg.r_bp = timeoutLimit;
intr(0x21, ®);
return _AL;
}
int ReleaseFile(char *fileName)
{
spregs.ds = FP_SEG(fileName);
ipregs.x.dx = FP_OFF(fileName);
ipregs.h.ah = 0xEC;
intdosx(&ipregs,&opregs,&spregs);
return(opregs.h.al);
}
int ReleaseFileSet()
{
ipregs.h.ah = 0xCD;
intdosx(&ipregs,&opregs,&spregs);
return(opregs.h.al);
}
int ClearFile(char *fileName)
{
spregs.ds = FP_SEG(fileName);
ipregs.x.dx = FP_OFF(fileName);
ipregs.h.ah = 0xED;
intdosx(&ipregs,&opregs,&spregs);
return(opregs.h.al);
}
int ClearFileSet()
{
ipregs.h.ah = 0xCF;
intdosx(&ipregs,&opregs,&spregs);
return(opregs.h.al);
}
int LockPhysicalRecordSet(char lockDirective, int timeoutLimit)
{
reg.r_ax = 0xC2<<8 | lockDirective;
reg.r_bp = timeoutLimit;
intr(0x21, ®);
return _AL;
}
int ReleasePhysicalRecordSet()
{
ipregs.h.ah = 0xC3;
intdosx(&ipregs,&opregs,&spregs);
return(opregs.h.al);
}
int ClearPhysicalRecordSet()
{
ipregs.h.ah = 0xC4;
intdosx(&ipregs,&opregs,&spregs);
return(opregs.h.al);
}
int LogPhysicalRecord (fp0, recordStartOffset, recordLength,
lockDirective, timeoutLimit)
int fp0;
long recordStartOffset;
long recordLength;
char lockDirective;
int timeoutLimit;
{
union LongToWord tmp;
tmp.dw = recordStartOffset;
reg.r_dx = tmp.w.HiWord;
reg.r_cx = tmp.w.LoWord;
tmp.dw = recordLength;
reg.r_di =tmp.w.HiWord;
reg.r_si =tmp.w.LoWord;
reg.r_ax = 0xBC<<8 | lockDirective;
reg.r_bx = fp0;
reg.r_bp = timeoutLimit;
intr(0x21, ®);
return _AL;
}
int ReleasePhysicalRecord(fp0, recordStartOffset, recordLength)
int fp0;
long recordStartOffset;
long recordLength;
{
union LongToWord tmp;
tmp.dw = recordStartOffset;
reg.r_dx = tmp.w.HiWord;
reg.r_cx = tmp.w.LoWord;
tmp.dw = recordLength;
reg.r_di =tmp.w.HiWord;
reg.r_si =tmp.w.LoWord;
reg.r_ax = 0xBD<<8 ;
reg.r_bx = fp0;
intr(0x21, ®);
return _AL;
}
int ClearPhysicalRecord(fp0, recordStartOffset, recordLength)
int fp0;
long recordStartOffset;
long recordLength;
{
union LongToWord tmp;
tmp.dw = recordStartOffset;
reg.r_dx = tmp.w.HiWord;
reg.r_cx = tmp.w.LoWord;
tmp.dw = recordLength;
reg.r_di =tmp.w.HiWord;
reg.r_si =tmp.w.LoWord;
reg.r_ax = 0xBE<<8 ;
reg.r_bx = fp0;
intr(0x21, ®);
return _AL;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -