Computer Science, asked by keerthanabkvrl5559, 8 months ago

The "seek" system call allows the application program to change the value of the file's offset so that subsequent read/write is performed from a new position in the file. Which of the following task will require the use of seek operation:

Answers

Answered by ranyodhmour892
0

Answer:

In Chapter 2, we looked at the basic I/O system calls in Linux. These calls form not only the basis of file I/O, but also the foundation of virtually all communication on Linux. In Chapter 3, we looked at how user-space buffering is often needed on top of the basic I/O system calls, and we studied a specific user-space buffering solution, C’s standard I/O library. In this chapter, we’ll look at the more advanced I/O system calls that Linux provides:

Scatter/gather I/O

Allows a single call to read or write data to and from many buffers at once; useful for bunching together fields of different data structures to form one I/O transaction.

Epoll

Improves on the poll( ) and select( ) system calls described in Chapter 2; useful when hundreds of file descriptors have to be polled in a single program.

Memory-mapped I/O

Maps a file into memory, allowing file I/O to occur via simple memory manipulation; useful for certain patterns of I/O.

File advice

Allows a process to provide hints to the kernel on its usage scenarios; can result in improved I/O performance.

Asynchronous I/O

Allows a process to issue I/O requests without waiting for them to complete; useful for juggling heavy I/O workloads without the use of threads.

The chapter will conclude with a discussion of performance considerations and the kernel’s I/O subsystems.

Scatter/Gather I/O

Scatter/gather I/O is a method of input and output where a single system call writes to a vector of buffers from a single data stream, or, alternatively, reads into a vector of buffers from a single data stream. This type of I/O is so named because the data is scattered into or gathered from the given vector of buffers. An alternative name for this approach to input and output is vectored I/O. In comparison, the standard read and write system calls that we covered in Chapter 2 provide linear I/O.

Scatter/gather I/O provides several advantages over linear I/O methods:

More natural handling

If your data is naturally segmented—say, the fields of a predefined header file—vectored I/O allows for intuitive manipulation.

Efficiency

A single vectored I/O operation can replace multiple linear I/O operations.

Performance

In addition to a reduction in the number of issued system calls, a vectored I/O implementation can provide improved performance over a linear I/O implementation via internal optimizations.

Atomicity

Unlike with multiple linear I/O operations, a process can execute a single vectored I/O operation with no risk of interleaving of an operation from another process.

Both a more natural I/O method and atomicity are achievable without a scatter/gather I/O mechanism. A process can concatenate the disjoint vectors into a single buffer before writing, and decompose the returned buffer into multiple vectors after reading—that is, a user-space application can perform the scattering and the gathering manually. Such a solution, however, is neither efficient nor fun to implement.

readv( ) and writev( )

POSIX 1003.1-2001 defines, and Linux implements, a pair of system calls that implement scatter/gather I/O. The Linux implementation satisfies all of the goals listed in the previous section.

The readv( ) function reads count segments from the file descriptor fd into the buffers described by iov:

Answered by AditiHegde
0

Q).The seek system call allows the application program to change the value of the file's offset so that subsequent read/write is performed from a new position in the file. Which of the following task will require the use of seeking operation:

A. Copying the contents of files A to B.

B. Reversing the contents of a file.

C. Insert/update/delete at a particular point.

D. Finding a particular character in a file.

Both options B and C are correct.

  • The Seek operation allows the application program to change the value of the file pointer leading to subsequent Read/Write performance from a new position in the file. The new value of the file pointer is obtained by adding the offset to the current value.
  • If a positive offset goes beyond the size of the file, the seek position will be set to the file size.
  • If a negative offset leads to an LSeek value below 0 will give an error.
  • The following example reads the text in the reverse direction, from the end of the file to the beginning of the file, by using the various SeekOrigin values with the Seek method.

C#

using System;

using System.IO;

public class FSSeek

{

   public static void Main()

   {

       long offset;

       int nextByte;

       // alphabet.txt contains "abcdefghijklmnopqrstuvwxyz"

       using (FileStream fs = new FileStream(@"c:\temp\alphabet.txt", FileMode.Open, FileAccess.Read))

       {

           for (offset = 1; offset <= fs.Length; offset++)

           {

               fs.Seek(-offset, SeekOrigin.End);

               Console.Write((char)fs.ReadByte());

           }

           Console.WriteLine();

           fs.Seek(20, SeekOrigin.Begin);

           while ((nextByte = fs.ReadByte()) > 0)

           {

               Console.Write((char)nextByte);

           }

           Console.WriteLine();

       }

   }

}

This code example displays the following output:

zyxwvutsrqponmlkjihgfedcba

The above program is an example of reversing the contents of a file.

Like this, we can also insert/update /delete a particular file using seek system call.

#SPJ3

Attachments:
Similar questions