Читаем Linux программирование в примерах полностью

45    argv[0], argv[1], rw_mode, strerror(errno));

46   (void)close(fd);

47   return 1;

48  }

49

50  /* заблокировать файл */

51  memset(&lock, '\0', sizeof(lock));

52  lock.l_whence = SEEK_SET;

53  lock.l_start = 0;

54  lock.l_len =0; /* блокировка всего файла */

55  lock.l_type = F_WRLCK; /* блокировка записи */

56

57  if (fcntl(fd, F_SETLK, &lock) < 0) {

58   fprintf(stderr, "%s: %s: cannot lock the file: %s\n",

59    argv[0], argv[1], strerror(errno));

60   (void)close(fd);

61   return 1;

62  }

63

64  pause();

65

66  (void)close(fd);

67

68  return 0;

69 }

Программа устанавливает права доступа и создает файл, указанный в командной строке (строки 25 и 26). Затем она записывает в файл некоторые данные (строка 34). Строка 41 добавляет к правам доступа бит setgid, а строка 43 изменяет их. (Системный вызов fchmod() обсуждался в разделе 5.5.2 «Изменение прав доступа: chmod() и fchmod()».)

Строки 51–55 устанавливают struct flock для блокировки всего файла, а затем блокировка осуществляется реально в строке 57. Выполнив блокировку, программа засыпает, используя системный вызов pause() (см. раздел 10.7 «Сигналы для межпроцессного взаимодействия»). После этого программа закрывает дескриптор файла и завершается. Вот расшифровка с комментариями, демонстрирующая использование обязательной блокировки файлов:

$ fdformat /dev/fd0 /* Форматировать гибкий диск */

Double -sided, 80 tracks, 18 sec/track. Total capacity 1440 kB.

Formatting ... done

Verifying ... done

$ /sbin/mke2fs /dev/fd0 /* Создать файловую систему Linux */

/* ...множество вывода опущено... */

$ su /* Стать root, чтобы использовать mount */

Password: /* Пароль не отображается */

# mount -t ext2 -о mand /dev/fd0 /mnt/floppy /* Смонтировать гибкий

диск, с возможностью блокировок */

# suspend /* Приостановить оболочку root */

[1]+ Stopped su

$ ch14-lockall /mnt/floppy/x & /* Фоновая программа */

[2] 23311 /* содержит блокировку */

$ ls -l /mnt/floppy/x /* Посмотреть файл */

-rw-r-Sr-- 1 arnold devel 13 Apr 6 14:23 /mnt/floppy/x

$ echo something > /mnt/floppy/x /* Попытаться изменить файл */

bash2: /mnt/floppy/x: Resource temporarily unavailable

 /* Возвращается ошибка */

$ kill %2 /* Завершить программу с блокировкой */

$ /* Нажать ENTER */

[2]- Terminated ch14-lockall /mnt/floppy/x /* Программа завершена */

$ echo something > /mnt/floppy/x /* Новая попытка изменения работает */

$ fg /* Вернуться в оболочку root */

su

# umount /mnt/floppy /* Демонтировать гибкий диск */

# exit /* Работа с оболочкой root закончена */

$

Пока выполняется ch14-lockall, она владеет блокировкой. Поскольку это обязательная блокировка, перенаправления ввода/вывода оболочки завершаются неудачей. После завершения ch14-lockall блокировки освобождаются, и перенаправление ввода/вывода достигает цели. Как упоминалось ранее, под GNU/Linux даже root не может аннулировать обязательную блокировку файла.

Немного отклоняясь в сторону, гибкие диски представляют отличный испытательный стенд для изучения того, как использовать инструменты, работающие с файловыми системами. Если вы сделаете что-то, что разрушит данные на гибком диске, это вряд ли будет катастрофическим, тогда как экспериментирование с действующими разделами на обычных жестких дисках значительно более рискованно.

<p>14.3. Более точное время</p>
Перейти на страницу:

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

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

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