Operating Systems Kernel, Processes, Sockets, Pipes and Signals Explained

Posted by Comunidad Wikiteka and classified in Computers

Written on in English with a size of 3.77 KB

Kernel Fundamentals

Kernel = FLIH (interrupt controller) + dispatcher + communication, synchronization and mutual exclusion.

Process States

States: Ready, Running, Blocked

Transitions:

  • Creation → Ready (OS creates it)
  • Ready → Running (dispatcher gives it the CPU)
  • Running → Ready (time slice ends)
  • Running → Blocked (waits for I/O)
  • Blocked → Ready (awaited event happens)
  • Running → Finished

Process Control Block (PCB)

PCB: PID, state, context (PC, flags, general registers, memory access registers) + scheduling info (priority).

Interrupt Handling

FLIH: store context → find which device interrupted (poll devices by priority) → run ISR.

Chain: hardware stores PC → FLIH → ISR → dispatcher

The Dispatcher

Dispatcher: called after an interrupt changes a process state. Is the current process still the best?

  • ✅ Yes → continue.
  • 🚫 No → save its context → load the new process's context from its PCB → give it the CPU.

Context Switch

A runs → timer interrupt pushes flags, IP, CS → ISR pushes registers → dispatcher saves A's SS/SP, loads B's → ISR pops B's registers → reti restores B's flags, IP, CS → B runs.

Needs: users can't enable/disable interrupts + periodic timer.

Network Programming and Inter-Process Communication

select()

select(): waits on inputs, no threads/processes needed. fd_set max 1024 fds. FD_ZERO empties · FD_SET adds · FD_CLR removes · FD_ISSET tests.

Sockets

Sockets: IPC, local/remote, IP/port, are file descriptors.

  • TCP (SOCK_STREAM)
  • UDP (SOCK_DGRAM)

Server and Client Flow

Server: socket → bind → listen → accept.
Client: socket → connect.

  • accept() blocks and returns a NEW fd.
  • bind() returns -1 if the port is busy.
  • listen(backlog) sets the maximum connections.
  • htons() converts the port to Big Endian.
  • inet_pton converts an IP to binary.
  • For a local server, use INADDR_ANY.
  • read() returning 0 means the connection is closed.
  • On a partial read(), read the rest (total − bytesRead).

Pipes, Exec and Fork

Pipes

Pipes: fd[0] reads, fd[1] writes. Raw bytes, destructive blocking reads, one-way, 64 KB.

  • Close unused ends (a reader holding the write end never gets EOF).
  • dup2(pipefd[1], 1) sends stdout into the pipe.

exec and fork

exec: replaces the program, creates no new process, and returns only on failure (-1).

fork(): -1 on error · 0 in the child · child PID in the parent. Use wait() for the child; parent and child share the file offset.

Threads and Signals

Threads

Threads share memory and resources (efficient, but risky).

  • pthread_create → pthread_join (like wait)
  • If main dies, all threads die.
  • Compile with -lpthread.

Signals

Signals:

  • signal(sig, handler)
  • kill(pid, sig)
  • raise(sig)
  • pause() waits for a signal
  • signal(SIGINT, SIG_DFL) inside a handler works only once