осуществляется работа с одним и тем же файлом. Каждый процесс открывает файл, Один процесс пишет в файл, другой - читает из него.
нужно сделать с блокировками, но никак не получается

int main()
{
    int  fd;
    int fd_1;
    int  ret_status;
    char  buffy[128];
    char buffy1[32];

    sprintf(buffy, "Hello World");

    struct flock fl;
    fl.l_type   = F_WRLCK;  /* F_RDLCK, F_WRLCK, F_UNLCK    */
    fl.l_whence = SEEK_SET; /* SEEK_SET, SEEK_CUR, SEEK_END */
    fl.l_start  = 0;        /* Offset from l_whence         */
    fl.l_len    = 0;        /* length, 0 = to EOF           */
    fl.l_pid    = getpid(); /* our PID                      */

    if (access("tel_position", F_OK) == 0) {
       fd = open("tel_position",O_RDWR|O_TRUNC);}
    else {
       fd = creat("tel_position",S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);}
       fd_1 = open("tel_position",O_RDONLY); /* get the file descriptor */

     fork();

    ret_status = fcntl(fd, F_SETLKW, &fl);
    write(fd,buffy,strlen(buffy));
//    sleep(5);
    fl.l_type   = F_UNLCK;
    fcntl(fd, F_SETLK, &fl); /* set the region to unlocked */
    close(fd);


    fcntl(fd_1, F_SETLKW, &fl);
    while (read(fd_1, buffy1, 32) > 0){
    fl.l_type   = F_UNLCK;   /* tell it to unlock the region */
    fcntl(fd_1, F_SETLK, &fl); /* set the region to unlocked */
    close(fd_1);

    //sscanf (buffy1,"%s" );
    printf("telescope position:", buffy1);}
}

2

В случае разных потоков это делается через семафор или mutex,
для процессов, запускаемых через fork, не прообовал.

Пётр.

Пётр.

3 (18.12.2013 12:53:07 отредактировано marikk)

Не совсем понятно что нужно и что не получается.
Пример поочередного захвата файлового дескриптора

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <pthread.h>
#include <sys/epoll.h>
#include <sys/file.h>
#include <unistd.h>

int main ()
{
  int s, i;
  struct flock fl;

  if ((s=open ("/tmp/man.txt", O_RDWR | O_CREAT)) < 0) {
    perror ("not open");
    return;
  }

  fl.l_whence = 0; /* SEEK_SET, SEEK_CUR, SEEK_END */
  fl.l_start  = 0;        /* Offset from l_whence         */
  fl.l_len    = 0;        /* length, 0 = to EOF           */

  int st = fork ();
  switch (st) {
   case -1:
     perror ("Ошибка нового процесса");
     break;
   case 0:
     fprintf (stderr, "Дочерний процесс создан \n");
     fl.l_type = F_RDLCK;
     if (fcntl(s, F_SETLKW, &fl) == 0)
       fprintf (stderr, "дочерний процесс захватил блокировку чтения \n");
     else
       perror ("блокировка чтения дочерним процессом провалена");

     sleep (10);
     close (s);
     fprintf (stderr, "завершился дочерний процесс \n");
     break;
   default:
     fprintf (stderr, "родительский процесс продолжает \n");
     fl.l_type = F_WRLCK;
     if (fcntl(s, F_SETLKW, &fl) == 0)
       fprintf (stderr, "родительский процесс захватил блокировку записи \n");
     else
       perror ("блокировка запси родительским процессом провалена");

     sleep (10);
     close (s);
     fprintf (stderr, "родительский процесс завершился \n");
     break;
  }
}