[Tex/LaTex] Gantt Chart for CPU Scheduling

pgfgantttables

What would be the best way to draw something like this?

Gantt Chart for CPU Scheduling

I'm not sure if I should use pdfgantt package, or if I should do this like a single row table. Please advise.

Best Answer

For a simple diagram like this, you can just code it up easily in TikZ. Note that if you want to make more complicated diagrams, I would look into automating some of these aspects, but here's a start nonetheless.

\documentclass[tikz, border=10pt]{standalone}

\usepackage{tikz}
\usetikzlibrary{positioning}
\tikzset{
  gray box/.style={
    fill=gray!20,
    draw=gray,
    minimum width={2*#1ex},
    minimum height={2em},
  },
  annotation/.style={
    anchor=north,
  }
}
\begin{document}
\begin{tikzpicture}[node distance=-0.5pt]
  \node [gray box=24] (p1) {\(P_{1}\)};
  \node [gray box=3, right=of p1] (p2) {\(P_{2}\)};
  \node [gray box=3, right=of p2] (p3) {\(P_{3}\)};

  \node [annotation] at (p1.south west) {0};
  \node [annotation] at (p1.south east) {24};
  \node [annotation] at (p2.south east) {27};
  \node [annotation] at (p3.south east) {30};
\end{tikzpicture}
\end{document}

output