process programming

This is a discussion on process programming within the Linux Programming forums, part of the Platform Specific Boards category; Hi all this is the first lesson in Linux programming :
it might be BAD or not but this is ...

process programming

Hi all this is the first lesson in Linux programming :
it might be BAD or not but this is what i got for now;
author : St0rM-MaN
license : Rippers License / GPL
site : http://programming-fr34ks.net
title : Process programming under Linux

Let's rock :

first of all what is a process ?

1-it's the system way to define running programs and manage it
2-you can imagine a process as a something that executes a given piece of code
3-you can imagine it as an instance or a programs runs sequentially .
didn't get any thing right?
remember the definition of a program ?

several instruction executes to give a meaningful something right
well , the process is the actual execution of this instruction (quoted from wiki)
remember that every process in Unix is a child of another one and has the same code as it (fork()-ed) ,
or child that has different code (exec*()-ed)so a process is the shell of program ;

process can executes several program ,
processes can executes one program or more at the same time;

Re-Entrancy :means that 2 or more processes try to execute a piece of code at the same time or a single process try to execute a function several times (simultaneously) .

data section in each process is separate from the other

that means if we have 2 process
p1 , p2
each process runs program ttt
both of this process has it's own distinct copy of ttt's variables

compile this program and run it don't worry your self about understanding it now;

you will notice that 17 printed once in child process , put in the parent one it prints 6

now it's time to say hi to fork(); system cal

Code:

SYNOPSIS
#include <sys/types.h>
#include <unistd.h>
pid_t fork(void);

creates new process by duplicating the parent process (where the fork was called)

return value :

0 in the child process this is why we switched and checked for 0 in the previous code
in parent process it returns the pid of it's child
-1 on error and global variable errno is set to the right error number

1- include headers
2-declare a variable to save return value form fork();
3- switch this variable
4- check for error
6-check for which process are we in (parent / child )
8-print messages

if you noticed that the pid of the child is the same as child-pid in parent
man fork right now;

there is one thing that we didn't mention yet ;
process death :
when the child is dead it's not actually dead (turned into a ZOMBIE (a living dead));
why is that ? and is it so important to know ? yes it Is
this zombie process is still hanging in our system
so how to get ride of it ?
first let your self know that :
1 - when a child dies it turns into zombie tell the parent know it's dead by waiting for it's exit status or by SIGNALS
2- when a parent is dead before it's child , child process is now orphan and has now father
so the INIT process inherit it . INIT has pid no 1 check it out :

Code:

ps -A | grep "init"

don't forget to acknowledge your child process death or it will turn into zombie and consume your system pids (remember fork bomb)
you can deal with it by

Code:

ps -A | grep "defunct"

and it will show you zombie process

first wait(); system call:

SYNOPSIS
#include <sys/types.h>
#include <sys/wait.h>

pid_t wait(int *status);

system call used to check child's status change
quote form man pages:

A state change
is considered to be: the child terminated; the child was stopped by a signal; or the
child was resumed by a signal. In the case of a terminated child, performing a wait
allows the system to release the resources associated with the child; if a wait is not
performed, then terminated the child remains in a "zombie" state (see NOTES below).

on success returns the terminated child pid;
on error returns -11
but there is a little problem that when you call this system call it suspends the parent process and waits for child termination that will disallow us to continue in our parent process execution but we will get over this in another example using the beautiful signals
also mention that if the process is already turned into zombie wait returns immediately

also waitpid(); is a good system call and I will explain it in another example after this

Code:

pid_t waitpid(pid_t pid, int *status, int options);

modify our example now
in this example we sleep 5 seconds in the child process ; it suppose that parent process will executes it's statement but no it waits for child termination try and remove this call and watch your it your self;

after 5 seconds child will exit and wait() system call store child termination number and executes it's statement
end of the story child was buried and didn't turned into zombie
but this is not a good usage of multi-process programming as you wait for another process to executes your process
know it's time to tell you the truth I am not your father
when a child dies it sends SIGCHLD signal to it's parent ; so we can set up a good signal handler to deal with it's death