26
Name: Conor Flanagan Student id: 6424020 Course: Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3 rd March Portfolio 1

Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

Embed Size (px)

Citation preview

Page 1: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

Name: Conor Flanagan

Student id: 6424020

Course: Computer Science

Module: 207SE Operating Systems, Security and Networks

Submission data: 3rd March

Portfolio 1

Page 2: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

Lab Activity 1 – Operating Systems Tasks and Programming a) In one paragraph describe what you feel the future of operating systems will be. You might include diagrams to support your predictions and describe how the future operating systems will differ from the current one.

In the past, operating systems have been developed upon hardware available at the time of creation, needing to be rebuilt each time technology advanced (such as the development of integrated circuits over transistors), often causing prior data and systems to be unusable and obsolete (ComputerWorld 2003). The increase in significance and volume of data in today’s world means that backwards compatibility is crucial for any new systems in order to preserve current data. In the future, the deceleration of software and hardware developments combined with streamlining of hardware means that OSes can and will build upon previous implementations; each new OS being a derivation of previous systems (like Android being a derivation of Linux, which in itself is derived from UNIX), primarily changing the method of interaction a user has with the system, while the background systems remain largely similar (Medium 2016).

b) Programming activity (b1).

def grammarCheck():

agent = ('bot', 'mike')

action = ('pick','put','lift','drop','go')

direction = ('forward','backwards','left','right')

pronoun = ('I','you','we')

objects = ('nuts','plum','cat','cup')

colour = ('red','blue')

initial = input("Enter string to parse: ")

wordList = list()

for w in initial.split(' '): #Split string into words using spaces to identify words

wordList.append(w) #Insert word at start of the list

print(wordList) #Print individual words as a list

print(" ")

while True:

if wordList[0] not in agent and wordList[0] not in pronoun:

print("Grammar incorrect")

break #If sentence does not start with an agent or pronoun

else:

if wordList[1] not in action:

print("Grammar incorrect")

break #If agent not followed by action

else:

if wordList[2] not in objects and wordList[2] not in direction and

(wordList[0] in pronoun and wordList[2] in colour):

print("Grammar incorrect")

break

else:

if wordList[0] in agent and wordList[2] in colour:

if len(wordList) <= 3:

print("Grammar correct")

break

elif wordList[3] not in objects:

print("Grammar incorrect")

break

elif len(wordList) <= 4:

print("Grammar correct")

break

else:

print("Grammar incorrect")

elif len(wordList) <= 3:

print("Grammar correct")

break

else:

print("Grammar incorrect")

break

print(" ")

grammarCheck() #Run program again

if __name__ == "__main__":

grammarCheck()

Page 3: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

Code output using example sentences provided:

Page 4: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

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

a) Create a directory (Portfolio1) in home directory, make it read/write/executable for you and your group, executable for others. Show evidence with ls command.

b) Download the script at http://www.centerkey.com/tree/tree.sh to your home directory using wget and make it executable.

c) Making Directories

Create a 207se directory in your Portfolio1 directory.

Create numbered directories for the labs. i.e. lab1 and lab2 etc.

]0;flanag20@hvs-its-lnx01: ~flanag20@hvs-its-lnx01:~$ mkdir Portfolio1

]0;flanag20@hvs-its-lnx01: ~flanag20@hvs-its-lnx01:~$ chmod 711

Portfolio1

]0;flanag20@hvs-its-lnx01: ~flanag20@hvs-its-lnx01:~$ ls

lab2.txt [0m[01;34mPortfolio1[0m

]0;flanag20@hvs-its-lnx01: ~flanag20@hvs-its-lnx01:~$ wget

http://www.centerkey.com/tree/tree.sh

--2017-02-28 16:44:18-- http://www.centerkey.com/tree/tree.sh

Resolving www.centerkey.com (www.centerkey.com)... 66.185.31.194

Connecting to www.centerkey.com (www.centerkey.com)|66.185.31.194|:80...

connected.

HTTP request sent, awaiting response... 301 Moved Permanently

Location: http://centerkey.com/tree/tree.sh [following]

--2017-02-28 16:44:19-- http://centerkey.com/tree/tree.sh

Resolving centerkey.com (centerkey.com)... 66.185.31.194

Reusing existing connection to www.centerkey.com:80.

HTTP request sent, awaiting response... 200 OK

Length: 2107 (2.1K) [text/plain]

Saving to: ‘tree.sh’

tree.sh 0%[ ] 0 --.-KB/s tree.sh

100%[====================================================================

==============================================>] 2.06K --.-KB/s in

0s

2017-02-28 16:44:19 (178 MB/s) - ‘tree.sh’ saved [2107/2107]

]0;flanag20@hvs-its-lnx01: ~flanag20@hvs-its-lnx01:~$ chmod tree.sh –x

]0;flanag20@hvs-its-lnx01: ~flanag20@hvs-its-lnx01:~$ mkdir bin

]0;flanag20@hvs-its-lnx01: ~flanag20@hvs-its-lnx01:~$ cd bin

]0;flanag20@hvs-its-lnx01: ~/binflanag20@hvs-its-lnx01:~/bin$ mv

~/tree.sh ./

]0;flanag20@hvs-its-lnx01: ~/binflanag20@hvs-its-lnx01:~/bin$ chmod 755

tree.sh

]0;flanag20@hvs-its-lnx01: ~flanag20@hvs-its-lnx01:~$ cd

Portfolio1

]0;flanag20@hvs-its-lnx01: ~/Portfolio1flanag20@hvs-its-

lnx01:~/Portfolio1$ mkdir 207se

]0;flanag20@hvs-its-lnx01: ~/Portfolio1flanag20@hvs-its-

lnx01:~/Portfolio1$ cd 207se

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207seflanag20@hvs-its-

lnx01:~/Portfolio1/207se$ mkdir lab1

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207seflanag20@hvs-its-

lnx01:~/Portfolio1/207se$ mkdir lab2

Page 5: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

Evidence of transferring lab1 activity into appropriate directory

Evidence of make directory activities using tree.sh

d) Display todays date and using the cal command show the month that you were born.

e) Move into the lab1 directory and use the appropriate command to show the current directory

f) What is talk, write and wall are for

Wall – Write a message to all users.

Write – Send a message to another user.

Talk – Talk with another user, even across different terminals.

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

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207seflanag20@hvs-its-

lnx01:~/Portfolio1/207se$ cd lab1

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207se/lab1flanag20@hvs-

its-lnx01:~/Portfolio1/207se/lab1$ mv ~/Week1.docx ./

]0;flanag20@hvs-its-lnx01: ~flanag20@hvs-its-lnx01:~$ tree.sh

/home/207SE/flanag20

.

|-bin

|-Portfolio1

|---207se

|-----lab1

|-----lab2

]0;flanag20@hvs-its-lnx01: ~flanag20@hvs-its-lnx01:~$ date

Tue 28 Feb 16:46:12 GMT 2017

]0;flanag20@hvs-its-lnx01: ~flanag20@hvs-its-lnx01:~$ cal 07 1997

July 1997

Su Mo Tu We Th Fr Sa

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

]0;flanag20@hvs-its-lnx01: ~flanag20@hvs-its-lnx01:~$ cd

~/Portfolio1/207se/lab1

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207se/lab1flanag20@hvs-its-

lnx01:~/Portfolio1/207se/lab1$ pwd

/home/207SE/flanag20/Portfolio1/207se/lab1

Page 6: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

h) Created a file called song.txt and entered a randomly generated song from http://writerbot.com/lyrics

Using wc count the number of words and lines in the file.

Use grep to get the lines containing "and" and the number of the lines contain “and” in

the document.

Output:

Use cat to show the contents of the file.

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207se/lab2flanag20@hvs-its-

lnx01:~/Portfolio1/207se/lab2$ wc song.txt

30 242 1168 song.txt

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207se/lab2flanag20@hvs-its-

lnx01:~/Portfolio1/207se/lab2$ grep and song.txt -n

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207se/lab2flanag20@hvs-its-

