Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This assignment is to be done individually. You may discuss this assignment with

ID: 3877143 • Letter: T

Question

This assignment is to be done individually. You may discuss this assignment with others, but do not share codel You may find it helpful to read OSTEP Appendix F "Laboratory: Tutorial) (available on the book website). Your programs should only use system calls. See below the types of system calls you can use. Variations of these calls are OK. If you have questions please send me an email. Each student is individually responsible for: Your C code and a Makefile. The Makefile should compile the source code for all programs and produce executables with the names listed below. A Readme file (text or pdf) that includes a brief description of the overall design of each program the amount of time spent for design, code, test, and documentation · Submit separate files in your dropbox: your readme file, makefile and each source code file Note: your programs should use read and write system calls instead of scanf and printf (or similar) C function calls to fetch input and display output. Capture utility filter The program should accept only one string argument that will be used to create a new file. Your program should check that the argument is valid and there isn't a file with that name already and then create the file. The filter program then reads from STDIN and writes to both STDOUT and to the (newly created) file 1. File line select program Write a program lines of text by interactively inputting line numbers. After reading the file into a buf the program 2. that accepts the name of a file and enables the user to view different should repeatedly prompt the user for a line number and then display the e line of text. You can assume that the file has less than 100 lines and that umber of characters in the file is less than 8192. the total create an empty file (truncate) 0-CREAT creat unlink remove a file openopen a file for reading/writing close up read write lseek close a previously open file duplicate an existing file descriptor extract info from a file put info into a file move to specified location in a file

Explanation / Answer

I/O System calls

Essentially there are add up to 5 sorts of I/O framework calls:

1. Make: Used to Create another vacant record. Linguistic structure in C dialect:

int create(char *filename, mode_t mode)

Parameter :

filename : name of the record which you need to make

mode : shows consents of new document.

Returns :

return first unused record descriptor (for the most part 3 when first creat use in process beacuse 0, 1, 2 fd are saved)

return - 1 when blunder

How it function in OS

Create new discharge document on plate

Create record table passage

Set first unused record descriptor to point to document table section

Return record descriptor utilized, - 1 upon disappointment

2.open: Used to Open the record for perusing, composing or both. Sentence structure in C dialect

#include<sys/types.h>

#includ<sys/stat.h>

#include <fcntl.h>

int open (const char* Path, int banners [, int mode ]);

Parameters

Path : way to record which you need to utilize use total way start with "/", when you are not work in same catalog of document.

Use relative way which is just document name with augmentation, when you are work in same registry of record.

flags : How you get a kick out of the chance to utilize O_RDONLY: read just, O_WRONLY: compose just, O_RDWR: read and compose, O_CREAT: make document on the off chance that it doesn't exist, O_EXCL: avoid creation in the event that it as of now exists

How it functions in OS

Find existing document on plate

Create document table section

Set first unused document descriptor to point to record table passage

Return record descriptor utilized, - 1 upon disappointment

/C program to represent

/open framework call

#include<stdio.h>

#include<fcntl.h>

#include<errno.h>

extern int errno;

int fundamental()

{

/if record does not have in catalog

/then record foo.txt is made.

int fd = open("foo.txt", O_RDONLY | O_CREAT);

printf("fd = %d/n", fd);

in the event that (fd ==-1)

{

/print which sort of blunder have in a code

printf("Error Number % d ", errno);

/print program detail "Achievement or disappointment"

perror("Program");

}

return 0;

}

Yield:

fd = 3

3.close: Tells the working framework you are finished with a record descriptor and Close the document which pointed by fd. Grammar in C dialect

#include <fcntl.h>

int close(int fd);

Parameter

fd :record descriptor

Return

0 on progress.

-1 on mistake.

How it functions in the OS

Destroy record table passage referenced by component fd of document descriptor table

– As long as no different procedure is indicating it!

Set component fd of document descriptor table to NULL

/C program to delineate close framework Call

#include<stdio.h>

#include <fcntl.h>

int primary()

{

int fd1 = open("foo.txt", O_RDONLY);

in the event that (fd1 < 0)

{

perror("c1");

exit(1);

}

printf("opened the fd = % d ", fd1);

/Using close framework Call

in the event that (close(fd1) < 0)

{

perror("c1");

exit(1);

}

printf("closed the fd. ");

}

Yield:

opened the fd = 3

