计算机代考程序代写 CSci 4061 – Section 010 – cscodehelp代写

CSci 4061 – Section 010
Discussion Week 6
24th Feb, 2020

Overview
1 Files and File I/O 2 Pipes
3 I/O Redirection
4 Exercise

Overview
1 Files and File I/O 2 Pipes
3 I/O Redirection
4 Exercise

File Descriptors
1 Non-negative integer
2 Access to a file or an input/output resource (pipe, socket)
3 Standard POSIX file descriptors
• STDIN FILENO – stdin
• STDOUT FILENO – stdout • STDERR FILENO – stderr

File Operations
Function open(filename, flags)
close(fd)
read(fd, buffer, int) write(fd, buffer, int) lseek(fd, off t, whence)
Description
Attempts to open file with name file- name. Return the file descriptor(fd) if successful, -1 otherwise
Closes the file referred by fd Reads int bytes from fd to buffer Writes int bytes from buffer to fd Moves the file pointer by off t bytes from the whence

lseek whence
MACRO
SEEK SET SEEK CUR
SEEK END
Description
Moves file pointer to start of file Moves file pointer to a given loca- tion
Moves file pointer to end of file

Flags
Flag Description
O CREAT O APPEND O RDONLY O WRONLY O RDWR
Creates file if it doesnt exist Appends to an existing file
Open file in Read only mode
Open file in Write only mode
Open file in both Read and Write mode

File Example
// read mode− create read.txt and add some text prior to this call int fdrd = open(”read.txt”, ORDONLY);
// write mode
int fdwt = open( ” write . txt ” , OWRONLY);
int count = 0;
// Read every nth byte of read.txt to write.txt char arr [100];
while(read(fdrd , arr , 1)) {
// first byte of input
if(count < n){ lseek(fdrd , n, SEEKCUR); write(fdwt, arr, 1); count = n; } else { count = 2 ∗ n; lseek(fdrd, count, SEEKCUR); write(fdwt, arr, 1); } } close(fdrd ); close(fdwt ); Source Overview 1 Files and File I/O 2 Pipes 3 I/O Redirection 4 Exercise Pipes • Interprocess communication channels • Treated like files • Uses file descriptors (fds) • Read(0)/write(1) ends / fds • During IPC ensure the pipe ends are properly closed • Pipes are copied across processes on forking • Pipe end is only closed, if all pipe fds pointing at the end are closed • close(fd[0]) - closes current process pipes read end, but OS ill close it properly only if all other process closes theirs • Try out the exercise • Once closed, they do not persist pipe Syntax int pipe(int fd[2]) Pipes Example int fd1[2]; // read and write ends char inputstr [100]; scanf(”%s” , inputstr ); pipe(fd1 ); // add error checking pidtp=fork(); if(p< 0) {printf(”Fork failed ”); exit(0);} else if (p>0){
close(fd1 [0]); // close read end
write(fd1[1], inputstr , strlen(inputstr) + 1); close(fd1[1]); // close write end
w a i t ( NULL ) ;
printf(”%d : Child received msg
”, getpid());
} else {
close(fd1[1]); // close write end
}
char recvstr [100];
read(fd1[0], recvstr, 100);
close(fd1 [0]); // close read end
printf(”%d : Received msg from parent : %s
”, getpid(), recvstr); exit (0);

Overview
1 Files and File I/O 2 Pipes
3 I/O Redirection
4 Exercise

I/O Redirection
• Create aliases or interchangeable file descriptors dup2 Syntax
dup2(int oldfd, int newfd)
• Creates a copy of file descriptor oldfd, using the newfd • oldfd and newfd can be used interchangeably

I/O Redirection Example
int fd = open(”sample.txt”, OAPPEND | OWRONLY); // add error checking // Now redirect standard output to the fd using dup2
if(dup2(fd, 1)<0){ close( file ); exit (0); } // Now standard output has been redirected printf(”Writing to the file ”); close( file ); Overview 1 Files and File I/O 2 Pipes 3 I/O Redirection 4 Exercise Exercise • Quiz on Canvas, due 11:59 pm Tuesday • Lab exercise : Covers pipes and redirection Mimic following pipe echo Hello There | grep −a Hello • Create a pipe in the main process • Create two child processes • P1 - Sends a message via pipe to P2 (No echo used, just write given message to pipe) • P2 - Redirects STDIN FILENO to the read end of the pipe and runs grep on the data received • Ensure to close the pipe ends correctly

Leave a Reply

Your email address will not be published. Required fields are marked *