Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Deadlock occurs when all of the following apply: 1. Mutual exclusion condition 2

ID: 3741847 • Letter: D

Question

Deadlock occurs when all of the following apply:
1. Mutual exclusion condition
2. Hold and wait condition
3. No pre-emption condition
4. Circular wait condition

Lecture5.pdf (2018)

In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When you open a shell session, for example, three file descriptors are in use 0. standard input (stdin) 1. standard output (stdout) 2 standard error (stderr) By default, the command interpreter (shell) reads keyboard input from file descriptor 0 (stdin) and writes output to file descriptor 1 (stdout), which appears on the screen. As you explored in Lab 2, input/output can be redirected such that the operating system modifies the file descriptors to point to alternative, named files. In this task, you will explore the use of file descriptors to read a file and print it to stdout using the system functions open, read, and close. Each of these has a man page accessible by typing man 2 and the function name. You are advised to find out more about how these functions work before beginning 1. Create a new source file named task4.c. Add a main function with argc/argv parameters and #include the following libraries: o fcntl.h o stdio.h o stdlib.h o sys/stat.h o unistdh Some of these should be familiar as we've used them many times. The unfamiliar ones are necessary for access to the aforementioned system functions. 2. Define a preprocessor macro named BUFFER SIZE. Assign it the value 1000 3. Inside main, declare the following two variables: int fd; // file descriptor char buffer 4. Use the open function to open the input file provided to the program as an argument. You only need to read the file, so the open function's access mode flags should reflect this. To retrieve the filename, use getopt with a suitable option (e.g. -f filename) or manually parse the argclargv values 5. Use malloc to dynamically allocate a block of memory sized BUFFER SIZE. The memory block should be assigned to the buffer pointer created in step 3. 6. If the memory was successfully allocated, read the contents of the file into it using the read function (take note of the function's return value) 7. Write the contents of the buffer, line by line, to stdout. 8. Deallocate the buffer memory block and close the file using the close function. Make use of Valgrind if necessary to confirm you have no leaks. 9. Compile the program using make/gcc. Run the program with the deadlock file (available on FLO) as its first and only argument. The expected output is as follows (Valgrind output provided for demonstration):

Explanation / Answer

Deadlock occurs when all of the following apply Hold and wait condition because if memory is allocated successfully then only it will read the file otherwise it will wait to complete the memory allocation(malloc).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote