Unix process
*fork()
in unistd.h and return int
when encountering a fork, it will create a child process of the following line
it will give 2 process . Everything above it will be executed once but the below will be executed
twice the main process and the child
child id will be always 0
if we call fork() below a fork() it will create 4 process (2^n process)
wait for a process to finish in <sys/wait,h> (wait())
the system decides what is the order of the process (child/main) eg print 1-10
it can do ping pong like or we get the proper order NO GUARANTY
note that the fflush is just for printf to not use like a call stack so it will print when there is a printf
not stock them print them together
we can use wait() : this wait for the child process to be done then run the main process so we use
this in the main not in the child because child may not have a child so it will be stuck
process id(pid)
Every single process has its own pid in unix (pid!= id)
to get this we use getpid() to get the current pid and getppid for the parent process pid
for the following example, the parent have finished but the child not. So the child gets attribuate a
new parent but the rule is PARENT must wait for the child to finish
wait() will wait for a child process so if no child no wait and it return also the pid of the child we
wait for and -1 if not (so need to use wait in main process)
calling multiple fork
note that if we use wait it will only wait for the one child to be finished the fastest one
for this the first one is z
for that we need to wait for all child to be executed with errno
communicating between process using pipes
pipe :
int pipe(int __pipedes[2])
Create a one-way communication channel (pipe).
If successful, two file descriptors are stored in PIPEDES;
bytes written on PIPEDES[1] can be read from PIPEDES[0].
Returns 0 if successful, -1 if not.
We can comminucate through pipe and we use it as a file descriptor and in it fd[0] is read and fd[1]
write
,,,
,,,
use fifo
,,,,
,,,
send an array through pipe
the must important note is to send the size of the array before the array itself
,,,
,,,
calling multiple pipe
to get n process , we use the following techniques