E N D
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
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
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
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
File Concept in UNIX • File is a sequence of bytes • No special file structure enforced • Kernel manages file metadata • Applications control data interpretation
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);
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
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
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
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
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);
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
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
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
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);
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>
Pipes in UNIX • • Used for inter-process communication • • Output of one command becomes input of another • • Uses pipe operator | • • Example: ls | grep txt
Advantages of UNIX I/O System • • Simple and consistent design • • Highly portable • • Efficient and flexible • • Supports powerful command chaining
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