Creating and Destroying a Pipe

The pipe( ) system call is serviced by the sys_pipe( ) function, which in turn invokes the do_pipe( ) function. To create a new pipe, do_pipe( ) performs the following operations:

1. Invokes the get_pipe_inode( ) function, which allocates and initializes an inode object for the pipe in the pipefs filesystem. In particular, this function executes the following actions:

a. Allocates a pipe_inode_info data structure and stores its address in the i_pipe field of the inode.

b. Allocates a page frame for the pipe buffer and stores its starting address in the base field of the pipe_inode_info structure.

c. Initializes the start, len, waiting readers, and waiting writers fields of the pipe_inode_info structure to 0.

d. Initializes the r counter and w counter fields of the pipe inode info structure to 1.

2. Sets the readers and writers fields of the pipe_inode_info structure to 1.

3. Allocates a file object and a file descriptor for the read channel of the pipe, sets the flag field of the file object to o_rdonly, and initializes the f_op field with the address of the read_ pipe_fops table.

4. Allocates a file object and a file descriptor for the write channel of the pipe, sets the flag field of the file object to o_wronly, and initializes the f_op field with the address of the write_ pipe_fops table.

5. Allocates a dentry object and uses it to link the two file objects and the inode object (see Section 12.1.1); then inserts the new inode in the pipefs special filesystem.

6. Returns the two file descriptors to the User Mode process.

The process that issues a pipe( ) system call is initially the only process that can access the new pipe, both for reading and writing. To represent that the pipe has both a reader and a writer, the readers and writers fields of the pipe_inode_info data structure are initialized to 1. In general, each of these two fields is set to 1 only if the corresponding pipe's file object is still opened by a process; the field is set to 0 if the corresponding file object has been released, since it is no longer accessed by any process.

Forking a new process does not increase the value of the readers and writers fields, so they never rise above however, it does increase the value of the usage counters of all file objects still used by the parent process (see Section 3.4.1). Thus, the objects are not released even when the parent dies, and the pipe stays open for use by the children.

Continue reading here: [2 As well see the readers and writers fields act as counters instead of flags when associated with FIFOs

Was this article helpful?

+1 0