shut the fd.

/C program to outline close framework Call

#include<stdio.h>

#include<fcntl.h>

int primary()

{

/accept that foo.txt is as of now made

int fd1 = open("foo.txt", O_RDONLY, 0);

close(fd1);

/accept that baz.tzt is as of now made

int fd2 = open("baz.txt", O_RDONLY, 0);

printf("fd2 = % d ", fd2);

exit(0);

}

Yield:

fd2 = 3

Here, In this code first open() returns 3 since when primary process made, at that point fd 0, 1, 2 are as of now taken by stdin, stdout and stderr. So first unused document descriptor is 3 in record descriptor table. After that in close() framework call is free it this 3 record descriptor and after that after set 3 document descriptor as invalid. So when we called second open(), at that point first unused fd is additionally 3. Along these lines, yield of this program is 3.

4.read: Read information from one cradle to record descriptor, Read measure bytes from the document determined by fd into the memory area. Punctuation in C dialect

size_t read (int fd, void* buf, size_t cnt);

Parameters

fd: record descripter

buf: support to peruse information from

cnt: length of support

Returns: what number bytes were really perused

return Number of bytes read on progress

return 0 on achieving end of document

return - 1 on mistake

return - 1 on flag hinder

Vital focuses

buf necessities to point to a legitimate memory area with length not littler than the predefined estimate as a result of flood.

fd ought to be a substantial document descriptor came back from open() to perform read operation on the grounds that if fd is NULL at that point read ought to create blunder.

cnt is the asked for number of bytes read, while the arrival esteem is the real number of bytes read. Likewise, a few times read framework call should read less bytes than cnt.

/C program to delineate

/read framework Call

#include<stdio.h>

#include <fcntl.h>

int fundamental()

{

int fd, sz;

roast *c = (scorch *) calloc(100, sizeof(char));

fd = open("foo.txt", O_RDONLY);

on the off chance that (fd < 0) { perror("r1"); exit(1); }

sz = read(fd, c, 10);

printf("called read(% d, c, 10). restored that"

" %d bytes were read. ", fd, sz);

c[sz] = '';

printf("Those bytes are as per the following: % s ", c);

}

Yield:

called read(3, c, 10). restored that 10 bytes were perused.

Those bytes are as per the following: 0 foo.

Assume that foobar.txt comprises of the 6 ASCII characters "foobar". At that point what is the yield of the accompanying system?

/C program to represent

/read framework Call

#include<stdio.h>

#include<fcntl.h>

int fundamental()

{

roast c;

int fd1 = Open("foobar.txt", O_RDONLY, 0);

int fd2 = Open("foobar.txt", O_RDONLY, 0);

Read(fd1, &c, 1);

Read(fd2, &c, 1);

printf("c = % c ", c);

exit(0);

}

Yield:

c = f

The descriptors fd1 and fd2 each have their own particular open record table passage, so every descriptor has its own document position for foobar.txt. Hence, the read from fd2 peruses the principal byte of foobar.txt, and the yield is c = f, not c = o.

5.write: Write information from document descriptor into cushion, Writes the bytes put away in buf to the record determined by fd. #include <fcntl.h>

size_t compose (int fd, void* buf, size_t cnt);

Parameters

fd: record descripter

buf: support to compose information to

cnt: length of support

Returns: what number bytes were really composed

return Number of bytes composed on progress

return 0 on achieving end of record

return - 1 on mistake

return - 1 on flag hinder

Imperative focuses

The record should be opened for compose operations

buf should be at any rate insofar as determined by cnt on the grounds that if buf estimate not exactly the cnt then buf will prompt the flood condition.

cnt is the asked for number of bytes to compose, while the arrival esteem is the genuine number of bytes composed. This happens when fd have a less number of bytes to compose than cnt.

/C program to outline

/compose framework Call

#include<stdio.h>

#include <fcntl.h>

primary()

{

int sz;

int fd = open("foo.txt", O_WRONLY | O_CREAT | O_TRUNC, 0644);

in the event that (fd < 0)

{

perror("r1");

exit(1);

}

sz = write(fd, "hi geeks ", strlen("hello geeks "));

printf("called write(% d, "hello geeks ", %d)."

" It returned %d ", fd, strlen("hello geeks "), sz);

close(fd);

}

Yield:

called write(3, "hi geeks ", 12). it returned 11