Thread: info on a program copying itself

  1. #1
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198

    info on a program copying itself

    I just don't understand how a program is able to make a copy of itself if it is running. Does anybody know of any sites or resources that at least explain the concept or something? The way I think it would work would be impossible... if you typed the source code into the source code and then printed that to a file and then compiled that... but then you'd have to infinitely type that code and keep nesting and nesting, it just wouldn't work. Otherwise a running program isn't accessible to copy... Maybe it's too complicated for me to understand right now but i'd at least like to try.

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Originally posted by linucksrox
    typed the source code into the source code
    What?
    If you mean how does a program fork() and stuff, you have to go to the kernel level. The kernel gets some memory and "copies" the stack of the program and the eac register (probably the wrong register, the one that has the executing code) and you are done. It also schedule()s the new process where it begins execution.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    haha, i didn't know how to ask... ok so in lame terms it actually copies the entire program from memory? i didn't know that was possible but it makes sense.
    Last edited by linucksrox; 05-11-2004 at 04:47 PM.

  4. #4
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    a program is just code in memory that the cpu can interpret as instructions, so it just copies the program (the instructions) to the other part of memory and you have a clone() (a linux system call).

  5. #5
    Advanced Novice linucksrox's Avatar
    Join Date
    Apr 2004
    Location
    Michigan
    Posts
    198
    ok, sounds pretty simple
    actually speaking of linux (the names hah!), i couldn't get ANYTHING to compile using gcc. i must be doing something wrong or i must have to configure gcc first or something... I mean the hello world program wouldn't even work. i was using what came with fedora core 1

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    have you tried
    Code:
    gcc -o helloworld helloworld.c -Wall -pedantic
    ??
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  7. #7
    Intmainer
    Join Date
    May 2004
    Posts
    7
    Quote Originally Posted by linucksrox
    I just don't understand how a program is able to make a copy of itself if it is running. Does anybody know of any sites or resources that at least explain the concept or something? The way I think it would work would be impossible... if you typed the source code into the source code and then printed that to a file and then compiled that... but then you'd have to infinitely type that code and keep nesting and nesting, it just wouldn't work. Otherwise a running program isn't accessible to copy... Maybe it's too complicated for me to understand right now but i'd at least like to try.
    yeh, I know it is a weird concept, but all you have to do to copy is just use the system command to call the xcopy function...if you are running windows...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  3. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  4. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM