fork troubles...

This is a discussion on fork troubles... within the C Programming forums, part of the General Programming Boards category; ok, just for background info, i'm attempting to write my own shell. Here's my problem though. Code: while(scanf("%s", &inputstr) !='x'){ ...

  1. #1
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429

    fork troubles...

    ok, just for background info, i'm attempting to write my own shell. Here's my problem though.

    Code:
    while(scanf("%s", &inputstr) !='x'){
    pid1=fork();
    if (pid == 0)
      execlp(inputstr);
    else
      wait(&statloc);
    
    printf("[%s %s]$ ", linebuf, pwd);
    Two problems: 1) my program doesn't exit when you type in 'x'. Secondly, when i do the fork, pid1 never equals 0, therefore my execlp never runs... for instance, I can type "ls" into my prompt and it just returns nothing and prints out my prompt again. HELP?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    Code:
    while(scanf("%s", &inputstr) !='x'){
    I think that you are trying to compare inputstr to 'x'? If you are, then your problem is that the return value of scanf() is the number of values that the function assigned, not the value read.
    So you need to compare inputstr to 'x' directly. I'm not sure about the fork problem, though.

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: fork troubles...

    Originally posted by ober5861
    ok, just for background info, i'm attempting to write my own shell. Here's my problem though.

    Code:
    while(scanf("%s", &inputstr) !='x'){
    pid1=fork();
    if (pid == 0)
      execlp(inputstr);
    else
      wait(&statloc);
    
    printf("[%s %s]$ ", linebuf, pwd);
    Two problems: 1) my program doesn't exit when you type in 'x'. Secondly, when i do the fork, pid1 never equals 0, therefore my execlp never runs... for instance, I can type "ls" into my prompt and it just returns nothing and prints out my prompt again. HELP?
    The scanf bit is wrong (as already stated by fyodor).

    The value of pid could be -1 if the fork fails, you better not do the wait in that in case as there is no child to wait for (not sure if that really matters tho, best read up about that this).
    Even so, it'd be good to tell the user fork failed.

    I believe the call to execlp is incorrect, again check your documentation. My docs say the function prototype is
    Code:
    int execlp(  file, arg0, arg1..., argn, NULL );
    Therefore the first arg is the prog name, the next batch are the arguments, and the last NULL is mandatory.

    Once you get execlp sorted, there is a good chance that it will fail (especially if the user enters a typo by mistake. You need to handle that too. My documentation says:
    When the invoked program is successfully initiated, no return occurs. When an error is detected while invoking the indicated program, exec...() returns -1, and errno is set to indicate the error.
    In your case, if execlp fails, the child process will print a prompt and ask the user for a command, and you will then be in a situation of having two shells !
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    5|-|1+|-|34|) ober's Avatar
    Join Date
    Aug 2001
    Posts
    4,429
    fyodor, can you explain how i might go about that? I don't understand what you mean.

    Secondly, I have another section in my code that takes a path string and parses it to get the actual current folder. is there an easy way to say

    array[count2] = array2[count]

    I just want to put whatever letter is in the second position in the first array into the certain position in the second array. anyone? Or is there a better way to parse that string and get to the last part of the string, say:

    /home/user/test/test2

    i want to get just "test2". help??

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Try looking at strrchr().. It finds the last occurence of a char in a string. Sounds like the sort of thing ya need.

    Also, strcmp() if for comparing strings.
    Last edited by Hammer; 05-02-2002 at 07:42 AM.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by ober5861
    fyodor, can you explain how i might go about that? I don't understand what you mean.
    Scanf returns the number of fields successfully converted. You don't want to check this return value with 'x'. You need to check if the first character in your input buffer is 'x':
    Code:
    char inputstr[1024];
    
    while(scanf("%s", inputstr) && inputstr[0] != 'x')
    {
        /* do something */
    }
    
    or
    
    while(scanf("%s", inputstr) && strcmp(inputstr, "x"))
    {
        /* do something */
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fork(), pause(), and storing PID's in a linked list
    By vital101 in forum C Programming
    Replies: 10
    Last Post: 09-28-2007, 02:16 AM
  2. Fork() not working.
    By BENCHMARKMAN in forum C++ Programming
    Replies: 3
    Last Post: 08-01-2007, 12:28 AM
  3. Fork - unpredictable?
    By fredkwok in forum Linux Programming
    Replies: 4
    Last Post: 03-26-2006, 01:49 PM
  4. fork(), exit() - few questions!
    By s3t3c in forum C Programming
    Replies: 10
    Last Post: 11-30-2004, 05:58 AM
  5. Daemon programming: allocated memory vs. fork()
    By twisgabak in forum Linux Programming
    Replies: 2
    Last Post: 09-25-2003, 02:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21