Читаем Programming with POSIX® Threads полностью

16 if (strlen (line) <= 1) continue;

17

18 /*

19 * Parse input line into seconds (%d) and a message

20 * (%64[^\n]), consisting of up to 64 characters

21 * separated from the seconds by whitespace.

22 */

23 if (sscanf (line, "%d %64[^\n]",

24 &seconds, message) < 2) {

25 fprintf (stderr, "Bad command\n");

26 } else {

27 pid = fork ( );

28 if (pid == (pid_t)-l)

29 errno_abort ("Fork");

30 if (pid == (pid_t)0) {

31 /*

32 * In the child, wait and then print a message

33 */

34 sleep (seconds);

35 printf ("(%d) %s\n", seconds, message);

36 exit (0);

37 } else {

38 /*

39 * In the parent, call waitpid() to collect children

40 * that have already terminated.

41 */

42 do {

43 pid = waitpid ((pid_t)-l, NULL, WNOHANG);

44 if (pid == (pid_t)-l)

45 errno_abort ("Wait for child");

46 } while (pid != (pid_t)0);

47 }

48 }

49 }

<p><strong>1.5.3 A version using multiple threads</strong></p>

Now, let us try another alarm program, alarm_thread.c. It is much like the fork version in alarm_fork.c, except that it uses threads instead of child processes to create asynchronous alarms. Four Pthreads calls are used in this program:

• pthread_create creates a thread running the routine specified in the third argument (alarm_thread), returning an identifier for the new thread to the variable referenced by thread.

• pthread_detach allows Pthreads to reclaim the thread's resources as soon as it terminates.

• pthread_exit terminates the calling thread.

• pthread_self returns the calling thread's identifier.

4-7 The alarm_t structure defines the information stored for each alarm command, the number of seconds until the alarm is due, and the message string that will be printed by the thread.

■ alarm_thread.c part 1 definitions

1 #include

2 #include "errors.h" 3

4 typedef struct alarm_tag {

5 int seconds;

6 char message[64];

7 } alarm_t;

1-8 The alarm_thread function is the "alarm thread." That is, each alarm thread is created running this function, and when the function returns the thread terminates. The function's argument (void *arg) is the fourth argument that was passed to pthread_create, in this case, a pointer to the control packet (alarm_t) created for the alarm request that the thread is to satisfy. The thread starts by "mapping" the void * argument as a pointer to a control packet. The thread detaches itself by calling pthread_detach, which informs Pthreads that the application does not need to know when the thread terminates or its termination status.

9-12 The thread sleeps for the number of seconds specified in its control packet, and then prints the message string. Finally, the thread frees the control packet and returns. When a thread returns from its initial routine, as it does here, the thread terminates. Normally, Pthreads would hold the thread's resources so that another thread could later determine that it had exited and retrieve a final result. Because the thread detached itself, none of that is necessary.

■ alarm_thread.c part 2 alarm_thread

1 void *alarm_thread (void *arg)

2 {

3 alarm_t *alarm = (alarm_t*)arg;

4 int status;

5

6 status = pthread_detach (pthread_self ());

7 if (status != 0)

8 err_abort (status, "Detach thread");

9 sleep (alarm->seconds);

10 printf ("(%d) %s\n", alarm->seconds, alarm->message);

11 free (alarm);

12 return NULL;

13 }

The main program of alarm_thread.c is much the same as the other two variants. It loops, reading and interpreting command lines as long as it can read from stdin.

12-25 In this variation, main allocates heap storage (alarm_t) for each alarm command. The alarm time and message are stored in this structure, so each thread can be given the appropriate information. If the sscanf call fails to "parse" a correct command, the heap storage is freed.

Перейти на страницу:

Похожие книги

1С: Бухгалтерия 8 с нуля
1С: Бухгалтерия 8 с нуля

Книга содержит полное описание приемов и методов работы с программой 1С:Бухгалтерия 8. Рассматривается автоматизация всех основных участков бухгалтерии: учет наличных и безналичных денежных средств, основных средств и НМА, прихода и расхода товарно-материальных ценностей, зарплаты, производства. Описано, как вводить исходные данные, заполнять справочники и каталоги, работать с первичными документами, проводить их по учету, формировать разнообразные отчеты, выводить данные на печать, настраивать программу и использовать ее сервисные функции. Каждый урок содержит подробное описание рассматриваемой темы с детальным разбором и иллюстрированием всех этапов.Для широкого круга пользователей.

Алексей Анатольевич Гладкий

Программирование, программы, базы данных / Программное обеспечение / Бухучет и аудит / Финансы и бизнес / Книги по IT / Словари и Энциклопедии
1С: Управление торговлей 8.2
1С: Управление торговлей 8.2

Современные торговые предприятия предлагают своим клиентам широчайший ассортимент товаров, который исчисляется тысячами и десятками тысяч наименований. Причем многие позиции могут реализовываться на разных условиях: предоплата, отсрочка платежи, скидка, наценка, объем партии, и т.д. Клиенты зачастую делятся на категории – VIP-клиент, обычный клиент, постоянный клиент, мелкооптовый клиент, и т.д. Товарные позиции могут комплектоваться и разукомплектовываться, многие товары подлежат обязательной сертификации и гигиеническим исследованиям, некондиционные позиции необходимо списывать, на складах периодически должна проводиться инвентаризация, каждая компания должна иметь свою маркетинговую политику и т.д., вообщем – современное торговое предприятие представляет живой организм, находящийся в постоянном движении.Очевидно, что вся эта кипучая деятельность требует автоматизации. Для решения этой задачи существуют специальные программные средства, и в этой книге мы познакомим вам с самым популярным продуктом, предназначенным для автоматизации деятельности торгового предприятия – «1С Управление торговлей», которое реализовано на новейшей технологической платформе версии 1С 8.2.

Алексей Анатольевич Гладкий

Финансы / Программирование, программы, базы данных