linux-assembly.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* sys_fork
@ 2005-11-26 17:06 HIToC
  2005-11-26 17:23 ` sys_fork Ricardo Nabinger Sanchez
  2005-11-26 19:26 ` sys_fork Claudio Fontana
  0 siblings, 2 replies; 12+ messages in thread
From: HIToC @ 2005-11-26 17:06 UTC (permalink / raw)
  To: linux-assembly

Hello list,
	I have written a small echo program that listen for a TCP
incoming connection on a port, then send all data received.

This program can accept only a session at time: I would to
handle multiple connections using fork(), but in assembly this
way is little difficult!
How to hold the controll over the child genereted?

Can anyone help me?
Thanks for every suggestion.
-- 
With regards,


					HIToC
					hitoc_mail@yahoo.it

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: sys_fork
  2005-11-26 17:06 sys_fork HIToC
@ 2005-11-26 17:23 ` Ricardo Nabinger Sanchez
  2005-11-26 19:26 ` sys_fork Claudio Fontana
  1 sibling, 0 replies; 12+ messages in thread
From: Ricardo Nabinger Sanchez @ 2005-11-26 17:23 UTC (permalink / raw)
  To: linux-assembly

Quoting  HIToC <hitoc_mail@yahoo.it>
Sent on  Sat, 26 Nov 2005 18:06:03 +0100

> How to hold the controll over the child genereted?

with syscalls, like one would normally do with C/C++.  :)

have a look under /usr/include/bits/syscall.h, and have fun!

Regards. :)

-- 
Ricardo Nabinger Sanchez
GNU/Linux #140696 [http://counter.li.org]
Slackware Linux + FreeBSD

  Left to themselves, things tend to go from bad to worse.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: sys_fork
  2005-11-26 17:06 sys_fork HIToC
  2005-11-26 17:23 ` sys_fork Ricardo Nabinger Sanchez
@ 2005-11-26 19:26 ` Claudio Fontana
  2005-11-27 18:22   ` sys_fork HIToC
  1 sibling, 1 reply; 12+ messages in thread
From: Claudio Fontana @ 2005-11-26 19:26 UTC (permalink / raw)
  To: HIToC; +Cc: linux-assembly


--- HIToC <hitoc_mail@yahoo.it> ha scritto: 

> Hello list,
> 	I have written a small echo program that listen for
> a TCP
> incoming connection on a port, then send all data
> received.
> 
> This program can accept only a session at time: I
> would to
> handle multiple connections using fork(),

also consider using select(2), which is syscall 82 or
poll (kernel 2.2+) which is 168 for descriptor
multiplexing to avoid process creation overhead.

> but in assembly this
> way is little difficult!
> How to hold the controll over the child genereted?
> 
> Can anyone help me?
> Thanks for every suggestion.

Bye,

CLaudio



	

	
		
___________________________________ 
Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB 
http://mail.yahoo.it

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: sys_fork
  2005-11-26 19:26 ` sys_fork Claudio Fontana
@ 2005-11-27 18:22   ` HIToC
  2005-11-27 19:27     ` sys_fork Ricardo Nabinger Sanchez
  0 siblings, 1 reply; 12+ messages in thread
From: HIToC @ 2005-11-27 18:22 UTC (permalink / raw)
  To: Claudio Fontana; +Cc: linux-assembly

Using select(2) is a possible solution; but I am interested
to understand how to use the fork() system call..
For example, I wish to code in assembly language the
following C code [Beej's Guide to Network Programming]:

//...
   while(1) {  // main accept() loop
            sin_size = sizeof(struct sockaddr_in);
            if ((new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size)) == -1) 
            {
                perror("accept");
                continue;
            }

            if (!fork()) { // this is the child process
                close(sockfd); // child doesn't need the listener

		// do something with new_fd socket descriptor

                close(new_fd);
                exit(0);
            }
            close(new_fd);  // parent doesn't need this
}

//...


My problem is to tell the OS to create a new child when
a connection is established.

HIToC


On Saturday 26 November 2005 20:26, Claudio Fontana wrote:
> --- HIToC <hitoc_mail@yahoo.it> ha scritto:
> > Hello list,
> > 	I have written a small echo program that listen for
> > a TCP
> > incoming connection on a port, then send all data
> > received.
> >
> > This program can accept only a session at time: I
> > would to
> > handle multiple connections using fork(),
>
> also consider using select(2), which is syscall 82 or
> poll (kernel 2.2+) which is 168 for descriptor
> multiplexing to avoid process creation overhead.
>
> > but in assembly this
> > way is little difficult!
> > How to hold the controll over the child genereted?
> >
> > Can anyone help me?
> > Thanks for every suggestion.
>
> Bye,
>
> CLaudio
>
>
>
>
>
>
>
> ___________________________________
> Yahoo! Mail: gratis 1GB per i messaggi e allegati da 10MB
> http://mail.yahoo.it
> -
> To unsubscribe from this list: send the line "unsubscribe linux-assembly"
> in the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

-- 
With regards,


					HIToC
					hitoc_mail@yahoo.it

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: sys_fork
  2005-11-27 18:22   ` sys_fork HIToC
@ 2005-11-27 19:27     ` Ricardo Nabinger Sanchez
  2005-11-28 10:10       ` sys_fork HIToC
  0 siblings, 1 reply; 12+ messages in thread
From: Ricardo Nabinger Sanchez @ 2005-11-27 19:27 UTC (permalink / raw)
  To: linux-assembly

Quoting  HIToC <hitoc_mail@yahoo.it>
Sent on  Sun, 27 Nov 2005 19:22:35 +0100

> My problem is to tell the OS to create a new child when
> a connection is established.

you'll have a loop blocking on accept(), with a call to fork() in the
body.  the child will handle the connection, the parent will block again on
accept().

can you give more details on your problem?  where are you stuck?

-- 
Ricardo Nabinger Sanchez
GNU/Linux #140696 [http://counter.li.org]
Slackware Linux + FreeBSD

  Left to themselves, things tend to go from bad to worse.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: sys_fork
  2005-11-27 19:27     ` sys_fork Ricardo Nabinger Sanchez
@ 2005-11-28 10:10       ` HIToC
  2005-11-28 13:48         ` sys_fork Frank Kotler
  2005-11-28 19:25         ` sys_fork Ricardo Nabinger Sanchez
  0 siblings, 2 replies; 12+ messages in thread
From: HIToC @ 2005-11-28 10:10 UTC (permalink / raw)
  To: Ricardo Nabinger Sanchez; +Cc: linux-assembly

I have thi piece of code:

wait_new_con:	mov	eax, [sock_fd]
		mov		ebx, their_addr
		mov		ecx, sockaddr_size
		mov		[their_addr_len], ecx
		mov		ecx, their_addr_len

		push	eax
		push	ebx
		push	ecx
		call	accept


		test	eax, eax
		jns	st_connection
		mov		ecx, accept_error
		mov		edx, accept_error_len
		call	print_string
		mov		ebx, 0x01
		jmp	exit

st_connection:	mov	[new_sock], eax	; new socket saved!

		mov	eax, 0x02	; sys_fork
		xor	ebx, ebx
		int	0x80

		mov		eax, 0x06	; sys_close
		mov		ebx, [sock_fd]
		int	0x80


echo_loop:	mov	eax, [new_sock]
		mov	ebx, buffer
		push	eax
		push	ebx
		push	dword MAX_DATA
		push	dword 0
		call	recv

		push	eax

		mov	edi, val
		call	hex2string
		mov	ecx, bytes
		mov	edx, bytes_len
		call	print_string

		pop	eax

		cmp	eax, 0x00000000
		jz	close_conct

		mov ecx, eax
		mov	eax, [new_sock]
		mov	ebx, buffer
		push	eax
		push	ebx
		push	ecx
		push	dword 0
		call	send_all


		jmp	echo_loop




close_conct:	mov		eax, 0x06	; sys_close
		mov		ebx, [new_sock]
		int	0x80

		mov		eax, 0x01	; sys_exit	Child exit?
		xor	ebx, ebx
		int	0x80

		jmp	wait_new_con


		xor	ebx, ebx
exit:		mov	eax, 0x01	; sys_exit
		int	0x80


When the caller has closed the TCP connection, I want that the child
exits, but happens that the entire program exits.
Also, if another client attemps to connect my server, the connecion is
refued.



On Sunday 27 November 2005 20:27, Ricardo Nabinger Sanchez wrote:
> Quoting  HIToC <hitoc_mail@yahoo.it>
> Sent on  Sun, 27 Nov 2005 19:22:35 +0100
>
> > My problem is to tell the OS to create a new child when
> > a connection is established.
>
> you'll have a loop blocking on accept(), with a call to fork() in the
> body.  the child will handle the connection, the parent will block again on
> accept().
>
> can you give more details on your problem?  where are you stuck?

-- 
With regards,


					HIToC
					hitoc_mail@yahoo.it

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: sys_fork
  2005-11-28 10:10       ` sys_fork HIToC
