Changeset 22174 for main/trunk/greenstone2/runtime-src/src/recpt
- Timestamp:
- 2010-05-26T00:01:29+12:00 (14 years ago)
- Location:
- main/trunk/greenstone2/runtime-src/src/recpt
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/runtime-src/src/recpt/os_process_unix.cpp
r22173 r22174 44 44 #define WRITE_PIPE_INDEX 1 45 45 46 #define FD_STDIN 0 47 #define FD_STDOUT 1 48 #define FD_STDERR 2 46 49 47 50 osprocessunix::osprocessunix(char* cmdline) … … 50 53 51 54 osprocessunix::osprocessunix(char* cmdline, OSProcessPipeMode mode) 52 : child_stdout_read_( NULL), child_stdin_write_(NULL),55 : child_stdout_read_(-1), child_stdin_write_(-1), 53 56 osprocess(cmdline,mode) 54 57 { … … 89 92 // Sort out input pipe 90 93 // child has no buisness accessing write end of input pipe => close 91 close(child_stdin_write_);94 ::close(child_stdin_write_); 92 95 93 96 if ((mode == uniWrite) || (mode == biReadWrite)) { … … 99 102 // in the child reading any input from the parent) 100 103 // => child input remains coming from stdin 101 close(child_stdin_read);104 ::close(child_stdin_read); 102 105 } 103 106 104 107 // Sort out output pipe 105 108 // child has no buisness accessing read end of output pipe => close 106 close(child_stdout_read_);109 ::close(child_stdout_read_); 107 110 if ((mode == uniRead) || (mode == biReadWrite)) { 108 111 // wire up child's stdout write so it is send down the pipe to the parent … … 113 116 // in any output produced by the child process) 114 117 // => child output remains going to stdout 115 close(child_stdout_write); 116 } 117 118 ::close(child_stdout_write); 119 } 120 121 /* 118 122 // execve ... 119 123 int rv = CreateProcess(NULL, // no application name … … 130 134 fprintf(stderr,"os_process_windows(): Error creating process"); 131 135 } 136 */ 137 132 138 133 139 } … … 137 143 // Sort out input pipe 138 144 // parent has no buisness accessing read end of input pipe => close 139 close(child_stdin_read);145 ::close(child_stdin_read); 140 146 141 147 // Sort out output pipe 142 148 // parent has no buisness accessing write end of output pipe => close 143 close(child_stdout_write);149 ::close(child_stdout_write); 144 150 145 151 … … 147 153 { 148 154 case uniRead: 149 close(child_stdin_write_);155 ::close(child_stdin_write_); 150 156 break; 151 157 case uniWrite: 152 close(child_stdout_read_);158 ::close(child_stdout_read_); 153 159 break; 154 160 case biReadWrite: … … 157 163 break; 158 164 case noPipe: 159 close(child_stdin_write_);160 close(child_stdout_read_);165 ::close(child_stdin_write_); 166 ::close(child_stdout_read_); 161 167 break; 162 168 } … … 178 184 { 179 185 180 int actual_write_len = write(child_stdin_write_, buffer, buffer_len);181 182 if ( data_written<0) {186 int actual_write_len = ::write(child_stdin_write_, buffer, buffer_len); 187 188 if (actual_write_len<0) { 183 189 cerr << "osproessunix::write() Error: failed to write data" << endl; 184 190 } … … 189 195 int osprocessunix::read(char* buffer, const int buffer_len) 190 196 { 191 int actual_read_len = read(child_stdout_read_, buffer, buffer_len);197 int actual_read_len = ::read(child_stdout_read_, buffer, buffer_len); 192 198 193 199 if (actual_read_len<0) { … … 201 207 202 208 bool osprocessunix::close() 203 {} 209 { 210 return true; 211 } 204 212 205 213 bool osprocessunix::close(OSProcessPipeMode mode) 206 {} 207 208 209 int main() { 210 int pipe_fd[2]; 211 pipe(pipe_fd); 212 213 if (fork() == 0) { /* create child process */ 214 char *av[3] = { "ls", "-c1", 0}; /* parameters for command */ 215 close(pipe_fd[0]); /* close read end of pipe */ 216 close(1); /* close standard output */ 217 dup(pipe_fd[1]); /* attach write end of pipe */ 218 /* stdout is lowest available fd */ 219 execve("/bin/ls", av, 0); /* run the ls code */ 220 } 221 else { /* in the parent process */ 222 int files; 223 char ch; 224 close(pipe_fd[1]); /* close write end of pipe */ 225 files = 0; /* counter for number of files */ 226 227 while ( 0 != read(pipe_fd[0], &ch, 1) )/* read from pipe until eof */ 228 if (ch == '\n') /* each newline represents a file*/ 229 ++files; 230 cout<< "There are " << files << " files\n"; 231 } 232 } 214 { 215 return true; 216 } 217 218 233 219 234 220 #endif -
main/trunk/greenstone2/runtime-src/src/recpt/os_process_unix.h
r22173 r22174 38 38 39 39 public: 40 osprocessunix(c onst char* cmd);41 osprocessunix(c onst char* cmd, constOSProcessPipeMode mode);42 vir utal ~osprocessunix();40 osprocessunix(char* cmd); 41 osprocessunix(char* cmd, OSProcessPipeMode mode); 42 virtual ~osprocessunix(); 43 43 44 virtual eop(); // end of pipe 45 virtual eop(OSProcessPipeMode); // end of pipe, for named pipe 44 /* 45 virtual bool eop(); // end of pipe 46 virtual bool eop(OSProcessPipeMode); // end of pipe, for designated pipe 47 */ 46 48 47 49 virtual int read(char* buffer, const int buffer_len); 48 virtual boolwrite(char* buffer, const int buffer_len);50 virtual int write(char* buffer, const int buffer_len); 49 51 50 52 virtual bool close(); … … 52 54 53 55 protected: 54 File* read_fd_;55 File* write_fd_;56 int child_stdin_write_; 57 int child_stdout_read_; 56 58 }; 57 59
Note:
See TracChangeset
for help on using the changeset viewer.