CS 3423 Systems Programming: Final Exam Review Key

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Listen to an AI-generated conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

What is the potential consequence of writing a message larger than PIPE_BUF bytes to a pipe?

  • The write operation will always fail.
  • The message is susceptible to corruption due to interleaving with other writes. (correct)
  • The process will be blocked until the entire message can be written atomically.
  • The pipe's capacity will dynamically increase to accommodate the message.

If a process attempts to write to a pipe for which no process has it open for reading, what occurs?

  • The data is buffered until a reader becomes available.
  • The process blocks indefinitely.
  • The write operation returns an error code.
  • The process receives a `SIGPIPE` signal and terminates. (correct)

A process attempts a read() on an empty pipe, and no other process has the pipe open for writing. What is the expected behavior?

  • The `read()` operation returns an error code.
  • The `read()` operation blocks indefinitely.
  • The `read()` operation returns 0, indicating end-of-file. (correct)
  • The `read()` operation returns -1, and `errno` is set to `EAGAIN`.

A process opens a FIFO with the O_WRONLY flag. What behavior would you expect?

<p>The <code>open()</code> call blocks until another process opens the FIFO for reading. (C)</p>
Signup and view all the answers

What happens to zombie or orphaned child processes when a process terminates?

<p>They are adopted by the <code>init</code> process (PID = 1). (D)</p>
Signup and view all the answers

What is the expected outcome if a process attempts to use write past the end of a file?

<p>The write operation will automatically extend the file. (C)</p>
Signup and view all the answers

What is the behavior of the dup2(a, b) system call if the file descriptor b is already open?

<p>The call will succeed, closing <code>b</code> first if it's open, and then making <code>b</code> a duplicate of <code>a</code>. (A)</p>
Signup and view all the answers

In the provided code fragment involving pipes and fork(), what is the primary reason the child process's read() operation might block indefinitely?

<p>Neither process closes the write end of the pipe, causing <code>read()</code> to wait for more data. (C)</p>
Signup and view all the answers

Consider a bash function divides() that checks if x is a factor of y. Which of the following implementations correctly returns 0 if x is a factor of y and 1 if it is not?

<p><code>divides() { [ &quot;$#&quot; -ne 2 ] &amp;&amp; return 0; return $(($2 % $1 != 0)); }</code> (C)</p>
Signup and view all the answers

Which sed command correctly removes the first number from a line, if it begins with a number?

<p><code>sed 's/^[0-9]+//'</code> (B)</p>
Signup and view all the answers

Which sed command correctly surrounds the entire line with <num>...</num> tags, but only if the line begins with a number?

<p><code>sed '/^[0-9]/ s/.*/&lt;num&gt;&amp;&lt;\/num&gt;/'</code> (D)</p>
Signup and view all the answers

Which sed command correctly trims any repeat sequences of tabs and/or spaces down to a single space?

<p><code>sed 's/[ \t]+/ /g'</code> (D)</p>
Signup and view all the answers

Which sed command correctly replaces all sequences of digits that reside immediately prior to a hyphen with a single 'x'?

<p><code>sed 's/[0-9]+-/x-/g'</code> (C)</p>
Signup and view all the answers

Consider the Awk script for computing the average of the last fields of odd-numbered lines. What will happen if the input has an even number of lines and all of them are odd?

<p>It will compute the average without problems. (A)</p>
Signup and view all the answers

In the provided regular expression table, which pattern would extract 'zero width' from the string 'snowman00'?

<p><code>[0-9](.*)[0-9]</code> (D)</p>
Signup and view all the answers

In the provided regular expression table, given the string 'dashedX5R2' and the pattern '([a-z]+)([A-Z]+[0-9]+)+', what would be the value of group (1)?

<p><code>dashed</code> (D)</p>
Signup and view all the answers

In the provided regular expression table, given the string '100@weak' and the pattern '([0-9]+[^a-z][a-z]+)', what would be the value of group (1)?

<p><code>100@weak</code> (A)</p>
Signup and view all the answers

In the provided regular expression table, given the string '2.1sfh' and the pattern '([0-9]*.([0-9])[A-Z]?)', what would be the value of group (1)?

<p><code>2.1</code> (B)</p>
Signup and view all the answers

How many blocks of direct pointers are necessary to reference 25,755,648 data blocks, assuming 4 byte addresses within the direct pointer blocks?

<p>25,152 blocks (A)</p>
Signup and view all the answers

How many blocks of indirect pointers are necessary to reference the direct pointer blocks from previous question, which are 25,152 blocks?

<p>25 (B)</p>
Signup and view all the answers

How many blocks of double indirect pointers are necessary to reference the indirect pointer blocks, which equates to 25 blocks, from previous question?

<p>1 (C)</p>
Signup and view all the answers

How many total blocks are needed (not including the inode)?

<p>25,780,826 total blocks (A)</p>
Signup and view all the answers

In a C function using low-level I/O to insert a record into a file at a specific record number, what is a critical step to ensure data integrity when inserting a new record in the middle of existing records?

<p>Shift all subsequent records towards the end of the file to make space for the new record. (C)</p>
Signup and view all the answers

Using low-level I/O functions, what sequence of operations should be performed to shift all subsequent records towards the end of the file when inserting a new record in the middle of existing records?

<p>Loop through a file to <code>read()</code> it, then <code>write()</code> it one at a time to shift records. (C)</p>
Signup and view all the answers

Given the code segment provided, how many different outputs are possible from the fprintf statements, considering the interleaving that can occur due to forking? (Enumerate all possible outputs)

<p>6 (A)</p>
Signup and view all the answers

Based on the readForkExec code, under which condition would you explicitly need to call exit(EXIT_SUCCESS) in the child process?

<p>In the sacrificial child immediately before <code>execvp</code> is called. (A)</p>
Signup and view all the answers

Within the context of the readForkExec function and the management of file descriptors, when is it safe to close the original file descriptors (buffer.fdin and buffer.fdout) within the parent process (after they have potentially been duplicated into stdin/stdout for the child process)?

<p>The parent process does not use the original file descriptors (<code>buffer.fdin</code> and <code>buffer.fdout</code>). (D)</p>
Signup and view all the answers

Flashcards

PIPE_BUF and Data Corruption

Data corruption can occur when writing messages larger than PIPE_BUF to a pipe due to internal shuffling among writes.

SIGPIPE Signal

If a process attempts to write to a pipe, but no process has it open for reading, the writing process is terminated by a SIGPIPE signal.

Reading from an Empty Pipe

When a process attempts to read from an empty pipe and no process currently has the pipe open for writing, the read operation returns 0.

FIFO and O_WRONLY

When a process opens a FIFO with O_WRONLY, it will block until another process opens the same FIFO for reading.

Signup and view all the flashcards

Orphaned Processes and Init

When a process terminates, any zombie or orphaned child processes are adopted by the init process (PID = 1).

Signup and view all the flashcards

write() past end of file

A write() error will not occur if a process seeks past the end of a file, then attempts to write at this location. The write will extend the file.

Signup and view all the flashcards

dup2() Behavior

If the file descriptor referred to by 'b' in dup2(a, b) is already open, the call will successfully close b first, and then a and b will refer to the same file.

Signup and view all the flashcards

Pipe Block Explanation

The child's final read() will block and wait indefinitely because neither process closes write end of the pipe.

Signup and view all the flashcards

Regex: 0-9[A-Z]

A regular expression that matches digits that are followed by zero or more characters, ending with a capital letter.

Signup and view all the flashcards

Regex A-Z+

Matches one or more uppercase letters followed by one or more non-lowercase letters.

Signup and view all the flashcards

Regex: 0-9[0-9]

Matches one or more digits followed by any characters followed by one or more digits

Signup and view all the flashcards

Regex: ^([0-9]\s[-a-zA-Z]+)

Matches zero or more digits followed by optional whitespace and one or more letters a-z or A-Z.

Signup and view all the flashcards

Regex dashedX5R2

Matches one or more lowercase characters followed by one or more uppercase chars and numbers.

Signup and view all the flashcards

Regex: 100@weak

Matches one or more numbers followed by the characters before lowercase.

Signup and view all the flashcards

Regex: ([0-9]*.([0-9])[A-Z]?)

Matches digits, any characters, and any uppercase character that is either A-Z or ?

Signup and view all the flashcards

Calculating Data Blocks

To calculate the number of data blocks, you must first divide the total size of the data by the block size. With 98.25 GB of data and 4KB blocks, the number of data blocks equals the total size divided by 4KB.

Signup and view all the flashcards

Calculating Direct Pointers

One must divide the number of data blocks by the number of addresses a single block can contain to find how many blocks of direct pointers one needs.

Signup and view all the flashcards

Calculating Blocks of Indirect Pointers

To calculate the blocks of indirect pointers, divide the number of blocks of direct pointers by the number of direct pointer blocks.

Signup and view all the flashcards

Calculating Blocks of Double Indirect Pointers

To calculate blocks of double indirect pointers, divide the number of indirect pointers by the number of direct pointer blocks.

Signup and view all the flashcards

Study Notes

  • Writing a message larger than PIPE_BUF bytes to a pipe can lead to corruption due to shuffling between writes.
  • If a process tries to write to a pipe without a reader, it gets terminated by a SIGPIPE signal.
  • A read() operation on an empty pipe with no writers returns 0.
  • If a process attempts to read () on a pipe containing data, and no process currently has the pipe opened for writing, the read () request will not return 0.
  • Opening a FIFO with O_WRONLY blocks until another process opens it for reading.
  • When a process ends, init (PID = 1) adopts zombie or orphaned children.
  • A write() error does not occur if a process seeks past the end of a file and then attempts to write.
  • dup2(a, b) does not fail if file descriptor b is already open; it usually closes b first.

Bash Script

  • A bash function divides() checks if integer x is a factor of y. It returns 0 if true, 1 if false.
  • $# checks the number of arguments passed to the function. -ne 2 test if it’s not equal to 2
  • $2 % $1 calculates the remainder of an integer division of y/x. != 0 test if the result is not equal to 0

Sed Script

  • s/^[0-9]+/ removes the first number from a line if it begins with one.
  • s/^/<num>/ surrounds the line with <num> and </num> tags.
  • s/$/<\/num>/ surrounds the line with <num> and </num> tags.
  • s/[\t ]{2,}//g trims repeated tabs/spaces to a single space.
  • s/([0-9]+)-/x-/g replaces digits before a hyphen with "x".

Awk Script

  • Computes the average of last fields in odd-numbered lines.
  • The script calculates the sum of the last fields ($NF) of odd-numbered lines (NR % 2 == 1).
  • NR % 2 checks whether the line number is odd or even. If the remainder of the division of NR (the line number) by 2 is equal to 1, then the line number is odd.
  • It then divides the sum by the number of odd-numbered lines to find the average.

Inodes Calculation:

  • For 98.25 GB data with 4KB blocks: 25,755,648 data blocks are utilized, equaling direct pointers.
  • 25,152 blocks of direct pointers are needed, equivalent to indirect pointers (4 byte addresses).
  • 25 blocks of indirect pointers are required, corresponding to double indirect pointers.
  • 1 block of double indirect pointers is necessary, which equals 1 triple indirect pointer.
  • The total blocks needed (excluding inode) are 25,780,826.

Regular Expressions

  • 762xcelR9 with pattern [0-9](.*)[A-Z] yields group(1) as 62xcel.
  • WXYZ100.1 with pattern [A-Z]([^a-z])+ yields group(1) as 1.
  • snowman00 with pattern [0-9](.*)[0-9] yields <zero width>.
  • regentGoliath-3 with pattern ^([0-9]*\s*[-a-zA-Z]+) yields group(1) as regentGoliath-.
  • dashedX5R2 with pattern ([a-z]+)([A-Z]+[0-9]+)+ yields group(1) as dashed.
  • 100@weak with pattern ([0-9]+[^a-z][a-z]+) yields group(1) as 100@weak.
  • 2.1sfh with pattern ([0-9]*.([0-9])[A-Z]?) yields group(1) as 2.1.

C Function: insertRec

  • The function insertRec inserts a record into a file at a specified record number, using low-level I/O.
  • It accepts a file descriptor fd, a record number iRecNum, and a pointer to a record rec.
  • It shifts existing records to make space for the new record, assuming zero-based indexing.
  • The function uses lseek, read, and write for file positioning and data manipulation.
  • It handles potential errors by printing error messages to stderr and returns -1 upon failure.

Process Control

  • A code segment involving fork() calls creates a total of 3 processes: 1 parent and 2 children.
  • Possible outputs from the code, due to interleaving of process execution, include: abccdc, abcdcc, acbcdc, acbdcc, baccdc, bacdcc.

C Function: readForkExec

  • Function readForkExec reads commands from a pipe, forks a child process for each, and executes the specified program.
  • It accepts the file descriptor (fd) of the read end of a pipe.
  • It reads structures from the pipe, each containing the name of an executable, arguments, and optional input/output file descriptors.
  • The child process redirects input/output if specified and executes the command.
  • The parent process does not wait for the child to complete to avoid blocking.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Indirect Cold Water Systems Quiz
3 questions
Aircraft Exhaust System Components
5 questions
Plumbing Waste Pipes and House Sewer Systems
37 questions
Operating Systems: Fork and Pipes
25 questions
Use Quizgecko on...
Browser
Browser