Untitled
#include <iostream> #include <sys/types.h> #include <unistd.h> #include <sys/wait.h> using namespace std; int katrori(int x) { return x * x; } int kubi(int x) { return x * x * x; } int fuqiakater(int x) { return x * x * x * x; } int trefishi(int x) { return 3 * x; } int main() { int numri = 2; pid_t pid1, pid2, pid3; // Fork the first child process if ((pid1 = fork()) == 0) { // This is the first child process int rezultati = katrori(numri); cout << "Procesi femije 1 ka numrin : " << getpid() << endl; cout << "Katrori u llogarit nga femija 1 dhe rezultati eshte " << rezultati << endl; return 0; // Ensure the child process exits here } // Fork the second child process if ((pid2 = fork()) == 0) { // This is the second child process int rezultati = kubi(numri); cout << "Procesi femije 2 ka numrin : " << getpid() << endl; cout << "Kubi u llogarit nga femija 2 dhe rezultati eshte " << rezultati << endl; return 0; // Ensure the child process exits here } // Fork the third child process if ((pid3 = fork()) == 0) { // This is the third child process int rezultati = fuqiakater(numri); cout << "Procesi femije 3 ka numrin : " << getpid() << endl; cout << "Fuqia e katert u llogarit nga femija 3 dhe rezultati eshte " << rezultati << endl; return 0; // Ensure the child process exits here } // This is the parent process int rezultati = trefishi(numri); cout << "Procesi prind ka numrin : " << getpid() << endl; cout << "Trefishi u llogarit nga prindi dhe rezultati eshte " << rezultati << endl; // Wait for each child process to complete waitpid(pid1, NULL, 0); cout << "Femija 1 ka perfunduar " << endl; waitpid(pid2, NULL, 0); cout << "Femija 2 ka perfunduar " << endl; waitpid(pid3, NULL, 0); cout << "Femija 3 ka perfunduar " << endl; return 0; }
Leave a Comment