Flash cards

Here is your comprehensive, consolidated master study guide covering compilers/Flex, TCP sockets, system calls, and byte-ordering wildcards.

Part 1: Flex & Lexical Analysis Functions

Lexical analysis is the initial phase of a compiler, breaking an input character stream into a sequence of tokens.

Functions

yylex()
  • Definition: The primary scanner function generated by Flex that reads input characters and converts them into tokens.

  • Parameters: void (takes no arguments).

  • Return Value: Returns an int representing the token type found, or 0 on End-of-File (EOF).

  • Caller: Called by the driver program / main compiler loop.

symlook(char *s)
  • Definition: Searches a symbol table for a given identifier string; if not found, it inserts a new entry into the table.

  • Parameters:

    • char *s: Pointer to the identifier string name.

  • Return Value: struct symtab * (pointer to the entry in the symbol table).

  • Caller: Called internally by yylex() or the main parser when an identifier token is scanned.

addfunc(char *name, double (*func)())
  • Definition: Registers and binds a function pointer to a specific identifier name in the symbol table.

  • Parameters:

    • char *name: Identifier string name for the function.

    • double (*func)(): Function pointer to the C implementation function.

  • Return Value: void

  • Caller: Called during symbol table initialization before parsing begins.

yyerror(char *s)
  • Definition: A standard error reporting routine invoked when a syntax or scanning error is encountered.

  • Parameters:

    • char *s: Pointer to the error message string.

  • Return Value: int or void

  • Caller: Called by the scanner/parser when an unexpected sequence occurs.

Part 2: TCP Socket Communication Functions

Sockets provide endpoints for network communication using TCP (SOCK_STREAM).

Functions

socket(int domain, int type, int protocol)
  • Definition: Creates an endpoint for network communication and returns a file descriptor.

  • Parameters:

    • int domain: Protocol family (e.g., AF_INET for IPv4, AF_UNIX for local sockets).

    • int type: Socket semantics type (e.g., SOCK_STREAM for TCP).

    • int protocol: Specific protocol number (typically 0 for default).

  • Return Value: Returns a new socket file descriptor on success, or -1 on error.

  • Caller: Called by both Server and Client.

setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen)
  • Definition: Manipulates configuration options on a socket, such as allowing local address reuse.

  • Parameters:

    • int sockfd: The socket descriptor.

    • int level: Protocol layer level (typically SOL_SOCKET).

    • int optname: Socket option name (e.g., SO_REUSEADDR).

    • const void *optval: Pointer to memory containing option setting.

    • socklen_t optlen: Size of option buffer.

  • Return Value: Returns 0 on success, -1 on error.

  • Caller: Typically called by the Server before binding.

bind(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
  • Definition: Assigns a local IP address and port number to a socket.

  • Parameters:

    • int sockfd: The socket descriptor.

    • const struct sockaddr *addr: Pointer to target address structure.

    • socklen_t addrlen: Size of the address structure.

  • Return Value: Returns 0 on success, -1 on error.

  • Caller: Called by the Server.

listen(int sockfd, int backlog)
  • Definition: Marks the socket as passive, ready to accept incoming client requests.

  • Parameters:

    • int sockfd: The bound socket descriptor.

    • int backlog: Maximum length of pending incoming connections queue.

  • Return Value: Returns 0 on success, -1 on error.

  • Caller: Called by the Server.

accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
  • Definition: Extracts the first connection request from the queue and creates a new connected socket descriptor.

  • Parameters:

    • int sockfd: Listening socket descriptor.

    • struct sockaddr *addr: Pointer to structure that receives client's IP/port details.

    • socklen_t *addrlen: Pointer to variable holding length of address structure.

  • Return Value: Returns a new connected socket descriptor on success, -1 on error.

  • Caller: Called by the Server.

connect(int sockfd, const struct sockaddr *addr, socklen_t addrlen)
  • Definition: Initiates a TCP connection request to a remote listening server socket.

  • Parameters:

    • int sockfd: Client's socket descriptor.

    • const struct sockaddr *addr: Server's destination address structure.

    • socklen_t addrlen: Size of destination address structure.

  • Return Value: Returns 0 on success, -1 on error.

  • Caller: Called by the Client.

read(int fd, void *buf, size_t count)
  • Definition: Reads data from a file descriptor or connected socket into a memory buffer.

  • Parameters:

    • int fd: Open socket/file descriptor.

    • void *buf: Pointer to target buffer.

    • size_t count: Maximum number of bytes to read.

  • Return Value: Returns number of bytes read, 0 on connection close / EOF, -1 on error.

  • Caller: Called by both Server and Client.

write(int fd, const void *buf, size_t count)
  • Definition: Writes data from a memory buffer to a file descriptor or connected socket.

  • Parameters:

    • int fd: Open socket/file descriptor.

    • const void *buf: Pointer to source data buffer.

    • size_t count: Number of bytes to send.

  • Return Value: Returns number of bytes written, -1 on error.

  • Caller: Called by both Server and Client.

Part 3: Process Control & I/O Redirection System Calls

System calls used for executing programs, duplicating file descriptors, and establishing pipes between processes.

Functions

open(const char *pathname, int flags)
  • Definition: Opens a file and returns a new file descriptor.

  • Parameters:

    • const char *pathname: Path to target file.

    • int flags: Open options (e.g., O_RDONLY, O_WRONLY, O_CREAT).

  • Return Value: Returns new file descriptor integer on success, -1 on error.

  • Caller: Called by any process (Server, Client, or Standalone).

dup2(int oldfd, int newfd)
  • Definition: Duplicates oldfd onto newfd, redirecting input or output streams.

  • Parameters:

    • int oldfd: Existing open file descriptor.

    • int newfd: File descriptor number to overwrite (e.g., 1 for standard output).

  • Return Value: Returns new file descriptor on success, -1 on error.

  • Caller: Called by process setting up I/O redirection.

pipe(int pipefd[2])
  • Definition: Creates a unidirectional data channel for interprocess communication.

  • Parameters:

    • int pipefd[2]: Array of 2 integers; pipefd[0] is read end, pipefd[1] is write end.

  • Return Value: Returns 0 on success, -1 on error.

  • Caller: Called by parent process prior to fork().

fork(void)
  • Definition: Creates a exact duplicate child process.

  • Parameters: void

  • Return Value: Returns 0 inside child process, child's PID inside parent process, -1 on error.

  • Caller: Called by parent process.

execvp(const char *file, char *const argv[])
  • Definition: Replaces current process image with a new executable program.

  • Parameters:

    • const char *file: File/command name.

    • char *const argv[]: Array of NULL-terminated string arguments.

  • Return Value: Does not return on success; returns -1 on failure.

  • Caller: Typically called by child process after fork().

wait(int *status)
  • Definition: Suspends execution of calling process until one of its child processes terminates.

  • Parameters:

    • int *status: Pointer to integer where exit status is saved.

  • Return Value: Returns PID of exited child, -1 on error.

  • Caller: Called by parent process.

close(int fd)
  • Definition: Closes an open file descriptor, releasing system resource handles.

  • Parameters:

    • int fd: Descriptor to close.

  • Return Value: Returns 0 on success, -1 on error.

  • Caller: Called by both Server and Client (or any process).

Part 4: Networking Constants, Wildcards & Byte-Order Utilities

Address Family Constants

  • AF_INET: Constant specifying the IPv4 protocol family (e.g., 192.168.1.1).

  • AF_UNIX: Constant specifying local filesystem communication sockets (UNIX domain sockets) on the local host.

Address Field Wildcards & Structures

  • serv_addr.sin_family: Member of struct sockaddr_in that stores the designated address family (e.g., AF_INET).

  • serv_addr.sin_addr: Member structure inside sockaddr_in containing the 32-bit IP address field (s_addr).

  • INADDR_ANY: Wildcard IP address constant. When assigned to serv_addr.sin_addr.s_addr, it instructs a server to bind and listen across all available local network interfaces (Ethernet, Wi-Fi, loopback/127.0.0.1) simultaneously instead of binding to a single specific IP address.

Network Byte-Order Functions

Network protocols strictly require big-endian format (Network Byte Order).

  • htonl(uint32_t hostlong): Host To Network Long. Converts a 32-bit integer (typically an IPv4 address like INADDR_ANY) from the host's native byte order to Network Byte Order.

  • htons(uint16_t hostshort): Host To Network Short. Converts a 16-bit integer (typically a port number like 8080) from host native byte order to Network Byte Order.