\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Gorge97} \pdfinfo{ /Title (c-multiprocessing-and-multithreading.pdf) /Creator (Cheatography) /Author (Gorge97) /Subject (C Multiprocessing \& Multithreading Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{E89300} \definecolor{LightBackground}{HTML}{FDF8EF} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{C Multiprocessing \& Multithreading Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Gorge97} via \textcolor{DarkBackground}{\uline{cheatography.com/154085/cs/33088/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Gorge97 \\ \uline{cheatography.com/gorge97} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 10th July, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Multiprocessing}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{\#include \textless{}unistd.h\textgreater{}\{\{nl\}\} \#include \textless{}sys/types.h\textgreater{}}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{int fork(void)}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Fork the current process creating a child. \{\{nl\}\}Return 0 in the child process or child ID for the parent} \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{int getpid(void)}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Return the ID of the calling process} \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{int getppid(void)}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Return the ID of the parent of the calling process} \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{void exit(status)}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Process termination} \tn % Row Count 13 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{unlisgned int sleep(unsigned int seconds)}}} \tn \mymulticolumn{1}{x{5.377cm}}{\hspace*{6 px}\rule{2px}{6px}\hspace*{6 px}Pause the execution of the calling process for {\emph{seconds}} seconds or until a signal is received} \tn % Row Count 16 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Process Syncronization}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\#include \textless{}sys/types.h\textgreater{}\{\{nl\}\} \#include \textless{}sys/wait.h\textgreater{} }}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} {\bf{pid\_t wait(*status)}} & Wait until a child process terminate the execution; \{\{nl\}\}On success return the pid of the child, on failure return -1;\{\{nl\}\} {\emph{status}} is the address of the variable cointaining the exit status \tn % Row Count 12 (+ 10) % Row 2 \SetRowColor{LightBackground} {\bf{pid\_t waitpid(pid\_t pid, int *status, int options)}} & Wait until the child specified with the {\emph{pid}} argument terminate the execution;\{\{nl\}\}{\emph{status}} is the exit status of the terminating process;\{\{nl\}\} \tn % Row Count 20 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Shared Memory}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\#include \textless{}sys/shm.h\textgreater{}}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{int shmget(key\_t key, int size, int shmflg)}} & Create a shared memory or connec to to an existing segment; \{\{nl\}\} {\emph{key}} is a numeric key assigned to the segment. If IPC\_PRIVATE is used the segment can be only used by parent and children;\{\{nl\}\} {\emph{size}} is the size of the memory segment; {\emph{shmflg}} is a flag field: IPC\_CREATE create a new segment, IPC\_EXCL cause the command to fail if the segment already exist.\{\{nl\}\} Return the shared memory segment id or -1 if fail. \tn % Row Count 22 (+ 21) % Row 2 \SetRowColor{LightBackground} {\bf{void {\emph{shmat(int shmid, const void }}shmaddr, int shmflg)}} & Attach to the shared memory segment and return the address.\{\{nl\}\}{\emph{shmid}} is the shared memory segment id;\{\{nl\}\} {\emph{shmadr}} is the variable where the address of the segment is stored; \{\{nl\}\} {\emph{shmflg}} is used to specify the access permissions for the shared memory segment and to request special attachment conditions, such as a read-only segment. \tn % Row Count 40 (+ 18) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Shared Memory (cont)}} \tn % Row 3 \SetRowColor{LightBackground} {\bf{int shmdt(const void *shmaddr)}} & Detaches the shared memory segment located at the address specified by shmaddr from the address space of the calling process. \tn % Row Count 7 (+ 7) % Row 4 \SetRowColor{white} {\bf{int shmctl(int shmid, int cmd, struct shmid\_ds *buf)}} & Performs the control operation specified by {\emph{cmd}} on the shared memory segment whose identifier is given in shmid. Typical usage: shmctl(shmid, IPC\_RMID, 0); Remove shared memory segment \tn % Row Count 17 (+ 10) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Message Queue}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\#include \textless{}sys/msg.h\textgreater{} }}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{int msgget(key\_t key, int flag)}} & Creates a message queue.\{\{nl\}\} {\emph{key}} is an integer that specifies the queue key;\{\{nl\}\} {\emph{flag}} indicates creation conditions and access permissions (same as shmget).\{\{nl\}\} Return a message queue identifier or -1 in case of failure. \tn % Row Count 13 (+ 12) % Row 2 \SetRowColor{LightBackground} {\bf{int msgsnd(int msqid, const void *msgp, size\_t nbytes, int flag);}} & Send a message on the queue.\{\{nl\}\} {\emph{msqid}} is the identifier returned by msgget command; \{\{nl\}\} {\emph{msgp}} is the pointer to the message struct.\{\{nl\}\}\{\{nl\}\}{\emph{Struct msgbuf \{ \{\{nl\}\}~~long mtype; \{\{nl\}\}~~char mtext {[}TEXT LENGHT{]}; \{\{nl\}\}\};}} \{\{nl\}\}{\emph{nbytes}} is the maximum lenght of the message;\{\{nl\}\} \tn % Row Count 29 (+ 16) % Row 3 \SetRowColor{white} {\bf{int msgrcv(int msqid, void *msgp, size\_t msgsz, long msgtype, int msgflg)}} & Receive a message from the queue.\{\{nl\}\}{\emph{msqid}} corresponds to the message queue identifier;\{\{nl\}\}{\emph{msgp}} is the pointer to a message struct (the same used in msgsnd) ;\{\{nl\}\}{\emph{msgsz}} is the number of bytes to read;\{\{nl\}\}{\emph{msgtype}} is used to filterthe message in the queue. If != 0 read only a message with the same id on the queue;\{\{nl\}\}{\emph{msgflg}} is a flag that modify the behavior of the command. Using IPC\_NOWAIT return immediately of no message is found.\{\{nl\}\}The function return the number of bytes read or -1 if unsuccessful \tn % Row Count 56 (+ 27) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Message Queue (cont)}} \tn % Row 4 \SetRowColor{LightBackground} {\bf{int msgctl(int msqid, int cmd, struct msqid\_ds *buf)}} & Modifies the properties of the queue or delete it. \{\{nl\}\}{\emph{msqid}} corresponds to the message queue identifier.\{\{nl\}\}\{\{nl\}\}Typical usage:\{\{nl\}\}{\emph{msgctl(msgid, IPC\_RMID, 0)}};\{\{nl\}\}Removes queue \tn % Row Count 10 (+ 10) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Unnamed Pipes}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\#include \textless{}unistd.h\textgreater{}}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{int pipe( int fd{[}2{]} )}} & Creates an unnamed pipe (unidirectional).\{\{nl\}\}{\emph{fd{[}2{]}}} are two descriptor associated with the "read" end of the pipe (fd{[}0{]}) and with the "write" end of the pipe (fd{[}1{]}).\{\{nl\}\}Return 0 if the kernel could allocate enough space, -1 otherwise. \tn % Row Count 14 (+ 13) % Row 2 \SetRowColor{LightBackground} {\bf{int write (int fd, char * buf, int size)}} & The classic write function is used to write inside the buffer.\{\{nl\}\}{\emph{fd}} in this case is the descriptor of the write end of the pipe. \tn % Row Count 21 (+ 7) % Row 3 \SetRowColor{white} {\bf{int read (int fd, char * buf, int size)}} & The classic read function is user to read data from the pipe.\{\{nl\}\}{\emph{fd}} is the descriptor of the read end of the pipe. \tn % Row Count 27 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Named Pipes or FIFO}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\#include \textless{}fcntl.h\textgreater{}}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{int mkfifo(const char *path, mode\_t mode);}} & Creates a named pipe (FIFO)\{\{nl\}\}{\emph{path}} corresponds to the name of the pipe;\{\{nl\}\}{\emph{mode}} corresponds to the permission mode flags. \tn % Row Count 8 (+ 7) % Row 2 \SetRowColor{LightBackground} {\bf{int open(const char *path, int flags)}} & Open the fifo defined by the name saved in {\emph{path}};\{\{nl\}\}for the {\emph{flag}} parameter use O\_WRONLY for a write only pipe or O\_EDONLY for a read only pipe. \tn % Row Count 16 (+ 8) % Row 3 \SetRowColor{white} {\bf{int write (int fd, char * buf, int size)}} & The classic write function is used to write inside the buffer.\{\{nl\}\}{\emph{fd}} in this case is the descriptor of the write end of the pipe. \tn % Row Count 23 (+ 7) % Row 4 \SetRowColor{LightBackground} {\bf{int read (int fd, char * buf, int size)}} & The classic read function is user to read data from the pipe.\{\{nl\}\}{\emph{fd}} is the descriptor of the read end of the pipe. \tn % Row Count 29 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Signals}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{\#include \textless{}signal.h\textgreater{}}}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} {\bf{int kill(pid\_t pid, int sig)}} & Send a signal to the process indicated by the parameter pid.\{\{nl\}\}{\emph{pid}} is the pid of the process that will receive the signal;\{\{nl\}\}if set to 0 all the processes in the process group of the current process receive the signal;\{\{nl\}\}{\emph{sig}} is the type of the signal \tn % Row Count 15 (+ 14) % Row 2 \SetRowColor{LightBackground} {\bf{sighandler\_t signal(int signum, sighandler\_t handler)}} & Installs a new signal handler for the signal with number {\emph{signum}}.\{\{nl\}\}The signal handler is set to handler which may be a user specified function or a standard function as {\bf{SIG\_IGN}} \tn % Row Count 25 (+ 10) % Row 3 \SetRowColor{white} {\bf{unsigned int alarm(unsigned int seconds)}} & Causes the system to generate a SIGALRM signal for the process after the number of real-time seconds specified by {\emph{seconds}} have elapsed. \tn % Row Count 32 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}