Question

This is for a Unix class.

Please fully answer the question :)

The figure that it references is on the attached picture.

I have also added the information that comes with that figure.
3.3 Assume that a process executes the following three function calls: fdl-open (path, oflags); fd2 = dup ( fd ) ; fd3-open (

dup and dup2 Functions An existing file descriptor is duplicated by either of the following functions: #include <unistd.h> inThe new file descriptor that is returned as the value of the functions shares the same file table entry as the fd argument. WIn this figure, we assume that when its started, the process executes newfd dup ( 1 ) ; We assume that the next available dedup2 is an atomic operation, whereas the alternate form involves two function calls. It is possible in the latter case to hav

3.3 Assume that a process executes the following three function calls: fdl-open (path, oflags); fd2 = dup ( fd ) ; fd3-open (path, oflags); Draw the resulting picture, similar to Figure 3.9. Which descriptors are affected by an fentl on fdl with a command of F_SETFD? Which descriptors are affected by an fentl on fdl with a command of F SETFL?
dup and dup2 Functions An existing file descriptor is duplicated by either of the following functions: #include int dup(int fd); int dup2 (int fd, int fd2); Both return: new file descriptor if OK, -1 on error The new file descriptor returned by dup is guaranteed to be the lowest-numbered available file descriptor. With dup2, we specify the value of the new descriptor with the fd2 argument. If fd2 is already open, it is first closed. If fd equals fd2, then dup2 returns fd2 without closing it. Otherwise, the FD_CLOEXEC file descriptor flag is cleared for fd2, so that fd2 is left open if the process calls exec
The new file descriptor that is returned as the value of the functions shares the same file table entry as the fd argument. We show this in Figure 3.9. process table entry file table file status flags current file offset v-node pointer v-node table v-node informatio v data fd flags pointer fd 0: fd 1 fd 2: fd 3: i-node informatiorn current file size i_vnode Figure 3.9 Kernel data structures after dup (1)
In this figure, we assume that when it's started, the process executes newfd dup ( 1 ) ; We assume that the next available descriptor is 3 (which it probably is, since 0, 1, and 2 are opened by the shell). Because both descriptors point to the same file table entry, they share the same file status flags-read, write, append, and so on-and the same current tile offset. Each descriptor has its own set of file descriptor flags. As we describe in Section 3.14, the close-on-exec file descriptor flag for the new descriptor is always cleared by the dup functions. Another way to duplicate a descriptor is with the fcntl function, which we describe in Section 3.14. Indeed, the call dup(fd); is equivalent to fcntl(fd, F_DUPFD, 0); Similarly, the call dup2 (fd, fd2) is equivalent to close (fd2); fcntl(fd, F DUPFD, fd2); In this last case, the dup2 is not exactly the same as a close followed by an fcntl. The differences are as follOwS:
dup2 is an atomic operation, whereas the alternate form involves two function calls. It is possible in the latter case to have a signal catcher called between the close and the fcntl that could modify the file descriptors. (We describe signals in Chapter 10.) The same problem could occur if a different thread changes the file descriptors. (We describe threads in Chapter 11.) 1. 2. There are some errno differences between dup2 and fcntl The dup2 system call originated with Version 7 and propagated through the BSD releases. The fentl method for duplicating file descriptors appeared with System III and continued with System V. SVR32 picked up the dup2 function, and 42BSD picked up the fentÏ function and the F_DUPFD functionality. POSIX.1 requires both dup2 and the F_DUPFD feature of fentl.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

fd1 = open(path,oflags);

fd2 = dup(fd1);

when it's duplicated the next available descriptor is 4 . That's the reason it is used.

fd3 = open(path,oflags);

Lvnedo

F_SETFD (int)

This command is used to manipulate the flags associated with a file descriptor. It sets the file descriptor fd1 flags to the value specified by arg.

F_SETFL (int)

It set the file status flags of fd1 descriptor to the value specified by argument. File access mode that are O_RDONLY, O_WRONLY, O_RDWR and file creation flags such as O_CREAT, O_EXCL, O_NOCTTY, O_TRUNC in argument are ignored.

Add a comment
Know the answer?
Add Answer to:
This is for a Unix class. Please fully answer the question :) The figure that it references is on...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using the file descriptor table for a process, answer the following questions. Note: the dup(), pipe()...

    Using the file descriptor table for a process, answer the following questions. Note: the dup(), pipe() , and open() system calls always use the lowest numbered available file descriptor posi-tion. (a) Label which entries in Table 2 refer to stdin, stdout, andstderr 0 1 2 3 4 5 (b) Assume that stdin, stdout, and stderr are all open. Label in Table 3 what the file descriptor table would look like after the following code is executed: int fd[2]; close(1); pipe(fd);...

  • In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use: 0 st...

    In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use: 0 standard input (stdin) 1 standard output (stdout) 2 standard error (stderr) By default, the command interpreter (shell) reads keyboard input from file descriptor 0 (stdin) and writes output to file descriptor 1 (stdout), which appears on the screen. As you explored in Lab 2, input/output can be...

  • GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h>...

    GIVEN CODE- FILL IN THE BLANK! #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/wait.h> // Function ptototypes int readX(); void writeX(int); int main() /// chi read x ---> divi ---> write x into file ---> par read x --> sub--> write x into file---> chi read x-->etc {    int pid;           // pid: used to keep track of the child process    int x = 19530;       // x: our value as integer   ...

  • Please, please help with C program. This is a longer program, so take your time. But...

    Please, please help with C program. This is a longer program, so take your time. But please make sure it meets all the requirements and runs. Here is the assignment: I appreciate any help given. Cannot use goto or system(3) function unless directed to. In this exercise you are to create a database of dogs in a file, using open, close(), read, write(), and Iseek(). Do NOT use the standard library fopen() ... calls! Be sure to follow the directions!...

  • GIVEN CODE. PLEASE FILL IN THE BLANK. // Include Block #include <fcntl.h> #include <unistd.h> // Preprocessor...

    GIVEN CODE. PLEASE FILL IN THE BLANK. // Include Block #include <fcntl.h> #include <unistd.h> // Preprocessor declarations #define BUF_SIZE 10 /* global constant buffer size: Generally this would be much larger, but I want to show you that it works in a loop until the entire file is read.*/ // main function int main(int argc, char *argv[]) {    // Variable declarations    int fd;                       // file descripter    char buffer[BUF_SIZE];       // string for...

  • ) Using Linux or Unix command line interpreter, compile and run the programs in Figure 3.8,...

    ) Using Linux or Unix command line interpreter, compile and run the programs in Figure 3.8, Figure 3.30. DO NOT compile and ron these programs on Windows Write the 3.16, Figare 317 and Figure 3 programs in Notepadt+, for example, then compile and run them at the command pr apt. Provide screenshots of your programs compilation, execution, and the results. 144 6 7 8 9 ry-maps a shared-memory object of the ws writing to the object. The flag shared-memory object...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue....

    Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ.   Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...

  • please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits...

    please write in c++. 4 Derived class JPG 4.1 Class declaration • The class JPG inherits from File and is a non-abstract class. 1. Hence objects of the class JPG can be instantiated. 2. To do so, we must override the pure virtual function clone() in the base class. • The class declaration is given below. class JPG : public File { public: JPG(const std::string& n, int n, int w, const double rgb()) : File(...) { // ... } *JPG()...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT