/docs/MyDocs

To get this branch, use:
bzr branch http://darksoft.org/webbzr/docs/MyDocs

« back to all changes in this revision

Viewing changes to Development/languages/C/Samples/fork/pipe.c

  • Committer: Suren A. Chilingaryan
  • Date: 2009-04-09 03:21:08 UTC
  • Revision ID: csa@dside.dyndns.org-20090409032108-w4edamdh4adrgdu3
import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <sys/types.h>
 
3
#include <sys/stat.h>
 
4
#include <fcntl.h>
 
5
#include <unistd.h>
 
6
#include <sys/socket.h>
 
7
 
 
8
extern int errno;
 
9
int f[2];
 
10
 
 
11
void runparent(pid_t child) {
 
12
        char buf[255];
 
13
        int readed=1;
 
14
        int writed;
 
15
        close(f[1]);
 
16
 
 
17
        signal(13,sp_handler);
 
18
        while (readed>0) {
 
19
                readed=read(f[0],buf,254);
 
20
                if (readed>0) writed=write(0,buf,readed);
 
21
        }
 
22
        close(f[0]);
 
23
        // Waiting for child to be finished!
 
24
        waitpid(child,NULL,0);
 
25
 
 
26
}
 
27
 
 
28
void runchild() {
 
29
        char fl[24];
 
30
        int err;
 
31
        char buf[100]="Helo!\n\0";
 
32
        close (f[0]);
 
33
 
 
34
        write(f[1],buf,6);
 
35
        close(f[1]);
 
36
}
 
37
 
 
38
main() {
 
39
        pid_t child;
 
40
        
 
41
        if (pipe(f)) {
 
42
                printf("Error creating pipe, error: $i!\n",errno);
 
43
                return;
 
44
        }
 
45
 
 
46
        child=fork();
 
47
        if (child>0) runparent(child);
 
48
        else if (child==0) runchild();
 
49
        else printf("Error forking child, error: %i!\n",errno);
 
50
}