char buffer [BUFSIZE]; int fd = open("fooey",0_WRONLY|O_CREAT|O_TRUNC,0777); strcpy (buffer, "First data"); write(fd, buffer, strlen(buffer)); 1seek (fd, 90, SEEK_SET); strcpy (buffer, "Second data"); write(fd, buffer, strlen(buffer)); 1seek (fd, 100, SEEK_END); strpcy (buffer, "Third data"); write(fd, buffer, strlen(buffer)); close(fd); a. What is the size of the file at the end of this section of code? b. Describe the contents of the file say exactly what byte offsets contain actual data, and what offsets constitute "holes". c. Why might holey files be useful?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
```plaintext
lseek(rfd,130,SEEK_SET);    read(rfd,buffer,40);

What will the read return, and what (if anything) will be placed in buffer?
```

**Explanation:**

In this code snippet, we see two functions used in file handling in C: `lseek` and `read`.

1. **lseek(rfd,130,SEEK_SET);**
   - `lseek` is a system call that repositions the file offset of the open file descriptor `rfd` to the specified location.
   - The first argument, `rfd`, is the file descriptor of the open file.
   - The second argument, `130`, is the offset in bytes to which the file pointer will be moved.
   - `SEEK_SET` is the directive indicating that the offset should be set relative to the beginning of the file.

2. **read(rfd,buffer,40);**
   - `read` attempts to read up to `40` bytes from the file associated with the descriptor `rfd` into `buffer`.
   - `rfd` is the file descriptor from which to read.
   - `buffer` is the location where the read data will be stored.
   - `40` specifies the maximum number of bytes to read.

**Discussion Question:**
- What will the `read` return, and what (if anything) will be placed in buffer?
  
Students should consider factors such as the file's length, content from the offset position, and any potential errors (e.g., if `rfd` does not refer to a valid file).
Transcribed Image Text:```plaintext lseek(rfd,130,SEEK_SET); read(rfd,buffer,40); What will the read return, and what (if anything) will be placed in buffer? ``` **Explanation:** In this code snippet, we see two functions used in file handling in C: `lseek` and `read`. 1. **lseek(rfd,130,SEEK_SET);** - `lseek` is a system call that repositions the file offset of the open file descriptor `rfd` to the specified location. - The first argument, `rfd`, is the file descriptor of the open file. - The second argument, `130`, is the offset in bytes to which the file pointer will be moved. - `SEEK_SET` is the directive indicating that the offset should be set relative to the beginning of the file. 2. **read(rfd,buffer,40);** - `read` attempts to read up to `40` bytes from the file associated with the descriptor `rfd` into `buffer`. - `rfd` is the file descriptor from which to read. - `buffer` is the location where the read data will be stored. - `40` specifies the maximum number of bytes to read. **Discussion Question:** - What will the `read` return, and what (if anything) will be placed in buffer? Students should consider factors such as the file's length, content from the offset position, and any potential errors (e.g., if `rfd` does not refer to a valid file).
```c
char buffer[BUFSIZE];
int fd = open("fooey", O_WRONLY|O_CREAT|O_TRUNC, 0777);
strcpy(buffer, "First data");
write(fd, buffer, strlen(buffer));
lseek(fd, 90, SEEK_SET);
strcpy(buffer, "Second data");
write(fd, buffer, strlen(buffer));
lseek(fd, 100, SEEK_END);
strcpy(buffer, "Third data");
write(fd, buffer, strlen(buffer));
close(fd);
```

a. What is the size of the file at the end of this section of code?

b. Describe the contents of the file—say exactly what byte offsets contain actual data, and what offsets constitute "holes".

c. Why might holey files be useful?

d. Suppose, after the above sequence of writes, the same program opens the file for reading and then does:
```
```

### Explanation

- **Code Explanation**: 
  - A buffer is defined to store data.
  - The file "fooey" is opened with write permissions; it will be created if it does not exist or truncated if it does.
  - "First data" is written to the file.
  - The file pointer is moved to byte offset 90, leaving a gap (or hole) between the first and second writes.
  - "Second data" is written starting at byte 90.
  - The file pointer is moved 100 bytes beyond the current end of the file.
  - "Third data" is written starting at the new position.

- **Questions and Concepts**:
  - **Holey Files**: These are files with holes (empty regions) between data blocks, which do not consume disk space.
  - **File Size Determination**: The file size reflects the position of the farthest byte written in the file.
  - **Practical Use**: Holey files allow efficient storage of sparse data by not allocating blocks for the empty gaps.
Transcribed Image Text:```c char buffer[BUFSIZE]; int fd = open("fooey", O_WRONLY|O_CREAT|O_TRUNC, 0777); strcpy(buffer, "First data"); write(fd, buffer, strlen(buffer)); lseek(fd, 90, SEEK_SET); strcpy(buffer, "Second data"); write(fd, buffer, strlen(buffer)); lseek(fd, 100, SEEK_END); strcpy(buffer, "Third data"); write(fd, buffer, strlen(buffer)); close(fd); ``` a. What is the size of the file at the end of this section of code? b. Describe the contents of the file—say exactly what byte offsets contain actual data, and what offsets constitute "holes". c. Why might holey files be useful? d. Suppose, after the above sequence of writes, the same program opens the file for reading and then does: ``` ``` ### Explanation - **Code Explanation**: - A buffer is defined to store data. - The file "fooey" is opened with write permissions; it will be created if it does not exist or truncated if it does. - "First data" is written to the file. - The file pointer is moved to byte offset 90, leaving a gap (or hole) between the first and second writes. - "Second data" is written starting at byte 90. - The file pointer is moved 100 bytes beyond the current end of the file. - "Third data" is written starting at the new position. - **Questions and Concepts**: - **Holey Files**: These are files with holes (empty regions) between data blocks, which do not consume disk space. - **File Size Determination**: The file size reflects the position of the farthest byte written in the file. - **Practical Use**: Holey files allow efficient storage of sparse data by not allocating blocks for the empty gaps.
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Array
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
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education