Systems Programming Exam Practice

0.0(0)
studied byStudied by 0 people
0.0(0)
full-widthCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/182

encourage image

There's no tags or description

Looks like no tags are added yet.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

183 Terms

1
New cards

What is system-level file I/O in C?

Low-level file input/output that uses file descriptors and system calls like open, read, write, and close instead of fopenand fprintf.

2
New cards

What is a file descriptor?

A small integer returned by the OS that identifies an open file.0 = stdin, 1 = stdout, 2 = stderr, 3+ = user files.

3
New cards

What does the open() system call do?

Opens a file and returns a file descriptor.Example: int fd = open("file.txt", O_RDONLY);

4
New cards

What header files are needed for system I/O?

#include <fcntl.h>

#include <unistd.h>

5
New cards

What flags can be used with open()?

O_RDONLY - read only

O_WRONLY - write only

O_RDWR - read/write

O_CREAT - create file if it doesn't exist

O_TRUNC - truncate file to 0 bytes

6
New cards

What does the third parameter in open() mean (e.g., 0644)?

File permission bits (read/write for owner, read for group/others).

7
New cards

What does read() do?

Reads bytes from a file descriptor into a buffer.Example: read(fd, buffer, sizeof(buffer));

8
New cards

What does write() do?

Writes bytes from a buffer to a file descriptor.Example: write(fd, msg, strlen(msg));

9
New cards

What does close() do?

Closes an open file descriptor and frees it for reuse.Example: close(fd);

10
New cards

What does lseek() do?

Moves the file offset (like a cursor).Example: lseek(fd, 0, SEEK_SET); moves to the start.

11
New cards

What are SEEK_SET, SEEK_CUR, and SEEK_END used for?

Positioning the file pointer: beginning, current position, or end.

12
New cards

What happens if open() fails?

Returns -1, and errno is set to indicate the error.

13
New cards

What are some features with system i/o?

File descriptiors, non(unbuffered), slower for small ops, low-level OS work

14
New cards

What are some features with standard io?

FILE*, Bufferred, faster, regular file reading/writing

15
New cards

What is standard file I/O in C?

It's a higher-level way to handle files using the C standard library (`stdio.h`), providing buffering and functions like `fopen`, `fclose`, `fread`, `fprintf`, etc.

16
New cards

What header file is required for standard file I/O?

#include <stdio.h>

17
New cards

What is a FILE pointer in C?

A pointer to a structure (`FILE *`) that represents an open file and stores information about it (mode, buffer, position, etc.).

18
New cards

What are the common fopen() modes?

"r" = read, "w" = write (truncates), "a" = append, "r+" = read/update, "w+" = write/read (truncates), "a+" = append/read.

19
New cards

What does fclose() do?

Closes an open file. Example: fclose(fp);

20
New cards

What does fprintf() do?

Writes formatted text to a file (like printf but with a FILE). Example: fprintf(fp, "Age: %d\n", age);

21
New cards

What does fscanf() do?

Reads formatted input from a file (like scanf but with a FILE). Example: fscanf(fp, "%d", &x);

22
New cards

What does fgetc() do?

Reads a single character from a file. Returns the character (as int) or EOF.

23
New cards

What does fputc() do?

Writes a single character to a file. Example: fputc('A', fp);

24
New cards

What does fgets() do?

Reads a line (string) from a file until newline or EOF. Example: fgets(buffer, 100, fp);

25
New cards

What is EOF in C file I/O?

End Of File marker, returned by reading functions when no more data is available.

26
New cards

What does feof() do?

Checks if the end of file has been reached. Example: while(!feof(fp)) { ... }

27
New cards

What does fread() do?

Reads binary data from a file. Example: fread(buffer, size, count, fp);

28
New cards

What does fwrite() do?

Writes binary data to a file. Example: fwrite(buffer, size, count, fp);

29
New cards

What does fseek() do?

Moves the file pointer to a specific location in the file. Example: fseek(fp, 0, SEEK_SET);

30
New cards

What do SEEK_SET, SEEK_CUR, and SEEK_END mean?

Constants for fseek(): start of file, current position, and end of file.

31
New cards

What does ftell() do?

Returns the current position of the file pointer. Example: long pos = ftell(fp);

32
New cards

What does rewind() do?

Moves the file pointer back to the start of the file.

33
New cards

What's the difference between fopen and open?

fopen uses FILE* and buffering (high-level); open uses file descriptors (low-level, unbuffered).

34
New cards

What's the main advantage of standard I/O?

Automatic buffering and convenience for formatted text operations.

35
New cards

What's the main disadvantage of standard I/O?

Slower for small, low-level operations compared to unbuffered system I/O.

36
New cards

What are the main types of errors in C?

Syntax errors, runtime errors, and logical errors.

37
New cards

What is a syntax error?

An error in the structure or grammar of the code (e.g., missing semicolon, undeclared variable).

38
New cards

When are syntax errors detected?

At compile time — the compiler stops and reports the issue.

39
New cards

What is a runtime error?

An error that occurs while the program is running (e.g., dividing by zero, invalid memory access).

40
New cards

When are runtime errors detected?

During execution — the program compiles but crashes or behaves unexpectedly.

41
New cards

What is a logical error?

The program runs without crashing but produces incorrect results due to faulty logic.

42
New cards

When are logical errors detected?

By testing and debugging — the compiler won't catch them.

43
New cards

What are common runtime errors in C?

