Thread: How to never exit

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    10

    How to never exit

    Hi guys

    This may sound stupid but how would I make a C program from display a single message, never exit and make sure no one can kill the program using Ctrl+C or Ctrl+Z to background it.

    Thanks!

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    the operating system pretty much controls the ability to kill a process. If you're talking unix, they can pretty much always kill it with the 'kill' instruction if they have permission. From windows you have the task manager
    Last edited by FillYourBrain; 12-14-2006 at 06:14 PM.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    Thanks for the reply.

    In my situation no one would have access to 'kill'

    Any idea how to have the program display a message and not automatically exit?

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Do a loop around the code and wait for the user to exit

    Code:
    while ( !=getchar() )
    {
       printf("Look at me...");
    }
    Double Helix STL

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Maybe something along this line.
    Code:
    #include <stdio.h>
    #include <signal.h>
    
    int main (void)
    {
       signal(SIGINT, SIG_IGN); 
       puts("message");
       fflush(stdout);
       for ( ;; ) ;
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    You are both geniuses!

    Thank you very much!

    I still have a lot to learn.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    There ought to be a system-dependant way of blocking the console (like what happens for input) and avoiding a loop too. Double check your signals and experiment.

  8. #8
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    On Unix you might try
    Code:
    main(){while(!fork())sleep(1);}
    This should flood your PID list.

    Either way, try to make it a system service or some critical driver and you can make it rather hard to get rid of.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >display a single message, never exit and make sure no one can kill the program using Ctrl+C or Ctrl+Z to background it.
    I'd love to know what benign purpose this behavior has.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    Hi jafet thank you I will try that too.

    Hi Prelude

    I don't know why you are attacking me I'm just trying to make it so no one can steal a script I've made by being able to get into the console and view other files

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The code jafet posted has only one purpose: demonstrate how a Unix system can be brought down. REALLY brought down. Modify it slightly and it will expand exponentially.

    Dave's code does busy waiting forever. It'll eat up all of your CPU power.

    I believe swgh's code is also busy waiting. In addition, it floods the console with messages.

    None of these have any benign purpose whatsoever. They are dangerous to system stability and interfere with the proper function of the system. The posts should all be deleted.


    If you want to achieve what you're doing here on Unix, create a system user that has your script set as the login program. Then, if anyone kills it, all he gets is a login prompt. Suspending simply won't work. You could then modify the system configuration in such a way that before login, it writes a welcome message telling people finding the screen that a previous idiot user tried to kill the program and they should login with the username and password supplied.


    Oh, and Prelude is not attacking you.
    Last edited by CornedBee; 12-15-2006 at 04:25 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    10
    Hi CornedBee,

    Thanks for your input. I'm not trying to create any dangerous code here.
    I thought Prelude's comment was meant to be sarcastic.

    Are the first 2 bits of code as dangerous as jafet's?

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I thought Prelude's comment was meant to be sarcastic.
    The only practical use I could think of for a program that never ends and can't be shut down is clearly malicious. It's certainly possible that my experience has blinded me to benign uses, so I decided to ask you for clarification. I'm sorry if it sounded like an attack.

    >I'm just trying to make it so no one can steal a script I've made by being able to get into the console and view other files
    Uh, there are much better ways to copy protect your programs than being completely antisocial.
    My best code is written with the delete key.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Quote Originally Posted by newcguy
    Are the first 2 bits of code as dangerous as jafet's?
    No. They just slow the computer to a crawl, and if they have a high priority, they can be nearly as dangerous. However, it takes only a simple kill command to eliminate the process.
    The problem with jafet's is that it continually spawns new processes. It would take a killall that's fast enough or a kill of a whole process group to destroy it.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Chat Program Help?
    By Paul22000 in forum Networking/Device Communication
    Replies: 9
    Last Post: 02-10-2009, 12:35 AM
  2. Odd memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 05-25-2006, 12:56 AM
  3. Replies: 1
    Last Post: 03-21-2006, 07:52 AM
  4. Dynamic array of pointers
    By csisz3r in forum C Programming
    Replies: 8
    Last Post: 09-25-2005, 02:06 PM
  5. exit() ?
    By smd in forum C++ Programming
    Replies: 4
    Last Post: 06-26-2002, 04:31 PM