I am writing a C program. All code is in while loop but I did not add it yet. It takes input from user like start ls -l
and run ls -l
. And wait new command. If user write "wait" and there is a process that is not end, It waits until the child process is completed and print child process id that is just completed and take new command. My wait command does not working, and I need your help.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main (int argc, char **argv)
{
char command[1024];
char *cmd;
char *ptr1;
char *ptr2 = (char*)malloc(10 * sizeof(char));
char *ptr3;
char *pos;
char *args[3];
char tmp[1024];
int count = 0;
int cstatus;
pid_t child;
pid_t c;
printf("newShell> ");
fgets( command, 1024, stdin );
if ((pos=strchr(command, '\n')) != NULL)
*pos = '\0';
if (strlen(command) > 1024)
{
printf("Command is too long\n");
exit(1);
}
strcpy(tmp,command);
ptr1 = strtok(tmp," ");
cmd = ptr1;
while(ptr1 != NULL)
{
ptr1 = strtok(NULL, " ");
if (ptr1 == NULL) break;
if (count == 0)
{
ptr3 = ptr1;
count = 1;
}
else
strcat(strcat(ptr2,ptr1)," ");
}
ptr2[strlen(ptr2) - 1] = '\0';
args[0] = ptr3;
args[1] = ptr2;
args[2] = NULL;
if (strcmp(cmd,"start") == 0)
{
if ((child = fork()) == 0)
{
printf("\nProcess %ld is started\n", (long) getpid());
execvp(args[0], args);
fprintf(stderr, "execvp() couldn't do the process\n");
exit(1);
}
if (child == (pid_t)(-1))
{
fprintf(stderr, "Fork error.\n"); exit(1);
}
}
else if (strcmp(cmd,"wait") == 0)
{
if (child > 0)
{
c = wait(&cstatus);
printf("Process %ld completed with code: %d \n",
(long) c, cstatus);
}
else
printf("There is no process\n");
}
}
Edit:
Should i change that part with:
child = fork();
if (child == (pid_t)(-1))
{
fprintf(stderr, "Fork failed.\n");
exit(1);
}
if (strcmp(cmd,"start") == 0)
{
if (child == 0)
{
printf("\nProcess %ld is started\n", (long) getpid());
execvp(args[0], args);
fprintf(stderr, "execvp() couldn't do the process\n");
exit(1);
continue;
}
else if (strcmp(cmd,"wait") == 0)
c = wait(&cstatus);
printf("Parent: Child %ld exited with status = %d\n",
(long) c, cstatus);
continue;
}