Pass File Descriptor - Execve (typecast)?

Simply casting the file descriptor to a char isn't a good idea. It could cause data loss if the OS chooses to copy the strings and stops at the 0 bytes, so the data isn't entirely copied. It would be safer to create a string containing the file descriptor.

Up vote 0 down vote favorite share g+ share fb share tw.

I am wondering how I can pass a file descriptor thru the execve command and then access it on the other side. I know that I can use dup2 to redirect the filedescriptor but I CANNOT do that. I am required to actually pass the file descriptor to the child and use it in the child.

What I have so far: Parent makes pipe + args like so: int pfd2; if(pipe(pfd) == -1) exitWithError("PIPE FAILED", 1); char *args_1 = {"reader", argv1, (char*) pfd, (char *) 0}; Then the child calls execve after fork like so: close(pfd1); execve(". /reader", args_1, NULL); Then, in the reader program I try to access pipe descriptor that was passed: int main(int argc, char* argv){ ... write(argv21, buf, read_test); ^^argv2 should be referencing the pipe descriptor, then the 1 should go to the write end of the pipe. I am almost positive that I need to do some type-casting differently here, but everything I try doesn't quite work out.

--NOTE::I have a working version of this program using dup2 to redirect to stdin and stdout for the children, but I have to actually pass the pipe descriptor to a child as per instructions of the project. Any help is appreciated. C homework casting file-descriptor execve link|improve this question asked Sep 12 '11 at 3:52forTruce144 100% accept rate.

Simply casting the file descriptor to a char* isn't a good idea. It could cause data loss if the OS chooses to copy the strings and stops at the 0 bytes, so the data isn't entirely copied. It would be safer to create a string containing the file descriptor.

Int pfd2; if(pipe(pfd) == -1) exitWithError("PIPE FAILED", 1); char string112; // A 32 bit number can't take more than 11 characters, + a terminating 0 snprintf(string1,12,"%i",pfd1); // Copy the file descriptor into a string char *args_1 = {"reader", argv1, &string10, (char *) 0}; The reader program then uses atoi to convert this back into an int. Int fd = atoi(argv2); write(fd,buf,read_test); If you really want to use a cast, then you need to cast argv2 as int* in your reader. Write(((int*)argv2)1,buf,read_test).

Thank you very much. I am still trying to wrap my head around what really went on in that transaction (type-casting confuses me a bit), but your post is very clear and I think I understand. – forTruce Sep 12 '11 at 4:31.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions