int main(int argc, char **argv) { int pid; sigset_t mask; Signal(SIGCHLD, handler); initjobs(); /* Initialize the job list */ while (1) { Sigemptyset(&mask); Sigaddset(&mask, SIGCHLD); Sigprocmask(SIG_BLOCK, &mask, NULL); /* Block SIGCHLD */ /* Child process */ if ((pid = Fork()) == 0) { Sigprocmask(SIG_UNBLOCK, &mask, NULL); /* Unblock SIGCHLD */ Execve("/bin/ls", argv, NULL); } /* Parent process */ addjob(pid); /* Add the child to the job list */ Sigprocmask(SIG_UNBLOCK, &mask, NULL); /* Unblock SIGCHLD */ } exit(0); }
/* * main - shell main routine */ int main(int argc, char **argv) { char cmdline[MAXLINE]; /* command line */ if (argc > 2) usage(); if (argc == 2) { if (!strcmp(argv[1], "-v")) verbose = 1; else usage(); } Signal(SIGINT, sigint_handler); /* ctrl-c */ Signal(SIGCHLD, sigchld_handler); Signal(SIGTSTP, sigtstp_handler); /* ctrl-z */ initjobs(jobs); /* read/eval loop */ while (1) { /* read command line */ printf("%s", prompt); Fgets(cmdline, MAXLINE, stdin); if (feof(stdin)) exit(0); /* evaluate command line */ eval(cmdline); } exit(0); }
/* * main - The shell's main routine */ int main(int argc, char **argv) { char c; char cmdline[MAXLINE]; int emit_prompt = 1; /* emit prompt (default) */ /* Redirect stderr to stdout (so that driver will get all output * on the pipe connected to stdout) */ dup2(1, 2); /* Parse the command line */ while ((c = getopt(argc, argv, "hvp")) != EOF) { switch (c) { case 'h': /* print help message */ usage(); break; case 'v': /* emit additional diagnostic info */ verbose = 1; break; case 'p': /* don't print a prompt */ emit_prompt = 0; /* handy for automatic testing */ break; default: usage(); } } /* Install the signal handlers */ Signal(SIGINT, sigint_handler); /* ctrl-c */ Signal(SIGTSTP, sigtstp_handler); /* ctrl-z */ Signal(SIGCHLD, sigchld_handler); /* terminated or stopped child */ Signal(SIGQUIT, sigquit_handler); /* so parent can cleanly terminate child*/ /* Initialize the job list */ initjobs(jobs); /* Execute the shell's read/eval loop */ while (1) { /* Read command line */ if (emit_prompt) { printf("%s", prompt); fflush(stdout); } if ((fgets(cmdline, MAXLINE, stdin) == NULL) && ferror(stdin)) app_error("fgets error"); if (feof(stdin)) { /* End of file (ctrl-d) */ fflush(stdout); exit(0); } /* Evaluate the command line */ eval(cmdline); fflush(stdout); fflush(stderr); } exit(0); /* control never reaches here */ }
int main() { int i; char buf[MAXJDESC]; initjobs(job_list); // Populate the job list for(i = 0; i < MAXJOBS; i++) addjob(job_list, i+1, "Job"); // delete third job deletejob(job_list, 3); listjobs(job_list, buf); return 0; }
int main() { char cmdline[MAXLINE]; forepid = 0; printf("tcgetsid: %d getsid=%d tcgetpgrp=%d myshell pgrp=%d\n", tcgetsid(STDIN_FILENO), getsid(getpid()), tcgetpgrp(STDIN_FILENO), getpgrp()); signal(SIGINT, sigint_handler); signal(SIGTSTP, sigtstp_handler); signal(SIGCHLD, sigchld_handler); signal(SIGALRM, sigalarm_handler); initjobs(); sigsetjmp(env, 1); while (1) { printf("myshell> "); Fgets(cmdline, MAXLINE, stdin); if (feof(stdin)) exit(0); eval(cmdline); } }
int main() { char cmdline[MAXLINE]; control_block(); shellpid = getpgrp(); forepid = 0; printf("tcgetsid: %d\ngetsid=%d\ntcgetpgrp=%d\nmyshell pgrp=%d\n", tcgetsid(STDIN_FILENO), getsid(getpid()), tcgetpgrp(STDIN_FILENO), getpgrp()); if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) { printf("signal error\n"); exit(-1); } initjobs(); while (1) { printf("myshell> "); Fgets(cmdline, MAXLINE, stdin); if (feof(stdin)) exit(0); eval(cmdline); } }
/* * main - The shell's main routine */ int main(int argc, char **argv) { char c; char cmdline[MAXLINE]; /* cmdline for fgets */ int emit_prompt = 1; /* emit prompt (default) */ shell_pid = getpid(); /* Redirect stderr to stdout (so that driver will get all output * on the pipe connected to stdout) */ dup2(1, 2); /* Parse the command line */ while ((c = getopt(argc, argv, "hvp")) != EOF) { switch (c) { case 'h': /* print help message */ usage(); break; case 'v': /* emit additional diagnostic info */ verbose = 1; break; case 'p': /* don't print a prompt */ emit_prompt = 0; /* handy for automatic testing */ break; default: usage(); } } /* Install the signal handlers */ /* These are the ones you will need to implement */ Signal(SIGINT, sigint_handler); /* ctrl-c */ Signal(SIGTSTP, sigtstp_handler); /* ctrl-z */ Signal(SIGCHLD, sigchld_handler); /* Terminated or stopped child */ Signal(SIGTTIN, SIG_IGN); Signal(SIGTTOU, SIG_IGN); /* This one provides a clean way to kill the shell */ Signal(SIGQUIT, sigquit_handler); /* Initialize the job list */ initjobs(job_list); /* Execute the shell's read/eval loop */ while (1) { if (emit_prompt) { printf("%s", prompt); fflush(stdout); } if ((fgets(cmdline, MAXLINE, stdin) == NULL) && ferror(stdin)) app_error("fgets error"); if (feof(stdin)) { /* End of file (ctrl-d) */ printf ("\n"); fflush(stdout); fflush(stderr); exit(0); } /* Remove the trailing newline */ cmdline[strlen(cmdline)-1] = '\0'; /* Evaluate the command line */ eval(cmdline); fflush(stdout); fflush(stdout); } exit(0); /* control never reaches here */ }
/* * main - The shell's main routine * * Requires: * argc should be less than MAXARGS arguments. * argv should contain less than MAXARGS arguments. * * Effects: * Runs an interactive command-line interpreter that runs programs on * behalf of the user. It waits for a command line and then carries out * the action of that command line. The shell executes built-in commands * immediately, but also can execute user programs by forking child * processes. It manages jobs running the foreground and background. */ int main(int argc, char **argv) { int c; char cmdline[MAXLINE]; char *path = NULL; bool emit_prompt = true; // Emit a prompt by default. /* * Redirect stderr to stdout (so that driver will get all output * on the pipe connected to stdout). */ dup2(1, 2); // Parse the command line. while ((c = getopt(argc, argv, "hvp")) != -1) { switch (c) { case 'h': // Print a help message. usage(); break; case 'v': // Emit additional diagnostic info. verbose = true; break; case 'p': // Don't print a prompt. // This is handy for automatic testing. emit_prompt = false; break; default: usage(); } } // Install the signal handlers. // These are the ones you will need to implement: Signal(SIGINT, sigint_handler); // ctrl-c Signal(SIGTSTP, sigtstp_handler); // ctrl-z Signal(SIGCHLD, sigchld_handler); // Terminated or stopped child // This one provides a clean way to kill the shell. Signal(SIGQUIT, sigquit_handler); // Initialize the search path. path = getenv("PATH"); initpath(path); // Initialize the jobs list. initjobs(jobs); // Execute the shell's read/eval loop. while (true) { // Read the command line. if (emit_prompt) { printf("%s", prompt); fflush(stdout); } if ((fgets(cmdline, MAXLINE, stdin) == NULL) && ferror(stdin)){ app_error("fgets error");} if (feof(stdin)) { // End of file (ctrl-d) fflush(stdout); exit(0); } // Evaluate the command line. eval(cmdline); fflush(stdout); fflush(stdout); } // Control never reaches here. assert(false); }
//main function which will be called first, envp[] consists all the environment variables int main(int argc, char *argv[],char *envp[]) { char *input=NULL; //stores user input int j,k; char *filename=NULL; pid_t pid3,pid4; char *cwd; char *usrcmdargs[100]; int is_background; //used to determine a background process FILE *fp; char *s; input = (char *) malloc (100 + 1); initjobs(job_list); while (1) { printf("in main %s\n",getcwd(NULL, 64)); if ((cwd = getcwd(NULL, 64)) == NULL) { perror("pwd"); exit(2); } printf("in main\n"); cwdtemp=cwd; signal(SIGINT,SIG_IGN); //capture SIGINT signal and ignore its default function. signal(SIGINT,handle_signal); //capture SIGINT signal and redirect to handle_signal. //signal(SIGCHLD,SIG_IGN); signal(SIGCHLD,handle_sigchild); printf("%s", cwd); printf("~quash>"); input = readLine(input,100); is_background = checkforsymbol(input,'&'); if(is_background !=0){ input[is_background] = '\0'; } j=checkforsymbol(input,'>'); //checking if output redirection is present in command k=checkforsymbol(input,'<'); //checking if output redirection is present in command if(j !=0){ filename=&input[j+1]; input[j]='\0'; //execute the output redirection block if(filename){ pid3=fork(); if(pid3 == 0){ //creates a child process fdout = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0666); dup2(fdout, STDOUT_FILENO); execute_command(input,is_background); close(fdout); exit(0); } else if(is_background !=0){ printf("[1] %d\n",pid3); } else{ waitpid(pid3,&status, 0); } } else{ printf("Please enter the file name"); } } else if(k !=0){ filename=&input[k+1]; input[k]='\0'; //execute the input redirection block if(strcmp(input,"quash") == 0){ //block used to read commands from a file and execute them fp = fopen(filename, "r"); while(fgets(input, 1024, fp) != NULL){ s=strchr(input,'\n'); if(s != NULL){ // if \n is present in the command, then remove it and replacing by \0 input[strlen(input)-1] = '\0'; } execute_command(input,is_background); } fclose(fp); } else{ //block used to perform input redirection operation pid4=fork(); if(pid4 == 0){ fdin = open(filename,O_RDONLY); dup2(fdin, STDIN_FILENO); close(fdin); split(input, usrcmdargs); if(usrcmdargs[0]){ //checking for \n condition, when user is pressing enter. if(strcmp(usrcmdargs[0],"cd") == 0){ changedir(usrcmdargs[1]); } else if((strcmp(usrcmdargs[0], "exit") == 0) || (strcmp(usrcmdargs[0], "quit") == 0)) { exit(0); } else{ err=execvp(usrcmdargs[0],usrcmdargs); printf("errno is %d\n", errno); if(err == -1){ printf("command do not exist\n"); return EXIT_FAILURE; } } } exit(0); } else if(is_background !=0){ printf("[1] %d\n",pid4); } else{ waitpid(pid4,&status, 0); } } } else{ execute_command(input,is_background); } } free(input); }