/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/socketpair.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
 
 
16
        while (readed>0) {
 
17
                readed=read(f[0],buf,254);
 
18
                if (readed>0) writed=write(0,buf,readed);
 
19
        }
 
20
 
 
21
        // Waiting for child to be finished!
 
22
        waitpid(child,NULL,0);
 
23
 
 
24
}
 
25
 
 
26
void runchild() {
 
27
        char fl[24];
 
28
        int err;
 
29
        char buf[100]="Helo!\n\0";
 
30
        write(f[1],buf,6);
 
31
        shutdown(f[1],2);
 
32
}
 
33
 
 
34
main() {
 
35
        pid_t child;
 
36
        
 
37
        if (socketpair(PF_UNIX,SOCK_STREAM,0,f)) {
 
38
                printf("Error creating socket pair, error: %i!\n",errno);
 
39
                return;
 
40
        }
 
41
 
 
42
        child=fork();
 
43
        if (child>0) runparent(child);
 
44
        else if (child==0) runchild();
 
45
        else printf("Error forking child, error: %i!\n",errno);
 
46
}