Operating Systems Tasks and Programming Lab

 

Lab Activity 1 – Operating Systems Tasks and Programming

a) Future of operating systems.

[Report on the future of operating systems here]

b) Programming activity

C++

  1. #include <iostream>
  2. using namespace std;
  3. int main() {
  4.   int id = 6669447;
  5. string name = “Salman Mohammed Fazal”;
  6. int counter = 0;
  7. for (int i = 0; i < name.length(); i++) { //for every character
  8. cout << name[i] << flush; //print characters on the same line
  9. counter++; //increment counter
  10. if (counter == id[-1]) { //if counter = last character of ID
  11. cout << endl; //jump to next line
  12. counter = 0; //reset counter
  13. }
  14. }
  15. }

JAVA

  1. public class test {
  2. public static void main(String[] args) {
  3. int id = 6669447;
  4. String name = “Salman Mohammed Fazal”;
  5. int counter = 0;
  6. for (int i = 0; i < name.length(); i++) { //for every character
  7. System.out.print(name.charAt(i)); //print character
  8. counter++; //increment counter
  9. if (counter == id[-1]) { //when counter is last character of ID
  10. System.out.println(); //new line
  11. counter = 0; //reset counter
  12. }
  13. }
  14. }
  15. }

PYTHON

  1. name = “Salman Mohammed Fazal”
  2. id = “6669447”
  3. for i in range (0, len(name), int(id[-1])): #for every n characters
  4. print(name[i:i+ int(id[-1]))]) #slice and print string

Code Outcome (same for all)

 

Lab Activity 2 – Linux Command Line (Commands and outcomes from a series of small tasks that require use of a number of Linux commands)

  1. How made Portfolio1 directory read/write/executable only for you and your group. That is, not for others.  Show evidence of this with ls command.

  1. How downloaded the script http://www.centerkey.com/tree/tree.sh to your home directory using wget and make it executable.

  1. Making Directories
  • How created a 207se directory in your Portfolio1 directory.
  • How created numbered directories for the labs. i.e. lab1 and lab2 etc.
  • Evidence of transferring lab1 activity into appropriate directory
  • Evidence of make directory activities using tree.sh

  1. Display todays date and using the cal command show the month that you were born.
  1. Move into the lab1 directory and use the appropriate command to show the current directory

  1. What is talk, write and wall are for

The Talk command is used for exchanging messages with other users who are logged on the same network.

The Write command is used to send messages to users in the same network. This one-way only.

The Wall command is used to send messages to all the users in the entire network. This is limited to the admins only.

  1.  What command prevents the effects of those three commands from interrupting you.

To prevent users from interrupting you, you can use the Mesg command. This command basically enables or disables users from writing to your terminal. ‘Mesg y’ to Allow access and ‘Mesg n’ to deny access.

  1. The song in song.txt.
  • Using wc the number of words and lines in the file.
  • Using grep to get the lines containing “and” and the number of the lines contain “and” in the document
  • Use cat to show the contents of the file.
  • Appropriate Linux command to see if the two files differ and how they differ.
  • Use sort to sort the file and redirect the output to a new file called song2.txt
  • Use sort and rev to reverse the sorted contents of song.txt and append the output to song2.txt
  • Total memory used and the total memory available
  • Find out how you can display your username on the screen.
  • List the processes that are running.
  • What are the differences between the Linux commands less, more and most.

The ‘more’ command works similarly to the cat command by displaying contents of a file, however the more command, you view bits of the text (a screenful), and is in a forward-scrollable manner.

The ‘less’ command is similar to the more command, however with this it is possible to scroll both, forward and backward.

The ‘most’ command is more like the less command, but the only difference is, it is possible to view several files at once with this command.

The basic syntax for these commands are:

  • more <filename>
  • less <filename>

 

Lab Activity 4 Bootloader

  1. Brief description of the Lab activity and what you did

This weeks task was to create a bootloader using Assembly which included my student details and a triangle of dots. We then have to boot the bootloader with bochs.

  1. Boot pragma linux with bochs

