Advertisement
Guest User

Untitled

a guest
May 11th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.41 KB | None | 0 0
  1. /*
  2.  *   Student: Varun Jain
  3.  *   ID: 260268676
  4.  *   Course: COMP 310
  5.  *   Assignment 2 - KERNEL
  6.  *
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13. #define RAM_SIZE 1000
  14. char *RAM[RAM_SIZE];
  15.  
  16. int RAM_MAX;
  17.  
  18. struct RAM_STRUCTURE
  19. {
  20.     int freeSpaceSize;
  21.     int zeroPageSize;
  22. } ramFormat;
  23.  
  24. struct PCB
  25. {
  26.     char filename[50];  // stores the filename of the script loaded into memory
  27.     int start;      // start address – where it is loaded into RAM
  28.     int length;         // the number of instructions
  29.     int current;        // current instruction being executed
  30.     char *heap[20];     // implemented as a list of Shell Memory variables and values
  31.     struct PCB *next;   // next node in the linked-list
  32. } *readyHead, *readyTail, *running, *terminatingHead, *terminatingTail;
  33.  
  34. struct CPU
  35. {
  36.     int quanta;
  37.     int returnCode;
  38.     int pc; // program counter
  39.     char *memory[20]; // to simulate the CPU registers by copying PCB heap (cell by cell)
  40. };
  41.  
  42. int myKernel (int argc, char *argv[])
  43. {
  44.     // Step 1: check for kernel specific switches
  45.     int i;
  46.    
  47.     // Used at a later point
  48.     RAM_MAX = -1;
  49.    
  50.     // Check through entire string
  51.     for(i = 0; i<argc; i++)
  52.     {
  53.         // If switch -f or -F is present
  54.         if((strncmp(argv[i], "-f", 2) == 0)||(strncmp(argv[i], "-F", 2) == 0))
  55.         {
  56.             // Initialize new string variables
  57.             char newstring[strlen(argv[i])];
  58.             char *result;
  59.            
  60.             // Copy input into the new string variable
  61.             strcpy(newstring, argv[i]);
  62.  
  63.             // Tokenize the string, seperated by colons
  64.             result = strtok(newstring, ':');
  65.            
  66.             // Token position
  67.             int token = 1;         
  68.            
  69.             // While not at third token
  70.             while(token != 3)
  71.             {
  72.                 // If at the second token
  73.                 if(token == 2)
  74.                 {
  75.                     // If the input was less then/equal 1000 and greater then 0,
  76.                     if(result <= RAM_SIZE && result > 0)
  77.                     {
  78.                         RAM_MAX = result;
  79.                     }
  80.                 }
  81.                 // Grab next value
  82.                 result = strtok( NULL,  ':' );
  83.                 token++;
  84.             }
  85.         }
  86.     }
  87.  
  88.     // Step 2: format RAM (since it was already initialized by boot-strap to nulls)
  89.  
  90.     ramFormat.zeroPageSize = 10;
  91.     if(RAM_MAX =! -1)
  92.     {
  93.         ramFormat.freeSpaceSize = RAM_MAX;
  94.     }
  95.     else
  96.     {
  97.         ramFormat.freeSpaceSize = RAM_SIZE-10;
  98.     }
  99.    
  100.     /* Step 3: initialize run-time environment
  101.     readyHead = NULL;
  102.     readyTail = readyhead;
  103.     running = NULL;
  104.     terminatingHead = NULL;
  105.     terminatingTail = terminatingHead;
  106.    
  107.     // Create struct CPU as cpu
  108.     struct CPU cpu;
  109.     CPU->quanta = 0;
  110.     CPU->returnCode = 0;
  111.     CPU->pc = 0;
  112.     cpu.quanta = 0;
  113.     cpu.returnCode = 0;
  114.     cpu.pc = 0;
  115.    
  116.     // Set memory to null
  117.     for(i=0;i<20;i++)
  118.     {
  119.         cpu.memory[i] = NULL;
  120.     }
  121.     */
  122.     // Step 4: start run-time environment with login screen and later shell
  123.  
  124.     while (!login());
  125.    
  126.     /*while (returnCode != -1)
  127.     {
  128.         returnCode = shell(); // returns -1 when user inputs EXIT or LOGOUT
  129.         // OS Overhead – Process execution
  130.         Running = NextReadyQueue();
  131.         TaskSwitchIn(Running);
  132.         SetQuanta(int);
  133.         RunForQuanta();
  134.         TaskSwitchOut(Running); // task switch back
  135.         // OS Overhead – Services
  136.         TerminateProcesses();
  137.     }*/
  138. }
  139.  
  140. int login(void)
  141. {
  142.     char username[25];
  143.     char password[25];
  144.    
  145.     // Get login credentials   
  146.     printf("=~=~=~=~=~=~==~=~=~=~=~=~==~=~=\n");
  147.     printf("=~=~=~=~=~=  LOGIN  =~=~=~=~=~=\n");
  148.     printf("=~=~=~=~=~=~==~=~=~=~=~=~==~=~=\n\n");
  149.     printf("Username:\n");
  150.     scanf("%s", username);
  151.     printf("Password:\n");
  152.     scanf("%s", password);
  153.    
  154.     // Read CSV
  155.     FILE *input;
  156.     input = fopen("pass.csv", "r+");
  157.    
  158.     char f_content[51];
  159.     char *f_token;
  160.    
  161.     if (input == NULL)
  162.     {
  163.         fclose(input);
  164.         printf("ERROR OPENING PASSWORD FILE");
  165.         return -1;
  166.     }
  167.    
  168.     while(!feof(input))
  169.     {
  170.         getline(&f_content, 51, input);
  171.         f_token = strtok(f_content, ",");
  172.         if(strcmp(f_token,username)==0)
  173.         {
  174.             f_token = strtok(NULL, ",");
  175.             if(strcmp(f_token,password)==0)
  176.             {
  177.                 return 1;
  178.             }
  179.         }  
  180.     }
  181.     return 0;
  182. }
  183.  
  184. int main(void)
  185. {
  186.     char *a[] = {"Hello World", "-f:123", "UP"};
  187.     myKernel(3, a );
  188.    
  189.     printf("%d\n", login());
  190.     printf("ABOVE IS THE RESULT OF LOGIN");
  191.    
  192.     /*
  193.     struct PCB *P1 = NULL;
  194.     P1 = (struct PCB*) malloc(sizeof(struct PCB));
  195.    
  196.     struct PCB *P2 = NULL;
  197.     P2 = (struct PCB*) malloc(sizeof(struct PCB));
  198.    
  199.     struct PCB *P3 = NULL;
  200.     P3 = (struct PCB*) malloc(sizeof(struct PCB));
  201.    
  202.     sscanf("FILE_1", "%s", P1->filename);
  203.     sscanf("FILE_2", "%s", P2->filename);
  204.     sscanf("FILE_3", "%s", P3->filename);
  205.    
  206.     PushReadyQueue(P1);
  207.     PushReadyQueue(P2);
  208.     PushReadyQueue(P3);
  209.    
  210.     printf("Head is: %s\n", readyHead->filename);
  211.     printf("Middle is: %s\n", readyHead->next->filename);
  212.     printf("Tail is: %s\n\n", readyTail->filename);
  213.    
  214.     NextReadyQueue();
  215.    
  216.     printf("RUNNING: %s\n", running->filename);
  217.    
  218.    
  219.     PushTerminateQueue(P2);
  220.    
  221.     printf("Head is: %s\n", readyHead->filename);
  222.     printf("Middle is: %s\n", readyHead->next->filename);
  223.     printf("Tail is: %s\n\n", readyTail->filename);
  224.    
  225.     PushTerminateQueue(P1);
  226.    
  227.     printf("Head is: %s\n", readyHead->filename);
  228.     printf("Middle is: %s\n", readyHead->next->filename);
  229.     printf("Tail is: %s\n\n", readyTail->filename);
  230.    
  231.     printf("RUNNING: %s\n", running->filename);
  232.    
  233.     printf("Head of DELETE is: %s\n", terminatingHead->filename);
  234.     printf("Middle of DELETE is: %s\n", terminatingHead->next->filename);
  235.     printf("Tail of DELETE is: %s\n\n", terminatingTail->filename);
  236.    
  237.     PushTerminateQueue(P3);
  238.    
  239.     printf("Head is: ");
  240.     printf("%d",(readyHead == NULL));
  241.     printf("\nMiddle is: ");
  242.    
  243.     printf("\nTail is: ");
  244.     printf("%d", (readyTail == NULL));
  245.    
  246.     printf("\nRUNNING: ");
  247.     printf("%d", (running == NULL));
  248.    
  249.     printf("\nHead of DELETE is: %s\n", terminatingHead->filename);
  250.     printf("Middle of DELETE is: %s\n", terminatingHead->next->filename);
  251.     printf("Tail of DELETE is: %s\n", terminatingTail->filename);
  252.    
  253.    
  254.     /*
  255.     NextReadyQueue();
  256.     NextReadyQueue();
  257.    
  258.     printf("Current is now: %d AFTER two\n", running->start);
  259.    
  260.     printf("Head is: %d\n", readyHead->start);
  261.     printf("Middle is: %d\n", readyHead->next->start);
  262.     printf("Tail is: %d\n\n", readyTail->start);
  263.    
  264.     NextReadyQueue();
  265.    
  266.     printf("Current is now: %d AFTER three\n", running->start);
  267.    
  268.     printf("Head is: %d\n", readyHead->start);
  269.     printf("Middle is: %d\n", readyHead->next->start);
  270.     printf("Tail is: %d\n\n", readyTail->start);
  271.    
  272.     NextReadyQueue();
  273.    
  274.     printf("Current is now: %d AFTER four\n", running->start);
  275.    
  276.     struct PCB *Running = running;
  277.     printf("RUNNING is now: %d AFTER four\n", Running->start);
  278.     printf("THE NEXT ONE IS: %d", Running->next->start);
  279.     */
  280.    
  281.    
  282.     return 0;
  283.    
  284. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement