Results 1 to 3 of 3
so i have the following function which is supposed to fork a child process, open a text editor (assumably Kate), wait for the user to finish, and print a statement ...
- 10-24-2007 #1Just Joined!
- Join Date
- Oct 2007
- Posts
- 60
wait() function problem
so i have the following function which is supposed to fork a child process, open a text editor (assumably Kate), wait for the user to finish, and print a statement to signify when the user has closed the editor..
I've tried this code on kubuntu, and it worked perfectly fine. The terminal waits and the CHILD PROCESS DONE statement only gets printed when the editor is closed..Code:#include <sys/wait.h> #include <sys/types.h> #include <stdio.h> #include <unistd.h> #include <string.h> #include <signal.h> void startEditor(char* fileName) { pid_t parent_id,child_id,exited_id; parent_id = getpid(); printf("Parent Process ID: %d\n",parent_id); int status; if((child_id = fork()) == 0){ child_id = getpid(); char* args[] = {"/usr/bin/kate",fileName,NULL}; execvp("/usr/bin/kate",args); } wait(&status); printf("CHILD PROCESS DONE!\n"); } int main(int argc, char** argv) { char* inputFile = argv[1]; //file read startEditor(); return 0; }
however, i tried this code on Fedora, (can't remember which version), it didn't run as planned..the process didn't wait..it openned the editor as its supposed to, but it didn't wait, the terminal printed out CHILD PROCESS DONE right after the editor openned..why didn't it waait..it did on Kubuntu..why didn't it on Fedora:S?
- 10-24-2007 #2
I don't know the full answer, but here's a start.
It's a good idea, whenever you call a library function or system function such as wait(), to examine the returned value. Always. You can find the meaning of the returned value by doing this at the command line:
The man page will tell you that it's the pid of the process which has just completed. Compare it with the pid of the process you fork()ed; in your code, that would be child_id. If they're different, you want to know this.Code:man 2 wait
If they're the same, then as soon as your program finishes, while kate is running, do this at the command line:
to see what kate's pid is. It can't possibly be (hmm hmm) the same as child_id. If it's different, maybe running kate on your version of Fedora actually runs a shell script or something which fires up kate and exits without waiting for kate to finish.Code:ps -ef | grep -i kate | grep -v grep
You can test that hypothesis by going to your Fedora system and just typing
at a shell prompt, and see whether you get a shell prompt back right away, or whether you don't get a shell prompt until kate is finished.Code:kate somefilename
Hope this helps.
- 10-25-2007 #3
I don't know the exact solution here, but one thing that I do note is that you're using the wait() function at all. wait() waits for any child process terminates. You may want to use waitpid(), where you can specify the child process to wait for. The equivalent code with waitpid() would be:
Alternatively, you could check the return value of wait(), and make sure that some error isn't occurring instead.Code:waitpid(child_id, &status, 0);
DISTRO=Arch
Registered Linux User #388732


Reply With Quote