How to use shared memory with Linux in C

There are two approaches: shmget and mmap. I’ll talk about mmap, since it’s more modern and flexible, but you can take a look at man shmget (or this tutorial) if you’d rather use the old-style tools. The mmap() function can be used to allocate memory buffers with highly customizable parameters to control access and permissions, and to back them with file-system storage if … Read more

Example of waitpid() in use?

Syntax of waitpid(): The value of pid can be: < -1: Wait for any child process whose process group ID is equal to the absolute value of pid. -1: Wait for any child process. 0: Wait for any child process whose process group ID is equal to that of the calling process. > 0: Wait for the child whose … Read more

What does WEXITSTATUS(status) return?

WEXITSTATUS(stat_val) is a macro (so in fact it does not “return” something, but “evaluates” to something). For how it works you might like to look it up in the headers (which should be #included via <sys/wait.h>) that come with the C-compiler you use. The implementation of this macro might differ from one C-implementation to the other. Please note, … Read more

How does wait(NULL) exactly work?

wait(NULL) will block parent process until any of its children has finished. If child terminates before parent process reaches wait(NULL) then the child process turns to a zombie process until its parent waits on it and its released from memory. If parent process doesn’t wait for its child, and parent finishes first, then the child process becomes orphan and is … Read more

How to use execvp()

The first argument is the file you wish to execute, and the second argument is an array of null-terminated strings that represent the appropriate arguments to the file as specified in the man page. For example: