Новые книги

This document contains reference on SGI STL implementation
With this practical book, you will attain a solid understanding of threads and will discover how to put this powerful mode of programming to work in real-world applications.

The primary advantage of threaded programming is that it enables your applications to accomplish more than one task at the same time by using the number-crunching power of multiprocessor parallelism and by automatically exploiting I/O concurrency in your code, even on a single processor machine. The result: applications that are faster, more responsive to users, and often easier to maintain. Threaded programming is particularly well suited to network programming where it helps alleviate the bottleneck of slow network I/O.

This book offers an in-depth description of the IEEE operating system interface standard, POSIX (Portable Operating System Interface) threads, commonly called Pthreads. Written for experienced C programmers, but assuming no previous knowledge of threads, the book explains basic concepts such as asynchronous programming, the lifecycle of a thread, and synchronization. You then move to more advanced topics such as attributes objects, thread-specific data, and realtime scheduling. An entire chapter is devoted to "real code," with a look at barriers, read/write locks, the work queue manager, and how to utilize existing libraries. In addition, the book tackles one of the thorniest problems faced by thread programmers-debugging-with valuable suggestions on how to avoid code errors and performance problems from the outset.

Numerous annotated examples are used to illustrate real-world concepts. A Pthreads mini-reference and a look at future standardization are also included.

Примеры.

Пример 14

/* Демонстрация работы с longjmp/setjmp и сигналами */
/* По мотивам книги М.Дансмура и Г.Дейвиса.         */
#include <stdio.h>
#include <fcntl.h>
#include <signal.h>
#include <setjmp.h>
/*#define IGN*/         /* потом откомментируйте эту строку */
jmp_buf cs_stack;       /* control point */
int in_cs;              /* флаг, что мы в критической секции */
int sig_recd;           /* флаг signal received */
/* активная задержка */
Delay(){
	int i; for( i=0; i < 10000; i++ ){ i += 200; i -= 200; }
}
interrupt( code ){
	fprintf( stderr, "\n\n***\n" );
	fprintf( stderr, "*** Обрабатываем сигнал (%s)\n",
			      code == 1 ? "разрешенный" : "отложенный" );
	fprintf( stderr, "***\n\n" );
}
/* аргумент реакции на сигнал - номер сигнала (подставляется системой) */
void mexit( nsig ){
  fprintf( stderr, "\nУбили сигналом #%d...\n\n", nsig ); exit(0);
}
void main(){
    extern void sig_vec(); int code; int killable = 1;
    signal( SIGINT,  mexit );
    signal( SIGQUIT, mexit );
 fprintf( stderr, "Данная программа перезапускается по сигналу INTR\n" );
 fprintf( stderr, "Выход из программы по сигналу QUIT\n\n\n" );
 fprintf( stderr, "Сейчас вы еще можете успеть убить эту программу...\n\n" );
    Delay(); Delay(); Delay();
    for(;;){
	if( code = setjmp( cs_stack )){
		/* Возвращает не 0, если возврат в эту точку произошел
		 * по longjmp( cs_stack, code ); где code != 0
		 */
		interrupt( code );    /* пришло прерывание */
	} /* else setjmp() возвращает 0,
	   * если это УСТАНОВКА контрольной точки (то есть
	   * сохранение регистров SP, PC и других в буфер cs_stack),
	   * а не прыжок на нее.
	   */
	signal( SIGINT, sig_vec ); /* вызывать по прерыванию */
	if( killable ){
	  killable = 0;
	  fprintf( stderr,
"\7Теперь сигналы INTR обрабатываются особым образом\n\n\n" );
	}
	body();                 /* основная программа */
    }
}
body(){
	static int n = 0; int i;
	fprintf( stderr, "\tВошли в тело %d-ый раз\n", ++n );
	ecs();
	for( i=0; i < 10 ; i++ ){
		fprintf( stderr, "- %d\n",i); Delay();
	}
	lcs();
	for( i=0; i < 10 ; i++ ){
		fprintf( stderr, "+ %d\n",i); Delay();
	}
}
/* запоминание полученных сигналов */
void sig_vec(nsig){
      if( in_cs ){    /* we're in critical section */
#ifdef IGN
	signal( SIGINT, SIG_IGN );      /* игнорировать */
	fprintf( stderr, "Дальнейшие прерывания будут игнорироваться\n" );
#else
	signal( SIGINT, sig_vec );
	fprintf( stderr, "Дальнейшие прерывания будут подсчитываться\n" );
#endif
	fprintf( stderr, "Получен сигнал и отложен\n" );
	sig_recd++  ;  /* signal received */
		       /* пометить, что сигнал пришел */
      }else{
	signal( SIGINT, sig_vec );
	fprintf( stderr, "Получен разрешенный сигнал: прыгаем на рестарт\n" );
	longjmp( cs_stack, 1);
      }
}
ecs(){  /* enter critical section */
	fprintf( stderr, "Откладываем прерывания\n" );
	sig_recd = 0;    in_cs = 1;
}
lcs(){  /* leave critical section */
    fprintf( stderr, "Разрешаем прерывания\n" );
    in_cs = 0;
    if( sig_recd ){
	fprintf( stderr,
	    "Прыгаем на рестарт, т.к. есть отложенный сигнал (%d раз)\n",
	    sig_recd );
	sig_recd = 0;
	signal( SIGINT, sig_vec );
	longjmp( cs_stack, 2);
    }
}

© Copyright А. Богатырев, 1992-95
Си в UNIX

Назад | Содержание | Вперед