HI Experten,
ich habe die ehrenvolle Aufgabe, eine Shell zu entwickeln. Soweit so gut. Alle Teilaufgaben funktionieren soweit wunderbar. Jetzt möchte ich aber einen Prozess den ich mittels STRG+Z angehalten und mit bg im Hintergrund wieder starte nicht auf ein STRG+C reagieren lassen.
hier mal mein code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | #include <iostream> #include <string> #include <unistd.h> //fuer exec #include <stdio.h> #include <sys/wait.h> #include <sys/types.h> #include <vector> #include <string.h> #include <stdlib.h> #include <signal.h> using namespace std; int fgNumber = -1; int bgNumber = 0; void ctrlC(int sig) //sig = Nr. des Signals SIGINT =2 { cout << "STRG+C wurde gedrückt"; // cout << "Prozess mit " << "[" << getpid() << "]" <<"wird gekillt" << endl; return; // exit (0); } void ctrlZ(int sig) { cout << "Stopped" << endl; return; } void startProcessInBackground(int pidNumber){ kill(pidNumber,SIGCONT); setpgid(pidNumber,pidNumber); cout <<"in function" << getgid() << endl; } void getProcessBackInFront(int pidNumber){ kill(pidNumber,SIGCONT); waitpid(pidNumber, NULL, 0); fgNumber = -1; } void sigchld(int sig) { //Zombieprozesse vermeiden while (waitpid(-1,0,WNOHANG) >0); } /* static void sighandler(sig) { switch (sig) { case SIGINT: cout << "Strg C wurde gedrückt" << endl; return; break; } } */ int main(int argc, char *argv[]) { int pidNumber = 0; vector<char *> commandVector; string input = ""; cout << "Welcome to TobMesFredGre-Shell \n"; cout << "Exit with logout" << endl; int found = 0; int terminated;//, status; while (true) { cout << "# "; cin.clear(); // cout << "# "; getline(cin, input); // cin.ignore(1000,'\n'); if (input == "logout") { return 0; } if (input == "fg") { cout << "Pid eingeben" << endl; cin >> pidNumber; fgNumber = pidNumber; getProcessBackInFront(pidNumber); } if (input == "bg") { input = ""; //damit bg nicht in das array geschrieben und ausgeführt wird cout << "Pid eingeben" << endl; cin >> pidNumber; //bgNumber = pidNumber; startProcessInBackground(pidNumber); cin.clear(); bgNumber =1; //cout << "in bg: " << bgNumber << endl; } signal(SIGINT, ctrlC); /*Sendet Signal CTRL+C an Signalhander*/ signal(SIGTSTP, ctrlZ); /* Sendet Signal CTRL+Z an Signalhandler*/ char *data = (char*) input.c_str(); char* pch = strtok(data, " "); commandVector.push_back(pch); while (pch != NULL) { pch = strtok(NULL, " "); commandVector.push_back(pch); } char ** command = &commandVector[0]; pid_t pid = fork(); pid_t gid = 666; if (pid == 0) { //child process cout << "child: " << "["<<getpid()<<"]" << endl; //cout << "child group: " << "[" << getgid() << "]" << endl; if (bgNumber == 1) { setpgid(pid,getpid()); cout << "child group in bg: " << "[" << getgid() << "]" << endl; execvp(command[0], command); bgNumber = 0; cout << bgNumber << endl; } // if (execvp(command[0], command) == -1) { // cout << "fail child" << endl; //return 0; // } else execvp(command[0], command); } if (pid > 0) { //parent process int status; if (waitpid(-1, &status,WUNTRACED) == -1) { cout << "fail parent" << endl; //return 0; } //cout << "parent: " << "["<<getpid()<<"]" << endl; //cout << "group parent: " << "[" << getgid() << "]" << endl; } commandVector.clear(); } return 0; } |
Ich habe vor im child-Prozess mittels setpgid die Prozessgruppe zu ändern, damit das SIGINT vom STRG+C nicht mehr vom Hintergrundprozess empfangen wird. Doch leider wir die GID nicht geändert. Kann mir da evtl. jemand weiterhelfen?
Danke schon mal.