#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
int main() {
int fd;
char buffer[100];
ssize_t bytes_leidos, bytes_escritos;
printf("=== DEMOSTRACIÓN: Gestión de Dispositivos ===\n\n");
//
=========================================================
===
// 1. INTERACCIÓN CON DISPOSITIVO DE ENTRADA: TECLADO (/dev/tty)
//
=========================================================
===
printf("1. Abriendo dispositivo de terminal (teclado/pantalla)...\n");
// open() - SYSCALL: Abre el dispositivo de terminal actual
// En Linux, todo es archivo. /dev/tty representa la terminal actual
fd = open("/dev/tty", O_RDWR);
if (fd == -1) {
perror("Error al abrir /dev/tty");
return 1;
}
printf(" Dispositivo abierto. Descriptor de archivo: %d\n\n", fd);
✓
//
=========================================================
===
// 2. ESCRITURA EN DISPOSITIVO DE SALIDA: PANTALLA
//
=========================================================
===
printf("2. Escribiendo en dispositivo de salida...\n");
const char *mensaje = " Mensaje escrito directamente al dispositivo!\n";
✓
// write() - SYSCALL: Escribe bytes al dispositivo
// Equivalente a WriteConsole() en Windows
bytes_escritos = write(fd, mensaje, strlen(mensaje));
if (bytes_escritos == -1) {
perror("Error en write");
close(fd);
return 1;
}
printf(" Bytes escritos: %zd\n\n", bytes_escritos);
//
=========================================================
===
// 3. LECTURA DESDE DISPOSITIVO DE ENTRADA: TECLADO
//
=========================================================
===
printf("3. Leyendo desde dispositivo de entrada (teclado)...\n");
printf(" Escribe algo y presiona ENTER: ");
fflush(stdout);
// read() - SYSCALL: Lee bytes desde el dispositivo
// Equivalente a ReadConsole() en Windows
bytes_leidos = read(fd, buffer, sizeof(buffer) - 1);
if (bytes_leidos == -1) {
perror("Error en read");
close(fd);
return 1;
}
// Eliminar el salto de línea
buffer[bytes_leidos] = '\0';
if (buffer[bytes_leidos - 1] == '\n') {
buffer[bytes_leidos - 1] = '\0';
}
printf(" Bytes leídos: %zd\n", bytes_leidos);
✓
printf(" Texto ingresado: '%s'\n\n", buffer);
✓
//
=========================================================
===
// 4. INTERACCIÓN CON OTRO DISPOSITIVO: NULL (descarte)
//
=========================================================
===
printf("4. Demostración con dispositivo /dev/null (agujero negro)...\n");
int fd_null = open("/dev/null", O_WRONLY);
if (fd_null != -1) {
const char *datos = "Este mensaje desaparece...";
write(fd_null, datos, strlen(datos));
printf(" Datos escritos a /dev/null (se descartan silenciosamente)\n");
✓
close(fd_null); // SYSCALL: Cierra el dispositivo
}
printf("\n");
//
=========================================================
===
// 5. CIERRE DEL DISPOSITIVO
//
=========================================================
===
printf("5. Cerrando dispositivo de terminal...\n");
// close() - SYSCALL: Cierra el descriptor de archivo
// Libera recursos del kernel asociados al dispositivo
if (close(fd) == -1) {
perror("Error al cerrar");
return 1;
}
printf(" Dispositivo cerrado correctamente\n\n");
✓
//
=========================================================
===
// RESUMEN DE SYSCALLS USADAS
//
=========================================================
===
printf("=== SYSTEM CALLS UTILIZADAS ===\n");
printf("┌─────────────┬───────────────────────────────────
──────┐\n");
printf("│ Syscall │ Función │\n");
printf("├─────────────┼───────────────────────────────────
──────┤\n");
printf("│ open() │ Abrir dispositivo/archivo │\n");
printf("│ read() │ Leer desde dispositivo de entrada │\n");
printf("│ write() │ Escribir a dispositivo de salida │\n");
printf("│ close() │ Cerrar dispositivo, liberar recursos │\n");
printf("└─────────────┴───────────────────────────────────
──────┘\n");
return 0;
}