@ 2005-11-28 13:48         ` Frank Kotler
  2005-11-28 19:25         ` sys_fork Ricardo Nabinger Sanchez
  1 sibling, 0 replies; 12+ messages in thread
From: Frank Kotler @ 2005-11-28 13:48 UTC (permalink / raw)
  To: linux-assembly

HIToC wrote:

...
> 		mov	eax, 0x02	; sys_fork
> 		xor	ebx, ebx
> 		int	0x80

I don't know much about socket programming, but I got sys_fork to work, 
once... At this point, you're "two places at once". If eax=0, you're the 
child (and want to do the echo_loop stuff, I guess). If eax is non-zero 
- and positive, we hope! - you're the parent, and eax is the PID of the 
child. In this case, I guess you want to jump back to the "accept" call. 
By not checking eax at this point, *both* the parent and child fall 
through into the echo_loop... and the sys_exit. At least, I *think* 
that's what's happening...

or eax, eax  ; or "test eax, eax"
js webefukt  ; might be an error!
jnz wait_new_con  ; parent
... ; child

See if something like that helps... There may be other things you need 
to do, besides just going back to "accept" (probably should clean up 
stack after the call?)...

Best,
Frank


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: sys_fork
  2005-11-28 10:10       ` sys_fork HIToC
  2005-11-28 13:48         ` sys_fork Frank Kotler
@ 2005-11-28 19:25         ` Ricardo Nabinger Sanchez
  2005-11-29 18:59           ` sys_fork HIToC
  1 sibling, 1 reply; 12+ messages in thread
From: Ricardo Nabinger Sanchez @ 2005-11-28 19:25 UTC (permalink / raw)
  To: linux-assembly

Quoting  HIToC <hitoc_mail@yahoo.it>
Sent on  Mon, 28 Nov 2005 11:10:51 +0100

Hello,

as Frank said, you're not handling fork() result.

> st_connection:	mov	[new_sock], eax	; new socket
> saved!
> 
> 		mov	eax, 0x02	; sys_fork
> 		xor	ebx, ebx
> 		int	0x80

you should test here whether eax is < 0 (error), 0 (child) or > 0 (parent)
and act accordingly.  but both processes are executing this right away:

> 
> 		mov		eax, 0x06	; sys_close
> 		mov		ebx, [sock_fd]
> 		int	0x80

and closing their listening socket (the one which is blocked on accept()).

do the program exit with error (EINVAL)?

-- 
Ricardo Nabinger Sanchez
GNU/Linux #140696 [http://counter.li.org]
Slackware Linux + FreeBSD

  Left to themselves, things tend to go from bad to worse.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: sys_fork
  2005-11-28 19:25         ` sys_fork Ricardo Nabinger Sanchez
@ 2005-11-29 18:59           ` HIToC
  2005-11-30  0:20             ` sys_fork Ricardo Nabinger Sanchez
  0 siblings, 1 reply; 12+ messages in thread
From: HIToC @ 2005-11-29 18:59 UTC (permalink / raw)
  To: linux-assembly; +Cc: Konstantin Boldyshev

Yes, the problem was that I do not considered the value in EAX!
A possible solution to free system resources used by chlid, is to call
sys_waitpid with argument pid setted to 0 and waiting until the
value returned in EAX is greater than 0...
Now I understand some more thing over the fork system call!!

Thanks a lot!!
HIToC


On Monday 28 November 2005 20:25, Ricardo Nabinger Sanchez wrote:
> you should test here whether eax is < 0 (error), 0 (child) or > 0 (parent)


-- 
With regards,


					HIToC
					hitoc_mail@yahoo.it

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: sys_fork
  2005-11-29 18:59           ` sys_fork HIToC
@ 2005-11-30  0:20             ` Ricardo Nabinger Sanchez
  2005-11-30  4:00               ` sys_fork Robert Plantz
  0 siblings, 1 reply; 12+ messages in thread
From: Ricardo Nabinger Sanchez @ 2005-11-30  0:20 UTC (permalink / raw)
  To: linux-assembly

Quoting  HIToC <hitoc_mail@yahoo.it>
Sent on  Tue, 29 Nov 2005 19:59:56 +0100

Hello,

> Yes, the problem was that I do not considered the value in EAX!
> A possible solution to free system resources used by chlid, is to call
> sys_waitpid with argument pid setted to 0 and waiting until the
> value returned in EAX is greater than 0...
> Now I understand some more thing over the fork system call!!

that's nice, have you studied man 2 fork already? :)

I wouldn't recommend you to call wait4pid, because your process (the accept
() one) will block, and thus fork() will not be an advantage (you can do
that naturally without forking).

