3

I want something to create something like this:

enter image description here

I found a similar answer here: https://tex.stackexchange.com/a/296389/209014

But in that link what is shown is a non-preemptive scheduling - meaning that the process cannot be repeated and has to be finished at once. In the above image, it is preemptive scheduling e.g. p2 is split up. How can I have a macro that I can easily use to do both quickly? Something like:

\begin{scheduling}
\process{2}{2}
\process{4}{2}
\process{2}{3}
\process{3}{7}
\process{1}{8}
\end{scheduling}

Above is just an example. Any other alternative or quicker way would also be appreciated.

3

It is very easy to come up with such an environment. The fill colors are stored in styles like 1/.style={fill=red!20}, and the overall style in the box style in the scheduling directory. So it is very easy to customize, too. But certainly not as versatile as a good, full-fledged package.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{chains}
\tikzset{scheduling/.cd,box/.style={draw=red!70!black,minimum height=1.8em},
1/.style={fill=red!20},
2/.style={fill=green!20},
3/.style={fill=gray!20},
4/.style={fill=cyan!20},
5/.style={fill=blue!20},
}
\newenvironment{scheduling}[1][]{%
\begin{tikzpicture}[start chain=A going right,node distance=0pt]
\edef\tmpx{0}%
\newcommand{\process}[3][]{%
\node[on chain,minimum width=##3*1em,scheduling/box,
style/.expanded={scheduling/##2},##1]{p##2};
\pgfmathsetmacro{\tmpx}{\tmpx+##3}
\node[anchor=north] at (A-\tikzchaincount.south east)
{$\pgfmathprintnumber\tmpx$};
\ifnum\tikzchaincount=1
\node[anchor=north] at (A-1.south west) {$0$};
\fi}}{\end{tikzpicture}}
\begin{document}
\begin{scheduling}
\process{2}{2}
\process{4}{2}
\process{2}{3}
\process{3}{7}
\process{1}{8}
\end{scheduling}
\end{document}

enter image description here

Here is a slight variation which has a style cycle list (the length of which is stored in n-styles) and allows the syntax

\processes{2/2,4/2,2/3,3/7,1/8,6/2,7/4}

to make the thingy a bit more user-friendly

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{chains}
\tikzset{scheduling/.cd,box/.style={draw=red!70!black,minimum height=1.8em},
1/.style={fill=red!20},
2/.style={fill=green!20},
3/.style={fill=gray!20},
4/.style={fill=cyan!20},
5/.style={fill=blue!20},
6/.style={fill=orange!20},
n-styles/.initial=6%<- cycle list reset
}
\makeatletter
\newenvironment{scheduling}[1][]{%
\begin{tikzpicture}[start chain=A going right,node distance=0pt]
\edef\tmpx{0}%
\newcommand{\process}[3][]{%
\pgfmathtruncatemacro{\istyle}{1+Mod(##2-1,\pgfkeysvalueof{/tikz/scheduling/n-styles})}
\node[on chain,minimum width=##3*1em,scheduling/box,
style/.expanded={scheduling/\istyle},##1]{p##2};
\pgfmathsetmacro{\tmpx}{\tmpx+##3}
\node[anchor=north] at (A-\tikzchaincount.south east)
{$\pgfmathprintnumber\tmpx$};
\ifnum\tikzchaincount=1
\node[anchor=north] at (A-1.south west) {$0$};
\fi}%
\def\pft##1/##2;{\edef\X{##1}\edef\Y{##2}}
\newcommand{\processes}[2][]{\@for\next:=##2\do{%
\expandafter\pft\next;
\process[##1]{\X}{\Y}}}%
}{\end{tikzpicture}}
\makeatother
\begin{document}
\begin{scheduling}
\processes{2/2,4/2,2/3,3/7,1/8,6/2,7/4}
\process[dashed]{3}{5} 
\end{scheduling}
\end{document}

enter image description here

As you can see, 7 and 1 have the same style since 7=1 mod 6 and n-styles is set to 6 (which is a so-called perfect number). One can also add further processes to continue the chain. One possible way the optional argument that already exists in the first, more basic version can be used is also added. Note that there are many more things one can think of, such as trading the hard coded distance 1em for a pgf key and so on and so forth.

9
  • 1
    I see this way supports 5 processes (in terms of colouring). So if I have more processes like 6, can I simply just add another colour like 6/.style={fill=orange!20},, or do I have to do something else as well? – user209014 May 21 '20 at 21:39
  • 2
    @levivah940 Yes, that's it. (Don't forget the commas between the styles.) – user194703 May 21 '20 at 21:40
  • 1
    Is there also a more automated way so instead of I adding colours manually each time if the number of processes is greater than 5, a random colour selection is automatically done (however still same processes that are repeated have the same colour)? – user209014 May 21 '20 at 21:41
  • 2
    @levivah940 You could define a cycle list, say. Random is also possible. One can also change the syntax to \processes{2/2,4/2,2/3,3/7,1/8}. All of this is easy. The above is just a very basic example. The only problem with adding feature after feature is that at some point no one will understand the code any more. – user194703 May 21 '20 at 21:45
  • 2
    @levivah940 I just added it as it was not much effort and is probably useful. – user194703 May 21 '20 at 21:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy