What this code should do is: there are parent.cpp and child.cpp. Parent will send whatever is in the buffer to child and child will send back whatever received to parent. I do not know what I am doing wrong. I am confused what is missing in the parent and what else I should include into the child.
//parent.cpp
//Check for fork error
if ( (pid = fork()) < 0 )
{
cerr << "FORK ERROR" << endl;
return -3;
}
else if (pid == 0) // Child
{
close(fd1[1]);//Close parent's stdout-write
close(fd2[0]);//Close child's stdin-read
if (fd1[0] != STDIN_FILENO)//Make sure file desc. matches
{
if (dup2(fd1[0], STDIN_FILENO) != STDIN_FILENO)
{
cerr << "dup2 error to stdin" << endl;
}
close(fd1[0]);
}
if (fd2[1] != STDOUT_FILENO)//Make sure file desc. mathces
{
if (dup2(fd2[1], STDOUT_FILENO) != STDOUT_FILENO)
{
cerr << "dup2 error to stdout" << endl;
}
close(fd2[1]);
}
if ( execl("./child", "child", (char *)0) < 0 )
{
cerr << "system error" << endl;
return -4;
}
return 0;
}//end of child
else //parent
{
int rv;
close(fd1[0]);//Close parent's read
close(fd2[1]);//close child's write
if ( write(fd1[1], buffer, strlen(buffer)) != strlen(buffer))
{
cerr << "Write ERROR FROM PIPE" << endl;
}
if ( (rv = read(fd2[0], buffer, MAXLINE)) < 0 )
{
cerr << "READ ERROR FROM PIPE" << endl;
}
else if (rv == 0)
{
cerr << "Child Closed Pipe" << endl;
return 0;
}
cout << "Output of child is: " << buffer;
return 0;
}//end of parent
//child.cpp
char line[1000];
int MAXLEN=1001;
read(STDIN_FILENO, line, MAXLEN);
My guess is that your processes are blocked. You write a small amount of data to the child, which the child processes. The child produces no output, however, but is waiting for more data. The parent is now waiting for the child to give it some data, and the child is waiting for the parent, and you have a classic deadlock. Try closing fd1[1] after the write in the parent, and before the read. The child will realize there is no more data and will produce output prior to terminating.
Related
I try to set up parallel calculations in processes instead of threads. I started with a very simple example: send an integer from parent process to child process. It works and this is the code of the function processParallel1():
//parent sends integer to child
bool processParallel1()
{
//define pipe
int parent2Child[2];
//create pipe
pipe(parent2Child);
//fork
pid_t child = fork();
if(child == 0) //child process
{
//close not needed end of pipe
close(parent2Child[1]);
for(;;)
{
struct pollfd pfd;
pfd.fd = parent2Child[0];
pfd.events = POLLIN;
//wait until data is available at the pipe
if(poll(&pfd, 1, -1) < 0)
{
cout << strerror(errno) << endl;
return false;
}
if((pfd.revents&POLLIN) == POLLIN)
{
int data;
if(!readData1(parent2Child[0], data))
return false;
cout << "c: received data: " << strerror(errno) << endl;
cout << "data received: " << data << endl;
if(data == -1)
break;
}
}
exit(0);
}
else //parent process
{
//close not needed end of pipe
close(parent2Child[0]);
//send data to child
if(!writeData1(parent2Child[1], 136758))
return false;
cout << "p: wrote data: " << strerror(errno) << endl;
//send stop data
if(!writeData1(parent2Child[1], -1))
return false;
cout << "p: wrote data: " << strerror(errno) << endl;
//wait for child to end
wait(nullptr);
//close all pipes
close(parent2Child[0]);
close(parent2Child[1]);
}
return true;
}
In processParallel1() I use this methods for writing and reading to the pipe:
bool writeData1(int fd, const int data)
{
int bytesWritten = write(fd, &data, sizeof(int));
if(bytesWritten < 0)
{
cout << strerror(errno) << endl;
return false;
}
return true;
}
bool readData1(int fd, int & data)
{
char intBuf[sizeof(int)];
int bytesRead = read(fd, intBuf, sizeof(int));
if(bytesRead > 0)
{
data = *(int *)intBuf;
}
else if(bytesRead < 0)
{
cout << strerror(errno) << endl;
return false;
}
return true;
}
Everything works as expected. Now I add more data and it becomes strange. I only exchanged the methods for writing and reading (and created a new version of the method now named processParallel3()):
bool writeData3(int fd, const int number, const std::string text)
{
int bytesWritten = write(fd, &number, sizeof(int));
if(bytesWritten < 0)
{
cout << strerror(errno) << endl;
return false;
}
int size = text.size() + 1;
bytesWritten = write(fd, &size, sizeof(int));
if(bytesWritten < 0)
{
cout << strerror(errno) << endl;
return false;
}
bytesWritten = write(fd, text.c_str(), size);
if(bytesWritten < 0)
{
cout << strerror(errno) << endl;
return false;
}
return true;
}
bool readData3(int fd, int & number, std::string & text)
{
char numberBuf[sizeof(int)];
int bytesRead = read(fd, numberBuf, sizeof(int));
if(bytesRead > 0)
{
number = *(int *)numberBuf;
}
else if(bytesRead < 0)
{
cout << strerror(errno) << endl;
return false;
}
char sizeBuf[sizeof(int)];
int size = -1;
bytesRead = read(fd, sizeBuf, sizeof(int));
if(bytesRead > 0)
{
size = *(int *)sizeBuf;
}
else if(bytesRead < 0)
{
cout << strerror(errno) << endl;
return false;
}
char textBuf[size];
bytesRead = read(fd, textBuf, size);
if(bytesRead > 0)
{
text = std::string(textBuf);
}
else if(bytesRead < 0)
{
cout << strerror(errno) << endl;
return false;
}
return true;
}
When I run processParallel3() in a main I get three times a "Success" after writing and reading, but the methods hangs after printing the received data in the child.
When I add the method processParallel1() to be run directly before processParallel3() in 1 everything is ok, but now I get three times "Bad file descriptor" and it hangs again.
Any idea what could happen or what further tests I could run?
I have some questions regarding the use of SIGINT and SIGTSTP in relation to managing processes in my own unix shell. But first of the code here:
void execute(vector<char *> argvv, bool x){
pid_t pid;
int status;
int error;
pid = fork();
a = pid;
argvv.push_back(NULL);
if(pid == -1){
cout << "error" << endl;
}else if(pid == 0){
error = execvp(argvv[0],argvv.data());
if(error == -1){
exit(-1);
}
// In Child Process
}else{
// If no "&", then wait for process
if(x == false){
if(wait(&status) != pid){
perror("wait()");
}
}else{
cout << "Pid des Hintergrundprozesses: " << pid << endl;
}
// in parent process
}
}
This function just receives the entered operation and parameters, forks a new process and executes it.
Now my signalhandler functions:
void signalHandlerSigInt(int signum){
cout << "Interrupt Signal (" << signum <<") received." << endl;
kill(a,SIGINT);
cout << "Killed Process: " << a << endl;
}
void signalHandlerSigTStp(int signum){
cout << "Interrupt Signal (" << signum <<") received." << endl;
kill(a,SIGTSTP);
cout << "Stop process..: " << a << endl;
}
and my main.cpp:
int main(int agc, char** argv) {
bool opBackground;
string operation;
vector<string> arguments;
vector<char *> argvv(arguments.size() + 1);
signal(SIGINT, signalHandlerSigInt);
signal(SIGTSTP, signalHandlerSigTStp);
while(true){
cout << "myshell>";
getline(cin,operation);
if(operation == "logout"){
logout();
}else{
opBackground = befehlUebersetzen(operation, &arguments);
vector<char *> argvv(arguments.size() + 1);
for(size_t i = 0; i != arguments.size(); ++i){
argvv[i] = &arguments[i][0];
}
execute(argvv, opBackground);
arguments.clear();
}
}
return 0;
}
The shell itself works fine, I now need to extend it to be able to kill the foreground process by pressing CTRL+C or stop the process with CTRL+Z.
I think I understood what a Signalhandler does, but is kill(a,SIGINT) the right way to transmit the signal SIGINT to my process? ("a" is a global variable for my forked pid, that means the last process I forked).
My problem is, when starting a process in the background and then start another process in the foreground it kills both processes when pressing CTRL+C.
Also the SIGTSTP signalhandler doesnt seem to work at all (does nothing - process just keeps running in the foreground).
Am I completely wrong with what im doing?
I have an assignment where I have to write a subshell in C++. Essentially, I am forking (if the input is not jobs or exit) and in the child process, I am calling exec to run the command. It needs to run alongside the parent process, so for example if I call sleep 100, the program should immediately be ready to run the next command since the parent is not "waiting" for it since I am using WNOHANG. However, my issue is when I need to track the actual state- if sleep is still running, I want to be able to get that the command is still running, but I am unable to do so. It always shows up as exited, when I use the various macros. I have no idea how to approach this differently and would appreciate some guidance.
(i didn't include the declarations of the variables since the editor on this website was messing it up for some reason)
do{
cout << "# ";
getline(cin, input);
if (input == "jobs")
{
cout << "Process ID | State | Command " << endl;
cout << "-----------+--------------+-----------------------------------------------" << endl;
//jobs stuff goes here, need to print pids, states, and commands
if(counter == 0)
{
cout << " [NO PROCESSES] " << endl;
}
else
{
for(i = 0; i < counter; i++)
{
cout << pidArray[i] << " ";
cout << statusArray[i] << " ";
cout << cmdArray[i] << endl;
}
}
}
else
{
cmdArray[i] = input;
i++;
counter++;
pid = fork();
if( pid == 0)
{
execl("/bin/sh", "sh", "-c", input.c_str(), (char *)0);
//break;
}
else if (pid == -1)
{
break;
}
else
{
pidArray[i-1] = pid;
//int rc = waitid(P_PID, pid, NULL, WNOHANG);
// int rc =waitpid(pid, &status, WNOHANG | WNOWAIT );
//exitChecker = waitpid(pid, &status, WNOHANG);
usleep(100000);
if (WIFEXITED(status))
{
cout << "terminated" << endl;
}
else
{
cout << "running" << endl;
}
}
}
}while(input != "exit");
return 0;
}
Thanks in advance.
I'm trying to have a parent process wait on multiple child processes signalling, before the parent continues, using an array of unnamed semaphores (one per child process). However, when using sem_wait(), the parent process waits indefinitely, while sem_trywait() returns a "resource temporarily unavailable" error, and continues without the child processes having signalled. Neither sem_init() nor sem_post() return an error.
Relevent portion of the code:
int numsems = concurrent_instrs.size();
std::cout << "Num sems: " << numsems << "\n";
// create semaphores
sem_t* sems = new sem_t[numsems];
for (int i = 0; i < numsems; i++)
{
if (sem_init(&sems[i], 1, 0) < 0)
{
perror("sem initialization failed");
exit(1);
}
}
int child_num = 0;
// perform all calculations in block concurrently
for(unsigned int i = 0; i < concurrent_instrs.size() && !isChild; i++)
{
int pid = fork();
if (pid == -1)
{
perror("Error forking:");
exit(1);
}
if (pid == 0)
{
isChild = true;
instr = concurrent_instrs[i];
}
else
{
child_num++;
}
}
if (isChild)
{
std::cout << "Child process " << child_num << " calculating: " << instr << "\n";
perform_calculation(instr, input_vars, internal_vars, shm_input, shm_internal);
std::cout << "Child process " << child_num << " finished calculating\n";
if (sem_post(&sems[child_num]) < 0)
{
perror("Child signal failed");
}
std::cout << "Child "<< child_num << " signalled\n";
// detach from shared memory
if (shmdt(shm_input) < 0)
{
perror("child shm_input detach failed");
}
if (shmdt(shm_internal) < 0)
{
perror("child shm_internal detach failed");
}
exit(0);
}
else
{
// parent waits for all children to finish
for (int i = 0; i < numsems; i++)
{
std::cout << "Waiting on subprocess " << i << " of " << numsems << "\n";
if (sem_trywait(&sems[i]) < 0)
perror("Parent wait failed");
else
std::cout << "Parent wait " << i << " working\n";
}
std::cout << "Finished waiting\n";
// destroy semaphores
for (int i = 0; i < numsems; i++)
{
if(sem_destroy(&sems[i]) < 0)
{
perror("Sem destroy failed");
exit(2);
}
else
{
std::cout << "Sem " << i << " destroyed\n";
}
}
delete[] sems;
}
Am I setting something up incorrectly, or just misunderstanding how to use semaphores in this situation?
Edit to add: sem_wait() encounters the error regardless of whether the child processes call sem_post() before or after the wait.
The semaphores allocated with sem_t* sems = new sem_t[numsems]; are not in the shared memory. Therefore each process has its own copy, and posting in the child doesn't affect the parent.
The parent's copy remains locked. sem_trywait fails with EAGAIN, which translates to resource temporarily unavailable explanation.
Ok I did some research and I couldn't turn up anything useful. I am trying to write a program that will receive input from iwconfig (on a linux machine). It will then sort through the input, do some calculations and output to a database. Sorting through the input and outputting isn't an issue (or so I really hope it not to be) but what I am struggling with is reading input from another command line program. What I have right now as a base Hello World program is:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
int numbr = 0;
cout << "Hello world!" << endl;
cin >> numbr;
cout << "number is " << numbr;
cout << system("iwconfig");
return 0;
}
However upon running the program, all it does is output hello world, ask for my random input and output it again. It does not output iwconfig (I also ran the line as just system("iwconfig"); without the output statement). Would someone be kind enough to explain how I could run a program like iwconfig and capture it's output?
"Would someone be kind enough to explain how I could run a program like iwconfig and capture it's output?"
Check the int system( const char *command ); documentation. It certainly doesn't provide to return the value, you want to output with your cout statement.
You probably want to have pipes established between your main and the iwconfig program, as described here, to control the input and output streams used by the child process.
To replicate the mentioned answer adapted:
int main() {
int fd_p2c[2], fd_c2p[2], bytes_read;
pid_t childpid;
char readbuffer[80];
string program_name = "iwconfig";
string receive_output = "";
if (pipe(fd_p2c) != 0 || pipe(fd_c2p) != 0) {
cerr << "Failed to pipe\n";
exit(1);
}
childpid = fork();
if (childpid < 0) {
cout << "Fork failed" << endl;
exit(-1);
}
else if (childpid == 0) {
if (dup2(fd_p2c[0], 0) != 0 ||
close(fd_p2c[0]) != 0 ||
close(fd_p2c[1]) != 0) {
cerr << "Child: failed to set up standard input\n";
exit(1);
}
if (dup2(fd_c2p[1], 1) != 1 ||
close(fd_c2p[1]) != 0 ||
close(fd_c2p[0]) != 0) {
cerr << "Child: failed to set up standard output\n";
exit(1);
}
execl(program_name.c_str(), program_name.c_str(), (char *) 0);
cerr << "Failed to execute " << program_name << endl;
exit(1);
}
else {
close(fd_p2c[0]);
close(fd_c2p[1]);
cout << "Writing to child: <<" << gulp_command << ">>" << endl;
int nbytes = gulp_command.length();
if (write(fd_p2c[1], gulp_command.c_str(), nbytes) != nbytes) {
cerr << "Parent: short write to child\n";
exit(1);
}
close(fd_p2c[1]);
while (1) {
bytes_read = read(fd_c2p[0], readbuffer, sizeof(readbuffer)-1);
if (bytes_read <= 0) break;
readbuffer[bytes_read] = '\0';
receive_output += readbuffer;
}
close(fd_c2p[0]);
cout << "From child: <<" << receive_output << ">>" << endl;
}
return 0;
}