C:UsersToshibaAppDataLocalMicrosoftWindowsINetCacheContent.Word2017-02-25-214318_1776x952_scrot.pngC:UsersToshibaAppDataLocalMicrosoftWindowsINetCacheContent.Word2017-02-25-214457_1776x952_scrot.pngC:UsersToshibaAppDataLocalMicrosoftWindowsINetCacheContent.Word2017-02-25-214636_1776x952_scrot.png

C:UsersToshibaAppDataLocalMicrosoftWindowsINetCacheContent.Word2017-02-25-220440_1776x952_scrot.png

  1. Make a bootloader that displays your student details and triangle
  • Commented bootloader code to display your student details and triangle

[BITS 16]

[ORG 0x7C00]

top:

;; Put 0 into ds (data segment)

;; Can’t do it directly

mov ax,0x0000

mov ds,ax

;; si is the location relative to the data segment of the

;; string/char to display

mov si, msg

call writeString ; See below

jmp $ ; Spin

writeString:

mov ah,0x0E ; Display a chacter (as before)

Read also  Difference Between Memory And Data Storage Computer Science Essay

mov bh,0x00

mov bl,0x07

nextchar:

Lodsb ; Loads [SI] into AL and increases SI by one

;; Effectively “pumps” the string through AL

cmp al,0 ; End of the string?

jz done

int 0x10 ; BIOS interrupt

jmp nextchar

done:

ret

msg db ‘Name: Salman Fazal’,13,10, ‘Email: ‘, 13,10, ‘Fav Module: 207SE ;)’, 13,10, ‘DOB: 01/08/1996 – 21’ ,13,10, ‘Std ID: 6669447’ ; Null-terminated

times 510-($-$$) db 0

dw 0xAA55

  • Output from Bochs showing student details and triangle

C:UsersToshibaAppDataLocalMicrosoftWindowsINetCacheContent.Word2017-02-25-230436_1776x952_scrot.png

 

Lab Activity 6 Memory Management

  1. Memory Allocation Activities

Due to the code outputs being too long, I have just snipped a portion of the result, however all of my answers match with the results in the code.

* NULL = Memory address not allocated.

First-Fit

 

Best-Fit

 

Worst-Fit

 

  1. Paging Activities

FIFO

4

2

7

7

5

6

3

9

3

2

2

Page Entry 0

4

4

4

4

5

5

5

9

9

9

9

Page Entry 1

2

2

2

2

6

6

6

6

2

2

Page Entry 2

7

7

7

7

3

3

3

3

3

Page Fault

F

F

F

H

F

F

F

F

H

F

H

Page Fault Total: 8

4

2

7

7

5

6

3

9

3

2

2

Page Entry 0

4

4

4

4

4

6

6

6

6

6

6

Page Entry 1

2

2

2

2

2

3

3

3

3

3

Page Entry 2

7

7

7

7

7

9

9

9

9

Page Entry 3

5

5

5

5

5

2

2

Page Fault

F

F

F

H

F

F

F

F

H

F

H

Page Fault Total: 8

RANDOM

4

2

7

7

5

6

3

9

3

2

2

Page Entry 0

4

4

4

4

5

6

6

6

6

2

2

Page Entry 1

2

2

2

2

2

2

9

9

9

9

Page Entry 2

7

7

7

7

3

3

3

3

3

Page Fault

F

F

F

H

F

F

F

F

H

F

H

Page Faults Total: 8

4

2

7

7

5

6

3

9

3

2

2

Page Entry 0

4

4

4

4

4

4

4

9

9

9

9

Page Entry 1

2

2

2

2

2

2

2

2

2

2

Page Entry 2

7

7

7

7

3

3

3

3

3

Page Entry 3

5

6

6

6

6

6

6

Page Fault

F

F

F

H

F

F

F

F

H

H

H

Page Fault Total: 7

The random algorithm did not give me the same result as the way I solved it. This is because There is no specific rule/method on what memory block to be replaced. Each time a process needs to be moved to a memory block, a random memory block is chosen in order to swap the process.

Evidence of running code

 

Lab Activity 7 Buffer

  1. Brief description of the Buffer Activity

