7

I am trying to create a gantt chart for my thesis on blocking analysis. I know of the pgfgantt package for generating gantt charts. But this package generates charts for normal schedules, what I need is gantt charts specifically for blocking analysis, like those used in Operating Systems text books.

Here is an example of what I need:

Gantt Chart for CPU scheduling

How can I make a chart similar to this one using pdflatex? I don't mind using pgfgantt either, except I don't see how it can render something like this.

6
  • It's not that hard to do it by hand with TikZ. However, if you're going to do a lot of them it could be tedious. Do you need a package like pgfgantt or you may give it a try to make it by hand?
    – Nico
    Commented Feb 7, 2014 at 14:03
  • 1
    Welcome to TeX.SX. Questions about how to draw specific graphics that just post an image of the desired result are really not reasonable questions to ask on the site. Please post a minimal compilable document showing that you've tried to produce the image and then people will be happy to help you with any specific problems you may have. See minimal working example (MWE) for what needs to go into such a document. Commented Feb 7, 2014 at 14:09
  • 1
    @nico: My problem with pgfgantt is I couldn't find anything in the documentation which would allow diagrams with blocking, preemption, etc to be drawn. I may give it a try by hand using TikZ but as you say it will be tedious since I will have loads of such charts to draw.
    – darnir
    Commented Feb 7, 2014 at 15:17
  • @MartinSchröder: Thanks! But my problem is that I have no MWE as of now. Unless I draw it by hand through TikZ, which would be too tedious. I haven't tried using pgfgantt since I couldn't find anything in the documentation which would lead me to believe that it can be done
    – darnir
    Commented Feb 7, 2014 at 15:19
  • 1
    The package rtsched can be used to create simple process scheduling diagrams, but I don't know if it is possible to do such a "complex" chart. See the package documentation for more details!
    – user99748
    Commented Mar 2, 2016 at 22:00

1 Answer 1

10

I could not resist:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,arrows.meta,decorations.pathreplacing}

\newcounter{gridw}
\newcounter{procs}
\newif\ifinpgroup 
\inpgroupfalse

\newenvironment{schedulechart}[1]{%
 \begin{tikzpicture}[x=10,y=15,>=latex,
  processbox/.style={draw,fill=white},
  pendingbox/.style={fill=black!25},
  midstroke/.style={path picture={\draw (path picture bounding box.west) -- (path picture bounding box.east);}},
  divided/.style={path picture={\draw (path picture bounding box.north west) -- (path picture bounding box.south east);}}]
 \setcounter{gridw}{#1}
 \setcounter{procs}{0}
 \def\gridw{\thegridw}
 \foreach\x in {0,...,\gridw}{
  \draw[black!25] ({\x+1},0) -- ++(0,1);
  \pgfmathparse{Mod(\x,5)==0?1:0};
  \ifnum\pgfmathresult>0
   \draw ({\x+1},0) coordinate (c\x) -- ({\x+1},.5);
   \node at ([yshift=-5]c\x.south) {\x};
  \else
   \draw ({\x+1},0) coordinate (c\x) -- ({\x+1},.25);
  \fi
 }
}{%
 \draw[<->,>={Latex[open]}] (0,{\procs+1}) -- (0,0) -- ({\gridw+2},0);%
 \end{tikzpicture}
}
    
\newcommand{\process}[3][]{
 \stepcounter{procs}
 \def\procs{\theprocs}
 \draw[black!25] (0,\procs) coordinate (r\procs) -- ({\gridw+1},\procs);
 \foreach\x in {0,...,\gridw}
  \draw[black!25] ({\x+1},\procs) -- ++(0,1);
 \ifinpgroup\else
  \if\relax\detokenize{#1}\relax\else
   \node[anchor=east] at ([yshift=5,xshift=-25]r\procs.west) {$#1$:};
  \fi
  \draw[black!25] (r\procs.west) -- ++(-5,0);
 \fi
 \node[anchor=east] at ([yshift=5,xshift=0]r\procs.west) {$#2$};
 \begin{scope}[yshift={\procs*15}]
  #3
 \end{scope};
}

\newcommand{\processstart}[2][]{
 \foreach\x/\y in {#2}
  \draw[processbox,#1] ({\x+1},0) coordinate (a) rectangle ({\y+1},.5);
 \draw[->] (a) -- ++(0,.9);
}

\newcommand{\processmid}[2][]{
 \foreach\x/\y in {#2}
  \draw[pendingbox,#1] ({\x+1},.1) rectangle ({\y+1},.4);
}

\newcommand{\processblock}[2][]{
 \foreach\x/\y in {#2}
  \draw[processbox,divided] ({\x+1},0) rectangle ({\y+1},.5) 
   node[anchor=north east,inner sep=1pt,xshift=1pt] {\tiny $#1$};
}

\newcommand{\processend}[2][]{
 \foreach\x/\y in {#2}
  \draw[processbox,#1] ({\x+1},0) rectangle ({\y+1},.5) coordinate (a);
 \draw[-|] (a) -- ++(0,.25);
}

\newenvironment{processgroup}[1]{
 \inpgrouptrue
 \def\procs{\theprocs}
 \draw[black!25] ([yshift=15]r\procs.west) coordinate (g0) -- ++(-5,0);
 \def\processgroupname{#1}
}{
 \path (g0) -- coordinate[midway] (m) (r\procs.west);
 \draw[decorate,decoration={brace}] ([xshift=-22]g0) -- ([xshift=-22,yshift=10]r\procs.west);
 \node[anchor=east] at ([yshift=5,xshift=-25]m) {$\processgroupname$:};
 \inpgroupfalse
}

\begin{document}

\begin{schedulechart}{15}

\process[P_1]{J_1}{\processstart{1/3}\processmid[fill=none,midstroke]{3/10}\processend{10/12}}
\process[P_2]{J_2}{\processstart{0/2}\processmid[fill=none,midstroke]{2/7}\processend{7/9}}
\process[P_3]{J_3}{\processstart{0/1}\processmid[fill=none,midstroke]{1/4}\processend{4/7}}

\begin{processgroup}{P_4}
 \process{J_4}{\processstart{0/1}\processmid{1/10}\processend{10/13}}
 \process{A_1}{\processstart{2/2}\processmid{2/4}\processblock[J_2]{4/7}\processblock[J_1]{7/10}}
 \process{A_2}{\processstart{1/1}\processblock[J_3]{1/4}}
\end{processgroup}

\end{schedulechart}

\end{document}

Yields:

enter image description here

2
  • 1
    \setcounter{procs}{0} should be added to the beginning of the definition of the schedulechart environment
    – Mike D.
    Commented Aug 2, 2023 at 14:40
  • @MikeD. Good point! If you want to use the diagram mulitple times, this is essential, of course! Thank you! Added Commented Aug 2, 2023 at 14:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .