Where is the error below: include <iostream> #include <sys/types.h> #include…
- User
Where is the error below: include #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <stdio.h> using namespace std;
int main(int argc, char* argv[]) //command line argument {
int number = argc - 1; cout << "I have " << number << " children. " << endl; for(int i = 0; i < number < i++){ pid_t pid = fork(); if (pid < 0){ perror("fork failed."); exit(0); } else if (pid == 0){ execl("./child","./child", number, argv[i+1],NULL]); exit(0); } } else{ wait(NULL); cout << " I have " << number << " children." << endl; cout << "All child processes terminated. Parent exits." <<endl; exit(0); }}
- Assistant
There are a few errors in the code:
- The line
include <iostream>should be#include <iostream>. - The line
using namespace stdshould be placed after the#includedirectives. - The line
for(int i = 0; i < number < i++){has an error in the condition. It should befor(int i = 0; i < number; i++){. - The line
execl("./child","./child", number, argv[i+1],NULL]);has a syntax error. The closing square bracket should be a closing parenthesis. It should beexecl("./child","./child", number, argv[i+1],NULL);. - There is an extra closing brace
}before the lineelse{. It should be removed.
Here is the corrected code:
#include <iostream> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <stdio.h> using namespace std; int main(int argc, char* argv[]) //command line argument { int number = argc - 1; cout << "I have " << number << " children. " << endl; for(int i = 0; i < number; i++){ pid_t pid = fork(); if (pid < 0){ perror("fork failed."); exit(0); } else if (pid == 0){ execl("./child", "./child", number, argv[i+1], NULL); exit(0); } } wait(NULL); cout << "I have " << number << " children." << endl; cout << "All child processes terminated. Parent exits." << endl; exit(0); } - The line
- User
number does not work in execl, please correct
- Assistant
To pass the value of
numberas a command line argument to the child process in theexeclfunction, you need to convert it from an integer to a string. You can use theto_stringfunction from the<string>header to achieve this.Here is the corrected code:
#include <iostream> #include <sys/types.h> #include <sys/wait.h> #include <unistd.h> #include <stdio.h> #include <string> using namespace std; int main(int argc, char* argv[]) //command line argument { int number = argc - 1; cout << "I have " << number << " children. " << endl; for(int i = 0; i < number; i++){ pid_t pid = fork(); if (pid < 0){ perror("fork failed."); exit(0); } else if (pid == 0){ execl("./child", "./child", to_string(number).c_str(), argv[i+1], NULL); exit(0); } } wait(NULL); cout << "I have " << number << " children." << endl; cout << "All child processes terminated. Parent exits." << endl; exit(0); }In the
execlfunction,to_string(number).c_str()converts thenumbervariable to a string and then retrieves the pointer to the underlying character array to pass as an argument.