My question is how would I implement sending arguments to a program running in the background? Can anyone point me in the right direction?

The two programs are running as separate processes, so you need to use IPC.

Do you know of any coding examples or tutorials. I am basically making a program that is kind of like an ssh client. But I will enter the commands from the shell, so what ever is imputed into the shell command will be sent to the client which will be running in the background.

Are you talking about *nix or MS-Windows?

well right now *nix, but I would like to maybe branch to ms-windows

On MS-Windows the background program(s) would be running as Windows Services. One way to communicate with a service is via COM programming. Of course the serice has to be written as a COM program in order for application programs to talk to it. One common type of service program is a database program. That is a very advanced programming, not for the faint of heart or for beginners. There are several books written about COM programming that will require a lot of studying.

Ok, well what about in *nix?? Wouldn't a way to achieve this is to have the program in the background to be looking for a file, that the other program will create with the command. If it finds a file, it opens it extracts the command and performs it. Is this a efficient way of doing this??

Ok, well what about in *nix?? Wouldn't a way to achieve this is to have the program in the background to be looking for a file, that the other program will create with the command. If it finds a file, it opens it extracts the command and performs it. Is this a efficient way of doing this??

What your talking about is a daemon..Here's a link on creating daemons.

http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html

Thanks a lot gerard. How would i communicate between the daemon and the user?? I know via the shell, but how would the daemon get the cmd?? Is the file method I suggested a good place to start?

Thanks a lot gerard. How would i communicate between the daemon and the user?? I know via the shell, but how would the daemon get the cmd?? Is the file method I suggested a good place to start?

If you want the process to run in the background independent of other processes then yes, this is a good starting place....

For communicating I would choose TCP sockets, having the daemon waiting in the background for a command to be received on its designated port.

If you use TCP sockets+daemon in this manner then you can pass the file and path to the daemon and have it act on the contents of the file....Just a suggestion.

Or maybe send it through a local namespace socket???

Or maybe send it through a local namespace socket???

The availability of your daemon really depends on your design criteria. If you want it available to the local machine only then yes explore the 'local namespace socket' or maybe unix sockets...If you want the daemon to be available to remote apps then you'll have to explore TCP/UDP sockets.

It is basically going to only be executed by the user from the local machine. Where would be a good place to look for help with the 'local namespace socket'

I found some old...old...old code I had. My apologizes to the language lawyers out there...

server.c

#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>

int main(int argc, char**argv)
{
	const int DEFAULT_PROTOCOL = 0;
	void *myvoid;
	char ch[] = "this is the server socket message!\n";
	int serverfd, serverlen, clientfd, clientlen;
	struct sockaddr_un serverunixaddress;
	struct sockaddr_un clientunixaddress;

	signal (SIGCHLD, SIG_IGN);

	struct sockaddr* serversockaddr;
	struct sockaddr* clientsockaddr;

	myvoid = &serverunixaddress;
	serversockaddr = (struct sockaddr*)(myvoid);
	serverlen = sizeof(serverunixaddress);

	myvoid = &clientunixaddress;
	clientsockaddr = (struct sockaddr*)(myvoid);
	clientlen = sizeof(clientunixaddress);

	serverfd = socket (AF_UNIX, SOCK_STREAM, DEFAULT_PROTOCOL);

	serverunixaddress.sun_family = AF_UNIX;
	strcpy(serverunixaddress.sun_path, "thesocket");

	unlink ("thesocket");

	bind (serverfd, serversockaddr, serverlen);

	listen (serverfd, 5);

	while (true)
	{
		clientfd = accept (serverfd, clientsockaddr, (socklen_t*)&clientlen);

		if (fork())
		{
			close (clientfd);
		}
		else
		{
			for (int i = 0; i < 1000; ++i)
				write (clientfd, ch, sizeof(ch));
			close (clientfd);
			return 0;
		}
	}
	
	return 0;
}

client.c

#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/types.h>
#include <unistd.h>

int main (int argc, char**argv)
{
	const int DEFAULT_PROTOCOL = 0;
	void *myvoid;
	char ch;
	int serverlen, clientfd, result, numread;
	struct sockaddr_un serverunixaddress;
	struct sockaddr* serversockaddr;

	myvoid = &serverunixaddress;
	serversockaddr = (struct sockaddr*)(myvoid);
	serverlen = sizeof(serverunixaddress);

	clientfd = socket (AF_UNIX, SOCK_STREAM, DEFAULT_PROTOCOL);
	
	serverunixaddress.sun_family = AF_UNIX;
	strcpy(serverunixaddress.sun_path, "thesocket");
	do
	{
		result = connect(clientfd, serversockaddr, serverlen);
		if (result == -1) sleep(1);
	}
	while (result == -1);
	do
	{
		numread = read(clientfd, (char*)&ch, sizeof(char));
		fputc(ch, stdout);
	}
	while (numread > 0);
	close (clientfd);
	return 0;
}

This should give you a starting place...Sorry for the lack of comments.

Note - you should be able to change AF_UNIX to PF_LOCAL and the programs should still work..

You could use sockets in the MS-Windows program too, so whatever you write for *nix is SORT OF portable to MS-Windows too.

Oh ok, thanks a lot for all your help. This should get me going in the right direction.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.