Computer Systems: A Programmer's Perspective (3rd Edition)
Computer Systems: A Programmer's Perspective (3rd Edition)
3rd Edition
ISBN: 9780134092669
Author: Bryant, Randal E. Bryant, David R. O'Hallaron, David R., Randal E.; O'Hallaron, Bryant/O'hallaron
Publisher: PEARSON
bartleby

Concept explainers

Question
Book Icon
Chapter 10, Problem 10.6HW
Program Plan Intro

Opening and Closing files:

Using “Open” function, a process can open an existing file or generates a new file.

  • This function is used to converts a filename to a file descriptor and returns the result as a descriptor number.
  • The descriptor returned is always the lowest descriptor that is not presently open in the process.
  • Each process in the LINUX begins life with three open files.
    • Descriptor 0 – standard input.
    • Descriptor 1 – standard output.
    • Descriptor 2 – standard error.
  • The “Open” function consists of three arguments. That is “Open(filename, flags, mode)”.
    • The argument “filename” defines the name of the given file
    • The argument “flags” represents how the process plans to access the file. Some flags names are as follows.
      • O_RDONLY – it means reading only.
      • O_WRONLY – it means writing only.
      • O_RDWR – it means reading and writing.
    • The “mode” argument identifies the access permission bits of new files.

Example:

The example for open an existing file for reading is shown below:

sample1 = Open("foo1.txt", O_RDONLY, 0);

From the above “open” function,

  • The filename is “foo1.txt”.
  • Flag name is “O_RDONLY”.
  • Mode is “0”.

Expert Solution & Answer
Check Mark

Explanation of Solution

Corresponding code from given question:

Main.c:

//Header file

#include "csapp.h"

//Main function

int main()

{

  //Declare int variable

  int fd1, fd2;

/* Open the file "foo.txt" using "Open" function and store descriptor number in fd1 */

  fd1 = Open("foo.txt",O_RDONLY, 0);

/* Open the file "bar.txt" using "open" function and store descriptor number in fd2 */

    fd2 = Open("bar.txt",O_RDONLY, 0);

  //Frees up the descriptor number in "fd2"

  Close(fd2);

/* Open the file "baz.txt" using "Open" function and store descriptor number in fd2 */

  fd2 = Open("baz.txt",O_RDONLY, 0);

  //Display the descriptor number in "fd2"

  printf("fd2 = %d\n", fd2);

  //Exit the process

  exit(0);

}

Explanation:

The given code is used to returns the descriptor number for given file.

  • Include the header file
  • Define the main function.
    • Declare two variables “fd1” and “fd2” in “int” data type.
    • Open the file “foo.txt” using “Open” function and store its descriptor number in “fd1”.
    • Open the file “bar.txt” using “Open” function and store its descriptor number in “fd2”.
    • Frees up the descriptor number in “fd2” using “Close” function.
    • Open the file “baz.txt” using “Open” function and store its descriptor number in “fd2”.
    • Then displays the descriptor number in “fd2”.
    • Finally exit the process using “exit” function.
  • Before run the program, user needs to create the three files “foo.txt”, “bar.txt” and “baz.txt”.

Reasons for displaying given output:

  • The “Open” function always returned the smallest unopened descriptor.
  • From the given code, first call to “Open” returns the descriptor 3 that is the descriptor 3 is in “fd1”.
  • Then call to “Open” returns the descriptor 4 that is the descriptor 4 is in “fd2”.
  • After that “fd2” frees up using “Close” function.
  • Now, call to “Open” function returns the descriptor 4 in “fd2”. Therefore, the output of the given code is “fd2 = 4”.
Sample Output

fd2 = 4

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Please answer the JAVA OOP Programming Assignment scenario below: Patriot Ships is a new cruise line company which has a fleet of 10 cruise ships, each with a capacity of 300 passengers. To manage its operations efficiently, the company is looking for a program that can help track its fleet, manage bookings, and calculate revenue for each cruise. Each cruise is tracked by a Cruise Identifier (must be 5 characters long), cruise route (e.g. Miami to Nassau), and ticket price. The program should also track how many tickets have been sold for each cruise. Create an object-oriented solution with a menu that allows a user to select one of the following options: 1. Create Cruise – This option allows a user to create a new cruise by entering all necessary details (Cruise ID, route, ticket price). If the maximum number of cruises has already been created, display an error message. 2. Search Cruise – This option allows to search a cruise by the user provided cruise ID. 3. Remove Cruise – This op…
I need to know about the use and configuration of files and folders, and their attributes in Windows Server 2019.
Southern Airline has 15 daily flights from Miami to New York.  Each flight requires two pilots.  Flights that do not have two pilots are canceled (passengers are transferred to other airlines).  The average profit per flight is $6000.  Because pilots get sick from time to time, the airline is considering a policy of keeping four *reserve pilots on standby to replace sick pilots.  Such pilots would introduce an additional cost of $1800 per reserve pilot (whether they fly or not). The pilots on each flight are distinct and the likelihood of any pilot getting sick is independent of the likelihood of any other pilot getting sick.  Southern believes that the probability of any given pilot getting sick is 0.15.  A) Run a simulation of this situation with at least 1000 iterations and report the following for the present policy (no reserve pilots) and the proposed policy (four reserve pilots): The average daily utilization of the aircraft (percentage of total flights that fly) The…
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
EBK JAVA PROGRAMMING
Computer Science
ISBN:9781337671385
Author:FARRELL
Publisher:CENGAGE LEARNING - CONSIGNMENT