Find the answer to your Linux question:
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 ...
  1. #1
    Just 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..
    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;
    }
    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..
    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?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    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:
    Code:
    man 2 wait
    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.

    If they're the same, then as soon as your program finishes, while kate is running, do this at the command line:
    Code:
    ps -ef | grep -i kate | grep -v grep
    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.

    You can test that hypothesis by going to your Fedora system and just typing
    Code:
    kate somefilename
    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.

    Hope this helps.

  3. #3
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    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:
    Code:
    waitpid(child_id, &status, 0);
    Alternatively, you could check the return value of wait(), and make sure that some error isn't occurring instead.
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...