Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

wait(1) and wait(2) system call

[b][red]This message was edited by se7en at 2004-11-3 4:5:3[/red][/b][hr]
Hi,

I am trying to do a little programming in Linux which is simulating the a shell, like this:

command > ls -l

The program will take in the input "ls -l" and call execvp to execute it.
In the program i uses fork and wait(1) which is wait() without parameter.

But the programs executes in a very strange manner in the second time it runs dunno why. But if I change the wait(1) to wait(2) with paramter like wait(NULL) or wait(&status); then evrything goes smooth... Can you guys explain, thanks a lot.

[code]
# include
# include
# include
# include
//# include
# include

# define terminatedString "Byenow"
# define maxLength 100
# define maxArg 3

typedef enum boolean {false, true} boolean;

int main(void)
{
int i=0, j=0, length=0, prePos=-1, t=0, numOfArg=0, pID=0;
char inChar, input[maxLength], temp[maxLength], *arg[maxArg];
boolean repeated = false;

do
{
do
{
printf("=S3055940> ");

repeated = false;
numOfArg = 0;
prePos = -1;

for(i=0; (inChar = getchar()) != '
'; i++)
{
input[i] = inChar;
}

input[i] = '';

length = strlen(input);

// run to the end of the string to avoid losing the last character.
for(i=0; i<=length; i++)
{
if(input[i] == ' ' || input[i] == '')
{
t=0;

// prePos+1 to avoid accidentally getting the " " in the input.
for(j=prePos+1; j<i; j++)
{
temp[t] = input[j];
t++;
}

temp[t] = '';

prePos = i;

arg[numOfArg] = (char *) malloc(sizeof(char) * (strlen(temp)));

strcpy(arg[numOfArg], temp);

numOfArg++;
}
}

if(numOfArg > 3)
{
printf("Too many arguments -> Invalid command !!!
");
repeated = true;
}

} while(repeated == true);

/* If the first argument is a exit command, program will terminate. */
if(strcmp(arg[0], terminatedString) == 0)
{
free(arg[0]);
exit(0);
}

else
{
/* Intialize the last pointer of the arg array to NULL
to satisfy the execvp requirement. */
arg[numOfArg] = (char *) malloc(sizeof(char));
arg[numOfArg] = NULL;

pID = fork();

/* The child process will execute the command */
if(pID == 0)
{
printf("Enter child with id %d
",pID);
if(execvp(arg[0], arg) < 0)
{
printf("The command is not valid !!!
");
/* get out of child process if the invalid command was found. */
_exit(1);
}
_exit(0);
}

/* The parent process will wait for the completed execution
of the child process*/
else
{
printf("Enter parent with id %d
",pID);
wait(); // wait(NULL); //use this then it'll run ok
printf("Exit parent with id %d
",pID);
}

/* Free all the allocations for the arguments */
for(i=0; i<numOfArg; i++)
{
free(arg[i]);
}
}

} while (true);

return 1;
}
[/code]


Sign In or Register to comment.