|
|
1.1 root 1: /*** pipes.c - parent program
2: *
3: * Example of DOSMAKEPIPE usage in parent/child communication
4: *
5: * This program gets some shared memory, makes a pipe, writes
6: * a string into it, and then execs a child. The child gets
7: * the shared memory segment, then reads from the pipe using
8: * the handle passed in the shared memory segment.
9: */
10: #include <stdio.h>
11: #include <doscalls.h>
12:
13: typedef struct { /* structure of shared mem segment */
14: unsigned read_handle; /* pipe read handle */
15: unsigned write_handle; /* pipe write handle */
16: } SharedData;
17:
18: SharedData far *fp; /* pointer to shared memory */
19: unsigned mem_handle; /* selector of the allocated segment */
20:
21:
22: main()
23: {
24:
25: static char pname[] = "\\SHAREMEM\\public"; /* shared mem name */
26: static char writeout[] = "Writing to the child"; /* pipe string */
27: char exec_buf[100]; /* buffer for DosExecPgm ObjName */
28: char *pgmname = "pchild.exe"; /* name of child program */
29: int i;
30: int retcode; /* holds return code from call */
31: struct ResultCodes tcodes; /* termination codes from DosExecPgm */
32: unsigned far *fpinit; /* pointer to shared memory */
33: unsigned buflen; /* DosWrite buffer length */
34: unsigned memsiz = 1024; /* size of memory segment requested */
35: unsigned pipe_size; /* size to reserve for the pipe */
36: unsigned written; /* number bytes written by DosWrite */
37:
38: /* allocate 1k shared memory segment and initialize it */
39: printf("Getting shared memory segment\n");
40: retcode = DOSALLOCSHRSEG( memsiz, (char far *)pname,
41: (unsigned far *)&mem_handle);
42:
43: /* create pointers to shared memory segment */
44: fp = (SharedData far *)(((unsigned long)mem_handle << 16) | 0);
45: fpinit = (unsigned far *)(((unsigned long)mem_handle << 16) | 0);
46:
47: /* zero initialize the segment */
48: for (i = 0; i < memsiz / 2; i++) {
49: *fpinit = 0;
50: fpinit++;
51: }
52:
53: /* make the pipe */
54: pipe_size = 0; /* use default size */
55: printf("Making the pipe\n");
56: retcode = DOSMAKEPIPE((unsigned far *)&fp->read_handle,
57: (unsigned far *)&fp->write_handle, pipe_size);
58:
59: if ( retcode ) {
60: printf("DosMakePipe returned error %d, aborting\n", retcode);
61: exit(2);
62: }
63: else
64: printf("DosMakePipe retcode %d, read_handle %d, write_handle %d\n",
65: retcode, fp->read_handle, fp->write_handle);
66:
67: /* write string to pipe */
68: buflen = strlen(writeout) + 1;
69: if( retcode = DOSWRITE( fp->write_handle, (char far *)writeout,
70: buflen, (unsigned far *)&written)) {
71: printf("Write to pipe failed, retcode %d\n", retcode);
72: exit(2);
73: }
74: else
75: printf("Write to write_handle %d, bytes written %d\n",
76: fp->write_handle, written);
77:
78: /* create the child */
79: printf("Creating child\n");
80: retcode = DOSEXECPGM( (char far *)exec_buf, 100, 0,
81: (unsigned far *)0L,
82: (unsigned far *)0L,
83: (struct ResultCodes far *)&tcodes, pgmname );
84:
85: if ( retcode )
86: printf("DosExecPgm of child error %d\n", retcode);
87: else
88: printf("Back in parent\n");
89:
90: /* close the pipe */
91: DOSCLOSE( fp->read_handle );
92: DOSCLOSE( fp->write_handle );
93:
94: DOSEXIT(1,0); /* Terminate, kill any dangling children */
95:
96: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.