Тексты программ Текст программы primes1.c 1 #include <stdio.h> 2 #include <signal.h> 3 #define OUTPUT "Primes" 4 #define MAXNUM 10000 5 6 int count; 7 FILE *fptr; 8 9 main() 10 { 11 int number, divisor; 12 void sigcatch(); 13 14 fptr = fopen(OUTPUT, "w"); 15 signal(SIGINT, sigcatch); 16 for (number = 1; number <= MAXNUM; number++) { 17 for (divisor = 2; divisor < number; divisor++) 18 if ((number % divisor) == 0) 19 break; 20 if (divisor == number) { 21 fprintf(fptr, "%d\n", number); 22 count++; 23 } 24 } 25 fclose(fptr); 26 } 27 28 29 void sigcatch() 30 { 31 printf("%d primes computed\n", count); 32 fclose(fptr); 33 exit(1); 34 } Текст программы - primes2.c 1 #include <stdio.h> 2 #include <signal.h> 3 #define OUTPUT "Primes" 4 #define MAXNUM 10000 5 int count; 6 FILE *fptr; 7 8 main() 9 { 10 int number, divisor; 11 void sigcatch(int); 12 13 fptr = fopen(OUTPUT, "w"); 14 signal(SIGINT, sigcatch); 15 signal(SIGQUIT, sigcatch); 16 for (number = 1; number <= MAXNUM; number++) { 17 for (divisor = 2; divisor < number; divisor++) 18 if ((number % divisor) == 0) 19 break; 20 if (divisor == number) { 21 fprintf(fptr, "%d\n", number); 22 count++; 23 } 24 } 25 fclose(fptr); 26 } 27 28 29 void sigcatch(int sig) 30 { 31 signal(sig, SIG_IGN); 32 printf("%d primes computed\n", count); 33 if (sig == SIGQUIT) { 34 fclose(fptr); 35 exit(1); 36 } 37 signal(sig, sigcatch); 38 } Текст программы - setjmp.c 1 #include <signal.h> 2 #include <setjmp.h> 3 jmp_buf env1; 4 void ljmain(int); 5 6 main() { 7 int retcode; 8 signal(SIGINT, ljmain); 9 retcode = setjmp(env1); 10 if (retcode != 0) printf("\nBefore loop\n"); 11 12 while( 1 ) menu(); 13 } 14 15 menu() { 16 char resp[2]; 17 prompt: printf("cmd: "); 18 scanf("%s",resp); 19 switch(resp[0]){ 20 case 's': sfn(); break; 21 case 't': tfn(); break; 22 case 'q': exit(0); 23 default: printf("?\n"); 24 goto prompt; 25 } 26 } 27 28 void ljmain(int sig) 29 { 30 signal(SIGINT, ljmain); 31 printf("\nINTERRUPTED\n"); 32 longjmp(env1, 1); 33 } 34 35 sfn() 36 { 37 int i, j=0; 38 39 for(i=0;;i++){ 40 printf("%d\t", i*i); 41 if(++j % 8 == 0) printf("\n"); 42 } 43 } 44 45 #include <sys/types.h> 46 #include <time.h> 47 48 tfn() 49 { 50 time_t now; 51 52 (void) time(&now); 53 printf("%s", ctime(&now)); 54 } Текст программы - primes3.c 1 #include <stdio.h> 2 #include <signal.h> 3 #define OUTPUT "Primes" 4 #define MAXNUM 10000 5 int count; 6 FILE *fptr; 7 8 main() 9 { 10 int number, divisor; 11 void sigcatch(); 12 13 fptr = fopen(OUTPUT, "w"); 14 sigset(SIGINT, sigcatch); 15 sigset(SIGQUIT, sigcatch); 16 for (number = 1; number <= MAXNUM; number++) { 17 for (divisor = 2; divisor < number; divisor++) 18 if ((number % divisor) == 0) 19 break; 20 if (divisor == number) { 21 fprintf(fptr, "%d\n", number); 22 count++; 23 } 24 } 25 fclose(fptr); 26 } 27 28 void sigcatch(sig) 29 int sig; 30 { 31 printf("%d primes computed\n", count); 32 if (sig == SIGQUIT) { 33 fclose(fptr); 34 exit(1); 35 } 36 }