lnx01:~/Portfolio1/207se/lab2$ cat song.tc[Kxt

New life through me let me, with all my

Soul no matter what they say the best doesn't mean

Giving me the way you used to touch me and

You'll succeed smile again just smile again just smile again

Just smile again I'll always be there for

You, gettin' it on the people

Looked behind me, givin' it all

For me need more and I can't even ask

For more 'cause boy, you you are come

As you are or where you're from come and

Dance to the side, so I wanna

Say thank you lord for showing me the way that

I played around with love before I

Never met no one loves you can

Bring the world tonight, said the hearts of

Stone, the only one for me

Every part of me so baby,

Baby the thought of no you cuts just

Like yesterday you turned my life is full of

Ecstasy where all you need and more and

More see I finally know and you dont have to

Makes lots of trust, and kissing and

Holding you tight letting you know I'll

Do what you need I found someone that's promise

To be necessary to me I just

Wanna be, you're so naughty and nice

Hey they think I'm in need of love if I

Was just a girl likes me never gonna

Settle for less but if you got me coin'

Craze boy, you lied you lied, you...

Page 7: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

Replace any two words with your first and second names, save as song_name.txt. Use an appropriate command to see if 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

Display Name and Student ID on screen.

Find out how you can display your username on the screen.

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207se/lab2flanag20@hvs-its-

lnx01:~/Portfolio1/207se/lab2$ differ song.txt song_name.txt

1c1

< New life through me let me, with all my

---

> New life through me let Conor, with all my

25c25

< To be necessary to me I just

---

> To be necessary to me Flanagan just

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207se/lab2flanag20@hvs-its-

lnx01:~/Portfolio1/207se/lab2$ sort song.txt > song2.txt

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207se/lab2flanag20@hvs-its-

lnx01:~/Portfolio1/207se/lab2$ sort song.txt | rev > song2.txt

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207se/lab2flanag20@hvs-its-

lnx01:~/Portfolio1/207se/lab2$ free -m

total used free shared buff/cache available

Mem: 32167 786 30683 62 696 30888

Swap: 1021 0 1021

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207se/lab2flanag20@hvs-its-

lnx01:~/Portfolio1/207se/lab2$ echo Conor Flanagan 6424020

Conor Flanagan 6424020

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207se/lab2flanag20@hvs-its-

lnx01:~/Portfolio1/207se/lab2$ echo "$USER"

flanag20

Page 8: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

List the processes that are running.

What are the differences between the Linux commands less, more and most.

More – Splits the output into individual screens which can be continued through using

the spacebar.

Less – Like ‘more’, however provides a way to scroll back through previous pages.

Most – Can display more than one file at a time.

]0;flanag20@hvs-its-lnx01: ~/Portfolio1/207se/lab2flanag20@hvs-its-

lnx01:~/Portfolio1/207se/lab2$ ps -a

PID TTY TIME CMD

10229 pts/82 00:00:00 less

17259 pts/70 00:00:00 pico

20896 pts/11 00:00:00 nano

20915 pts/11 00:00:00 nano

22711 pts/82 00:00:11 bochs-bin

30539 pts/3 00:00:00 bochs-bin

31939 pts/0 00:00:00 bash

31943 pts/0 00:00:01 bochs-bin

32152 pts/0 00:00:00 bash

32156 pts/0 00:00:00 bochs-bin

32367 pts/70 00:00:00 pico

34561 pts/49 00:00:00 man

34571 pts/49 00:00:00 man

34573 pts/49 00:00:00 pager

37770 pts/20 00:00:00 nano

38329 pts/8 00:00:00 pico

39710 pts/29 00:00:00 pico

39836 pts/67 00:00:00 pico

39842 pts/27 00:00:00 vi

40302 pts/16 00:00:00 script

40318 pts/36 00:00:00 ps

60508 pts/79 00:00:00 man

60520 pts/79 00:00:00 pager

Page 9: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

Lab Activity 4 Bootloader

a) Brief description of the Lab activity and what you did:

Booted pragma linux with bochs on the os-207se linux server. Created a bootloader using the

helloWorld.asm assembly code on moodle as a base. Bootloader (name.asm) displays name,

email address, favourite second year module, date of birth, age and student ID on separate

lines.

b) Boot pragma linux with bochs.

c) Make a bootloader that displays your name, email address, your favourite second year module, date of birth, age and student number on separate lines.

Source code sensibly commented for your bootloader. (next page)

]0;flanag20@hvs-its-lnx01: ~flanag20@hvs-its-lnx01:~$ tar xvfz pragma-

207.tgz

pragmalinux-img/

pragmalinux-img/README

pragmalinux-img/bootloader

pragmalinux-img/a.img

pragmalinux-img/bochsrc

pragmalinux-img/bochsrc1

pragmalinux-img/bochsout.txt

pragmalinux-img/TESTFORM.txt

pragmalinux-img/bootloader.asm

]0;flanag20@hvs-its-lnx01: ~flanag20@hvs-its-lnx01:~$ cd pragmalinux-img

]0;flanag20@hvs-its-lnx01: ~/pragmalinux-imgflanag20@hvs-its-

lnx01:~/pragmalinux-img$ ls

a.img bochsout.txt bochsrc bochsrc1 bootloader bootloader.asm README

TESTFORM.txt

Page 10: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

Output from Bochs showing student details.

[BITS 16] [ORG 0X7C00] top: mov ax,0x0000 mov ds,ax mov si,Name call writeString ;Use writeString to display Name mov si,Email call writeString ;Use writeString to display Email mov si,Module call writeString ;Use writeString to display Module mov si,Dob call writeString ;Use writeString to display Date of Birth mov si,Age call writeString ;Use writeString to display Age mov si,sID call writeString ;Use writeString to display Student ID jmp $ writeString: mov ah,0x0E ;Display Character mov bh,0x00 mov bl,0x07 nextchar: lodsb cmp al,0 jz done int 0x10 jmp nextchar done: ret Name db 'Conor Flanagan',13,10,0 ;Declaring values Email db '[email protected]',13,10,0 Module db '210CT',13,10,0 Dob db '18/07/1997',13,10,0 Age db '19',13,10,0 sID db '6424020',13,10,0 times 510-($-$$) db 0 dw 0xAA55

Page 11: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

Lab Activity 6 Memory Management

a) Determine. using first-fit, best-fit and worst-fit which unallocated memory blocks the process memory requirements will be allocated to, and prove using the .exe files provided.

1. Processes:

Process 1 (300)

Process 2 (350)

Process 3 (450)

Process 4 (400)

Process 5 (150)

Memory:

Block 1 (300)

Block 2 (500)

Block 3 (250)

Block 4 (220)

Block 5 (270)

First-fit method: Process 1 (300) allocated to Block 1 (300). Process 2 (350) allocated to Block 2 (500). Process 3 (450) cannot be allocated. Process 4 (400) cannot be allocated. Process 5 (150) allocated to Block 3 (250). First-fit.exe output:

Results match my own determinations using the first-fit method.

Page 12: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

2. Best-fit method: Process 1 (300): - Block 1 (300) difference: 0 - Block 2 (500) difference: 200 - Block 3 (250) does not fit. - Block 4 (220) does not fit. - Block 5 (270) does not fit. Process 1 allocated to Block 1. Process 2 (350): - Block 2 (500) difference: 150 - Block 3 (250) does not fit. - Block 4 (220) does not fit. - Block 5 (270) does not fit. Process 2 allocated to Block 2. Process 3 (450): - Block 3 (250) does not fit. - Block 4 (220) does not fit. - Block 5 (270) does not fit. Process 3 cannot be allocated. Process 4 (400): - Block 3 (250) does not fit. - Block 4 (220) does not fit. - Block 5 (270) does not fit. Process 4 cannot be allocated. Process 5 (150): - Block 3 (250) difference: 100 - Block 4 (220) difference: 70 - Block 5 (270) difference: 120 Process 5 allocated to Block 4. Best-fit.exe output:

Results match my own determinations using the best-fit method.

Page 13: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