Segmentation fault, divide by zero, invalid pointer dereference, file not found, memory leaks.

44
New cards

What causes a segmentation fault?

Accessing memory that doesn't belong to your process (like dereferencing a NULL or invalid pointer).

45
New cards

What does errno represent in C?

A global variable set by system calls and library functions when an error occurs.

46
New cards

What header is needed to use errno?

#include

47
New cards

How can you print a human-readable error message from errno?

Use perror("message") or strerror(errno).

48
New cards

What does perror() do?

Prints an error message for the last system or library error to stderr.

49
New cards

Example of using perror()

FILE *fp = fopen("missing.txt", "r"); if (!fp) perror("File open failed");

50
New cards

What does strerror() do?

Converts an error code (like errno) into a human-readable string.

51
New cards

Example of using strerror()

FILE *fp = fopen("missing.txt", "r"); if (!fp) printf("Error: %s\n", strerror(errno));

52
New cards

What is debugging?

The process of finding and fixing errors or bugs in a program.

53
New cards

What tool is commonly used to debug C programs?

gdb (GNU Debugger)

54
New cards

What compiler flag should you use to enable debugging info?

`-g` flag in gcc (example: gcc -g program.c -o program)

55
New cards

How do you start debugging with gdb?

`gdb ./program`

56
New cards

What command in gdb runs the program?

run

57
New cards

What does a breakpoint do in gdb?

Stops execution at a specific line or function to inspect program state.

58
New cards

How to set a breakpoint in gdb?

break or break

59
New cards

How to view variable values in gdb?

print

60
New cards

How to step line by line in gdb?

step (enters functions) or next (skips over functions)

61
New cards

What does backtrace show in gdb?

Shows the call stack (which functions were called before a crash).

62
New cards

How can you check for memory leaks in C?

Use Valgrind: `valgrind ./program`

63
New cards

What does Valgrind do?

Detects memory leaks, invalid reads/writes, and uninitialized memory usage.

64
New cards

How can you use compiler warnings to find bugs?

Compile with flags like `-Wall -Wextra` to show more potential issues.

65
New cards

What's the difference between a warning and an error?

Warnings don't stop compilation but suggest potential issues; errors stop the compiler.

66
New cards

What is assert() used for?

Used to test assumptions in code; if false, it stops the program.

67
New cards

What header is needed for assert()?

#include

68
New cards

Example of using assert()

int x = 5; assert(x > 0);

69
New cards

Why is assert() helpful?

Helps catch logic errors early during development and testing.

70
New cards

What are the main memory areas used by a C program?

Code (text) segment, data segment, heap, and stack.

71
New cards

What is stored in the code (text) segment?

The compiled machine instructions (the actual program code).

72
New cards

What is stored in the data segment?

Global and static variables — split into initialized and uninitialized parts.

73
New cards

What is the heap used for?

Dynamically allocated memory (allocated at runtime using malloc, calloc, realloc).

74
New cards

What is the stack used for?

Function calls, local variables, and function parameters — grows and shrinks as functions are called and return.

75
New cards

When is memory allocated on the stack?

Automatically when a function is called.

76
New cards

When is memory released from the stack?

Automatically when the function returns.

77
New cards

When is heap memory allocated?

At runtime, manually using functions like malloc().

78
New cards

When must heap memory be freed?

Manually using free() to prevent memory leaks.

79
New cards

What is static memory allocation?

Memory is assigned at compile time (e.g., global/static variables).

80
New cards

What is dynamic memory allocation?

Memory is assigned at runtime (e.g., malloc, calloc, realloc).

81
New cards

What header file is required for dynamic memory allocation?

#include

82
New cards

What does malloc() do?

Allocates a block of memory of a given size and returns a pointer to it.

83
New cards

Example of malloc() usage

int arr = (int) malloc(5 * sizeof(int));

84
New cards

What does calloc() do?

Allocates memory for multiple elements and initializes them to zero.

85
New cards

Example of calloc() usage

int arr = (int) calloc(5, sizeof(int));

86
New cards

What does realloc() do?

Resizes an existing memory block previously allocated by malloc or calloc.

87
New cards

Example of realloc() usage

arr = realloc(arr, 10 * sizeof(int));

88
New cards

What does free() do?

Deallocates memory previously allocated on the heap to prevent memory leaks.

89
New cards

Example of freeing memory

free(arr);

90
New cards

What is a memory leak?

When allocated memory is never freed, causing loss of available memory over time.

91
New cards

What is a dangling pointer?

A pointer that refers to memory that has been freed or is invalid.

92
New cards

How to avoid dangling pointers?

Set pointers to NULL after freeing memory.

93
New cards

What is a NULL pointer?

A pointer that doesn't point to any valid memory address (used to indicate "nothing").

94
New cards

What happens if you dereference a NULL pointer?

Usually causes a segmentation fault (crash).

95
New cards

What are segmentation faults caused by?

Accessing invalid memory (like dereferencing a NULL or uninitialized pointer).

96
New cards

What is pointer arithmetic?

Adding or subtracting integers from pointers to navigate through memory.

97
New cards

What is sizeof used for in memory management?

Returns the number of bytes required for a data type or variable.

98
New cards

Example of sizeof usage

printf("%zu", sizeof(int));

99
New cards

What is the lifetime of a stack variable?

Exists only while the function it's in is executing.

100
New cards

What is the lifetime of a static variable?

Exists for the entire duration of the program (even if declared inside a function).