_pipe
Membuat pipa untuk membaca dan menulis.
Penting
API ini tidak dapat digunakan dalam aplikasi yang dijalankan di Windows Runtime. Untuk informasi selengkapnya, lihat Fungsi CRT yang tidak didukung di aplikasi Platform Windows Universal.
Sintaks
int _pipe(
int *pfds,
unsigned int psize,
int textmode
);
Parameter
pfds
Penunjuk ke array dua int
untuk menahan deskriptor file baca dan tulis.
psize
Jumlah memori yang akan dipesan.
textmode
Mode file.
Nilai hasil
Mengembalikan 0 jika berhasil. Mengembalikan -1 untuk menunjukkan kesalahan. Jika terjadi kesalahan, errno
diatur ke salah satu nilai ini:
EMFILE
, yang menunjukkan bahwa tidak ada lagi deskriptor file yang tersedia.ENFILE
, yang menunjukkan luapan tabel file sistem.EINVAL
, yang menunjukkan bahwa arraypfds
adalah penunjuk null atau bahwa nilai yang tidak valid untuktextmode
diteruskan.
Untuk informasi selengkapnya tentang kode pengembalian ini dan lainnya, lihat errno
, , _doserrno
_sys_errlist
, dan _sys_nerr
.
Keterangan
Fungsi ini _pipe
membuat pipa, yang merupakan saluran I/O buatan yang digunakan program untuk meneruskan informasi ke program lain. Pipa menyerupai file, karena memiliki penunjuk file, deskriptor file, atau keduanya. Dan, dapat dibaca dari atau ditulis ke dengan menggunakan fungsi input dan output Pustaka Standar. Namun, pipa tidak mewakili file atau perangkat tertentu. Sebaliknya, ini mewakili penyimpanan sementara dalam memori yang independen dari memori program itu sendiri dan dikontrol sepenuhnya oleh sistem operasi.
_pipe
menyerupai _open
tetapi membuka pipa untuk membaca dan menulis dan mengembalikan dua deskriptor file alih-alih satu. Program ini dapat menggunakan kedua sisi pipa atau menutup salah satu yang tidak diperlukan. Misalnya, prosesor perintah di Windows membuat pipa saat menjalankan perintah seperti PROGRAM1 | PROGRAM2
.
Deskriptor PROGRAM1
output standar dilampirkan ke deskriptor tulis pipa. Deskriptor PROGRAM2
input standar dilampirkan ke deskriptor baca pipa. Lampiran ini menghilangkan kebutuhan untuk membuat file sementara untuk meneruskan informasi ke program lain.
Fungsi mengembalikan _pipe
dua deskriptor file ke pipa dalam pfds
argumen . Elemen pfds
[0] berisi deskriptor baca, dan elemen pfds
[1] berisi deskriptor tulis. Deskriptor file pipa digunakan dengan cara yang sama seperti deskriptor file lainnya. (Fungsi _read
input dan output tingkat rendah dan _write
dapat membaca dari dan menulis ke pipa.) Untuk mendeteksi kondisi akhir pipa, periksa _read
permintaan yang mengembalikan 0 sebagai jumlah byte yang dibaca.
Argumen psize
menentukan jumlah memori, dalam byte, untuk dicadangkan untuk pipa. Argumen textmode
menentukan mode terjemahan untuk pipa. Konstanta manifes _O_TEXT
menentukan terjemahan teks ANSI, dan konstanta _O_BINARY
menentukan terjemahan biner. (Lihat fopen
, _wfopen
untuk deskripsi teks dan mode biner.) textmode
Jika argumen adalah 0, _pipe
gunakan mode terjemahan default yang ditentukan oleh variabel _fmode
mode default .
Dalam program multithreaded, tidak ada penguncian yang dilakukan. Deskriptor file yang dikembalikan baru dibuka dan tidak boleh dirujuk oleh utas apa pun sampai setelah _pipe
panggilan selesai.
Untuk menggunakan _pipe
fungsi untuk berkomunikasi antara proses induk dan proses anak, setiap proses hanya boleh memiliki satu deskriptor yang terbuka pada pipa. Deskriptor harus berlawanan: jika induk membuka deskriptor baca, maka anak harus membuka deskriptor tulis. Paling mudah menggunakan bitwise "atau" (|
) pada _O_NOINHERIT
bendera dengan textmode
. Kemudian, gunakan _dup
atau _dup2
untuk membuat salinan deskriptor pipa yang dapat diwariskan yang ingin Anda teruskan ke anak. Tutup deskriptor asli, lalu munculkan proses anak. Saat kembali dari panggilan spawn, tutup deskriptor duplikat dalam proses induk. Untuk informasi selengkapnya, lihat contoh 2 nanti di artikel ini.
Dalam sistem operasi Windows, pipa dihancurkan ketika semua deskriptornya telah ditutup. (Jika semua deskriptor baca pada pipa telah ditutup, maka penulisan ke pipa menyebabkan kesalahan.) Semua operasi baca dan tulis pada pipa menunggu hingga ada cukup data atau ruang buffer yang cukup untuk menyelesaikan permintaan I/O.
Secara default, status global fungsi ini dicakup ke aplikasi. Untuk mengubah perilaku ini, lihat Status global di CRT.
Persyaratan
Rutin | Header yang diperlukan | Header opsional |
---|---|---|
_pipe |
<io.h> |
<fcntl.h> ,12 <errno.h> |
1 Untuk _O_BINARY
dan _O_TEXT
definisi.
2 errno
definisi.
Untuk informasi kompatibilitas selengkapnya, lihat Kompatibilitas.
Pustaka
Semua versi pustaka run-time C.
Contoh 1
// crt_pipe.c
/* This program uses the _pipe function to pass streams of
* text to spawned processes.
*/
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include <process.h>
#include <math.h>
enum PIPES { READ, WRITE }; /* Constants 0 and 1 for READ and WRITE */
#define NUMPROBLEM 8
int main( int argc, char *argv[] )
{
int fdpipe[2];
char hstr[20];
int pid, problem, c;
int termstat;
/* If no arguments, this is the spawning process */
if( argc == 1 )
{
setvbuf( stdout, NULL, _IONBF, 0 );
/* Open a set of pipes */
if( _pipe( fdpipe, 256, O_BINARY ) == -1 )
exit( 1 );
/* Convert pipe read descriptor to string and pass as argument
* to spawned program. Program spawns itself (argv[0]).
*/
_itoa_s( fdpipe[READ], hstr, sizeof(hstr), 10 );
if( ( pid = _spawnl( P_NOWAIT, argv[0], argv[0],
hstr, NULL ) ) == -1 )
printf( "Spawn failed" );
/* Put problem in write pipe. Since spawned program is
* running simultaneously, first solutions may be done
* before last problem is given.
*/
for( problem = 1000; problem <= NUMPROBLEM * 1000; problem += 1000)
{
printf( "Son, what is the square root of %d?\n", problem );
_write( fdpipe[WRITE], (char *)&problem, sizeof( int ) );
}
/* Wait until spawned program is done processing. */
_cwait( &termstat, pid, WAIT_CHILD );
if( termstat & 0x0 )
printf( "Child failed\n" );
_close( fdpipe[READ] );
_close( fdpipe[WRITE] );
}
/* If there is an argument, this must be the spawned process. */
else
{
/* Convert passed string descriptor to integer descriptor. */
fdpipe[READ] = atoi( argv[1] );
/* Read problem from pipe and calculate solution. */
for( c = 0; c < NUMPROBLEM; c++ )
{
_read( fdpipe[READ], (char *)&problem, sizeof( int ) );
printf( "Dad, the square root of %d is %3.2f.\n",
problem, sqrt( ( double )problem ) );
}
}
}
Son, what is the square root of 1000?
Son, what is the square root of 2000?
Son, what iDad, the square root of 1000 is 31.62.
Dad, the square root of 2000 is 44.72.
s the square root of 3000?
Dad, the square root of 3000 is 54.77.
Son, what is the square root of 4000?
Dad, the square root of 4000 is 63.25.
Son, what is the square root of 5000?
Dad, the square root of 5000 is 70.71.
Son, what is the square root of 6000?
SonDad, the square root of 6000 is 77.46.
, what is the square root of 7000?
Dad, the square root of 7000 is 83.67.
Son, what is the square root of 8000?
Dad, the square root of 8000 is 89.44.
Contoh 2
Kode sampel adalah aplikasi filter dasar. Ini menghasilkan aplikasi crt_pipe_beeper
setelah membuat pipa yang mengarahkan aplikasi stdout
yang ditelurkan ke filter. Filter menghapus karakter ASCII 7 (bip).
// crt_pipe_beeper.c
#include <stdio.h>
#include <string.h>
int main()
{
int i;
for(i=0;i<10;++i)
{
printf("This is speaker beep number %d...\n\7", i+1);
}
return 0;
}
Aplikasi filter aktual:
// crt_pipe_BeepFilter.C
// arguments: crt_pipe_beeper.exe
#include <windows.h>
#include <process.h>
#include <memory.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#define OUT_BUFF_SIZE 512
#define READ_FD 0
#define WRITE_FD 1
#define BEEP_CHAR 7
char szBuffer[OUT_BUFF_SIZE];
int Filter(char* szBuff, ULONG nSize, int nChar)
{
char* szPos = szBuff + nSize -1;
char* szEnd = szPos;
int nRet = nSize;
while (szPos > szBuff)
{
if (*szPos == nChar)
{
memmove(szPos, szPos+1, szEnd - szPos);
--nRet;
}
--szPos;
}
return nRet;
}
int main(int argc, char** argv)
{
int nExitCode = STILL_ACTIVE;
if (argc >= 2)
{
HANDLE hProcess;
int fdStdOut;
int fdStdOutPipe[2];
// Create the pipe
if(_pipe(fdStdOutPipe, 512, O_NOINHERIT) == -1)
return 1;
// Duplicate stdout file descriptor (next line will close original)
fdStdOut = _dup(_fileno(stdout));
// Duplicate write end of pipe to stdout file descriptor
if(_dup2(fdStdOutPipe[WRITE_FD], _fileno(stdout)) != 0)
return 2;
// Close original write end of pipe
_close(fdStdOutPipe[WRITE_FD]);
// Spawn process
hProcess = (HANDLE)_spawnvp(P_NOWAIT, argv[1],
(const char* const*)&argv[1]);
// Duplicate copy of original stdout back into stdout
if(_dup2(fdStdOut, _fileno(stdout)) != 0)
return 3;
// Close duplicate copy of original stdout
_close(fdStdOut);
if(hProcess)
{
int nOutRead;
while (nExitCode == STILL_ACTIVE)
{
nOutRead = _read(fdStdOutPipe[READ_FD],
szBuffer, OUT_BUFF_SIZE);
if(nOutRead)
{
nOutRead = Filter(szBuffer, nOutRead, BEEP_CHAR);
fwrite(szBuffer, 1, nOutRead, stdout);
}
if(!GetExitCodeProcess(hProcess,(unsigned long*)&nExitCode))
return 4;
}
}
}
return nExitCode;
}
This is speaker beep number 1...
This is speaker beep number 2...
This is speaker beep number 3...
This is speaker beep number 4...
This is speaker beep number 5...
This is speaker beep number 6...
This is speaker beep number 7...
This is speaker beep number 8...
This is speaker beep number 9...
This is speaker beep number 10...