简体   繁体   中英

Trouble creating a C program using fork() and execvp() functions

Here is the following code I am current having issues with:

#include <stdio.h>
#include <unistd.h>
#include<sys/types.h>
#include<sys/wait.h>

#define MAX_LINE 80

int main(void)
{
    char *args[MAX_LINE/2+1];
    int background= 0;//integer that acts a boolean for if & appears
    int should_run = 1;
    int status;

        while(should_run)//Just to keep the interface going until the user chooses to stop
        {
            printf("osh>");//prompt
            fflush(stdout);

            int i = 0;
            while(getchar() != '\n')//Use scanf until a new line is found
            {
                scanf("%s", &args[i]);

                if(strcmp(&args[i], "exit") == 0)//temporary exit protocal
                {
                    printf("Exiting now...");
                    return 0;
                }


                if(strcmp(&args[i], "&") == 0)//If we find a & in our input then we changed background to 1 or true
                    background = 1;
                printf("Args[%i] = %s\n", i, &args[i]);//Tester
                i++;
            }


            printf("Background = %i\n",background);//Test
            pid_t pid= fork();// Create new child process
            printf("process %i created\n", pid);//Test



            if(pid < 0)//fork() failed
            {
                printf("Fork Failed.\n");
                return 1;
            }

            else if(pid == 0)//Child process id
            {
                printf("Child process started %s command\n", &args[0]);
                if(execvp(args[0], args) < 0)//change the current child process to execute the input given by the user 
                                //with args[0] being the command and args being the parameters(ls -l is an example).
                {
                    printf("Command failed\n");
                }
                return 0;
            }

            else//Parent Process
            {
                if(background == 1)//If the user typed in a & then the parent will wait for a change in state from the child, if there is no &
                            //then we will just finish the parent process
                {
                    printf("Parent process waiting on child\n");
                    wait(NULL);
                }
            }

        }

    return 0;

I have one major issue and one minor issue right now. The major issue is that I have a printf method before execvp starts that says "Child Process started" and I get this line to print, but then nothing else happens. No interrupts are thrown, the program just seems to be frozen on my execvp command.

My minor issue is that when my program starts a prompt "osh>" before asking for input. Now if, for example, I would type in "osh>ls -l" then I get args[0] = s, args 1 = -l. Now if I put "osh> ls -l" in that exact format I get args[0] = ls, args 1 = -l. Is that a part of scanf() that I am not using properly here to make sure I get ever character after "osh>" and between blank spaces as strings?

EDIT: here is my output for user input "ls -l" prog.c的输出

The problem you're having with the missing character is because getchar() is consuming the first character of your input before scanf gets to take a stab at it. You probably want to do something like:

while (scanf("%s", &buffer) > 0)
{
    strcpy(args[i], buffer);
    /* then do stuff with args[i] */
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM