China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-07-01 22:01
中国DOS联盟论坛 » DOS疑难解答 & 问题讨论 (解答室) » Ctrl+Break interrupt under DOS? View 1,100 Replies 13
Original Poster Posted 2007-10-31 13:57 ·  中国 河南 郑州 联通
初级用户
Credits 91
Posts 32
Joined 2007-06-22 16:33
19-year member
UID 92089
Gender Male
Status Offline
I have a DOS system and want to use its Ctrl+Break interrupt, and do file operations in its interrupt service routine, that is, read one file and write it to another file. But when operating on files, it always hangs, and I don't know why. I hope you experts won't hesitate to teach me! QQ:36014789
The program is as follows

#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <process.h>
#include <sys b.h>
#include <fcntl.h>
#include <sys\stat.h>
#include <share.h>
#include <IO.H>

#ifdef __cplusplus
#define __ARGU ...
#else
#define __ARGU
#endif

void interrupt (*oldhandler)(void); /*function declaration*/
void interrupt newint(__ARGU); /*function declaration*/
void install (void interrupt (*fadd)(__ARGU), int num);


main()
{
install (newint,0x1b); /*Ctrl+Break interrupt number: 1BH*/
keep(0,_SS+(_SP/16)-_psp); /*TSR program*/
return 0;
}

void interrupt newint(__ARGU)
{ int fd1=-1; //file number for reading, returned by the open function;
int fd2=-1; //file number for writing, returned by the open function;
int read_res=-1; //return value of the read function, -1 means error;
int write_res=-1; //return value of the write function, -1 means error;
char buf[10]; //buffer; //file number for reading, returned by the open function;

disable();
fd1=open("c:\\fr.txt", O_CREAT | O_TRUNC | O_TEXT, S_IREAD|S_IWRITE);
fd2=open("c:\\fw.txt", O_CREAT | O_TRUNC | O_TEXT, S_IREAD|S_IWRITE);

if(fd1==-1||fd2==-1)
puts("Open file failure!\n");
else
{
puts("Open files success!\n");
//fseek cannot be used, because it involves DOS reentrancy issues; only unbuffered file system functions can be used;

lseek(fd1,0,SEEK_SET);//move the file position pointer to the specified location;
//fread cannot be used, because it involves DOS reentrancy issues; only unbuffered file system functions can be used;
read_res=read(fd1,buf,COUNT); //file reading function;
if(read_res==-1) //if file reading fails;
puts("Read file failure!\n");
else
{
puts("Read files success!\n");
//fseek cannot be used, because it involves DOS reentrancy issues; only unbuffered file system functions can be used;
lseek(fd2,0,SEEK_SET);//move the file position pointer to the specified location;
//fwrite cannot be used, because it involves DOS reentrancy issues; only unbuffered file system functions can be used;
write_res=write(fd2,buf,COUNT); //file writing function;
if(write_res==-1) //if file writing fails;
puts("Write file failure!\n");
else
puts("Write files success!\n");
}
close(fd1); //close file;
close(fd2); //close file;
}
enable();
}


void install (void interrupt (*fadd)(__ARGU), int num)
{
disable(); //disable interrupts;
setvect(num,fadd); /*set interrupt*/
enable(); //enable interrupts;
}
Floor 2 Posted 2007-10-31 15:29 ·  中国 广东 江门 新会区 电信
初级用户
★★
Credits 117
Posts 57
Joined 2007-10-23 20:57
18-year member
UID 100584
Gender Male
Status Offline
The Ctrl+Break interrupt cannot return with IRET, only with RET
Floor 3 Posted 2007-10-31 17:08 ·  IANA 局域网IP(Private-Use)
新手上路
Credits 2
Posts 1
Joined 2007-10-31 15:55
18-year member
UID 101298
Gender Male
Status Offline
Oh... then I need to go try that
Floor 4 Posted 2007-11-01 09:03 ·  中国 河南 郑州 联通
初级用户
Credits 91
Posts 32
Joined 2007-06-22 16:33
19-year member
UID 92089
Gender Male
Status Offline
To jojand

I don't quite understand what you mean. File operations cannot be done in the Ctrl+Break interrupt because the Ctrl+Break interrupt cannot return with IRET, only with RET? If that's the case, then I still need to look up how IRET\RET are used. My QQ: 36014789, I hope to get your guidance
Floor 5 Posted 2007-11-01 14:45 ·  中国 广东 江门 新会区 电信
初级用户
★★
Credits 117
Posts 57
Joined 2007-10-23 20:57
18-year member
UID 100584
Gender Male
Status Offline
Even if an unbuffered file system is used, it still cannot guarantee that reentrancy is safe.
Floor 6 Posted 2007-11-02 13:55 ·  中国 河南 郑州 联通
初级用户
Credits 91
Posts 32
Joined 2007-06-22 16:33
19-year member
UID 92089
Gender Male
Status Offline
To jojand
Then how should this be handled? Give me an idea
Floor 7 Posted 2007-11-02 16:23 ·  中国 广东 江门 电信
初级用户
★★
Credits 117
Posts 57
Joined 2007-10-23 20:57
18-year member
UID 100584
Gender Male
Status Offline
One method is to hook the ctrl+break interrupt onto int28, and only allow the ctrl+break interrupt during int28.
Another method is to monitor int21, and before the system enters int21, first save DOS's internal data area, then restore it after execution. But this method depends heavily on DOS. The data structures and data locations differ between different DOS versions, so it is quite difficult to use.
Floor 8 Posted 2007-11-02 16:34 ·  中国 广东 江门 电信
初级用户
★★
Credits 117
Posts 57
Joined 2007-10-23 20:57
18-year member
UID 100584
Gender Male
Status Offline
There is another method: monitor int13, and immediately after int13 completes, check whether there is a ctrl+break interrupt; if there is, execute the interrupt; if not, return.

In short, if you want the ctrl+break interrupt to work properly under all circumstances, at minimum you have to monitor int10, int13, and int16
Floor 9 Posted 2007-11-03 10:17 ·  中国 河南 郑州 联通
初级用户
Credits 91
Posts 32
Joined 2007-06-22 16:33
19-year member
UID 92089
Gender Male
Status Offline
Original DOS system situation:
There is a program A running normally, generating data and storing it in file A.

Design requirement:
Now there is a board using the 8253 for timer interrupts, and it is connected to IRQ3, so interrupts can be generated normally; now I have made a TSR interrupt program, and I want to use the IRQ3 interrupt to operate on file A. I used the ctrl+break interrupt to simulate file operations inside an interrupt, but it always hangs. I hope capable friends can help solve this together;

I am willing to use one month's food expenses as a reward!
QQ:36014789
Floor 10 Posted 2007-11-03 16:17 ·  中国 广东 江门 新会区 电信
初级用户
★★
Credits 117
Posts 57
Joined 2007-10-23 20:57
18-year member
UID 100584
Gender Male
Status Offline
If you write to a file in an interrupt like this, the file will definitely be corrupted

Maybe it can be done this way:
When the IRQ3 interrupt occurs, set a flag, then monitor Int13, and before each call to int13, check the IRQ3 interrupt flag. If it is set, call close file; if not, continue the original operation.

[ Last edited by jojand on 2007-11-3 at 04:21 PM ]
在她弯下腰的瞬间 我彷佛看见 那神秘柔和的曲线 如云雾中岭壑的蜿蜒
Floor 11 Posted 2007-11-05 11:33 ·  中国 河南 郑州 联通
初级用户
Credits 91
Posts 32
Joined 2007-06-22 16:33
19-year member
UID 92089
Gender Male
Status Offline
To jojand
Is my design flow reasonable? Can program A and the IRQ3 TSR interrupt program not work interactively?
Can file operations actually be done inside an interrupt or not

[ Last edited by xyh36014789 on 2007-11-5 at 11:36 AM ]
Floor 12 Posted 2007-11-05 14:53 ·  中国 广东 江门 新会区 电信
初级用户
★★
Credits 117
Posts 57
Joined 2007-10-23 20:57
18-year member
UID 100584
Gender Male
Status Offline
As long as a program can achieve the design goal, it is reasonable.
In DOS, operating on files inside an interrupt is absolutely unsafe.
在她弯下腰的瞬间 我彷佛看见 那神秘柔和的曲线 如云雾中岭壑的蜿蜒
Floor 13 Posted 2007-11-06 10:38 ·  中国 河南 郑州 联通
初级用户
Credits 91
Posts 32
Joined 2007-06-22 16:33
19-year member
UID 92089
Gender Male
Status Offline
I have an IRQ3 interrupt, the hardware has already been initialized, and it can generate interrupts, but the program still feels somewhat incomplete, and I don't know what needs to be added?
The program below is IRQ3 implementing the timer interrupt function. Right now it prints "Welcome to JHW of ZhengZhou" (it was originally meant for timed file operations). If this program runs at boot, there is no problem at all, but as soon as I press the keyboard, the program stops. I don't know why. Is it because the effect of the keyboard interrupt was not considered in the program? I don't know how to hook int 13H and int 28H
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <process.h>
#include <sys/timeb.h>
#include <fcntl.h> d4
#include <sys\stat.h>
#include <share.h>
#include <IO.H>

#define INTR 0X0b
#define Freq1 400
#define Freq2 10000
#define Data1_add 0x2bc
#define Data2_add 0x2bd
#define Control_add 0x2bf
#ifdef __cplusplus
#define __CPPARGS ...
#else
#define __CPPARGS
#endif

void interrupt ( *oldhandler)(__CPPARGS);
void interrupt handler(__CPPARGS);
void init8253();
void install_newtimer();
int main(void)
{
install_newtimer();//install interrupt
init8253(); //initialize
keep(0,_SS+(_SP/16)-_psp); //stay resident
return 0;
}

//install interrupt
void install_newtimer()
{
disable();
oldhandler = getvect(INTR);
setvect(INTR, handler);
zd=inportb(0x21);
delay(1);
outportb(0x21,zd&~(0x08));
enable();
}
//interrupt function
void interrupt handler(__CPPARGS)
{
disable();

puts("Welcome to JHW of ZhengZhou\n");

//should there be a return here????؟؟؟؟؟؟

outportb(0x20,0x20);
enable();
}

//hardware initialization already successful
void init8253()
{
....
}
Floor 14 Posted 2007-11-06 10:40 ·  中国 河南 郑州 联通
初级用户
Credits 91
Posts 32
Joined 2007-06-22 16:33
19-year member
UID 92089
Gender Male
Status Offline
jojand
What's your QQ number? I have some materials to send you to take a look at
Forum Jump: