i am trying to change the directory of my linux terminal using c language. the thing is i am using threads. i have tried to use chdir() but its not working, n yes chdir() is also a process function.
Actually the thing i am trying to implement is, i am trying to make a multi threaded program which compiles Linux kernel, and that is not possible without specifying directories( i have tried to do it without specifying directories but failed :) )
can anybody help me out with this issue?
Thanks in advance :)
You can't! No process can change the working directory of another. A process can only change its own WD. When you launch an external command such like your C program, then a new process is launched.
The cd command of your shell is an internal one.
It isn't very clear what you are trying to achieve. It appears that somehow you wish to emulate the behaviour of a script (or the make tool) using a C program, that is to say having the C program you wrote launch new processes (using system or perhaps a combination of fork and exec*). If that is the case, then what you actually want is to modify the environment variables of these processes for them to find the files they need. A forked process will inherit the environment of its parent, so all you need is to use getenv, putenv, setenv and unsetenv to retrieve and set the environment variables you want to add, update or remove; or you may use the specific execve, execvpe which let you specify exactly what environment should be available to the new program.
references:
fork system call
exec family of system calls
unix environment
Related
(All programs running on Linux and through command line) So I want to be able to run a compiled C program from within my own C program and then be able to read the output of that compiled C program and have my C program provide input for that program. (Also assume I only have the compiled version of the other C program). I am aware of the system() function, but I am not sure if I can use that to read what the compiled Program outputs and then provide my own input.
You need to setup an inter-process communication mechanism between your two programs. There are several options to implement this:
Shared memory / shared files
Message Queues
Sockets
Pipes
You did not give a lot of background on your problem, but from your description it sounds that both your processes run in parallel and exchange data more than once. In this case, you will also have to think about synchronization issues.
A helpful introduction can be found here.
You can use system() like this:
system("executable arg1 ... > file.txt");
arg1 ... is the argument list to the executable. file.txt is the output of the executable redirected to a file. This system call can be made in a forked process. The parent program can wait on it. Synchronization tools can be used to enable concurrency if needed on the file file.txt.
I need to build a console app (A) that shows a menu where you can select an option and it will execute previous console apps (B,C,D...) that i built in class. How can i go about that? can i call these previous apps or do i need to add them into my program?, Because it needs to include about 15 previous projects and i think that's a lil' bit excesive. i know fOpen() is used to open txt files i don't know if it works with apps. FILE *fopen( const char * filename, const char * mode );
You will want to start a child process, then redirect its stdout to your own process' output, and its stdin to your own process' input - but presumably you'll want to intercept certain keystrokes to allow a user to terminate the child process without killing your own.
In a POSIX environment you would use popen() which gives you a pipe for redirecting input and output. On Windows you would use CreatePipe instead. Windows does not implement the POSIX popen() but it does have an internal function _popen but I understand it has different semantics.
But there is no function or capability in the C Standard Library to pipe between processes (C itself doesn't even require the computer to support a concept of a "process" either - it's a surprisingly platform-agnostic language and library - you can even use it for a platform without a malloc implementation).
Anyway, if you're okay with targeting just Windows, or for writing a wrapper library, I recommend you read this MSDN article which describes exactly what you're looking to do:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682499(v=vs.85).aspx
Creating a Child Process with Redirected Input and Output
The example in this topic demonstrates how to create a child process using the CreateProcess function from a console process. It also demonstrates a technique for using anonymous pipes to redirect the child process's standard input and output handles. Note that named pipes can also be used to redirect process I/O.
you can use the function system and path the program
example :
system ("ConsoleProgram2.exe"); // if it were in the same path
system() is a shell script method so you can use it as any way you want but people say that is a bad thing to use system function but in you'r case i don't see a problem.
This question already has answers here:
Closed 11 years ago.
Similar to:
program not executing anything after a call to system()
I am fairly new to using C but basically, I want to execute the following line:
int a = system("python -m plotter");
which will launch a python module I developed. However, I want the rest of my c program to keep running instead of waiting for the command to finish executing (the python app is on an infinite loop so it wont close automatically). Is there any way to do this using C/C++?
Update:
the solution was:
int a = system("start python -m plotter &");
system() simply passes its argument to the shell (on Unix-like systems, usually /bin/sh).
Try this:
int a = system("python -m plotter &");
Of course the value returned by system() won't be the exit status of the python script, since it won't have finished yet.
This is likely to work only on Unix-like systems (probably including MacOS); in particular, it probably won't work on MS Windows, unless you're running under Cygwin.
On Windows, system() probably invokes cmd.exe, which doesn't accept commands with the same syntax used on Unix-like systems. But the Windows start command should do the job:
int a = system("start python -m plotter");
As long as you're writing Windows-specific code (start won't work on Unix unless you happen to have a start command in your $PATH), you might consider using some lower-level Windows feature, perhaps by calling StartProcess. That's more complicated, but it's likely to give you more control over how the process executes. On the other hand, if system() meets your requirements, you might as well use it.
I believe if you add a '&' to the end of the command it will work. '&' tells the command to run in the background
int a = system("python -m plotter &");
There's no way in the standard library, but you can fork out or create a separate thread that would run it on the background, if your OS supports it.
"System" command on Linux will let the rest of code execute only when it has done executing itself.
You should use fork() if you want simultaneous processing.
The word for this is an Asynchronous function/method call. C and C++ don't have such a feature so you have to either call the function in a separate thread or use a system call that will run it in a separate process. Both of these methods tend to be platform specific, although there are cross platform threading libraries.
I know that in unix to do this in a separate process you would use fork(2) to create a new process and exec(3) to execute a new program in that process. I do not know what system calls would be required in Windows, but should be something similar.
I need my program go to some specific directory then run
system( ... ); there and get me back where I was before. How can I do it?
Just do the cd in your system call:
system("cd wherever; command");
The whole string gets passed to sh(1), so you can do any normal shell things in there. Since it's in a subshell, the working directory of your program won't be changed.
On Linux and Posix systems, the system(3) function is implemented above system calls like fork(2), execve(2), waitpid(2) and others. You could use the chdir(2) syscall to change the current directory. You could use getcwd(2) to retrieve (and memoize) it before changing it. (you could also call getcwd and chdir before system, then restore the original current directory with another chdir).
I suggest reading a good book like Advanced Linux Programming and Advanced Unix Programming, they have several chapters to answer your questions.
Don't expect a forum to explain all this to you. Read books.
Use chdir before calling system. You can even use getcwd before chdir to return to the directory after system.
Go to specific directory with chdir
Run system(...) (as Basile said, if system is taking user-defined variables, make sure to sanitize them)
When the program executes system(..), it will be in the changed directory. After system(...), it will be in the changed directory. When the program exits, it will be in the original directory.
I would like to write a small program that will run other programs. I'm not just trying to get their output as stdio for the current process, but rather want to simply use the program to use as a dispatch program.
I don't want to compile them together, but rather keep all the different programs separate.
I'm assuming that using a shell script would be the normal way of doing this, but I specifically want to know how it would be done in C on Linux.
You could do something like fork and use execve.
I'm not entirely understanding the problem though. Do you need the dispatcher to be able to read the output of the dispatched program?
You can use the system() API to call these other programs. What system() does is actually forks a shell and runs the program in that shell.
You can specify arguments to these external programs and even check their return status.
"man system" is your friend