There are 2 threat to run at the same time. For example, there are number 1 and number 2, how can I run it at the same time. my coding still exit some problem, number 2 can not restore the previous number after I press ctrl^c, can anyone help me to debug it,thanks a lot:
Code:
#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <setjmp.h>
#include <conio.h>
jmp_buf buf_main, buf_p1, buf_p2;
void processOne();
void processTwo();
void main(void)
{
char line[81];
char option;
int retVal;
for (;;)
{
retVal=setjmp(buf_main);
printf("\nEnter process number to execute the process, 'e' to exit: ");
gets(line);
option=line[0];
switch(option)
{ case '1':
if (retVal==0)
processOne();
else
longjmp(buf_p1,1);
break;
case '2':
if (retVal==1)
processTwo();
else
longjmp(buf_p2,1);
break;
case 'e':
exit(0);
break;
}
}
}
void processOne()
{
int breakRoutine();
register clocktick=0;
ctrlbrk(breakRoutine);
for (;;)
{ setjmp(buf_p1);
clrscr();
printf("Clock of process 1 is %d", ++clocktick);
delay(500);
}
}
void processTwo()
{
int breakRoutine();
register clocktick=0;
ctrlbrk(breakRoutine);
for (;;)
{ setjmp(buf_p2);
clrscr();
printf("Clock of process 2 is %d", ++clocktick);
delay(500);
}
}
int breakRoutine()
{
longjmp(buf_main,1);
return 1;
}