[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 Решения
Создание двух процессов

twoprocs.c:

  1 #include <sys/types.h>
  2 #include <stdio.h>
  3 #include <unistd.h>
  4 #include <stdlib.h>
  5 #include <wait.h>
  6
  7 main()
  8 {
  9     pid_t pid, ret;
 10     int status;
 11
 12     if ((pid = fork()) == 0)        /* child */
 13         execl("/bin/cat", "cat", "/etc/passwd", (char *) 0);
 14
 15     printf("parent: waiting for child: %ld\n", pid);
 16
 17 /* by adding a wait(2) the parent is guaranteed to finish
 18    after its child
 19 */
 20     ret = wait(&status);
 21
 22     printf("parent: wait's return value: %ld,", ret);
 23     printf("child's status: %d\n", WEXITSTATUS(status));
 24
 25     exit(0);
 26 }
Код завершения команды

exitstat.c:

  1 #include <sys/types.h>
  2 #include <stdio.h>
  3 #include <unistd.h>
  4 #include <stdlib.h>
  5 #include <wait.h>
  6
  7 main(int argc, char *argv[ ])
  8 {
  9     int status;
 10
 11     if (fork() == 0) {
 12                 execvp(argv[1], &argv[1]);
 13         perror(argv[1]);
 14         exit(127);
 15     }
 16         wait(&status);
 17
 18     printf("exit status: %d\n", WEXITSTATUS(status));
 19         exit(0);
 20 }
Функция execvpe()

execvpe.c:

  1 #include <unistd.h>
  2 #include <stdlib.h>
  3 int execvpe(char *file, char *argv[], char *envp[]);
  4
  5 #if DEBUG
  6 #include <stdio.h>
  7 main()
  8 {
  9     static char *argv[ ] =
 10                 { "testpgm", "hello", "world", (char *) 0 };
 11     static char *nenv[ ] =
 12                 { "RUN=yes", "FILE=data", (char *) 0 };
 13
 14     execvpe(argv[0], argv, nenv);
 15         perror(argv[0]);
 16     exit(1);
 17 }
 18 #endif
 19
 20
 21 int execvpe(char *file, char *argv[], char *envp[])
 22 {
 23     extern char **environ;
 24
 25     environ = envp;
 26         execvp(file, argv);
 27     return(-1);
 28 }
Командный интерпретатор shell

Исполнение команды

shell1.c:

  1 #include <sys/types.h>
  2 #include <stdio.h>
  3 #include <unistd.h>
  4 #include <stdlib.h>
  5 #include <wait.h>
  6 #include "shell.h"
  7
  8 char *infile, *outfile, *appfile;
  9 struct command cmds[MAXCMDS];
 10 char bkgrnd;
 11
 12 main(int argc, char *argv[])
 13 {
 14     register int i;
 15         char line[1024];        /*  allow large command lines  */
 16     int ncmds;
 17         char prompt[50];        /* shell prompt */
 18     int cpid;
 19
 20     /* PLACE SIGNAL CODE HERE */
 21
 22     sprintf(prompt,"[%s] ", argv[0]);
 23
 24 while (promptline(prompt, line, sizeof(line)) > 0) {    /*until eof  */
 25                 if ((ncmds = parseline(line)) <= 0)
 26             continue;   /* read next line */
 27 #ifdef DEBUG
 28 {
 29     int i, j;
 30         for (i = 0; i < ncmds; i++) {
 31         for (j = 0; cmds[i].cmdargs[j] != (char *) NULL; j++)
 32             fprintf(stderr, "cmd[%d].cmdargs[%d] = %s\n",
 33              i, j, cmds[i].cmdargs[j]);
 34         fprintf(stderr, "cmds[%d].cmdflag = %o\n", i,cmds[i].cmdflag);
 35     }
 36 }
 37 #endif
 38
 39         for (i = 0; i < ncmds; i++) {
 40             if (!(cpid = fork())) {
 41                 execvp(cmds[i].cmdargs[0], cmds[i].cmdargs);
 42                 fprintf(stderr, "%s: not found\n",
 43                     cmds[i].cmdargs[0]);
 44                 exit(1);        /* make sure child exits */
 45             }
 46             while (cpid != wait((int *) 0))
 47                 ;       /*  wait for child */
 48         }
 49
 50     }  /* close while */
 51 }
 52
 53 /* PLACE SIGNAL CODE HERE */
Исполнение в фоновом режиме

shell2.c:

  1 #include <sys/types.h>
  2 #include <stdio.h>
  3 #include <unistd.h>
  4 #include <stdlib.h>
  5 #include <wait.h>
  6 #include "shell.h"
  7
  8 char *infile, *outfile, *appfile;
  9 struct command cmds[MAXCMDS];
 10 char bkgrnd;
 11
 12 main(int argc, char *argv[])
 13 {
 14     register int i;
 15         char line[1024];        /*  allow large command lines  */
 16     int ncmds;
 17         char prompt[50];        /* shell prompt */
 18     int cpid;
 19
 20     /* PLACE SIGNAL CODE HERE */
 21
 22     sprintf(prompt,"[%s] ", argv[0]);
 23
 24  while (promptline(prompt, line, sizeof(line)) > 0) {    /*until eof  */
 25                 if ((ncmds = parseline(line)) <= 0)
 26             continue;   /* read next line */
 27 #ifdef DEBUG
 28 {
 29     int i, j;
 30         for (i = 0; i < ncmds; i++) {
 31         for (j = 0; cmds[i].cmdargs[j] != (char *) NULL; j++)
 32             fprintf(stderr, "cmd[%d].cmdargs[%d] = %s\n",
 33              i, j, cmds[i].cmdargs[j]);
 34         fprintf(stderr, "cmds[%d].cmdflag = %o\n", i,cmds[i].cmdflag);
 35     }
 36 }
 37 #endif
 38
 39         for (i = 0; i < ncmds; i++) {
 40             if (!(cpid = fork())) {
 41                 execvp(cmds[i].cmdargs[0], cmds[i].cmdargs);
 42                 fprintf(stderr, "%s: not found\n",
 43                     cmds[i].cmdargs[0]);
 44                 exit(1);        /* make sure child exits */
 45             }
 46             if (!bkgrnd)
 47                 while (cpid != wait((int *) 0))
 48                     ;   /*  wait for child */
 49             else
 50                 fprintf(stderr, "%d\n", cpid);
 51         }
 52
 53     }  /* close while */
 54 }
 55
 56 /* PLACE SIGNAL CODE HERE */
Перенаправление ввода/вывода

shell3.c:

  1 #include <sys/types.h>
  2 #include <stdio.h>
  3 #include <unistd.h>
  4 #include <stdlib.h>
  5 #include <fcntl.h>
  6 #include <wait.h>
  7 #include "shell.h"
  8
  9 char *infile, *outfile, *appfile;
 10 struct command cmds[MAXCMDS];
 11 char bkgrnd;
 12
 13 main(int argc, char *argv[])
 14 {
 15     register int i;
 16         char line[1024];        /*  allow large command lines  */
 17     int ncmds;
 18         char prompt[50];        /* shell prompt */
 19     int cpid;
 20         int ifd, ofd;   /* for i/o file redirection */
 21
 22     /* PLACE SIGNAL CODE HERE */
 23
 24     sprintf(prompt,"[%s] ", argv[0]);
 25
 26 while (promptline(prompt, line, sizeof(line)) > 0) {    /*until eof  */
 27                 if ((ncmds = parseline(line)) <= 0)
 28             continue;   /* read next line */
 29 #ifdef DEBUG
 30 {
 31     int i, j;
 32         for (i = 0; i < ncmds; i++) {
 33         for (j = 0; cmds[i].cmdargs[j] != (char *) NULL; j++)
 34             fprintf(stderr, "cmd[%d].cmdargs[%d] = %s\n",
 35              i, j, cmds[i].cmdargs[j]);
 36         fprintf(stderr, "cmds[%d].cmdflag = %o\n", i,cmds[i].cmdflag);
 37     }
 38 }
 39 #endif
 40
 41         for (i = 0; i < ncmds; i++) {
 42             if (!(cpid = fork())) {
 43
 44                 /* input redirection */
 45                 if ((infile != (char *) NULL) && i == 0) {
 46                     /* check for i == 0 because only */
 47                     /* first command can have input */
 48                     /* redirected */
 49                     if ((ifd = open(infile, O_RDONLY)) < 0) {
 50                         fprintf(stderr,
 51                                 "%s: %s: cannot open\n",
 52                                 argv[0], infile);
 53                         exit(2);
 54                     }
 55                     close(0);   /*  close standard input  */
 56                     dup(ifd);   /* ifd is now stdin */
 57                     close(ifd);
 58                 }
 59
 60                 /* output redirection  */
 61                 if ((outfile != (char *) NULL) && i == ncmds-1) {
 62                     /* create new file */
 63                     if ((ofd = open(outfile,
 64                      O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {
 65                         fprintf(stderr,
 66                                 "%s: %s: cannot create\n",
 67                                 argv[0], outfile);
 68                         exit(2);
 69                     }
 70                     close(1);
 71                     dup(ofd);
 72                     close(ofd);
 73                 } else if ((appfile != (char *) NULL) &&
 74                     i == ncmds-1) {
 75                     if ((ofd = open(appfile,
 76                      O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0) {
 77                         fprintf(stderr,
 78                                 "%s: %s: cannot open\n",
 79                                 argv[0], appfile);
 80                         exit(2);
 81                     }
 82                     close(1);
 83                     dup(ofd);
 84                     close(ofd);
 85                 }
 86
 87                 execvp(cmds[i].cmdargs[0], cmds[i].cmdargs);
 88                 fprintf(stderr, "%s: not found\n",
 89                     cmds[i].cmdargs[0]);
 90                 exit(1);        /* make sure child exits */
 91             }
 92             if (!bkgrnd)
 93                 while (cpid != wait((int *) 0))
 94                     ;   /*  wait for child */
 95             else
 96                 fprintf(stderr, "%d\n", cpid);
 97         }
 98
 99     }  /* close while */
100 }
101
102 /* PLACE SIGNAL CODE HERE */
.