can anyone in here help me with my programming project.

Your C program must be invoked exactly as follows:

shell [batchFile]

The command line arguments to your shell are to be interpreted as follows.
batchFile: an optional argument (often indicated by square brackets as above). If present, your shell will read each line of the batchFile for commands to be executed. If not present, your shell will run in interactive mode by printing a prompt to the user at stdout and reading the command from stdin.
For example, if you run your program as

shell /u/j/v/batchfile
then your program will read commands from /u/j/v/batchfile until it sees the quit command.

Parsing
For reading lines of input, you may want to look at fgets(). To open a file and get a handle with type FILE * , look into fopen(). Be sure to check the return code of these routines for errors! (If you see an error, the routine perror() is useful for displaying the problem.) You may find the strtok() routine useful for parsing the command line (i.e., for extracting the arguments within a command separated by whitespace or a tab or ...).

Executing Commands
Look into fork(), execvp(), and wait/waitpid().
The fork() system call creates a new process. After this point, two processes will be executing within your code. You will be able to differentiate the child from the parent by looking at the return value of fork; the child sees a 0, the parent sees the pid of the child.
You will note that there are a variety of commands in the exec family; for this project, you must use execvp(). Remember that if execvp() is successful, it will not return; if it does return, there was an error (e.g., the command does not exist). The most challenging part is getting the arguments correctly specified. The first argument specifies the program that should be executed, with the full path specified; this is straight-forward. The second argument, char *argv[] matches those that the program sees in its function prototype:
int main(int argc, char *argv[]);

Recommended Answers

All 5 Replies

Oh, dear, dear. Where's your effort on the matter? No posted code showing what you have tried already towards the assignment; no help you'll receive, I am afraid.
It is not my rule, it is the policy of this board.

Instead of all that fork/exec stuff, how far can you get with all the command line stuff and parsing, so you can just get to things like printf( "The command to execute is %s", command ); Until this works reliably, there's no point even worrying about the fork/exec, and it shows us you're not being a total sponge.

Or maybe even something simpler than that. It doesn't matter, so long as you show some actual code effort, we'll help.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/*
 * This program is intended to help you test your shell.
 * You can use it to test that you are correctly starting and waiting
 * for multiple processes.
 * 
 * Recommended Usage: 
 * Run multiple copies of this program simultaneously
 *
 * You should see the output from each intermingled 
 * (you can tell which process is which because each prints its "pid").
 *
 * You might only see the output intermingled when one of the processes 
 * goes to sleep; this is okay.  However, it is not okay if you see the
 * output of one process completely before the output of the other.
 *
 */
int main(int argc, char *argv[])
{

  int pid;
  int i;
  int j;
  int outer = 5;
  int inner = 10;
  int count = 0;
  long max_usleep = 1000000; // 1 second
  int seed;
  long usec;
  int c;
 
  pid = getpid();
  while ((c = getopt(argc, argv, "o:i:s:r:")) != -1) {
    switch (c) {
    case 'o':
      outer = atoi(optarg);
      break;

    case 'i':
      inner = atoi(optarg);
      break;

    case 's':
      max_usleep = (long)atoi(optarg);
      break;

    case 'r':
      seed = atoi(optarg);
      srand(seed);
      break;
      
    default:
      fprintf(stderr, "Usage: %s [-o <outerloops>] [-i <innerloops>] [-s <maxusecsleep>] [-r <random seed>]\n", argv[0]);
      exit(1);
    }
  }

  for (i = 0; i < outer; i++) {

    for (j = 0; j < inner; j++) {

      printf("%d: %d\n", pid, count);
      count++;
    }

    usec = max_usleep * ((float)rand() / (float)RAND_MAX);
    printf("%d sleeping for  %ld usec\n", pid, usec);
    usleep(usec);
  }

  printf("Job completed\n");
}


//this is the given program and i have to place my code in this program which i'm unable to do


// my code v
//              v 

// using fork  
   
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>


int main(void)
{
    pid_t child;


    if((child = fork()) == -1) {
        perror("fork");
        exit(EXIT_FAILURE);
    } else if(child == 0) {
        puts("in child");
        printf("\tchild pid = %d\n", getpid());
        printf("\tchild ppid = %d\n", getppid());
        exit(EXIT_SUCCESS);
    } else {  
 
 
fputs("in parent");
        printf("\tparent pid = %d\n", getpid());
        printf("\tparent ppid = %d\n", getppid());
    }
    exit(EXIT_SUCCESS);
}


// uses the kill function to send two signals to a sleeping child process, 
// one that will be ignored, and one that will kill the process  
 
#include <sys/types.h>
#include <wait.h>
#include <unistd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>


int main(void)
{
    pid_t child;
    int errret;


    if((child = fork()) < 0) {
        perror("fork");
        exit(EXIT_FAILURE);
    } else if(child == 0) { /* in the child process */
        sleep(30);
    } else { /* in the parent */
        /* send a signal that gets ignored */
        printf("sending SIGCHLD to %d\n", child);
        errret = kill(child, SIGCHLD);
        if(errret < 0)
            perror("kill:SIGCHLD");
        else
            printf("%d still alive\n", child);


        /* now murder the child */
        printf("killing %d\n", child);
        if((kill(child, SIGTERM)) < 0)
            perror("kill:SIGTERM");
        /* have to wait to reap the status */
        waitpid(child, NULL, 0 ); 
}
     exit(EXIT_SUCCESS);
}
commented: You were so eager to show that you could do it, don't you? READ the rules of the forum, first. -2

Another lazy attempt at getting help -- with no Code Tags. Why didnt you read any of the requested information posted all over this site about CODE tags, like
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) on the background of the box you actually typed you message in

> this is the given program and i have to place my code in this program which i'm unable to do
More like unable to try.
You've been given 99% of the code, and still you're stuck.

Here's the one line of code execl( "/bin/ls", "/bin/ls", "-l", (char*)NULL ); Now I'm going to leave just one last bit of mystery to see if you can figure out where that lines goes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.