-
question
i made a program that keeps going until user press ctrl+c i am wonder if its possible to make it run in background so the user can still type linux commands even though the prog is still running, and user can still run other programs without stopping it. how can i do this?
-
Hook it to the clock tick ISR (0x1c) with the setvect function found in dos.h. I've can't be bothered to put the full code up. Investigate.
-
i cant
i cant do that cause im coding in linux :(
is there any other ways to do it?
-
-
> i am wonder if its possible to make it run in background
Read your shell manual page
> prog
This runs the program, but holds the input
> prog &
This runs the program in the background, allowing you to continue
-
hmmm
ok thanks that was helpful, but what i actually wanted to do was make my code automatically do it without the user having to put any parametres or anything when running it. because what im making is a server, but when the server waits to recieve data it just sits there, and i want to be able to let the user still do commands and things while the server runs quietly in the background :)
if thats possible or if anyone knows how to do it please relply :)
thanks!
-
What you could do is write a shell script to just prepare your program and then execute it in the background
Code:
#!/bin/bash
clear;
echo "Starting Server..."
./server &
I hope that is what you were looking towards. It's not a very portable idea, but if you are just looking for one executable that will start your server in the background, this should do it.
-
That's easy enough
Code:
int main ( ) {
if ( fork() == 0 ) {
// the rest of your code
}
return 0;
}
This creates two copies of your program, but only the child goes on to do anything, whilst the parent (the one you ran) exits immediately and returns your prompt
You might want to tidy up where stdin, stdout and stderr go.
If it has no stdio, then do this at the start of the child process.
close ( 0 ); close ( 1 ) ; close ( 2 );
-
didn't work?
hmm the fork() idea sounded great, but when i did it it didn't return a prompt :(
can anyone help here?
also, ive heard of daemon() and i read the (small) manpage but i didn't really get that stuff about non-zero and stuff, would this function work? if so anyone know of any examples?
thanks alot :)
-
heres code
ok heres what ive got, my real code doesn't actually print anything to the screen after the server started part. if your on a DOS-type system you may need to edit it a bit if you need to compile cause its actually written for linux.
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
int sockfd, new_fd, sin_size, errno = 0;
char *buf[1024], *buf2[1024], *result[256];
#define MYPORT 8276
#define backlog 20
struct sockaddr_in my_addr;
struct sockaddr_in their_addr;
int main(int argc, char **argv)
{
if (fork() == 0) {
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
printf("Socket error.\n\n");
exit(1); }
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(MYPORT);
my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
memset(&(my_addr.sin_zero), '\0', 8);
bind(sockfd, (struct sockaddr *)&my_addr, sizeof(struct sockaddr));
if (errno != 0) {
printf("Bind error.\n\n");
exit(1); }
if ((listen(sockfd, backlog)) != 0) {
printf("Listen error.\n\n");
exit(1); }
printf("server has started :)\n\n");
sin_size = sizeof(struct sockaddr_in);
newsock:
new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
send(new_fd, "Welcome to server\n\n", 21, 0);
inputget:
memset(buf, 0, sizeof(buf));
if ((recv(new_fd, buf, sizeof(buf), 0)) == 0) {
close(new_fd);
goto newsock; }
printf("recieved: %s\n\n", buf);
send(new_fd, "got buffer :)\n", 31, 0);
goto inputget; }
return 0;
}
-
1. char *buf[1024], *buf2[1024], *result[256];
You do not mean this - these are arrays of pointers to char, not arrays of char
char buf[1024], buf2[1024], result[256];
> my real code doesn't actually print anything to the screen after the server started part
So it spawned the task OK then. As for the networking stuff, that's a different question.
1. Try printing to a log file (not stdout), to verify what is going on
2. recv doesn't reassemble whole messages - this you have to do yourself
> send(new_fd, "Welcome to server\n\n", 21, 0);
> send(new_fd, "got buffer \n", 31, 0);
Check your counting of characters