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: 3742053 • 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)

1. Create a new source file named task4.c. Add a main function with argc/argv parameters and #include the following libraries: o stdio.h o stdlib.h o sys/stat.h o unistd.h 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; I 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 argc/argv 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 Usage: /task4

Explanation / Answer

#include<fcntl.h>

#include<stdio.h>

#include<stdlib.h>

#include<sys/stat.h>

#include<unistd.h>

#define BUFFER_SIZE 1000 //Preprocessor macro

int main(int argc, char *argv[]){ //main function with argument

int fd,sz;

char *buffer;

if(argc != 2){

return 0;

}

fd= fopen(argv[1], O_RDONLY); //to open the input file in read only mode

buffer=(char*) malloc(BUFFER_SIZE * sizeof(char)); //allocate memory using malloc

if (buffer==NULL){ //check if memory allocation is successful

return 0;

}

sz=read(fd,buffer,BUFFER_SIZE); //read the content of the file in buffer

buffer[sz]= '';

printf(' %s',buffer); //write content of buffer to stdout

free(buffer); //deallocate buffer memory

close(fd); //close file

return 1;

}

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