Professional Documents
Culture Documents
Ders9 2
Ders9 2
A
Yazıldığı Zaman :
Okunduğu Zaman :
1
Pipe’tan Okuma Örneği
char buf[100];
…
while((count = read(p[0], buf, 100)>0)
yazma okuma
pipe
ucu ucu
30 100 100
read 1
read 2 (100 bytes)
(100 bytes)
read 3
(30 bytes)
read 4
2
Basit bir Örnek
User
write(fd[1], …) ; Process
read(fd[0], … ) ;
OS kernel
pipe
fd[1] fd[0]
#include <stdio.h>
main()
{ int n, fd[2] ;
char buf[128] ;
if ( write(fd[1], “Mersin\n”, 7) != 7 )
{ /* hata mesajı ve exit */ }
Process B: Process C:
who sort
Standard
Input
4
Dosya Tanımlayıcılarının Kopyalanması
new_fd =dup(fd) ; /* fd tanımlayıcısını, tanımlayıcı
tabloda yeni bir ID (new_fd) ile kopyalar. */
Kullanıcı Dosya
Sistem Dosya Tablosu
Tanım. Tablosu
dup2’den Adres A0
önce 1 .
Giriş 1 dosya1
. Giriş 0 dosya2
fd Adress A1
0 Adress A1
dup2’den
1 .
sonra
Giriş 1 dosya1
.
fd Adress A1 Giriş 0
Örnekler:
a)
int p[2] ;
pipe(p) ; p[1] p[0]
b)
int p[2] ;
pipe(p) ; p[1] p[0]
dup2(p[0], 0) ; p[1] stdin
p[0] from pipe
6
“who” ve “sort” pipe ile bağlanmasında program
yapısı
parent (A)
pipe(fd);
pid = fork(); pipe
reading process (C)
pipe pid = 0 (child 1)
writing process (B) pid = fork();
dup2(fd[0], 0) ;
pid = 0
close(fd[1] ;
(child 2)
execlp(“sort”, “sort”, 0) ;
dup2(fd[1], 1) ;
close(fd[0] ;
execlp(“who”, “who”, 0) ; “sort”
…
exit()
“who” close fd[0] ;
… close fd[1] ;
exit() wait(0) ;
wait(0) ;
exit(0) ;
fd[1] fd[0]
who sort
pipe
7
#include <stdio.h>
#include <unistd.h>
main()
{ int fd[2] ; int pid ;
pipe(fd) ;
pid = fork() ;
if ( pid == 0)
{ dup2( fd[0], 0) ; /* stdinp pipe’ın okuma ucuna bağlanır*/
close(fd[1]) ; /* yazma ucunu kapat*/
execlp(“sort”, “sort”, 0 ) ; /* “sort” u çalıştır */
}
pid = fork() ;
if (pid == 0 )
{ dup2(fd[1], 1) ; /*stdout pipe yazma ucuna bağlanır*/
close ( fd[0]) ; /* Okuma ucunu kapat */
execlp(“who”, “who”, 0) ; /* “who” çalıştır*/
close ( fd[0]) ;
close(fd[1]) ;
wait(0) ;
wait(0) ;
}
8
UNIX te Adlandırılmış Pipe
yazma okuma
pipe
Process A Process B
(FIFO)
Sistem Çağrıları:
open()
mknode()
read()
write()
close()
Örnek:
int a ;
. . .
a = mknod(pipe_adı, S_IFIFO | 0666, 0 ) ;
if ( a < 0 ) { /*Hata */ }
9
İki process için bir program:
Process A adlandırılmış Pipe ‘ yaratır veya yazar
Process B adlandırılmış Pipe ‘ yaratır veya okur
#include <fcntl.h>
char text[] = “mersin” ;
main(argc, argv)
int argc ; char *argv[] ;
for ( ; ; )
if ( argc == 2 ) write(fd, text, 6 ) ;
else { int n = read(fd, buf, 6) ;
buf[n]=’\0’; print (“%s\n”, buf);
}
}
10
Adlandırılmış Pipe ile istemci/sunucu
Sunucu UNIX
host
cevap cevap
Özel pipe 1 Özel pipe 2
Public pipe
/tmp/abc
İstemci 1 İstemci 2
istek
mesaj
11
İstemci programı (bir parçası)
#include …
struct message {
char pname[128] ;
char istek[512] ; /* sunucuya mesaj yapısı */
} ; Kullanım:
%prog_adı ozel_pipe
main ( argc, argv )
int argc ; char *argv[] ;
{ int n, public, ozel ;
struct message msg ;
char buf[PIPE_BUF] ;
msg.pname = argv[1] ;
msg.istek = … ;
write( public, &msg, sizeof(msg) ) ;
12
Sunucu programı (bir parçası)
#include …
struct message { … } ; /* istemci ile aynı */
13
Pipe Limitleri
1. Bir pipe tek-yönlüdür
14