executing code under a child process and another tcp question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I haven't written multi-threaded code in C for years but here's a few 
vague pointers from memory.

Calling fork() returns an integer, the status of that lets you know 
whether or not you're in the parent or child.  A common way of working 
therefore is something like:

int id = fork() ;

if (id == something)
{
/* in child */

execl( <path-to-executable>...) ;
/* error check here */
}
else
/* in parent */

So the parent carries on with the main code, the child executes  another 
executable, script or whatever and that is the child process code. 
Obviously the child code can just be embedded in the same source file but 
unless its really trivial it becomes unwieldy.

Pipes are pretty straightforward.  Create a pipe, use dup2 to hook up the 
parent stdout to the child stdin and vice versa and you have a very crude 
but workable way of passing info back and forth.

So, read up on fork(), execlp(), pipe() and dup2() and you should work it 
all out.

One point though - C is a really unfriendly language to learn things like 
multithreaded or socket programming.  If you want to learn the concepts 
(and with multi-threaded code it will take a very long time) as opposed to 
an immediate need to actually use it in C then I'd suggest doing your 
learning in another language.  The difficulties of the material are great 
enough without getting caught around the axle of the particular language 
idiosyncracies.

Regards,
Garry

-- 
Garry Turkington
garry.turkington at gmail.com

On Mon, 27 Mar 2006, tyler wrote:

> Hello everyone,,
> Though these questions may bwe boardering on the relm of programming, I
> wasn't really sure where else to ask them, I am aware of the
> blindprogramming list, but don't like to sign up to lists with high traffic
> like that one, because sometimes its hard for me to check my email, and when
> I get back (just from being on the blindgamers list, and the three lists
> owned by the same organization as the programming at blindprogramming) after
> not reading email for like 4 days, I had over 700 emails.
> Anyway, I have a couple questions,
> I read up on fork, and found that it isn't just something used to eat your
> food with, but you can spawn other processes with it.
> But, my main idea was a pipe, I wanted to spawn a child process, and create
> a pipe to it, so I read up on pipes to.
> The only thing I have been able to find is how to get my process to execute
> shell commands,
> Do I have to stick to shell commands, or can I actually switch execution
> between child and parent.
> Also, how do I get the pid of the parent process?
> Last, I want to start playing with TCP.
> If I send in the raw mode, can I just send a struct as a packet?
> If so, that will only take me reading up on how packetts are formed (I know
> the basics) but not the exact ways, and then I can just create a struct to
> represent a packet, and send it to where ever I wish. I also as a little
> example wish to create a ping program, and a port scanner, I can do the port
> scanner fairly easy I think, with minimal research, but what does the ping
> program do? I haven't been able to figure that one out, and I have used
> numerous packett sniffers.
> Things just look garbled.
> Thanks for your help,
>
>
> Tyler Littlefield.
> Check out our website:
> http://tysplace.the-leetest.net
> check out my blog:
> livejournal.com/~tylerrl
> [my programs don't have bugs, just randomly added features]
> [failure is not an option, it comes bundled with windows!]
>
>
> _______________________________________________
> Speakup mailing list
> Speakup at braille.uwo.ca
> http://speech.braille.uwo.ca/mailman/listinfo/speakup
>




[Index of Archives]     [Linux for the Blind]     [Fedora Discussioin]     [Linux Kernel]     [Yosemite News]     [Big List of Linux Books]
  Powered by Linux