Monday 15 April 2013

c - How to get data from read program to main program using execve and pipe? -


I am doing a project in which I have to make a child procedure using Kanka and then tell the basic procedure to the hair process If using Execve to execute another C program "read.c" (which reads .txt and compute averages from all. Integers) then I must send that average value to the original process through the pipes. I do not know how to get the "average value" result of the "read.c" program in the child process of the "process.c" program. Some friends said that I have a file descriptor useful for pipes (PFD [2]) executable And other programs (to read). I have to use the pipe to write data to process.c program. But I do not know how to do this, so it is not working properly. I am posting my code for both process.c and read.c and please tell me how to make it to perform well.

 Add  & lt; Stdio.h & gt; # Include & lt; Unistd.h & gt; #to & lt include, stdlib.h & gt; Main () {int pid; Int PFD [2]; Int i; Four string 1 [12]; Snprintf (string1,12, "% i", pfd [1]); Char * args [] = {"read", and string 1 [0], NULL}; Pipe (pfd); Pid = fork (); If (pid == 0) {execve ("read", args, NULL); Int size = 100; Four buff [size]; Close (pfd [1]); Read (PFD [0], buf, size); Printf ("% s \ n", buf); }}   

read.c

  contains # lt; Stdio.h & gt; # Include & lt; Unistd.h & gt; #to & lt include, stdlib.h & gt; Int main (int argc, char * argv [], char * envp []) {int pfd [2]; PFD [1] = Atoi (RGR [1]); Close (pfd [0]); File * ptr_file; Four buff [1000]; Int sum = 0; Int average = 0; Int no = 0; Int count = 0; Ptr_file = fopen ("data.txt", "r"); If (! Ptr_file) return 1; While (fgets (buf, 1000, ptr_file)! = Null) {printf ("% s \ n", buf); No = atio (buff); Sum = sum + no; Printf ("sum =% d \ n", sum); Count ++; } Fclose (ptr_file); Avg = sum / count; Printf ("average:% d \ n", average); Write (PFD [1], "Hello", 6); / * I just want to see Namaste whether it is working or not * / 0 is returning; }   

If you have a solution to process.c from the process.c file in the hair process, please let me know.

Your "read.c" result should be printed on standard output. Therefore, open the file and calculate the result as you are doing. After this, use a printf () to print the result. It can be run from the command line and the output should go to the terminal.

  printf ("% f \ n", result); Off (1); // Just in case, be sure to flush the pipe   

Here is the code that establishes the pipe between the parent and the child I think it will solve your project Should help.

Snap to process.c

  # include & lt; Stdio.h & gt; # Include & lt; Unistd.h & gt; #to & lt include, stdlib.h & gt; Int main (int argc, char * argv [], char * envp []) {int fd [2], pid; Pipe (FD); Pid = fork () if (pid == 0) {/ * hair process * / off (1); // close STDOUT dup (FD [1]); // STDOUT FD [1] will be closed (0); // Close STDIN, it is not needed. Off (2); // stop STDERR, this is not needed. Closed (FD [0]); // do not need it off (FD [1]); // This does not need Execvpe ("./read", argv, envp); // never returns the exec () family of your exec () version / call. The process reads here * /} and {/ * the basic process here implements * / four CR [256]; Float average = 0; Int rate = 0; / * The parent process is making it very easy without any kind of duplication etc. * / / * No errors are checked ... * / / * You can experiment with enhancements here !!! * / Rate = Read (FD [0], Crow, 255); // read blocked syscall if (ret & lt; = 0) {printf ("error or nothing has been read. Debug problem !!! \ n"); Exit (1); } Chr [ret] = '\ 0'; Printf ("Read basic process [pid% d] =% s \ n", pid, chr); }}    

No comments:

Post a Comment