liyujie
2025-08-28 b3810562527858a3b3d98ffa6e9c9c5b0f4a9a8e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 *   Copyright (c) International Business Machines  Corp., 2001
 *
 *   This program is free software;  you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
 *   the GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program;  if not, write to the Free Software
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */
/* FUNCTIONS: Scheduler Test Suite */
 
/*---------------------------------------------------------------------+
|                               sched_tc1                              |
| ==================================================================== |
|                                                                      |
| Description:  Creates short-term disk I/O bound process              |
|                                                                      |
| Algorithm:    o  Set process priority                                |
|               o  Open a large file                                   |
|               o  Continuously read from file until interrupted       |
|               o  Close file upon receiving interrupt                 |
|                                                                      |
| To compile:   cc -o sched_tc1 sched_tc1.c -L. -lpsc                  |
|                                                                      |
| Usage:        sched_tc1 [-p priority] [-v] [-d]                      |
|                                                                      |
| Last update:   Ver. 1.4, 4/10/94 23:05:00                           |
|                                                                      |
| Change Activity                                                      |
|                                                                      |
|   Version  Date    Name  Reason                                      |
|    0.1     050689  CTU   Initial version                             |
|    0.2     010402  Manoj Iyer Ported to Linux                   |
|                                                                      |
+---------------------------------------------------------------------*/
 
#include   <stdlib.h>
#include   <signal.h>
#include <sys/time.h>
#include <sys/resource.h>
#include   "sched.h"
 
/*
 * Defines:
 *
 * USAGE: usage statement
 *
 * DEFAULT_PRIORITY_TYPE: default priority
 *
 */
#define DEFAULT_PRIORITY_TYPE    "variable"
#define USAGE "Usage:  %s  [-p priority] [-v] [-d]               \n" \
              "        -p priority priority (default variable)          \n" \
              "        -v          verbose                              \n" \
              "        -d          enable debugging messages            \n"
 
/*
 * Function prototypes:
 *
 * process_file: reads data file
 *
 * parse_args: parse command line arguments
 */
void process_file(char *);
void parse_args(int, char **);
void signal_handler();
 
/*
 * Global variables:
 *
 * verbose: enable normal messages
 *
 * debug: enable debugging messages
 *
 * signaled: set upon receiving SIGUSER1 signal
 *
 * priority: process type (fixed priority, variable priority)
 */
int verbose = 0;
int debug = 0;
int signaled = 0;
char *priority = DEFAULT_PRIORITY_TYPE;
 
/*---------------------------------------------------------------------+
|                                 main                                 |
| ==================================================================== |
|                                                                      |
| Function:  ...                                                       |
|                                                                      |
+---------------------------------------------------------------------*/
int main(int argc, char **argv)
{
   char *filename = NULL;
 
   if ((filename = getenv("KERNEL")) == NULL) {
       errno = ENODATA;
       sys_error("environment variable KERNEL not set", __FILE__,
             __LINE__);
   }
   /*
    * Setup signal handler & setup alarm so we do not loop forever...
    */
   signal(SIGUSR1, signal_handler);
   signal(SIGALRM, signal_handler);
   alarm(600);        /* wait 10 minutes before aborting */
 
   /*
    * Process command line arguments...
    */
   parse_args(argc, argv);
   if (verbose)
       printf("%s: Scheduler TestSuite program\n\n", *argv);
   if (debug) {
       printf("\tpriority:       %s\n", priority);
   }
 
   /*
    * Adjust the priority of this process if the real time flag is set
    */
   if (!strcmp(priority, "fixed")) {
#ifndef __linux__
       if (setpri(0, DEFAULT_PRIORITY) < 0)
           sys_error("setpri failed", __FILE__, __LINE__);
#else
       if (setpriority(PRIO_PROCESS, 0, 0) < 0)
           sys_error("setpri failed", __FILE__, __LINE__);
#endif
   }
 
   /*
    * Continuously read through file until interrupted...
    */
   while (!signaled)
       process_file(filename);
 
   /*
    * Exit with success!
    */
   if (verbose)
       printf("\nsuccessful!\n");
   return (0);
}
 
/*---------------------------------------------------------------------+
|                            process_file ()                           |
| ==================================================================== |
|                                                                      |
| Function:  Opens a file, reads from it until it encounters an        |
|            end-of-file and then closes the file..                    |
|                                                                      |
+---------------------------------------------------------------------*/
void process_file(char *filename)
{
   char record[100];    /* holds each record of the file read */
   FILE *datafile;        /* file pointer to the open file */
 
   /*
    * Try and open the datafile
    */
   if ((datafile = fopen(filename, "r")) == NULL)
       sys_error("fopen failed", __FILE__, __LINE__);
 
   /*
    * Read the first record of the datafile, then read until end-of-file
    */
   while (fgets(record, 80, datafile)) {
       if (feof(datafile))
           break;
   }
 
   /*
    * Close the datafile
    */
   if (fclose(datafile))
       sys_error("fclose failed", __FILE__, __LINE__);
}
 
/*---------------------------------------------------------------------+
|                           signal_handler ()                          |
| ==================================================================== |
|                                                                      |
| Function:  ...                                                       |
|                                                                      |
+---------------------------------------------------------------------*/
void signal_handler(int signal)
{
   printf("signal received is %d\n", signal);
   if (signal == SIGUSR1) {
       signaled++;
       if (debug)
           printf("\n\t<< caught SIGUSR1 interrupt>>\n");
   } else if (signal == SIGALRM) {
       error("Failed to receive SIGUSR1 signal before timeout!",
             __FILE__, __LINE__);
   } else
       error("received unexpected signal", __FILE__, __LINE__);
}
 
/*---------------------------------------------------------------------+
|                             parse_args ()                            |
| ==================================================================== |
|                                                                      |
| Function:  Parse the command line arguments & initialize global      |
|            variables.                                                |
|                                                                      |
| Updates:   (command line options)                                    |
|                                                                      |
|            [-p] priority: "fixed" or "variable"                      |
|            [-t] n:        execution time in hours                    |
|            [-v]           verbose                                    |
|            [-d]           enable debugging messages                  |
|                                                                      |
+---------------------------------------------------------------------*/
void parse_args(int argc, char **argv)
{
   int opt;
   int pflg = 0;
   int errflag = 0;
   char *program_name = *argv;
   extern char *optarg;    /* Command line option */
 
   /*
    * Parse command line options.
    */
   if (argc < 2) {
       fprintf(stderr, USAGE, program_name);
       exit(0);
   }
 
   while ((opt = getopt(argc, argv, "p:t:vd")) != EOF) {
       switch (opt) {
       case 'p':    /* process type */
           pflg++;
           priority = optarg;
           break;
       case 'v':    /* verbose */
           verbose++;
           break;
       case 'd':    /* enable debugging messages */
           verbose++;
           debug++;
           break;
       default:
           errflag++;
           break;
       }
   }
 
   /*
    * Check percentage, execution time and process slots...
    */
   if (pflg) {
       if (strcmp(priority, "fixed") && strcmp(priority, "variable"))
           errflag++;
   }
   if (errflag) {
       fprintf(stderr, USAGE, program_name);
       exit(2);
   }
}