Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

While using Ubuntu/Unix File I/O This section describes some of the basics of UNIX file I/O - it is a prelude to understanding interprocess communication

While using Ubuntu/Unix

File I/O

This section describes some of the basics of UNIX file I/O - it is a prelude to understanding interprocess communication via pipes (see any UNIX reference book for more details). UNIX promotes device-independent input and output by the use of handles. This means that handles are used for many different types of I/O (e.g. files, pipes, sockets, physical devices). A handle is also known as a file descriptor, which is basically an integer index into a table of entries, each of which is associated with a file or device. Each process is initially given the file descriptors 0, 1, 2 (accessible via the fileno() function call or the macros STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO). These file descriptors correspond to standard input (keyboard), standard output (display), and standard error (unbuffered display). The file descriptor table for a process initially looks like this:

[0]

standard input

[1]

standard output

[2]

standard error

UNIX also permits file redirection, by modifying an entry in the file descriptor table. Suppose the process that owns the above table is executing the "ls" utility, and its output is redirected to a file as in: "ls > directory.txt". Then its file descriptor table would now appear as:

[0]

standard input

[1]

WRITE directory.txt

[2]

standard error

indicating that the normal stdout (to the display) is instead being written to the file "directory.txt".

This redirection can also be performed from within a program by using the dup2() system call, which closes an existing file descriptor and points it at the same file as another file descriptor (see the man pages for details). As its name implies, dup2 is used to copy or duplicate a file handle. The file redirection described above is implemented in the shell by using dup2().

Basic I/O-related System Calls

Most UNIX I/O can be performed using the system calls open(),close(),read(), and write(). A file is opened by giving its name, the mode (read/write), and file permissions (if you are creating it). The associated file descriptor is returned. For example:

fd = open("input.dat", O_RDONLY); or fd = open("record.dat", O_RDWR | O_CREAT, 0644);

A file is closed by giving its file descriptor as the only argument:

close(fd);

Input and output (reading/writing) are symmetric system calls:

numRead = read(fd, buffer, bufSize); and numSent = write(fd, buffer, bufSize);

Familiarize yourself with the use of the open(), close(), read(), write(), and dup2() system calls.

Answer the following questions:

  • suppose a process has used dup2() to make its standard output point to a file named temp. The process then issues a fork() system call to spawn a child. The child code issues an exec() system call to execute a different program.

1. where does the standard output of the child process go? Explain.

  • suppose a process issues a fork() system call to spawn a child process. The parent then uses dup2() to make its standard output point to a file named temp. The child code issues an exec() system call to execute a different program.

2. where does the standard output of the child process go? Explain.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions