Вход/Регистрация
Основы программирования в Linux
вернуться

Мэтью Нейл

Шрифт:

int main {

 int file_desc;

 struct flock region_to_lock;

 int res;

 file_desc = open(test_file, O_RDWR | O_CREAT, 0666);

 if (!file_desc) {

fprintf(stderr, "Unable to open %s for read/write\n", test_file);

exit(EXIT_FAILURE);

 }

В оставшейся части программы задаются разные участки файла, и делается попытка установить для них блокировки разных типов:

 region_to_lock.l_type = F_RDLCK;

 region_to_lock.l_whence = SEEK_SET;

 region_to_lock.l_start = 10;

 region_to_lock.l_len = 5;

 printf("Process %d, trying F_RDLCK, region %d to %d\n", getpid, (int)region_to_lock.l_start,

(int)(region_to_lock.l_start + region_to_lock.l_len));

 res = fcntl(file_desc, F_SETLK, &region_to_lock);

 if (res == -1) {

printf("Process %d - failed to lock region\n", getpid);

 } else {

printf("Process %d — obtained lock region\n", getpid);

 }

 region_to_lock.l_type = F_UNLCK;

 region_to_lock.l_whence = SEEK_SET;

 region_to_lock.l_start = 10;

 region_to_lock.l_len = 5;

 printf("Process %d, trying F_UNLCK, region %d to %d\n", getpid, (int)region_to_lock.l_start,

(int)(region_to_lock.l_start + region_to_lock.l_len));

 res = fcntl(file_desc, F_SETLK, &region_to_lock);

 if (res == -1) {

printf("Process %d — failed to unlock region\n", getpid);

 } else {

printf("Process %d — unlocked region\n", getpid);

 }

 region_to_lock.l_type = F_UNLCK;

 region_to_lock.l_whence = SEEK_SET;

 region_to_lock.l_start = 0;

 region_to_lock.l_len = 50;

 printf("Process %d, trying F_UNLCK, region %d to %d\n", getpid", (int)region_to_lock.l_start,

(int)(region_to_lock.l_start + region_to_lock.l_len));

 res = fcntl(file_desc, F_SETLK, &region_to_lock);

 if (res == -1) {

printf("Process %d — failed to unlock region\n", getpid);

 } else {

printf("Process %d — unlocked region\n", getpid);

 }

 region_to_lock.l_type = F_WRLCK;

 region_to_lock.l_whence = SEEK_SET;

 region_to_lock.lstart = 16;

 region_to_lock.l_len = 5;

 printf("Process %d, trying F_WRLCK, region %d to %d\n", getpid, (int)region_to_lock.l_start,

(int)(region_to_lock.l_start + region_to_lock.l_len));

 res = fcntl(file_desc, F_SETLK, &region_to_lock);

 if (res == -1) {

printf("Process %d — failed to lock region\n", getpid);

 } else {

printf("Process %d — obtained lock on region\n", getpid);

 }

 region_to_lock.l_type = F_RDLCK;

 region_to_lock.l_whence = SEEK_SET;

 region_to_lock.l_start = 40;

 region_to_lock.l_len = 10;

 printf("Process %d, trying F_RDLCK, region %d to %d\n", getpid, (int)region_to_lock.l_start,

(int)(region_to_lock.l_start + region_to_lock.l_len));

 res = fcntl(filedesc, F_SETLK, &region_to_lock);

 if (res == -1) {

printf("Process %d — failed to lock region\n", getpid);

 } else {

printf("Process %d — obtained lock on region\n", getpid);

  • Читать дальше
  • 1
  • ...
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • ...

Ебукер (ebooker) – онлайн-библиотека на русском языке. Книги доступны онлайн, без утомительной регистрации. Огромный выбор и удобный дизайн, позволяющий читать без проблем. Добавляйте сайт в закладки! Все произведения загружаются пользователями: если считаете, что ваши авторские права нарушены – используйте форму обратной связи.

Полезные ссылки

  • Моя полка

Контакты

  • chitat.ebooker@gmail.com

Подпишитесь на рассылку: