0

I am writing a C code that acts as a Linux shell. I'm trying to create a program that can run a command such as 'sleep 7 &' in the background while simultaneously running a command like 'ps -l' in the foreground. Currently, my code is triggering my "perror("wait")" error message. Am I not forking at the correct location? Any insight is appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/wait.h>
#include <string.h>

int execute(char *argv[])
/*
 * purpose: run a program passing it arguments
 * returns: status returned via wait, or -1 on error
 *  errors: -1 on fork() or wait() errors
 */
{
  int i = 0;                                        /* This will be our array iterator */
  size_t size = sizeof *(argv) / sizeof *(argv[0]); /* This is the size of the argv array that we will iterate through */
  int pid;
  int child_info = -1;
  
  /* Check if the first argument exists */
  if (argv[0] == NULL)
  { /* nothing succeeds     */
    return 0;
  }
  /* Using a for loop to traverse the argvs */
  for (i; i < size; i++)
  {
    /* if the argv exists */
    if (argv[i] != NULL)
    {
      /* check to see if the argv is an ampersand */
      if (strcmp(argv[i], "&") == 0)
      {        
        /* Let us know there is infact an ampersand */
        printf("We have an ampersand\n");
        argv[i] = '\0';               
      }      
    }    
  }  
  
  if ((pid = fork()) == -1)
  {
    perror("fork");
  }
  else if (pid == 0)
  {    
    signal(SIGINT, SIG_DFL);    
    signal(SIGQUIT, SIG_DFL);      
    execvp(argv[0], argv);            
    perror("cannot execute command");
    exit(1);    
  }
  else
  {     
    printf("\nProcess %d has begun in the background.\n", pid);
    fork();  !!!   <---- Where I'm trying to fork() and allow a new process to run in the foreground.
    if (waitpid(pid, &child_info, WNOHANG) == -1)
    {      
      perror("wait");
    }
    else if (waitpid(pid, &child_info, WNOHANG == 0))
    {      
      printf("Process %d done.\n", pid);
      kill(pid, SIGKILL);
    }
  }
return child_info;
  
}

Currently, it runs the sleep command but says "Wait: No Child Process".

Oka
  • 23,367
  • 6
  • 42
  • 53
  • Notes: `size_t size = sizeof *(argv) / sizeof *(argv[0]);` is [incorrect](https://stackoverflow.com/q/8269048/2505965). Pass the length as a separate argument, or just rely on the `NULL` sentinel value. `argv[i] = '\0';` instead of `argv[i] = NULL;` is strange, if generally harmless. – Oka May 10 '22 at 15:49
  • 1
    It's kinda useless to call `fork();` and then let the child process do exactly the same thing as the parent process. Do you want the child process and the parent process to do *different* things? – user253751 May 10 '22 at 15:49
  • Please post a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example), and use syntactically correct comments to detail parts of the code that require particular focus. – Oka May 10 '22 at 15:50
  • `waitpid(pid, &child_info, WNOHANG == 0)` -> `waitpid(pid, &child_info, WNOHANG) == 0`? – Oka May 10 '22 at 15:59
  • @user253751 Yes, that is exactly what I'm trying to do. Run two separate and different processes at the same time. – Looprachaun May 10 '22 at 16:21
  • @Looprachaun but presumably you want the two processes to do different things, right? – user253751 May 10 '22 at 16:48
  • 1
    `fork(); waitpid()` with nothing intervening doesn't make much sense. In the child, `waitpid` is guaranteed to fail, since the child has no children to wait for. – William Pursell May 10 '22 at 17:18
  • @user253751 Correct. – Looprachaun May 10 '22 at 17:36
  • @William Pursell I'm thinking I need to create another child or have a conditional statement if we are at the last process (child)? – Looprachaun May 10 '22 at 17:36
  • @Oka True, NULL is much more readable. Ill play around with your other suggestion, thank you. – Looprachaun May 10 '22 at 17:36
  • @Looprachaun So what do you want each process to do? By the way, waitpid can only wait for the current process's children, not just any process. – user253751 May 10 '22 at 17:49

0 Answers0