As the one in Linux, in which I can pass as parameters the function I want to execute in the child, the memory to be used, etc. I attach an example, in which I'm trying to start a child process that would execute the chld_func function using the memory allocated within stack_memory().
#include <iostream>
#include <sched.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
// ...
int main(int argc, char** argv)
{
printf("Hello, World! (parent)\n");
clone(chld_func, stack_memory(), SIGCHLD, 0);
wait(nullptr);
return EXIT_SUCCESS;
}
Maybe I could try to do something similar using fork(), but I don't know where to begin.
Thanks in advance!
As stated here and here clone is specific to Linux.
The macOS system calls you can do include fork and vfork, so you can use one of then.
See also this answer for some reasoning about clone and fork and read man pages:
http://man7.org/linux/man-pages/man2/clone.2.html
http://man7.org/linux/man-pages/man2/vfork.2.html
http://man7.org/linux/man-pages/man2/fork.2.html
Related
im new to sfml and c++.and I have a project that uses the sfml library's to draw the graphics but when I add an additional thread to my program it fails to execute the code inside the thread. this is my code:(please help me!)
#include <SFML\Graphics.hpp>
#include <SFML\window.hpp>
#include <SFML\system.hpp>
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int h(sf::RenderWindow* win){
//do something
win->close();
this_thread::sleep_for(chrono::milliseconds(10));
return 0;
}
int main(){
sf::RenderWindow window(sf::VideoMode(800,600),"My window");
thread t1(h,&window);
_sleep(10000000);
t1.join();
return 0;
}
http://www.sfml-dev.org/tutorials/2.0/graphics-draw.php#drawing-from-threads
SFML supports multi-threaded drawing, and you don't even have to do
anything to make it work. The only thing to remember is to deactivate
a window before using it in another thread; that's because a window
(more precisely its OpenGL context) cannot be active in multiple
threads at the same time.
call window.setActive(false); in your main(), before you pass it off to the thread.
And remember that you must handle events in the GUI thread (the main thread) for maximum portability.
I've started debbuging on some app, which hangs up in a loop based on readdir call.
Step by step I've cut everything but problem code, this is it:
So, in basic, it shows name of first entry and nothing more. It even does not exits, just waiting for something.
Also, I've found, that if don't lin it against libpocofoundation, it works.
But I have to do it because it used in the original app.
I'm a little bit confused, I don't use Poco in this example in any way, but it some way hangs it.
Please help me, I'm in panic :D
#include <iostream>
#include <sys/types.h>
#include <dirent.h>
#include <cstring>
#include <string>
#include <fcntl.h>
using namespace std;
int main(int argc, char *argv[])
{
const char TMP_DIR[] = "/opt";
DIR *dir = opendir(TMP_DIR);
std::cerr
<< readdir(dir)->d_name
<< readdir(dir)->d_name
<< std::endl;
return 0;
}
So... I don't know why it was happening. So I just dropped libpoco.
im new to sfml and c++.and I have a project that uses the sfml library's to draw the graphics but when I add an additional thread to my program it fails to execute the code inside the thread. this is my code:(please help me!)
#include <SFML\Graphics.hpp>
#include <SFML\window.hpp>
#include <SFML\system.hpp>
#include <iostream>
#include <thread>
#include <chrono>
using namespace std;
int h(sf::RenderWindow* win){
//do something
win->close();
this_thread::sleep_for(chrono::milliseconds(10));
return 0;
}
int main(){
sf::RenderWindow window(sf::VideoMode(800,600),"My window");
thread t1(h,&window);
_sleep(10000000);
t1.join();
return 0;
}
http://www.sfml-dev.org/tutorials/2.0/graphics-draw.php#drawing-from-threads
SFML supports multi-threaded drawing, and you don't even have to do
anything to make it work. The only thing to remember is to deactivate
a window before using it in another thread; that's because a window
(more precisely its OpenGL context) cannot be active in multiple
threads at the same time.
call window.setActive(false); in your main(), before you pass it off to the thread.
And remember that you must handle events in the GUI thread (the main thread) for maximum portability.
I got two console applications that the first one runs the second one:
1_first console application:
#include <Tchar.h>
#include <windows.h>
#include <iostream>
using namespace std;
void main(){
PROCESS_INFORMATION obj1;
memset(&obj1,0,sizeof(PROCESS_INFORMATION));
STARTUPINFOW obj2;
memset(&obj2,0,sizeof(STARTUPINFOW));
obj2.cb=sizeof(STARTUPINFOW);
CreateProcessW(_TEXT("c:\\runme.exe"),_TEXT("hello what's up?"),NULL,NULL,FALSE,NULL,NULL,NULL,&obj2,&obj1);
}
2_second console application named runme.exe:
#include <stdio.h>
#include <iostream>
using namespace std;
int main(int argc,char * * argv){
if (argc>0)
for (int i=0;i<argc;i++)
cout <<"**->**"<<argv[i]<<"\n";
}
Now my problem is that both applications will use the same command prompt window, what should I do to get them using separate ones ?
Pass CREATE_NEW_CONSOLE in the process creation flags (sixth parameter) when you call CreateProcess.
CreateProcessW(L"c:\\runme.exe",L"hello what's up?",NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&obj2,&obj1);
When you call CreateProcessW you do not want to use _TEXT on strings. CreateProcessW always takes wide strings, so you should always use an L prefix on them. _TEXT (or _T) is only for use with CreateProcess (no suffix), so it can change from narrow to wide strings based on whether you define UNICODE/_UNICODE.
Specifically, I need to call a version of exec that maintains the current working directory and sends standard out to the same terminal as the program calling exec. I also have a vector of string arguments I need to pass somehow, and I'm wondering how I would go about doing all of this. I've been told that all of this is possible exclusively with fork and exec, and given the terrible lack of documentation on the google, I've been unable to get the exec part working.
What exec method am I looking for that can accomplish this, and how do I call it?
If you have a vector of strings then you need to convert it to an array of char* and call execvp
#include <cstdio>
#include <string>
#include <vector>
#include <sys/wait.h>
#include <unistd.h>
int main() {
using namespace std;
vector<string> args;
args.push_back("Hello");
args.push_back("World");
char **argv = new char*[args.size() + 2];
argv[0] = "echo";
argv[args.size() + 1] = NULL;
for(unsigned int c=0; c<args.size(); c++)
argv[c+1] = (char*)args[c].c_str();
switch (fork()) {
case -1:
perror("fork");
return 1;
case 0:
execvp(argv[0], argv);
// execvp only returns on error
perror("execvp");
return 1;
default:
wait(0);
}
return 0;
}
You may be looking for execv() or execvp().
You don't necessarily need google to find this out, you should have the man command available so you can man fork and man exec (or maybe man 2 fork and man 3 exec) to find out about how the parameters to these system and library functions should be formed.
In Debian and Ubuntu, these man pages are in the manpages-dev package which can be installed using synaptic or with:
sudo apt-get install manpages-dev