question

This is a discussion on question within the C Programming forums, part of the General Programming Boards category; i made a program that keeps going until user press ctrl+c i am wonder if its possible to make it ...

  1. #1
    Unregistered
    Guest

    Question 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?

  2. #2
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    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.
    MSVC++ 6.0

  3. #3
    Unregistered
    Guest

    Unhappy i cant

    i cant do that cause im coding in linux

    is there any other ways to do it?

  4. #4
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Oh, yeah. Sorry.
    MSVC++ 6.0

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    > 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

  6. #6
    Unregistered
    Guest

    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!

  7. #7
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    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.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    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 );

  9. #9
    Unregistered
    Guest

    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

  10. #10
    Unregistered
    Guest

    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;
    }

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,822
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 03:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 06:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 01:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 10:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21