Operating Systems 2014F: Tutorial 2: Difference between revisions

From Soma-notes
No edit summary
No edit summary
Line 5: Line 5:
# Change the prompt to be the current user (e.g., "student $"), as reported by the USER environment variable.
# Change the prompt to be the current user (e.g., "student $"), as reported by the USER environment variable.
# Make it not call wait() on the command if there is an & as the last argument to a command; instead, just return another prompt.  Can you see the zombies that are now produced?
# Make it not call wait() on the command if there is an & as the last argument to a command; instead, just return another prompt.  Can you see the zombies that are now produced?
# Use [http://pubs.opengroup.org/onlinepubs/7908799/xsh/sigaction.html sigaction()] and [http://pubs.opengroup.org/onlinepubs/009695399/functions/wait.html waitpid()] to create a signal handler for SIGCHLD that prevents the creation of zombies for background commands.
# Use [http://pubs.opengroup.org/onlinepubs/9699919799/functions/sigaction.html sigaction.html sigaction()] and [http://pubs.opengroup.org/onlinepubs/9699919799/functions/wait.html waitpid()] to create a signal handler for SIGCHLD that prevents the creation of zombies for background commands.





Revision as of 17:21, 12 September 2014

In this tutorial you will be playing with and modifying csimpleshell.c by Enrico Franchi. The source is also listed below.

In tutorial you should do the following exercises in order to better understand this code and the related operating system concepts. This week's assignment focuses on the concepts you should be learning specifically.

  1. Change the prompt to be the current user (e.g., "student $"), as reported by the USER environment variable.
  2. Make it not call wait() on the command if there is an & as the last argument to a command; instead, just return another prompt. Can you see the zombies that are now produced?
  3. Use sigaction.html sigaction() and waitpid() to create a signal handler for SIGCHLD that prevents the creation of zombies for background commands.


Code

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#define BUFFER_SIZE 1<<16
#define ARR_SIZE 1<<16

void parse_args(char *buffer, char** args, 
                size_t args_size, size_t *nargs)
{
    char *buf_args[args_size]; /* You need C99 */
    char **cp;
    char *wbuf;
    size_t i, j;
    
    wbuf=buffer;
    buf_args[0]=buffer; 
    args[0] =buffer;
    
    for(cp=buf_args; (*cp=strsep(&wbuf, " \n\t")) != NULL ;){
        if ((*cp != '\0') && (++cp >= &buf_args[args_size]))
            break;
    }
    
    for (j=i=0; buf_args[i]!=NULL; i++){
        if(strlen(buf_args[i])>0)
            args[j++]=buf_args[i];
    }
    
    *nargs=j;
    args[j]=NULL;
}


int main(int argc, char *argv[], char *envp[]){
    char buffer[BUFFER_SIZE];
    char *args[ARR_SIZE];

    int *ret_status;
    size_t nargs;
    pid_t pid;
    
    while(1){
        printf("$ ");
        fgets(buffer, BUFFER_SIZE, stdin);
        parse_args(buffer, args, ARR_SIZE, &nargs); 

        if (nargs==0) continue;
        if (!strcmp(args[0], "exit" )) exit(0);       
        pid = fork();
        if (pid){
            printf("Waiting for child (%d)\n", pid);
            pid = wait(ret_status);
            printf("Child (%d) finished\n", pid);
        } else {
            if( execvp(args[0], args)) {
                puts(strerror(errno));
                exit(127);
            }
        }
    }    
    return 0;
}