Professional Documents
Culture Documents
Process Management
Process Management
Interprocessus Synchronisation
Memory Management
Running P1
Ready
P1 Waiting
P2
P3
P4
P5
…
Ready queue
Process PCB
Each process is represented in the operating system by a process control block
(PCB).
It contains many pieces of information associated with a specific process, including
these:
#include <sys/types.h>
#include <unistd.h>
pid_t fork()
Operations on Processes (POSIX)
Process Creation with fork()
#include <unistd.h>
Void exit(int returncode);
Pid_t getpid() : returns the pid of the process executing the system call
Pid_t getppid() : returns the pid of the parent process executing the system call
Operations on Processes (POSIX)
Exec family : After a fork() system call, one of the two processes typically
uses the exec() system call to replace the process’s memory space with
a new program. The exec() system call loads a binary file into memory
(destroying the memory image of the program containing the exec()
system call) and starts its execution.
char *arguments[4]
...
#include <unistd.h> arguments[0]="/bin/ls";
int execl(const char *path, const char *argv, ...); arguments[1]="-l";
int execv(const char *path, const char *argv[]); arguments[2]="/etc";
int execlp(const char *file, const char *argv, ...); arguments[3]="NULL";
int execvp(const char *file, const char *argv[]);
execv("/bin/ls", arguments);
execl("/bin/ls", "/bin/ls " ,"-l" , "/etc" , "NULL");
execvp("ls", arguments);
execlp("ls", "ls " ,"-l" , "/etc" , "NULL");
Operations on Processes (POSIX)
Wait and waitpid
The parent can create more children; or, if it has nothing else to do while the child
runs, it can issue a wait() system call to move it self off the ready queue until the
termination of the child.
A parent process may wait for the termination of a child process by using the wait()
system call. The wait() system call is passed a parameter that allows the parent to
obtain the exit status of the child. This system call also returns the process identifier
of the terminated child so that the parent can tell which of its children has
terminated:
#include <wait.h>
int status;
int pid ;
pid = wait(&status);
pid = waitpid(int pid, &status);
Operations on Processes (POSIX)
Example :
Operations on Processes (POSIX)
Application : Write prog.c that implements this process tree.
Prints the pid of his child and Prints its pid in the pid of his father
waits for his terminisation
P2 P3
Executes « ls –l »
P4