This weeks task involved using buffers in terms of reading and writing from a file

  1. Commented Buffer.c code
  1. #include <fcntl.h> //library for file control options
  2. #include <stdlib.h> //library for general purpose tools
  3. #include <unistd.h> //header file
  4. #include <stdio.h> //file IO
  5. #define BUF_SIZE 500 //sets buffer size to 500
  6. #define OUTPUT_MODE 0700 //defines the output mode, sets file permissions
  7. int main(int argc, char *argv[]) {
  8. //Define variables
  9. int in_fd, out_fd; //hold associated numbers on both files
  10. int rd_size = 1; //hold amount of bytes in buffer (final should be 500)
  11. int wr_size; //hold amount of bytes on the output file
  12. char buf[BUF_SIZE]; //initialise buffer and its size
  13. if (argc != 3) //check for correct number of arguments
  14. exit(1); //exit if too many or too few parameters
  15. in_fd = open(argv[1], O_RDONLY); //open file to read from (read only)
  16. //if file isn’t found, variable will have a negative number
  17. if (in_fd < 0)
  18. exit(2); //exit if file’s empty
  19. out_fd = creat(argv[2], OUTPUT_MODE); //create the output file
  20. //if file isn’t created, variable will have a negative number
  21. if (out_fd < 0)
  22. exit(3); //exit if cannot write to the file
  23. while (rd_size > 0) { //while characters (in file) still exist:
  24. rd_size = read(in_fd, buf, BUF_SIZE); //read the file into buffer
  25. if (rd_size < 0)
  26. exit(4); //exit if error while reading
  27. wr_size = write(out_fd, buf, rd_size); //write from buffer into file
  28. if (wr_size <= 0) {
  29. close(in_fd);
  30. close(out_fd); //close both of the files if error found
  31. exit(5); //exit if error while writing
  32. }
  33. }
  34. }
  1. Update the code to so that it prints if an error has occurred or if a file is successfully created with the content of the review in it.

After running code what is in hamlet.txt

  1. #include <fcntl.h> //library for file control options
  2. #include <stdlib.h> //library for general purpose tools
  3. #include <unistd.h> //header file
  4. #include <stdio.h> //file IO
  5. #define BUF_SIZE 500 //sets buffer size to 500
  6. #define OUTPUT_MODE 0700 //defines the output mode, sets file permissions
  7. int main(int argc, char *argv[]) {
  8. //Define variables
  9. int in_fd, out_fd; //hold associated numbers on both files
  10. int rd_size = 1; //hold amount of bytes in buffer (final should be 500)
  11. int wr_size; //hold amount of bytes on the output file
  12. char buf[BUF_SIZE]; //initialise buffer and its size
  13. if (argc != 3) { //check for correct number of arguments
  14. printf(“Error: Invalid number of parameters passed.”);
  15. exit(1); //exit if too many or too few parameters
  16. }
  17. in_fd = open(argv[1], O_RDONLY); //open file to read from (read only)
  18. //if file isn’t found, variable will have a negative number
  19. if (in_fd < 0) {
  20. printf(“Error: File not found.”);
  21. exit(2); //exit if file’s empty
  22. }
  23. out_fd = creat(argv[2], OUTPUT_MODE); //create the output file
  24. //if file isn’t created, variable will have a negative number
  25. if (out_fd < 0) {
  26. printf(“Error: File not created.”);
  27. exit(3); //exit if cannot write to the file
  28. }
  29. while (rd_size > 0) { //while characters (in file) still exist:
  30. rd_size = read(in_fd, buf, BUF_SIZE); //read the file into buffer
  31. if (rd_size < 0) {
  32. printf(“Error: Can’t read from file.”);
  33. exit(4); //exit if error while reading
  34. }
  35. wr_size = write(out_fd, buf, rd_size); //write from buffer into file
  36. if (wr_size <= 0) {
  37. close(in_fd);
  38. close(out_fd); //close both of the files if error found
  39. exit(5); //exit if error while writing
  40. }
  41. }
  42. printf(“Operation Successful!”);
  43. }
Read also  Simulation Of Inmarsat C Channels Using Matlab Computer Science Essay

After compiling and running the code, the hamlet.txt file contained the exact same text that was in the main review.txt file

Evidence:

  1. Updated buffer.c code to show how many character are read to buffer, how many character read at a time into the buffer, how many words in the document and how many times the buffer is filled
  1. #include <fcntl.h> //library for file control options
  2. #include <stdlib.h> //library for general purpose tools
  3. #include <unistd.h> //header file
  4. #include <stdio.h> //file IO
  5. #define BUF_SIZE 500 //sets buffer size to 500
  6. #define OUTPUT_MODE 0700 //defines the output mode, sets file permissions
  7. int main(int argc, char *argv[]) {
  8. //Define variables
  9. int in_fd, out_fd; //hold associated numbers on both files
  10. int rd_size = 1; //hold amount of bytes in buffer (final should be 500)
  11. int wr_size; //hold amount of bytes on the output file
  12. char buf[BUF_SIZE]; //initialise buffer and its size
  13. int rd_count = 0, buf_count = 0, wd_count = 0;
  14. if (argc != 3) { //check for correct number of arguments
  15. printf(“Error: Invalid number of parameters passed.”);
  16. exit(1); //exit if too many or too few parameters
  17. }
  18. in_fd = open(argv[1], O_RDONLY); //open file to read from (read only)
  19. //if file isn’t found, variable will have a negative number
  20. if (in_fd < 0) {
  21. printf(“Error: File not found.”);
  22. exit(2); //exit if file’s empty
  23. }
  24. out_fd = creat(argv[2], OUTPUT_MODE); //create the output file
  25. //if file isn’t created, variable will have a negative number
  26. if (out_fd < 0) {
  27. printf(“Error: File not created.”);
  28. exit(3); //exit if cannot write to the file
  29. }
  30. while (rd_size > 0) { //while characters (in file) still exist:
  31. rd_size = read(in_fd, buf, BUF_SIZE); //read the file into buffer
  32. if (rd_size < 0) {
  33. printf(“Error: Can’t read from file.”);
  34. exit(4); //exit if error while reading
  35. }
  36. if (rd_size == 500){ //if read,
  37. rd_count += 500;
  38. buf_count += 1; //increment character and buffer counter
  39. } else{
  40. rd_count += rd_size;
  41. }
  42. for (int i=0; i<rd_size; i++){
  43. if(buf[i] == ‘ ‘)
  44. wd_count+=1; //increment if space found
  45. }
  46. wr_size = write(out_fd, buf, rd_size); //write from buffer into file
  47. if (wr_size <= 0) {
  48. printf(“Successfully written to file!n”);
  49. close(in_fd);
  50. close(out_fd); //close both files
  51. printf(“Total number of characters read is: %d.n”, rd_count);
  52. printf(“Total number of words: %d.n”, wd_count);
  53. printf(“Buffer filled %d times.n”, buf_count);
  54. exit(5);
  55. }
  56. }
  57. }

  1. Impact of changing buffer size

I altered the buffer 3 times, the first was changing the size to 1000, the second was 1300 and the third was 2000. What I noticed was as the buffer size increases, the amount of times the buffer is filled decreases as the buffer is able to fill in more characters each time. Below is a screen shot of when the buffer size was set to 2000:

We can see the buffer at this time was filled 0 times, meaning the entire text was placed into the buffer.

  1. Updated buffer.c code to compare if two files are the same
  1. #include <fcntl.h> //library for file control options
  2. #include <stdlib.h> //library for general purpose tools
  3. #include <unistd.h> //header file
  4. #include <stdio.h> //file IO
  5. #define BUF_SIZE 500 //sets buffer size to 500
  6. #define OUTPUT_MODE 0700 //defines the output mode, sets file permissions
  7. int main(int argc, char *argv[]) {
  8. //Define variables
  9. int in_fd, in_fd2; //hold associated numbers on both files
  10. int rd_size = 1; //hold amount of bytes in buffer (final should be 500)
  11. int rd_size2 = 1;
  12. char buf[BUF_SIZE]; //initialise buffer and its size
  13. char buf2[BUF_SIZE]; //initialise buffer and its size
  14. //int rd_count = 0, buf_count = 0, wd_count = 0;
  15. if (argc != 3) { //check for correct number of arguments
  16. printf(“Error: Invalid number of parameters passed.”);
  17. exit(1); //exit if too many or too few parameters
  18. }
  19. in_fd = open(argv[1], O_RDONLY); //open first file to read from
  20. if (in_fd < 0) {
  21. printf(“Error: File not found.”);
  22. exit(2); //exit if file’s empty
  23. }
  24. in_fd2 = open(argv[2], O_RDONLY); //open second file to read from
  25. //if file isn’t found, variable will have a negative number
  26. if (in_fd2 < 0) {
  27. printf(“Error: File not found.”);
  28. exit(3); //exit if file’s empty
  29. }
  30. while (rd_size > 0 && rd_size2 > 0) { //while characters (in file) still exist:
  31. rd_size = read(in_fd, buf, BUF_SIZE); //read file 1 to buffer
  32. rd_size2 = read(in_fd2, buf2, BUF_SIZE); //read file 2 to buffer
  33. if (rd_size < 0 || rd_size2 < 0) { //error if any file is empty
  34. printf(“Error: Can’t read from file.”);
  35. exit(4);
  36. }
  37. for (int i=0; i<BUF_SIZE; i++){ //loop through buffer
  38. if(buf[i] != buf2[i]){ //if characters of the 2 files are not the same:
  39. printf(“The files are not the same!”);
  40. close(in_fd);
  41. close(in_fd2);
  42. exit(5); //print message and close
  43. }
  44. }
  45. }
  46. printf(“YESSSSSS!! The files are the same.”); //if code passes the loop, they’re the same!
  47. }
  • Comparison of review.txt and hamlet.txt
  • Comparison of hamlet.txt and review_observer.txt

 

Lab Activity 8 Cache Buffer

  1. Brief Description of Cache Buffer Activity
Read also  Simple Mail Transfer Protocol Computer Science Essay

This weeks task was to alter the cache_reader.c file and complete the cr_read_byte function. Additionally, we also had to add a count of the total number of bytes and the number of times the buffer was filled.

  1. Commented implementation of the cr_read_byte function
  1. char cr_read_byte(cr_file* f){
  2. /* 1. check if buffer needs refilling
  3. 2. return currently pointed character
  4. 3. move pointer to next character */
  5. if(f->usedbuffer >= f->bufferlength){
  6. refill(f);
  7. }
  8. char currentChar = f->buffer[f->usedbuffer];
  9. f->usedbuffer++;
  10. return currentChar;
  11. }

  1. Comment updated code to show that each byte is being read, and when the buffer is being refilled.

To show that the buffer is being refilled, I added a print statement each time the buffer refills in the cr_read_byte() function. Also the next part of the question will show that the buffer is being refilled and the bytes are being read by keeping a count each time that happens.

*The code for this part of the question is combined with the next part (cache_reader.c).

(I set the buffer to 200)

  1. Commented updated code showing to show how many bytes were read in total, and how many times the buffer was refilled

In this part, I had to update the code in 3 different files, below are the screenshots of every update I made:

cache_reader.h

In this section, I initialized two variables, one to keep count of the number of times the buffer was refilled and the second to keep count of the total number of bytes.

cache_reader.c

For the second section, I incremented the buffer (filled) count each time the buffer was refilled.

cache_example.c

Lastly, each time a byte is printed to the screen, a byte count is incremented. And at the end, before closing the files, I printed both the counts.

Cache_reader.h

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //The internals of this struct aren’t important
  4. //from the user’s point of view
  5. typedef struct {
  6. FILE* file; //File being read
  7. int bufferlength; //Fixed buffer length
  8. int usedbuffer; //Current point in the buffer
  9. char* buffer; //A pointer to a piece of memory
  10. // same length as “bufferlength”
  11. int buffer_count; //buffer filled count
  12. int byte_count; //number of bytes count
  13. } cr_file;
  14. //Open a file with a given size of buffer to cache with
  15. cr_file* cr_open(char* filename, int buffersize);
  16. //Close an open file
  17. void cr_close(cr_file* f);
  18. //Read a byte. Will return EOF if empty.
  19. char cr_read_byte(cr_file* f);
  20. //———————————————————
  21. //Refill an empty buffer. Not intended for users
  22. int refill(cr_file* buff);

cache_reader.c

  1. #include “cache_reader.h”
  2. //http://www.phim.unibe.ch/comp_doc/c_manual/C/SYNTAX/struct.html
  3. //http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/structs.html
  4. int refill(cr_file* buff) {
  5. //Refills a buffer
  6. //Only works when completely used buffer
  7. if (buff->usedbuffer != buff->bufferlength)
  8. return 0;
  9. else {
  10. buff->usedbuffer = 0;
  11. int len = fread(buff->buffer, sizeof (char), buff->bufferlength, buff->file);
  12. //If we didn’t fill the buffer, fill up with EOF
  13. if (len < buff->bufferlength)
  14. for (int i = len; i < buff->bufferlength; i++)
  15. buff->buffer[i] = EOF; //Accessing like an array!
  16. return len;
  17. }
  18. }
  19. void cr_close(cr_file* f) {
  20. free(f->buffer);
  21. fclose(f->file);
  22. }
  23. cr_file* cr_open(char * filename, int buffersize) {
  24. FILE* f;
  25. if ((f = fopen(filename, “r”)) == NULL) {
  26. fprintf(stderr, “Cannot open %sn”, filename);
  27. return 0;
  28. }
  29. cr_file* a = (cr_file*) malloc(sizeof (cr_file));
  30. a->file = f;
  31. a->bufferlength = buffersize;
  32. a->usedbuffer = buffersize; //Start off with no characters, so refill will work as expected
  33. a->buffer = (char*) malloc(sizeof (char)*buffersize);
  34. refill(a);
  35. return a;
  36. }
  37. //——————————————————————
  38. char cr_read_byte(cr_file* f) {
  39. /* 1. check if buffer needs refilling
  40. -if refilled, increase buffer count.
  41. 2. return currently pointed character
  42. 3. move pointer to next character */
  43. if (f->usedbuffer >= f->bufferlength) {
  44. refill(f);
  45. f->buffer_count++;
  46. printf(“nnBuffer Refilled..nn”);
  47. }
  48. char currentChar = f->buffer[f->usedbuffer];
  49. f->usedbuffer++;
  50. return currentChar;
  51. }

Cache_example.c

  1. #include “cache_reader.h”
  2. #import “cache_reader.h”
  3. //Simple file display to show how easy it is to use the cached reader functions
  4. int main() {
  5. char c;
  6. //Open a file
  7. cr_file* f = cr_open(“text”, 200);
  8. //While there are useful bytes coming from it
  9. while ((c = cr_read_byte(f)) != EOF) {
  10. //Print them
  11. f->byte_count++; //increase count for every char
  12. printf(“%c”, c);
  13. }
  14. //Then close the file
  15. printf(“nnTotal number of bytes: %d”, f->byte_count);
  16. printf(“Buffer filled %d times.”, f->buffer_count);
  17. cr_close(f);
  18. //And finish
  19. return 0;
  20. }

 

Lab 10: The Cache Buffer from week 8 with system calls

  1. Brief description of the activity

This weeks task involved changing the library to use system calls (open(), close(), read()) instead of fopen, fread and fclose.

  1. Changes the cache_reader library from using the fopen, fread, fclose functions to the system call versions open, read, close

The changes I made to the code are:

Firstly, I added the includes to all the codes.

cache_reader.c

Changed the commented code to the second one

 

Cache_reader.h

RUNNING CODE

  1. Changes cache_reader library to remove (as far as possible) the effects of caching on the library.

[Commented code outlining your changes to the .h and .c files here]

[Output from running code here and if possible prove not using cache here]

References

Order Now

Order Now

Type of Paper
Subject
Deadline
Number of Pages
(275 words)