3. Worst-fit method: Process 1 (300): - Block 1 (300) difference: 0 - Block 2 (500) difference: 200 - Block 3 (250) does not fit. - Block 4 (220) does not fit. - Block 5 (270) does not fit. Process 1 allocated to Block 2. Process 2 (350): - Block 1 (300) does not fit. - Block 3 (250) does not fit. - Block 4 (220) does not fit. - Block 5 (270) does not fit. Process 2 cannot be allocated. Process 3 (450): - Block 1 (300) does not fit. - Block 3 (250) does not fit. - Block 4 (220) does not fit. - Block 5 (270) does not fit. Process 3 cannot be allocated. Process 4 (400): - Block 1 (300) does not fit. - Block 3 (250) does not fit. - Block 4 (220) does not fit. - Block 5 (270) does not fit. Process 4 cannot be allocated. Process 5 (150): - Block 1 (300) difference: 150 - Block 3 (250) difference: 100 - Block 4 (220) difference: 70 - Block 5 (270) difference: 120 Process 5 allocated to Block 1. Worst-fit.exe output:

Results match my own determinations using the Worst-fit method.

Page 14: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

First-fit and Best-fit allocate the processes with the least fragmentation, both leaving only 2 processes unallocated.

b) Use first-in-first-out and random page allocation methods to show how pages will be swapped in and out of main memory for the examples.

1. First-in-first-out: Paging Accessing Sequence: 42775639322 Three table page entries:

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 * * * * * * * *

Page Fault Total: 8 Fifo.exe returns the same result:

Four table page entries:

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 * * * * * * * *

Page Fault Total: 8 Fifo.exe returns the same result:

Page 15: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

2. Random page allocation: Three table page entries:

4 2 7 7 5 6 3 9 3 2 2

Page Entry 0 4 4 4 4 4 4 4 4 3 3 3

Page Entry 1 2 2 2 5 5 3 9 9 9 9

Page Entry 2 7 7 7 6 6 6 6 2 2

Page Fault * * * * * * * * *

Page Fault Total: 9 Random.exe returns a different result because the victim pages are selected randomly, which can have a positive or negative impact.

Four table page entries:

4 2 7 7 5 6 3 9 3 2 2

Page Entry 0 4 4 4 4 4 4 3 3 3 3 3

Page Entry 1 2 2 2 2 2 2 2 2 2 2

Page Entry 2 7 7 7 6 6 6 6 6 6

Page Entry 3 5 5 5 9 9 9 9

Page Fault * * * * * * *

Page Fault Total: 7 Random.exe returns a different result, again because the victim pages are randomly selected.

Page 16: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

Lab Activity 7 Buffer

a) Brief description of the Buffer Activity. Task is to first comment the buffer C code provided on moodle, then modify it, first to display error messages, then to display total characters read, characters read from the buffer, words in the document and times the buffer was filled. Finally a function added to compare the file with another, and if they are different display how.

b) Commented Buffer.c code.

#include <fcntl.h>

#include <stdlib.h>

#include <unistd.h>

#include <stdio.h>

#define BUF_SIZE 500 //Define buffer size at 500

#define OUTPUT_MODE 0700 //Define file permission

int main(int argc, char *argv[])

{

int in_fd, out_fd;

int rd_size = 1, wr_size;

char buf[BUF_SIZE]; //Declare buffer

if (argc != 3)

exit(1);

in_fd = open(argv[1], O_RDONLY); //Open input file

if (in_fd < 0)

exit(2);

out_fd = creat(argv[2],OUTPUT_MODE); //Create output file

if (out_fd < 0)

exit(3);

while (rd_size > 0) {

rd_size = read(in_fd, buf,BUF_SIZE); //Read from input file to buffer

if (rd_size <0)

exit(4);

wr_size = write(out_fd, buf, rd_size); //Write from buffer to output

if (wr_size<=0){

close(in_fd);

close(out_fd); //Close input and output

exit(5);

}

}

}

Page 17: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

c) 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.

Contents of hamlet.txt:

#include <fcntl.h>

#include <stdlib.h>

#include <unistd.h>

#include <stdio.h>

#define BUF_SIZE 500 //Define buffer size at 500

#define OUTPUT_MODE 0700 //Define file permission

int main(int argc, char *argv[])

{

int in_fd, out_fd;

int rd_size = 1, wr_size;

char buf[BUF_SIZE]; //Declare buffer

if (argc != 3){

printf("Too many or too few arguments.\n");

exit(1);

}

in_fd = open(argv[1], O_RDONLY); //Open input file

if (in_fd < 0) {

printf("Given file is empty.\n");

exit(2);

}

out_fd = creat(argv[2], OUTPUT_MODE); //Create output file

if (out_fd < 0) {

printf("File not created.\n");

exit(3);

}

while (rd_size > 0) {

rd_size = read(in_fd, buf, BUF_SIZE); //Read from input file to buffer

if (rd_size <0) {

printf("Nothing to read from file.\n");

exit(4);

}

wr_size = write(out_fd, buf, rd_size); //Write from buffer to output

if (wr_size<=0){

close(in_fd);

close(out_fd); //Close input and output

exit(5);

}

}

}

Page 18: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

d) 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

#include <fcntl.h>

#include <stdlib.h>

#include <unistd.h>

#include <stdio.h>

#define BUF_SIZE 500 //Define buffer size at 500

#define OUTPUT_MODE 0700 //Define file permission

int main(int argc, char *argv[])

{

int in_fd, out_fd;

int rd_size = 1, wr_size;

int characters = 0, filled = 0;

int wordcount = 1, loop;

char buf[BUF_SIZE]; //Declare buffer

if (argc != 3){

printf("Too many or too few arguments.\n");

exit(1);

}

in_fd = open(argv[1], O_RDONLY); //Open input file

if (in_fd < 0) {

printf("Given file is empty.\n");

exit(2);

}

out_fd = creat(argv[2], OUTPUT_MODE); //Create output file

if (out_fd < 0) {

printf("File not created.\n");

exit(3);

}

while (rd_size > 0) {

rd_size = read(in_fd, buf, BUF_SIZE); //Read from input file to buffer

if (rd_size <0) {

printf("Nothing to read from file.\n");

exit(4);

}

if (rd_size==500) { //If rd_size is 500

characters = characters + 500; //Add 500 to char count

filled = filled + 1; //Add 1 filled buffer

}

else{

characters = characters + rd_size; //Else add rd_size to char count

}

for (loop = 0; loop < rd_size; loop++){

if (buf[loop] == ' '){ //If char is space add to word count

wordcount = wordcount + 1;

}

else if (buf[loop] == '.'){ //If char is space add to word count

wordcount = wordcount + 1;

loop = loop + 1; //Next char likely ' ' so skip

}

}

wr_size = write(out_fd, buf, rd_size); //Write from buffer to output

if (wr_size<=0){

close(in_fd);

close(out_fd); //Close input and output

printf("Character count: %d\n", characters);

printf("Word count: %d\n", wordcount);

printf("Buffers filled: %d\n", filled);

exit(5);

}

}

}

Page 19: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

Output of updated code:

e) Alter buffer size and explain how this influences the number of times the buffer is filled.

When the buffer size is increased, the buffer is filled less often. This means there is less of an

overhead when filling the buffer and so the operation takes less time.

f) Adapt the code in buffer.c so it is possible to compare two files and determine if they are the

same. If they are different explain how they differ.

#include <fcntl.h>

#include <stdlib.h>

#include <unistd.h>

#include <stdio.h>

#define BUF_SIZE 500 //Define buffer size at 500

#define OUTPUT_MODE 0700 //Define file permission

int compare(char *File) //Compare with another file

{

int in0fd, in1fd;

char compareFile[50];

int rd_size0 = 1, rd_size1 = 2;

char buf0[BUF_SIZE], buf1[BUF_SIZE];

int loop, bool=0;

char q;

printf("File to compare? "); //Ask for second filename

scanf("%s", compareFile);

in0fd = open(compareFile, O_RDONLY);

in1fd = open(File, O_RDONLY);

if (in0fd<0) {

printf("Given file is empty.\n"); //Exit if file is empty

exit(6);

}

while (rd_size0 > 0) {

rd_size0 = read(in0fd, buf0, BUF_SIZE); //Read each file

rd_size1 = read(in1fd, buf1, BUF_SIZE); //into each buffer

if (rd_size0 <0) {

printf("Nothing to read from file.\n");

exit(4);

}

if (rd_size1 <0) {

printf("Nothing to read from file.\n");

exit(4);

}

for (loop = 0; loop < rd_size0; loop++) {

q = buf1[loop];

if (buf0[loop] != q) { //Compares characters in each buffer

if (bool == 0)

printf("File differs with characters: \n");

printf("%c", q); //Prints characters that differ

Page 20: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

bool = 1;

}

}

}

printf("\n");

if (bool == 0) //Declare if files are identical

printf("Files are the same.\n");

}

int main(int argc, char *argv[])

{

int in_fd, out_fd;

int rd_size = 1, wr_size;

int characters = 0, filled = 0;

int wordcount = 1, loop;

char buf[BUF_SIZE]; //Declare buffer

char cmpr[1];

if (argc != 3){

printf("Too many or too few arguments.\n");

exit(1);

}

in_fd = open(argv[1], O_RDONLY); //Open input file

if (in_fd < 0) {

printf("Given file is empty.\n");

exit(2);

}

out_fd = creat(argv[2], OUTPUT_MODE); //Create output file

if (out_fd < 0) {

printf("File not created.\n");

exit(3);

}

while (rd_size > 0) {

rd_size = read(in_fd, buf, BUF_SIZE); //Read from input file to buffer

if (rd_size <0) {

printf("Nothing to read from file.\n");

exit(4);

}

if (rd_size==BUF_SIZE) { //If rd_size is 500

characters = characters + BUF_SIZE; //Add 500 to char count

filled = filled + 1; //Add 1 filled buffer

}

else{

characters = characters + rd_size; //Else add rd_size to char count

}

for (loop = 0; loop < rd_size; loop++){

if (buf[loop] == ' '){ //If char is space add to word count

wordcount = wordcount + 1;

}

else if (buf[loop] == '.'){ //If char is space add to word count

wordcount = wordcount + 1;

loop = loop + 1; //Next char likely ' ' so skip

}

}

wr_size = write(out_fd, buf, rd_size); //Write from buffer to output

if (wr_size<=0){

close(in_fd);

close(out_fd); //Close input and output

printf("Character count: %d\n", characters);

printf("Word count: %d\n", wordcount);

printf("Buffers filled: %d\n", filled);

printf("Compare file with another [yes/no]? ");

scanf("%s", cmpr);

if (cmpr[0]=='y'){

compare(argv[2]);

}

exit(5);

}

}

}

Page 21: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

Comparison of review.txt and hamlet.txt

Comparison of hamlet.txt and review_observer.txt

Page 22: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

Lab Activity 8 Cache Buffer

a) Brief Description of Cache Buffer Activity. Alter the cache-buffer file provided so that it outputs the bytes to the screen, then alter further to prove that the output is being buffered, and display statistics including byte count and buffer count.

b) Commented implementation of the cr_read_byte function.

Output (some file text cropped to the side for visibility):

Explanation: Checks if the usedbuffer pointer is past the end of the buffer length. If so, calls the refill function to refill the buffer and reset the usedbuffer pointer. If not, increases the pointer by 1 and returns the byte the pointer was at. Char variable is used so that the pointer can be increased before the return statement without returning the incorrect (the next) byte.

char cr_read_byte(cr_file* f){

char curByte; //Character for current byte

if (f->usedbuffer >= f->bufferlength){

refill(f); //If buffer is empty refill

}

else{

curByte = f->buffer[f->usedbuffer]; //Assigns current byte to

character

f->usedbuffer += 1; //Increases buffer pointer

by 1.

return curByte; //Returns current byte character

return EOF;

}

}

Page 23: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

c) Additional tasks 1 and 2: Prove the file output is being buffered, and provide statistics for bytes read in total and times buffer was refilled. Declaration line added to cache_reader.h library:

cache_reader.c with changes highlighted:

int totalBytes; //Stores total amount of bytes from file

#include "cache_reader.h"

int refill(cr_file* buff){

//Refills a buffer

//Only works when completely used buffer

if(buff->usedbuffer!=buff->bufferlength)

return 0;

else{

buff->usedbuffer=0;

int len=fread(buff->buffer, sizeof(char), buff->bufferlength, buff->file);

//If we didn't fill the buffer, fill up with EOF

if(len<buff->bufferlength)

for(int i=len;i<buff->bufferlength;i++)

buff->buffer[i]=EOF; //Accessing like an array!

buff->totalBytes += len; //Add len to total byte count

return len;

}

}

void cr_close(cr_file* f){

free(f->buffer);

fclose(f->file);

}

cr_file* cr_open(char * filename, int buffersize){

//Info on malloc

//http://www.space.unibe.ch/comp_doc/c_manual/C/FUNCTIONS/malloc.html

FILE* f;

if ((f = fopen(filename, "r")) == NULL){

fprintf(stderr, "Cannot open %s\n", filename);

return 0;

}

cr_file* a=(cr_file*)malloc(sizeof(cr_file));

a->file=f;

a->bufferlength=buffersize;

a->usedbuffer=buffersize; //Start off with no characters, so refill will work

as expected

a->buffer=(char*)malloc(sizeof(char)*buffersize);

a->totalBytes=0; //Start byte total at 0

refill(a);

return a;

}

//------------------------------------------------------------------

char cr_read_byte(cr_file* f){

char curByte; //Character for current byte

if (f->usedbuffer >= f->bufferlength){

printf("\n"); //Separates each buffer refill to individual lines

refill(f); //If buffer is empty refill

}

else{

curByte = f->buffer[f->usedbuffer]; //Assigns current byte to character

f->usedbuffer += 1; //Increases buffer pointer by 1.

return curByte; //Returns current byte character

return EOF;

}

}

Page 24: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

cache_example.c with changes highlighted:

Output:

#include "cache_reader.h"

//Simple file display to show how easy it is to use the cached reader functions

int main(){

char c;

//Open a file

cr_file* f = cr_open("text",20);

//While there are useful bytes coming from it

while((c=cr_read_byte(f))!=EOF)

//Print them

printf("%c",c);

//Then close the file

printf("\nTotal Bytes: %d",f->totalBytes); //Prints total byte count.

printf("\nTotal Refills: %d",f->totalBytes/f->bufferlength); //Prints

total number of refills performed.

printf(“\n”);

cr_close(f);

//And finish

return 0;

}

Page 25: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

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

a) Brief description of the activity

Change cache_reader.h library from using fopen, fread and fclose functions to use the system

call versions (open, read, close). Also remove effects of caching on the library.

b) Changes the cache_reader library from using the fopen, fread, fclose functions to the system

call versions open, read, close.

No changes were made to cache_reader.h

Output is the same as running previously. Screenshot on next page.

#include "cache_reader.h"

#include <fcntl.h>

#include <unistd.h>

#include <sys/types.h>

#include <sys/stat.h>

int refill(cr_file* buff){

//Refills a buffer

//Only works when completely used buffer

if(buff->usedbuffer!=buff->bufferlength)

return 0;

else{

buff->usedbuffer=0;

int len=read(buff->file, buff->buffer, buff->bufferlength);

//If we didn't fill the buffer, fill up with EOF

if(len<buff->bufferlength)

for(int i=len;i<buff->bufferlength;i++)

buff->buffer[i]=EOF; //Accessing like an array!

buff->totalBytes += len; //Add len to total byte count

return len;

}

}

void cr_close(cr_file* f){

free(f->buffer);

close(f->file);

}

cr_file* cr_open(char * filename, int buffersize){

FILE* f;

if ((f = open(filename, O_RDONLY)) < 0){ //Changed == NULL to < 0

fprintf(stderr, "Cannot open %s\n", filename);

return 0;

}

cr_file* a=(cr_file*)malloc(sizeof(cr_file));

a->file=f;

a->bufferlength=buffersize;

a->usedbuffer=buffersize; //Start off with no characters, so refill

will work as expected

a->buffer=(char*)malloc(sizeof(char)*buffersize);

a->totalBytes=0; //Start byte total at 0

refill(a);

return a;

}

Page 26: Name: Conor Flanagan Student id: 6424020 Course: … Computer Science Module: 207SE Operating Systems, Security and Networks Submission data: 3rd March Portfolio 1 . Lab Activity 1

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

library.

References

D. Gelernter, ComputerWorld (2003) Future of operating systems: simplicity [online] available from

<http://www.computerworld.com/article/2580106/operating-systems/future-of-operating-systems--simplicity.html>

R. Rosini, Medium (2016) AI and the Future of Operating Systems [online] available from

<https://medium.com/@rodolfor/the-future-of-operating-systems-and-weaveos-e4c9bc3f23d7#.ntbsqvoqb>