[ antix @ 21.05.2003. 19:11 ] @
evo sta me zanima:
kako sistemski poziv read (pri radu sa pajpovima) zna da li treba
da se blokira i da ceka sledeci upis ili vise niko nece pisati u taj
pajp pa treba da zavrsi sa radom??? Naime, zamislite
da jedan proces pravi svog potomka i potomak ce da salje roditelju
neke podatke (u vise navrata) preko pajpa, a roditelj ce da cita te
podatke (u vise navrata) iz tog istog pajpa... evo otprilike koda:


...
int tmp,status,pid,i;
int [2] fd;
tip podaci;
...
tmp=pipe(fd);
pid=fork();
if(pid=0){
tmp=close(fd[1]);
for(i=0;i<200;i++){
tmp=write(fd[0],&podaci,velicina_podataka)
}
}else{
tmp=read(fd[1],&podaci,velicina_podataka);
while(tmp>0){
ispisipodatke();
tmp=read(fd[1],&podaci,velicina_podataka)
}
}
...

e, sad posto poslije poziva fork()-a moze da se izvrsava ili roditelj
ili potomak (ne znamo) zamislite da se prvo izvrsi roditelj i proba
da procita prvi podatak!!! Posto potomak nije upisao nista u pajp onda
ce on da procita 0 bajtova i kraj???? Kako zna da treba da ceka upis, a ne da
se zavrsi bas tu???
[ leka @ 22.05.2003. 10:26 ] @
Sa fcntl() fino podesavas svaki file descriptor.
Dakle, preporucujem man fcntl.

Evo sta kaze man read() :
Code:

RETURN VALUE
       On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this
       number.   It  is  not an error if this number is smaller than the number of bytes requested; this may happen for example
       because fewer bytes are actually available right now (maybe because we were close to  end-of-file,  or  because  we  are
       reading  from a pipe, or from a terminal), or because read() was interrupted by a signal.  On error, -1 is returned, and
       errno is set appropriately. In this case it is left unspecified whether the file position (if any) changes.

[ Jovan Marjanovic @ 22.05.2003. 11:20 ] @
Kod koji si napisao nevalja.
Lepo ti to stavis u jedan while loop, i imas uslov da ti tmp nije EOF ili sl. pa da vidis miline :)
[ antix @ 22.05.2003. 13:10 ] @
pa zar se moze raditi sa pipe-ovima i koristiti EOF???
[ leka @ 22.05.2003. 14:37 ] @
C/C++ forum postaje zapravo humoristicna diskusiona grupa, ja verujem da neki ljudi dolaze ovamo samo da citaju provale.
[ Predrag Damnjanovic @ 22.05.2003. 15:17 ] @
Koliko vidim, antrix koristi windows, a na njemu ne postoji fcntl() funkcija.
Jedino ako antrix koristi cygwin, ili programira iz nekog unix-a a windows koristi samo za net...

Drugo, eof() funkcija ne postoji u stdc, niti u windowsu kao winapi funkcija.
eof() postoji samo u script jezicima, tcl, php, perl...

U stdc-u postoji feof(), i ona, koliko je meni poznato (a mozda gresim, ispravite me) prima samo file stream.
Mozda je na UNIX-ima pipe=stream, ali nisam strucan da na to pitanje odgovorim, nisam upoznat sa implementacijama pipe-ova i stream-ova...
[ Jovan Marjanovic @ 22.05.2003. 15:27 ] @
Code:

Example

/* READ.C: This program opens a file named
 * READ.C and tries to read 60,000 bytes from
 * that file using _read. It then displays the
 * actual number of bytes read from READ.C.
 */

#include <fcntl.h>      /* Needed only for _O_RDWR definition */
#include <io.h>
#include <stdlib.h>
#include <stdio.h>

char buffer[60000];

void main( void )
{
   int fh;
   unsigned int nbytes = 60000, bytesread;

   /* Open file for input: */
   if( (fh = _open( "read.c", _O_RDONLY )) == -1 )
   {
      perror( "open failed on input file" );
      exit( 1 );
   }

   /* Read in input: */
   if( ( bytesread = _read( fh, buffer, nbytes ) ) <= 0 )
      perror( "Problem reading file" );
   else
      printf( "Read %u bytes from file\n", bytesread );

   _close( fh );
}




evo primera za read direkt iz MSDN-a

EOF i WEOF su definisani u visual studiu, i mogu da se koriste uz getc fgetc i slicne funkcije:

Code:

/* GETC.C: This program uses getchar to read a single line
 * of input from stdin, places this input in buffer, then
 * terminates the string before printing it to the screen.
 */

#include <stdio.h>

void main( void )
{
   char buffer[81];
   int i, ch;

   printf( "Enter a line: " );

   /* Read in single line from "stdin": */
   for( i = 0; (i < 80) &&  ((ch = getchar()) != EOF) 
                        && (ch != '\n'); i++ )
      buffer[i] = (char)ch;

   /* Terminate string with null character: */
   buffer[i] = '\0';
   printf( "%s\n", buffer );
}

[ antix @ 22.05.2003. 16:18 ] @
***i ga,
IZVINITE STO NE ZNAM. Pa da znam vjerovatno ne bih ni pitao ovo!!! I da,
trenutno koristim Win, a inace radim i u UNIX-u!!! 'Ajde snaci cu se nekako i sam...

hvala svima na pokusaju!