#include #include #include #include #include #include #include #include #include #define U1 12 /* Gets 1 process */ #define U2 13 /* Gets 3 processes */ #define U3 14 /* Gets 6 processes */ #define U4 15 /* Gets a low priority process */ long count; FILE *outfile; void kill(int pid, int signum); void sig_handler(int signum) { /* If we get this signal, we need to write count to file and exit */ fprintf(outfile, "%d\n%d\n ",getpid(),count); fflush(outfile); exit(count); } int main(int argc, char *argv[]) { int status; int i, j, k, cpid; int pids[11]; int pid_counts[11]; double percent; long total; printf("Welcome to the test program for the Fair-Share Scheduler\n"); printf("Forking and running children...\n"); signal(SIGQUIT, sig_handler); outfile = fopen("stats", "w"); if (outfile == NULL) { printf("Failed to open stats file!\n"); return 1; } cpid = fork(); if (cpid == 0){ /* Parent */ /* Set to be U1 */ status = setuid(U1); if(status) printf("Failed to set UID to %d\n",U1); /* Now start an infinite loop */ printf("PID %d starting under user %d\n",getpid(), U1); count = 0; while(1){ for(i=0;i<1000000;i++); count++; } } else { /* PID of first child */ pids[0] = cpid; /* Child */ for (i = 0; i < 3; i++) { cpid = fork(); if (cpid == 0) { status = setuid(U2); if(status) printf("Failed to set UID to %d\n",U2); printf("PID %d starting under user %d\n",getpid(),U2); count = 0; while(1){ for(i=0;i<1000000;i++); count++; } } else { pids[i+1] = cpid; } } for (i = 0; i < 6; i++) { cpid = fork(); if (cpid == 0) { status = setuid(U3); if(status) printf("Failed to set UID to %d\n",U3); printf("PID %d starting under user %d\n",getpid(), U3); count = 0; while(1){ for(i=0;i<1000000;i++); count++; } } else { pids[i+4] = cpid; } } } /* Fork off a low priority process */ cpid = fork(); if (cpid == 0) { status = setuid(U4); printf("PID %d starting low priority under user %d\n",getpid(), U4); setpriority(PRIO_PROCESS, getpid(), 20); count = 0; while(1){ for(i=0;i<1000000;i++); count++; } } else { pids[10] = cpid; sleep(60); printf("Reaping children!\n"); /* Do a loop to kill all processes as close together as possible */ for(i=0;i<11;i++){ /* printf("Signalling PID %d\n",pids[i]); */ kill(pids[i], SIGQUIT); } /* And another loop to synchronize */ for(i=0;i<11;i++){ wait(&pids[i]); } printf("They're all dead now here's some stats about their lives:\n"); fclose(outfile); /* Re-open the file for reading now */ fopen("stats", "r"); /* Process the data nicely (sort of!) */ total = 0; for(i=0;i<11;i++){ status = fscanf(outfile, "%d\n",&pids[i]); status = fscanf(outfile, "%d\n",&pid_counts[i]); /* printf("Read: %d %d from file\n",pids[i], pid_counts[i]);*/ total += pid_counts[i]; } printf("Total count: %d\n",total); printf("Results should be: 1x33%%, 3x11%%, 6x5.5%%, 1x1%%\n"); for(i=0;i<11;i++){ percent = (((double)pid_counts[i]/total)*100); printf("PID %d: %d = %f%% share\n",pids[i],pid_counts[i], percent); } } return 0; }