|
CS456 - Systems Programming
Spring 2025
|
Displaying ./code/midterm.txt
CS 456/556 Midterm - Spring 2025 Solutions
1. The only directories actually required by UNIX are:
Answer: /dev and /tmp
2. All the values in the argv array are passed in as strings
Answer: True
3. File descriptor for stderr is:
Answer: 2
4. What value does the close system call return on success?
Answer: 0
5. Give me a -single line of code- using a system call that opens "text.txt" for reading only and
stores the return value in an integer variable called fd:
Answer: int fd = open("text.txt", O_RDONLY);
6. Consider the following lines of code
char text[BUF];
int buffer;
while((buffer = read(fd, text, BUF)) > 0){
write(1, text, buffer);
}
Tell me what is happening here.
Answer:
Something along the lines of:
The contents of a file represented by the file descriptor fd are being read into the
charcter array "text". The amount of data read was stored in "buffer". While data is being
read the contents of "text" are being outputted to the screen.
Note: There was a typo in the write function where it originally said "data" instead of
text. It has been corrected here
7. System calls are functions provided by the kernel.
Answer: True
8. Fill in the blank: Each file in a filesystem has a unique [inode] number.
9. Which value in the stat structure contains the the time of the last access of file data?
Answer: st_atime
10. When you print a value for time from the stat structure, you may see a value like 1740775853,
what does this value mean?
Answer: Unix Time, number of seconds since 1 Jan 1970
11. The file mode is generally represented by what type of number?
Answer: octal
12. The rightmost three digits of the mode of a file represent:
Answer: file permissions
13. Consider the following lines of code:
int rt;
rt = stat(argv[1], &st);
if(S_ISLNK(mode))
printf("This is a Symbolic Link\n");
else
printf("This is NOT a Symbolic Link\n");
Suppose somebody asks you for help, saying that whenever a symbolic link is passed in, the
program prints that it isn't a link. Why is this happening?
Answer: Should use lstat instead of stat
From man 2 stat: lstat() is identical to stat(), except that if pathname is a symbolic
link, then it returns information about the link itself, not the file that it refers to
14. I run ls -l on a directory and see the following output:
-rwxr-xr-x 1 cs456 class 596 Feb 19 15:13 h2report.sh*
-rw-r--r-- 1 cs456 class 9753 Feb 19 15:13 h2report.txt
-rwxr-xr-x 1 cs456 class 2184 Feb 14 09:19 h2.sh*
-rwxr-xr-x 1 cs456 class 665 Feb 19 11:04 h2test.sh*
drwxr-xr-x 2 cs456 class 4096 Feb 19 10:27 test/
lrwxrwxrwx 1 cs456 class 13 Feb 19 10:28 testlink -> test/file.txt
What would the octal value for st_mode be for the following files? (put answer in blank)
- test : [ 40755]
- h2.sh : [100755]
- h2report.txt : [100644]
- testlink : [120777]
15. Apart from init, the only way to create new processes is through the fork system call
Answer: True
16. Consider the following line of code:
while (1) fork();
What is this an example of, and why shouldn't you ever run this code?
Answer: Something along the lines of:
This is a fork bomb, and this will attempt to create an infinite number of process.
The number doubling with every iteration of the loop. Leading to a system crash.
17. Fill in the blank: To change the current process to a new one, you need to use a variant of the
[exec] function.
18. In any of the variants of exec if the function is sucessful, what value does it return?
Answer: does not return a value
19. Which of these does the child NOT inherit from the parent?
A. Open file handles
B. Signal handlers
(C. Memory Locks)
D. Current working directory
E. Environment variables
F. None of the Above
20. The [wait] system call suspends execution of a process until the child returns.
21. Hitting CTRL-C on your keyboard sends what signal?
Answer: SIGINT
22. What signal can you send to temporarily pause a running process?
Answer: SIGSTOP
23. The parent process will get a SIGCHILD signal from every child that finishes
Answer: True
24. How would you know if fork and exec have failed?
Answer: These functions return -1 if they fail
25. A '.' in the first character of a filename indicates a [hidden] file.
26. In the glibc implementation, the dirent structure is defined as follows:
struct dirent {
ino_t d_ino; /* Inode number */
off_t d_off; /* Not an offset; see below */
unsigned short d_reclen; /* Length of this record */
unsigned char d_type; /* Type of file; not supported
by all filesystem types */
char d_name[256]; /* Null-terminated filename */
};
Not all of these fields are mandated. out of these fields, which ones are mandated by POSIX.1?
Answer: d_name and d_ino
27. I tried to open a directory, but opendir failed. I looked at the value of errno, and I see a
value of EACCES. What is the issue here?
Answer: permission denied
28. If opendir fails because the directory doesn't exist, what value will errno be set to?
Answer: ENOENT
29. Which header files do I need to include if i want to run opendir, readdir, and closedir
Answer: sys/types.h and dirent.h
30. Here, have a free point, The exam is finally over.
(A. Sure, I'll take it!)
B. No thanks, I don't want it
|