1 / 19

UNIX Input Output System

UNIX Input Output System

Ayan22
Download Presentation

UNIX Input Output System

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. UNIX Input Output System---An Approach Prepared By : Ayan Banerjee Master Of Technology-Computer Science • UNIX provides a simple , uniform and powerful I/O model • All devices and files are treated uniformly as file • Input and Output is handled using file descriptors • This presentation explains UNIX input–output mechanisms and details explanation

  2. Introduction to UNIX I/O • Input and Output is a main component of UNIX OS • Input and Outputallows programs to communicate with files and devices • UNIX isdesigned for simplicity and flexibility • UNIX supports redirection and inter-process communication

  3. UNIX I/O Philosophy • In UNIX everything, I/O device , physical file , directory is treated as a file • Uniform interface for devices and regular files • Device independence , means no device work depending other device. • Simple system calls form the foundation

  4. Types of I/O in UNIX • File I/O – reading and writing files.Physical file like abcd.txt , abcd.xls • Device I/O – keyboard, mouse, printer,usb device • Inter-process I/O – pipes and sockets ls | grep ".txt , output of ls is passed as input to grep using a pipe • Network I/O – communication over network curl https://example.com--command sends a network request and receives data from a remote server

  5. File Concept in UNIX • File is a sequence of bytes • No special file structure enforced • Kernel manages file metadata • Applications control data interpretation

  6. File Descriptors • Integer values used to identify open files Example : fd = open("data.txt", O_RDONLY); • Managed by the kernel Example:lsof -p <pid> (show descriptors for a process) • Used in all I/O system calls Example:read(3, buffer, 100); • Enables process-level I/O abstraction Example :write(1, "Hello\n", 6);

  7. Standard File Descriptors • • File Descriptor 0 → Standard Input (stdin) • • File Descriptor 1 → Standard Output (stdout) • • File Descriptor 2 → Standard Error (stderr) • • Automatically assigned at process start

  8. Standard Input (stdin) • Default input from keyboard Example:cat(waits for keyboard input) • Can be redirected from files Example:sort < input.txt • Used by commands like read, scanf read name (reads input from stdin) • Example: command < input.txt

  9. Standard Output (stdout) • Default output to terminal screen Example:echo "Hello UNIX" • Can be redirected to files Example:ls > files.txt • Buffered output stream Example:printf "Data buffered\n" • Example: command > output.txt

  10. Standard Error (stderr) • Used for error messages Example:ls nofile.txt • Not buffered by default Example:cat nofile.txt 2>/dev/null • Helps separate error output Example:ls file.txt nofile.txt > out.txt 2> err.txt • Example: command 2> error.txt

  11. UNIX I/O System Calls • open() – open a file or device Example:int fd = open("data.txt", O_RDONLY); • read() – read data from file Example:read(fd, buffer, 100); • write() – write data to file Example:write(fd, "Hello UNIX", 10); • close() – close the file descriptor Example:close(fd);

  12. open() System Call • • Opens an existing file or creates a new file • • Requires file name and access mode • • Returns a file descriptor • • Failure returns -1

  13. read() System Call • Reads data from file descriptor Example:read(fd, buffer, 100); • Transfers data into buffer • Returns number of bytes read • Returns 0 at end-of-file

  14. write() System Call • Writes data from buffer to file Example:int n = write(fd, buffer, 100); • Uses file descriptor • Returns number of bytes written • Error returns -1

  15. close() System Call • Releases file descriptor Example:close(fd); /* kernel releases file resources */ • Frees kernel resources • Important to avoid resource leaks Example:if(fd != -1) close(fd); • Called after I/O completion Example:read(fd, buf, 100); close(fd);

  16. Redirection in UNIX • Changes source or destination of I/O Example:sort < data.txt > sorted.txt • Input redirection using < Example:wc -l < input.txt • Output redirection using > Example:ls > files.txt • Error redirection using 2>

  17. Pipes in UNIX • • Used for inter-process communication • • Output of one command becomes input of another • • Uses pipe operator | • • Example: ls | grep txt

  18. Advantages of UNIX I/O System • • Simple and consistent design • • Highly portable • • Efficient and flexible • • Supports powerful command chaining

  19. Conclusion • • UNIX I/O system is a fundamental OS concept • • File descriptor-based approach is powerful • • Enables redirection and pipelines • • Forms the basis of UNIX programming

More Related