[<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
 Решения


ding.c:

 1           #include <stdio.h>
 2           #include <signal.h>
 3           #define   BEL   '\07'
 4
 5           int count;
 6
 7           main()
 8           {
 9           void sigcatch(int);
10
11           signal(SIGINT, sigcatch);
12           signal(SIGQUIT, sigcatch);
13
14           for (;;)
15           pause();
16           }
17
18           void sigcatch(int sig)
19           {
20           if (sig == SIGQUIT) {
21           printf("bell was rung %d times\n", count);
22           exit(1);
23           }
24           printf("%c\n", BEL);
25           count++;
26           signal(sig, sigcatch);
27           }
Мультиплексирование ввода
multiplex.c:

 1  #include <stdio.h>
 2  #include <fcntl.h>
 3  #include <signal.h>
 4  #include <errno.h>
 5
 6  #define TIME_OUT  5 /* seconds before switching to a different
 7  #define MAXOPEN     20   /*  maximum number of open files  */
 8  int nfiles;
 9  char *fname[MAXOPEN];
10  int fd[MAXOPEN];
11  int alrmflg; /* set by alrmtrp(), reset on return of read in r
12  extern int errno;
13
14  main(int argc, char **argv)
15  {
16       void rdfiles(void);
17
18       if (argc < 2) {
19            fprintf(stderr,"usage: %s filename [filename...]\n",
20                 argv[0]);
21            exit(1);
22       }
23
24       nfiles = 0;
25       while (--argc) {
26            fname[nfiles] = argv[nfiles+1]; /* +1 to skip comman
27            if ((fd[nfiles]=open(fname[nfiles],O_RDONLY))==-1) {
28                 fprintf(stderr, "%s: cannot open %s\n",
29                      argv[0], fname[nfiles]);
30                 exit(2);
31            }
32            ++nfiles;
33            if (nfiles >= 20) {
34                 fprintf(stderr,"%s:too many file names", argv[0
35                 exit(3);
36            }
37       }
38
39       rdfiles();
40  }
41
42  void rdfiles(void)
43  {
44       register int i;
45       int opfiles;
46       char line[256];
47       void alrmtrp();
48       void wrline();



49       int n;
50
51       signal(SIGALRM, alrmtrp);
52
53       opfiles = nfiles;   /*  number of open files */
54       while (opfiles) { /*  as long as there are open files  */
55            for (i = 0; i < nfiles; i++) {
56                 if (fd[i] == -1)   /*  this file was closed  */
57                      continue;
58                 alarm(TIME_OUT);
59                 line[0] = '\0';
60                 if ((n = read(fd[i],line,sizeof(line))) <= 0) {
61                      if (errno == EINTR && alrmflg) {
62                           /*  alrmflg set in alrmtrp routine */
63                           errno = alrmflg = 0;
64                           continue;  /*  try next line */
65                      } else {
66                           close (fd[i]);  /*  assume eof */
67                           fd[i] = -1;  /* don't read anymore */
68                           --opfiles;
69                      }
70                 }
71                 alarm(0); /*  reset alarm  */
72                 line[n] = '\0';
73                 wrline(fname[i], line);
74            }
75       }
76  }
77
78
79  void wrline(fnam, lp)
80  register char *fnam, *lp;
81  {
82       char buf[256];
83
84       sprintf(buf, "%s: %s", fnam, lp);
85       write(1, buf, strlen(buf)); /*print line and file name */
86  }
87
88
89  void alrmtrp(sig)
90  int sig;
91  {
92       signal(sig, alrmtrp);
93       alrmflg = 1;
94  }
Командный интерпретатор - продолжение

shell4.c:

 1     /* PRIMITIVE JOB CONTROL SHELL */
 2 #include <sys/types.h>
 3 #include <stdio.h>
 4 #include <signal.h>
 5 #include <fcntl.h>
 6 #include <setjmp.h>
 7 #include <wait.h>
 8 #include "shell.h"
 9 #include <sys/procset.h>
10 #include <siginfo.h>
11
12 char *infile, *outfile, *appfile;
13 struct command cmds[MAXCMDS];
14 char bkgrnd;
15 jmp_buf env;  /* NEW - SIGNAL HANDLING */
16 pid_t cpid;
17
18 main(int argc,  char **argv)
19 {
20     register int i;
21     char line[1024];    /*  allow large command lines  */
22     int ncmds;
23     char prompt[50];    /* shell prompt */
24     int ifd, ofd;    /* for i/o file redirection */
25     void sigint(int);  /*NEW - SIGNAL HANDLING & JOB CONTROL */
26     siginfo_t siginfo_ds; /*NEW - JOB CONTROL */
27
28     sigset(SIGINT, sigint);   /*NEW - SIGNAL HANDLING*/
29     sigset(SIGTSTP, sigint);  /*NEW - JOB CONT.*/
30     sprintf(prompt,"[%s] ", argv[0]);
31
32     setjmp(env);   /*NEW - SIGNAL HANDLING*/
33     while (promptline(prompt, line, sizeof(line)) > 0){ /* unti
34         if ((ncmds = parseline(line)) <= 0)
35             continue;    /* read next line */
36 #ifdef DEBUG
37 {
38     int i, j;
39     for (i = 0; i < ncmds; i++) {
40         for (j = 0; cmds[i].cmdargs[j] != (char *) NULL; j++)
41             fprintf(stderr, "cmd[%d].cmdargs[%d] = %s\n",
42                  i, j, cmds[i].cmdargs[j]);
43         fprintf(stderr,"cmds[%d].cmdflag = %o\n",i,
cmds[i].cmdflag);
44     }
45 }
46 #endif
47
48         for (i = 0; i < ncmds; i++) {
 49             if (!(cpid = fork())) {
 50                     if(setpgid(0,0) < 0) /*NEW - JOB CONTROL*/
 51                         perror("setpgid");
 52
 53                 if (bkgrnd) {    /* NEW - SIGNAL HANDLING */
 54                     signal(SIGINT, SIG_IGN);
 55                     signal(SIGQUIT, SIG_IGN);
 56                 }
 57                 else {
 58                     signal(SIGTTOU,SIG_IGN); /* NEW - JOB СONT
 59                     if(tcsetpgrp(0, getpid()) < 0)
 60                        perror("tcsetpgrp - child");
 61                     signal(SIGTTOU, SIG_DFL);
 62                 }
 63
 64                 /* input redirection */
 65                 if ((infile != (char *) NULL) && i == 0) {
 66                     /* check for i == 0 because only */
 67                     /* first command can have input */
 68                     /* redirected */
 69                     if ((ifd = open(infile, O_RDONLY)) < 0) {
 70                         fprintf(stderr,
 71                             "%s: %s: cannot open\n",
 72                              argv[0], infile);
 73                         exit(2);
 74                     }
 75                     close(0);    /*  close standard input  */
 76                     dup(ifd);    /* ifd is now stdin */
 77                     close(ifd);
 78                 }
 79
 80                 /* output redirection  */
 81                 if ((outfile != (char*)NULL) && i == ncmds-1){
 82                     /* create new file */
 83                     if ((ofd = open(outfile,
 84                      O_WRONLY|O_CREAT|O_TRUNC, 0666)) < 0) {
 85                         fprintf(stderr,
 86                             "%s: %s: cannot create\n",
 87                              argv[0], outfile);
 88                         exit(2);
 89                     }
 90                     close(1);
 91                     dup(ofd);
 92                     close(ofd);
 93                 } else if ((appfile != (char *) NULL) &&
 94                     i == ncmds-1) {
 95                     if ((ofd = open(appfile,
 96                      O_WRONLY|O_CREAT|O_APPEND, 0666)) < 0) {
 97                         fprintf(stderr,
 98                             "%s: %s: cannot open\n",
 99                              argv[0], appfile);
100                       exit(2);
101                   }
102                   close(1);
103                   dup(ofd);
104                   close(ofd);
105               }
106
107               execvp(cmds[i].cmdargs[0], cmds[i].cmdargs);
108               fprintf(stderr, "%s: not found\n",
109                   cmds[i].cmdargs[0]);
110               exit(1);    /* make sure child exits */
111           } /* close child block */
112           if (!bkgrnd) {
113
114             waitid(P_PID,cpid,&siginfo_ds,WEXITED | WSTOPPED);
115
116             signal(SIGTTOU, SIG_IGN);
117             if(tcsetpgrp(0, getpgrp()) < 0) /* Put parent in f
118                 perror("tcsetpgrp - parent");
119             signal(SIGTTOU, SIG_DFL);
120             if(siginfo_ds.si_code == CLD_STOPPED) { /* Restart
121                 fprintf(stderr, "%d", cpid);  /* child  which
122                 sigsend(P_PID, cpid, SIGCONT);/* now in backgr
123              }
124              putchar('\n');
125           }
126           else
127               fprintf(stderr, "%d\n", cpid);
128       }
129
130   }  /* close while */
131 }
132
133 void sigint(int sig) /* NEW - SIGNAL HANDLING & JOB CONTROL */
134 {
135     putchar('\n');
136     sigrelse(sig);
137     longjmp(env, 1);
138 }