c - Program hangs after using pipe, fork and exec -
I use pipe fork and exec to implement a normal pipe for any two shell programs. I'm specifically using ls. Grep does this to test it, the data is copied to copy, search grep for the match, and then outputs them on stdout. However, after that the program is still hanging.
This is my code when the pipe is detected. I have fork, and then fork again because I want to continue to run the original process of the fork after the execution call. Because of the debug code I believe that grep is executed after calling exec (), nothing is happening
if (pipeflag == 1) {pipe (FD); Pid = fork (); If (PID == 0) {// hair process fPID = fork (); If (FPID == 0) {// baby child of printf ("child's baby \ n"); Dup2 (FD [1], 1); Execvp (command, argv); // Printing error is required ("mysh:% s: command not found \ n", argv [0]); Exhaust (EXIT_FAILURE); } If (FPID & gt; 0) {Parents of the second child's child ("in the second child's parents \ n"); dup2 (FD [0], 0); Execvp (command1, argv1); // error is required to check printf ("mysh:% s: command not found \ n", argv [0]); Exhaust (EXIT_FAILURE); } If (PID == -1) {printf ("Error: \ n"); Switch (error) {case EAGAIN: printf ("not of kanka process: system process limit can be reached \ n"); Case ENOMEM: printf ("The process can not be cut: out of memory \ n"); } Return 1; }} If (PID & gt; 0) {// Wait for Parent (PID, 0, 0); printf ("external parent \ n"); } If (PID == -1) {printf ("Error: \ n"); Switch (error) {case EAGAIN: printf ("not of kanka process: system process limit can be reached \ n"); Case ENOMEM: printf ("can not be processed: memory out \ n"); } Return 1; }}
Below is my solution to the problem. I'm not sure that this is a permanent solution . I am not 100% sure even if I have argued why this works and not the previous code. What I did was the command that was waiting for the input from the pipe (GRP) in the original process, and was writing the command to do the pipe (LS) in the hair process.
My reasoning is that why does this work: I was testing with LS Grep, LS was already finished writing the pipe before the hair process of grep was established, and therefore Never got pipes and grep never got the EOF GRP was prepared by changing its position and LS was waiting to write the ongoing process of LS. I believe that this is a very unsuccessful fix, so who reads it in the future, I hope you can provide better answers. If someone does such work, then my argument is that it may still be messy.
if (pipe flag == 1) {pipe (FD); Pid = fork (); If (PID == 0) {// hair process fPID = fork (); If (FPID == 0) {// baby child printf ("child's baby \ n"); Dup2 (FD [0], 0); Execvp (command1, argv1); // error is required to check printf ("mysh:% s: command not found \ n", argv [0]); Exhaust (EXIT_FAILURE); } If (FPID> gt; {2} second child's printf ("in another child's parents \ n"); Dup2 (FD [1], 1); Execvp (command, argv); // Printing error is required ("mysh:% s: command not found \ n", argv [0]); Exhaust (EXIT_FAILURE); } If (PID == -1) {printf ("Error: \ n"); Switch (error) {case EAGAIN: printf ("not of kanka process: system process limit can be reached \ n"); Case ENOMEM: printf ("can not be processed: memory out \ n"); } Return 1; }} If (PID & gt; 0) {/ Wait for Parent (PID, 0, 0); In printf ("external parent \ n"); } If (PID == -1) {printf ("Error: \ n"); Switch (error) {case EAGAIN: printf ("not of kanka process: system process limit can be reached \ n"); Case ENOMEM: printf ("can not be processed: memory out \ n"); } Return 1; }}
Comments
Post a Comment