What would be a nice thing to do is to prepare a signal handler for
SIG_CHILD, to clean up its children.  It is very single to use, all you
need is to tell the kernel to use your custom function for handling
SIG_CHILD, instead of the default action (which is to ignore the signal,
and you'll end with a bunch of zombies).

Btw, such a function is like this in C:

void handle_sigchild() {
	wait();
}

notice that the process will not block, as the signal is being delivered
because of a terminating child.  it will be cleaned even if the process is
blocked in accept().

I'm almost sure that you'll also need to handle interrupted accept(), which
will return -1 and errno will have EINTR (interrupted system call).  in
this case, call accept() again :)

have fun :)

-- 
Ricardo Nabinger Sanchez
GNU/Linux #140696 [http://counter.li.org]
Slackware Linux + FreeBSD

  Left to themselves, things tend to go from bad to worse.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: sys_fork
  2005-11-30  0:20             ` sys_fork Ricardo Nabinger Sanchez
@ 2005-11-30  4:00               ` Robert Plantz
  2005-11-30 10:26                 ` sys_fork Ricardo Nabinger Sanchez
  0 siblings, 1 reply; 12+ messages in thread
From: Robert Plantz @ 2005-11-30  4:00 UTC (permalink / raw)
  To: linux-assembly

If you're going to use signals (a good idea), you should get a good book,  
like
Stevens or Robbins & Robbins. The signal coverage in Robbins & Robbins
is more up to date than the orginial Stevens. I haven't seen the second  
edition
of Stevens, but the descriptions say that signal coverage has been  
expanded.


On Tue, 29 Nov 2005 16:20:13 -0800, Ricardo Nabinger Sanchez  
<rnsanchez@terra.com.br> wrote:

> Quoting  HIToC <hitoc_mail@yahoo.it>
> Sent on  Tue, 29 Nov 2005 19:59:56 +0100
>
> Hello,
>
> I wouldn't recommend you to call wait4pid, because your process (the  
> accept
> () one) will block, and thus fork() will not be an advantage (you can do
> that naturally without forking).
>
> What would be a nice thing to do is to prepare a signal handler for
> SIG_CHILD, to clean up its children.  It is very single to use, all you
> need is to tell the kernel to use your custom function for handling
> SIG_CHILD, instead of the default action (which is to ignore the signal,
> and you'll end with a bunch of zombies).
>
> Btw, such a function is like this in C:
>
> void handle_sigchild() {
> 	wait();
> }
>
> notice that the process will not block, as the signal is being delivered
> because of a terminating child.  it will be cleaned even if the process  
> is
> blocked in accept().
>
> I'm almost sure that you'll also need to handle interrupted accept(),  
> which
> will return -1 and errno will have EINTR (interrupted system call).  in
> this case, call accept() again :)
>
> have fun :)
>

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: sys_fork
  2005-11-30  4:00               ` sys_fork Robert Plantz
@ 2005-11-30 10:26                 ` Ricardo Nabinger Sanchez
  0 siblings, 0 replies; 12+ messages in thread
From: Ricardo Nabinger Sanchez @ 2005-11-30 10:26 UTC (permalink / raw)
  To: linux-assembly

Quoting  "Robert Plantz" <plantz@sonoma.edu>
Sent on  Tue, 29 Nov 2005 20:00:21 -0800

> I haven't seen the second edition
> of Stevens, but the descriptions say that signal coverage has been  
> expanded.

if you mean W. Richard Stevens, the second edition is up-to-date.  and it's
very nice, the new authors made an excellent job.

-- 
Ricardo Nabinger Sanchez
GNU/Linux #140696 [http://counter.li.org]
Slackware Linux + FreeBSD

  Left to themselves, things tend to go from bad to worse.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2005-11-30 10:26 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-11-26 17:06 sys_fork HIToC
2005-11-26 17:23 ` sys_fork Ricardo Nabinger Sanchez
2005-11-26 19:26 ` sys_fork Claudio Fontana
2005-11-27 18:22   ` sys_fork HIToC
2005-11-27 19:27     ` sys_fork Ricardo Nabinger Sanchez
2005-11-28 10:10       ` sys_fork HIToC
2005-11-28 13:48         ` sys_fork Frank Kotler
2005-11-28 19:25         ` sys_fork Ricardo Nabinger Sanchez
2005-11-29 18:59           ` sys_fork HIToC
2005-11-30  0:20             ` sys_fork Ricardo Nabinger Sanchez
2005-11-30  4:00               ` sys_fork Robert Plantz
2005-11-30 10:26                 ` sys_fork Ricardo Nabinger Sanchez

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).