Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 12
so im trying to fork another process in a program im writing that will open the text editor on a specific file.. like this: pid_t parent_id, child_id,exited_id; parent_id = getpid(); ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60

    execve() issue

    so im trying to fork another process in a program im writing that will open the text editor on a specific file..
    like this:
    pid_t parent_id, child_id,exited_id;
    parent_id = getpid();

    if((child_id = fork()) == 0){
    int status;
    pid_t id = getpid();
    char* newProg = "/home/mazazino/Desktop/dsk/272.1/powers-1/powers_one";
    char* newArgs = {NULL};
    char* newEnv = {NULL};
    status = execve(newProg,newArgs,newEnv);
    exit(0);
    }
    exited_id = wait();
    printf("child process done\n");

    so in the execve() instruction, i want to load the executable of the text editor (Kate in Kubuntu), which is supposedly somehwere in the root but i didnt find it..and then i wanna open a specific file directly in the editor...so if anyone has any idea what the executable file of the text editor is, and how to automatically load the text file i want to open in the editor, i would appreciate the help..

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    There are several things wrong with this program, one of which is that it won't compile.

    When you fix the compilation problems, consider also these points:

    If you're going to use execve(), you don't really want to pass an empty environment variable list. You want to look at your current environment variable list and change (or weed out) only what's absolutely necessary to change (or weed out).

    You can get a list of the current process's environment variables by declaring your main() function thus:
    Code:
    int
    main(int    argc,
         char **argv,
         char **arge
        )
    You can then examine arge.

    You'll want newProg to be the name of Kate itself (herself?). You'll need to find the full pathname of that (/usr/bin/kate, or whatever).

    If you're willing to hardwire that pathname into your program, do this at the command line to find that pathname:
    Code:
    which kate
    If you want to be more general about this, have your program look at all the paths in the PATH environment variable, and find the first one which contains a copy of kate which is executable.

    The first element in newArgs should be the same as newProg (that is, the fully qualified pathname to kate that you just found).

    The remaining elements in newArgs are the same command-line arguments you'd use when running kate from the command line, including the name of the file you'll be editing.

    For further info, please enter these commands at the command line, and read them carefully before asking your next question. (grin)
    Code:
    man kate
    man execve
    man wait
    man which
    Hope this helps.

  3. #3
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60
    i tried typing 'kate mytextfile' in thhe directory that 'mytexyfile' exists from the shell and it openned kate at the text file i want(which is called 'mytextfile'..so i know now that the first argument the program takes is "kate", and the second is the file name which has to be in the same directory im in..so i modified the code..but i dont know what environment variables kate should take...actually i dont know what are environment variables..i used the which command to find the path to kate and i did..and i put it in...so tell me where'd i go wrong in this..('file' is the name of the text file i wanna open)
    if((child_id = fork()) == 0){
    int status;
    char* newProg = "/usr/bin/kate";
    char* newArgs = {"kate","/home/mazazino/Desktop/dsk/Trials/mytextfile",NULL};
    char* newEnv = {NULL};
    status = execve(newProg,newArgs,newEnv);
    exit(0);
    }
    exited_id = wait();
    printf("done\n");

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You couldn't have possibly run the code you post here, because it won't compile without errors. Fix that first, then let's continue.

  5. #5
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60
    i did...it gave me warnings but it did run..

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I've done some experimenting, and there's much to untangle here.

    First, is it true that this isn't the whole source file? Is this in a function main()? Do you have any #include statements? Because it's the set of #include statements you used that could provide the path to enlightenment.

    Could you please post a program which:
    1. is a complete source file, so that we can compile it and watch the behavior you talk about; and
    2. is tiny enough that we can see clearly just what's going on?

    And to make it more readable, could you please put it within CODE markers? To do this:
    1. highlight the code as it sits in your reply window at this site; and
    2. click the # icon just above your editing space.

  7. #7
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60
    this is the full code...
    #include <stdio.h>
    #include <sys/types.h>
    #include <termios.h>
    #include <unistd.h>
    #include <ctype.h>
    #include <signal.h>
    int main()
    {
    pid_t parent_id, child_id,exited_id;
    parent_id = getpid();

    if((child_id = fork()) == 0){
    int status;
    char* newProg = "/usr/bin/kate";
    char* newArgs = {"kate","/home/mazazino/Desktop/dsk/Trials/file",NULL};
    char* newEnv = {NULL};
    status = execve(newProg,newArgs,newEnv);
    exit(0);
    }
    exited_id = wait();
    printf("done\n");
    return 0;
    }

  8. #8
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    If you dont' pass an environment variable with the 3th argument you can use execvp:

    Code:
    char* arg_list[] = "/usr/bin/kate","/home/mazazino/Desktop/dsk/Trials/mytextfile","/",NULL};
    
    status = execvp ("/usr/bin/kate", arg_list);
    Regards

  9. #9
    Just Joined!
    Join Date
    Oct 2007
    Posts
    60
    Quote Originally Posted by Franklin52 View Post
    If you dont' pass an environment variable with the 3th argument you can use execvp:

    Code:
    char* arg_list[] = "/usr/bin/kate","/home/mazazino/Desktop/dsk/Trials/mytextfile","/",NULL};
    
    status = execvp ("/usr/bin/kate", arg_list);
    Regards
    i tried it but it gave me an error when it started Kate..it said that the file/ is not a folder..so i tried to change your code and i erased the third entry in the args array, and it worked..something like this:
    Code:
    char* args_list[] = {"/usr/bin/kate","/home/mazazino/Desktop/dsk/Trials/file",NULL};
     status = execvp("/usr/bin/kate",args_list);
    so it works..thanks..but a few questions i dont know about..what's the difference between execvp and execve, and what should have been in the environment variables array if i was to use execve?..in other words, whats the environment variable array?..and thanks for the help

  10. #10
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    what's the difference between execvp and execve
    Do this at the command line:
    Code:
    man execvp
    man execve
    what should have been in the environment variables array if i was to use execve
    As was mentioned in the second post of this thread:
    You want to look at your current environment variable list and change (or weed out) only what's absolutely necessary to change (or weed out).
    Unless you know of a specific reason to do so, don't weed out anything.
    whats the environment variable array?
    For a good, full explanation, go here. You have access to your environment variable array by using the arge parameter to main(), as shown in the second post.

Page 1 of 2 1 2 LastLast

Posting Permissions

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