Results 1 to 1 of 1
I have tryed to make a background process but iam not sure about that is working or not. Or that should it be implement this way ??
cmd &
cmd ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
- 05-29-2003 #1Just Joined!
- Join Date
- May 2003
- Posts
- 6
background process
I have tryed to make a background process but iam not sure about that is working or not. Or that should it be implement this way ??
cmd &
cmd > file &
cmd < file &
cmd | cmd &
Code:/*************************************************************************** * * *Eksempletet viser en shell der skal vise brug af pipe og background processr* * * ***************************************************************************/ #ifdef HAVE_CONFIG_H #include <config.h> #endif #include <sys/types.h> #include <sys/wait.h> #include <stdlib.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <fcntl.h> /* The following define's belongs to read_command(..) */ #define MAXPARMS 20 #define MAXPARMLEN 255 #define MAXINPUT 255 #define shell "cesh" /* for pwd command*/ #define SIZE 200 /* The Command line parser */ /*------------------------------------------------------------------*/ /* read_command: */ /* Print the prompt and get one characters from the users which */ /* makes up an entire command line entry */ /*------------------------------------------------------------------*/ void read_command(char * command, char * parameters[]) { char input[MAXINPUT]; char * parm; char c; int i; /* initialize the whole parameters array to NULL pointers */ for (i=0;i<MAXPARMS;i++) { parameters[i]=NULL; } i=0; input[0]='\0'; /* loop until some characters are entered on the command line */ do { printf("%s> ", shell); c=getchar(); while ((c != '\n') && (i < MAXINPUT-1)) { input[i]=c; i++; c=getchar(); } input[i]='\0'; } while (strlen(input)==0); /* copy the first "word" on the command line into the command var */ /* this also initializes the use of the strtok function */ strcpy(command, strtok(input, " ")); parameters[0]=malloc((MAXPARMLEN+1)*sizeof(char)); strcpy(parameters[0], command); i=1; /* separate all of the "words" on the command line. Each becomes */ /* a string pointed to by the parameters array using strtok. */ do { if (parm=strtok(NULL, " ")) { parameters[i]=malloc((MAXPARMLEN+1)*sizeof(char)); strcpy(parameters[i],parm); } else { parameters[i]=NULL; } i++; } while((parameters[i-1]!=NULL) && (i < MAXPARMS)); return; } /* Test of Command line parser: Enter a command line with parameters and see the line seperated in pure command and an array of pointers to all the "words" of the complete command line. */ int main(void) { char name[10]; char *argv[MAXPARMS]; int i; int PID, status; int BGRND = 0; read_command(name, argv); /*background process*/ if ((strcmp(name, "back")==0) & (strcmp(argv[1], "&")==0)) { BGRND = 1; if ( (PID = fork()) < 0 ) return 0; if(PID == 0 ) { if ((execvp(argv[1], argv)) == -1) perror("Fejl"); //printf("%s: Command not found.\n", tokenstr); return 0; } if(BGRND == 0) { while (waitpid(-1, &status, 0) != PID); } BGRND = 0; } return 0; }


Reply With Quote
