Question

In Unix/Linux, input and output are treated as files and referenced by the operating system using file descriptors. When youDeallocate the buffer memory block and close the file using the close function. Make use of Valgrind if 8. necessary to confi

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 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. Add a main function with argc/argv parameters and #include the following libraries fcntl.h stdio.h . stdlib.h .sys/stat.h 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; // 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.
Deallocate the buffer memory block and close the file using the close function. Make use of Valgrind if 8. necessary to confirm you have no leaks. Compile the program using make/gcc. Run the program with the deadlock file (available on FLO) as its 9. first and only argument. The expected output is as follows (Valgrind output provided for demonstration): $ ./task4 Usage: -/task4 $ ./task4 deadlock 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) 3099HEAP SUMMARY: in use at exit: 0 bytes in blocks 3099 3099total heap usage: 1 allocs, 1 frees,1,000 bytes 3099 3099All heap blocks were freed -- no leaks are possible allocated
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Program
#include <stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<sys/stat.h>
#include<stdlib.h>

int main(int argc, char **argv)
{
int fd;
char *buffer;
char ch;
int i=0,ret;
  
//check if filename is given as argument to this program, otherwise print message and exit
if(argc < 2)
{
printf("Usage: %s filename\n",argv[0]);
exit(-1);
}
//allocate memory for bufffer
buffer=(char*)malloc(100*sizeof(char));
//open file for reading
fd=open(argv[1],O_RDONLY);
if(fd < 0)
{
perror("read error: ");
exit(-2);
}
//read from file till EOF
while(read(fd,&ch,1 > 0))
{
if(ch == EOF)
break;
if(ch == '\n')
{
buffer[i]='\0';
printf("%s\n",buffer);
i=0;
}
buffer[i++]=ch;
}
buffer[i]='\0';
printf("%s\n",buffer);
return 0;
}
===============================
//output
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

Add a comment
Know the answer?
Add Answer to:
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 st...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • UNIX is all about manipulating files and input/output streams fluidly, so it is important to get a strong grasp of how...

    UNIX is all about manipulating files and input/output streams fluidly, so it is important to get a strong grasp of how this fundamentally works at the system call level to understand higher-level system programming concepts. Every program automatically has three file descriptors opened by the shell standard input standard output standard error 1 2 One can use read and write other open file. Normally, standard input and output on the terminal are line-buffered, so, for example, the specified number of...

  • Purpose This assignment should give you experience in using file descriptors, open(), close(), wr...

    Purpose This assignment should give you experience in using file descriptors, open(), close(), write(), stat() and chmod(), perror(), and command line arguments. Program Write a C++ program that will allow you to add messages to a file that has NO permissions for any user. A Unix system has many files that have sensitive information in them. Permissions help keep these files secure. Some files can be publicly read, but can not be altered by a regular user (ex.: /etc/passwd). Other...

  • Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue....

    Using C++ in Visual Studios Rewrite the code to use a Stack instead of a Queue. You will need to think about the differences in the Stack and Queue. Think about how they operate and how the nodes may differ.   Modify the code as necessary. You will need to push to the Stack, pop from the Stack, peek at the Stack. You will need a member functions like in the example code. Your program will need to show that everything...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT