Computer Systems: A Programmer's Perspective Plus Mastering Engineering With Pearson Etext -- Access Card Package (3rd Edition)
Computer Systems: A Programmer's Perspective Plus Mastering Engineering With Pearson Etext -- Access Card Package (3rd Edition)
3rd Edition
ISBN: 9780134123837
Author: Randal E. Bryant, David R. 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
System A System B Line 1: y x- 4 Line 1: y = -x+ Line 2: y=- -x+1 6 L2 4 -4 -D LI Line 2: y=2x+5 4 Ll 2 6 L2 This system of equations is: consistent dependent consistent independent inconsistent This means the system has: O a unique solution Solution: () no solution infinitely many solutions This system of equations is: consistent dependent consistent independent inconsistent This means the system has: O a unique solution Solution: () no solution infinitely many solutions
Here is the text extracted from the uploaded image:   Question 6: A major Saudi book retailer is planning to develop a computer system to handle the store’s new online bookshop: BookShop.com. You have been chosen to do the analysis and design. As part of the development of the system, perform the following tasks: 1.Write Expanded Use Cases for buying a book from the shop using a credit card. 2.Draw an Expanded Use Case for the following interactions: •Buying a book from the shop using a credit card. •Checking the status of one’s orders (being filled, shipped, etc.) •Pay for Product •Validate customer identity   Use Case: Buy Book   Actors: Customer Purpose: To buy a book from BookShop.com using a credit card. Overview: A customer has selected a book to buy at the Murray.com website. The system prompts for the customer’s username and password. The customer enters these details. The system verifies the customer’s identity and retrieves the customer’s name and address, then prompts for…
- Apply a SQL injection in the Login - Apply a SQL injection (Search / GET) to list the database tables - Apply a SQL injection (Search / GET) to list the users and passwords of the DB Questions: 1.- What was the vulnerability used for the attack? What service was affected? 2.- What type(s) of attack(s) were made? 3.-What was the damage caused to the information asset(s)? - Include screenshots of the entire procedure and the answers to each question bWAPP link: http://www.itsecgames.com/download.htm To download bWAPP Virtual Disk https://drive.google.com/file/d/1eckLtRkqsZIlbnSz5fWWhpotEZFvx2vo/view?usp=sharing
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
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY