1) Potential definition and representation A process is an instance of a computer program that is being executed. It contains the program code and its current activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently. A computer system process consists of (or is said to own) the following resources: - An image of the executable machine code associated with a program. - Memory (typically some region of virtual memory) which includes - the executable code - process-specific data (input and output) - a call stack (to keep track of active subroutines and/or other events) - a heap to hold intermediate computation data generated during run time - Operating system descriptors of resources that are allocated to the process, such as file descriptors (Unix terminology) or handles (Windows), and data sources and sinks. - Security attributes, such as the process owner and the process' set of permissions (allowable operations). - Processor state (context), such as the content of registers, physical memory addressing, etc. The state is typically stored in computer registers when the process is executing, and in memory otherwise. 2) Linux processes - Every process in the system is associated an unique identifier, a positive number which is called process identifier or PID getpid() - every new process is created by an already existing process. This creates a parent-child relationship. The only exception is the process which has the PID equal to 0. This process has no parent, being created by the operating system at boot time. getppid() - The fork system call is used to create a new process and has the following syntax: #include <sys/types.h> #include <unistd.h> pid_t fork(); - The child process, being a copy of the parent process, contains the same source code and begins its execution in a similar way its parent continues its own one, i.e. returning from fork. pid=fork(); /* source code executed by both processes */ switch (pid) case -1: /* Error! Unsuccesful fork! */ case 0 : /* source code executed only by the child*/ break; default: /* source code executed only by the parent*/ } /*source code executed by both processes*/ - All the child process' variables have initially their value inherited from the parent process. Also, all the file descriptors are the same as in the parent process. - The physical memory the two processes are located are different and the resources allocated by the operating system to them are different, meaning that the two processes are distinct. - exec, execl, execlp, execv and execvp creates processes that are not identicall with the parent process - process execution synchronization: the parent process waits until the termination (successfully or with error) of the child process wait(), waitpid() - A process which calls wait or waitpid can: - be blocked, if all its children are currently in execution; the blocked process resumes its execution when one of the children terminates; - receive the termination state of the child, if at least one of the children was terminated before the call of wait; - receive an error if it has no child process. - The main differences between the two system calls are the following: - wait blocks the calling process until the termination of a child, while waitpid has an option (WNOHANG) which can avoid this (it is stated by the argument opt); - waitpid does not necessarily blocks just until one of the child processes terminates: one can specify using the argument pid the child process of which termination is waited; - There are three situations a process is terminated: - when the process voluntarily calls exit() - after receiving a terminating signal or a signal not processed by the process - when the system crashes. 3) Example #include <sys/types.h> #include <sys/wait.h> main() { int pid, state; printf(" The parent: before the fork()\n"); if ((pid = fork()) != 0) { wait(&state); } else { int pid; printf("The child: begins the execution \n"); pid=getpid(); printf("The son: %d terminates\n", pid); exit(10); } printf("The parent: after fork()\n"); state = WEXITSTATUS(state); printf("PID child=%d; terminated with the code %d=%x\n", pid, state, state); } 4) Practice Sa se scrie un program c care citeste un nume de fisier de la tastatura si un caracter (cheie de criptare) tot de la tastatura. Programul va cripta (intr-un process fiu) continutul fisierului introdus de la tastatura folosind ca si cheie de criptare caracterul introdus de utilizator. Rezultatul criptarii va fi scris intr-un fisier cu acelasi nume ca si fisierul initial la care se adauga extensia: .crypt. Criptarea se va realiza conform urmatorului algoritm: crypt = original XOR cheie. Exemplu: Introduceti nume fisier: test.txt Introduceti cheie criptare: X Rezultat scris in: test.txt.crypt