linux-assembly.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* multithreating HOWTO ? :)
@ 2009-08-22 16:48 £ukasz
  2009-08-23 10:29 ` Appendix: " £ukasz
  0 siblings, 1 reply; 9+ messages in thread
From: £ukasz @ 2009-08-22 16:48 UTC (permalink / raw)
  To: linux-assembly

Hi All.
I'm trying to start writing multithread programs with assembler (no pthreads), i want to just ask what is wrong in folowing simple code:


.section .data

signal1:
.quad	1

signal2:
.quad	1

x:
.quad	0

y:
.quad	0

.section .text
.globl _start
_start:
    nop
    movq	$2,%rax           # sys_fork
    xorq	%rbx,%rbx
    int		$0x80
    
    cmpq	$0,%rax
    jl		end
    
    cmpq	$0,%rax
    jg		parent            # jump if parent
    

child:
    movq	$100,x
    movq	$0,signal1

1:	
    cmpq	$0,signal2
    jnz		1b
    jmp 	end


parent:
    movq	$100,y
    movq	$0,signal2

1:	
    cmpq	$0,signal1
    jnz		1b            # INF. LOOP ?
    
end:
    movq        x,%rbx
    addq        y,%rby
    movq	$1,%rax
    int		$0x80

Program stacks at parent part in infinite loop waiting for "signal1" change to 0. Anybody knows how to write it properly with using two threads? Thanks in advance.

Lukas


      

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

* Appendix: multithreating HOWTO ? :)
  2009-08-22 16:48 multithreating HOWTO ? :) £ukasz
@ 2009-08-23 10:29 ` £ukasz
  2009-08-23 13:35   ` Claudio Fontana
  0 siblings, 1 reply; 9+ messages in thread
From: £ukasz @ 2009-08-23 10:29 UTC (permalink / raw)
  To: linux-assembly

in appendix i want to just precize problem. Broblem is how to write, create etc, shared memory for parent and child proceses, where they could for example exchange wariables

--- On Sat, 8/22/09, £ukasz <blurrpp@yahoo.com> wrote:

> From: £ukasz <blurrpp@yahoo.com>
> Subject: multithreating HOWTO ? :)
> To: linux-assembly@vger.kernel.org
> Date: Saturday, August 22, 2009, 6:48 PM
> Hi All.
> I'm trying to start writing multithread programs with
> assembler (no pthreads), i want to just ask what is wrong in
> folowing simple code:
> 
> 
> .section .data
> 
> signal1:
> .quad    1
> 
> signal2:
> .quad    1
> 
> x:
> .quad    0
> 
> y:
> .quad    0
> 
> .section .text
> .globl _start
> _start:
>     nop
>     movq    $2,%rax   
>        # sys_fork
>     xorq    %rbx,%rbx
>     int       
> $0x80
>     
>     cmpq    $0,%rax
>     jl        end
>     
>     cmpq    $0,%rax
>     jg       
> parent            # jump if
> parent
>     
> 
> child:
>     movq    $100,x
>     movq    $0,signal1
> 
> 1:    
>     cmpq    $0,signal2
>     jnz        1b
>     jmp     end
> 
> 
> parent:
>     movq    $100,y
>     movq    $0,signal2
> 
> 1:    
>     cmpq    $0,signal1
>     jnz       
> 1b            # INF. LOOP ?
>     
> end:
>     movq        x,%rbx
>     addq        y,%rby
>     movq    $1,%rax
>     int       
> $0x80
> 
> Program stacks at parent part in infinite loop waiting for
> "signal1" change to 0. Anybody knows how to write it
> properly with using two threads? Thanks in advance.
> 
> Lukas
> 
> 
>       
> --
> 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
> 


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

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

* Re: Appendix: multithreating HOWTO ? :)
  2009-08-23 10:29 ` Appendix: " £ukasz
@ 2009-08-23 13:35   ` Claudio Fontana
  2009-08-23 15:04     ` Frank Kotler
  0 siblings, 1 reply; 9+ messages in thread
From: Claudio Fontana @ 2009-08-23 13:35 UTC (permalink / raw)
  To: £ukasz; +Cc: linux-assembly

£ukasz wrote:
> in appendix i want to just precize problem. Broblem is how to write, create etc, shared memory for parent and child proceses, where they could for example exchange wariables

You are using fork. Fork is used to create new processes.
You probably want clone instead, to create new threads and specify
which resources to share between the two threads.

man 2 clone

sys_clone is 120 (0x78).


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

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

* Re: Appendix: multithreating HOWTO ? :)
  2009-08-23 13:35   ` Claudio Fontana
@ 2009-08-23 15:04     ` Frank Kotler
  2009-08-23 15:23       ` Claudio Fontana
  0 siblings, 1 reply; 9+ messages in thread
From: Frank Kotler @ 2009-08-23 15:04 UTC (permalink / raw)
  To: Claudio Fontana; +Cc: £ukasz, linux-assembly

Claudio Fontana wrote:
> £ukasz wrote:
>> in appendix i want to just precize problem. Broblem is how to write, 
>> create etc, shared memory for parent and child proceses, where they 
>> could for example exchange wariables
> 
> You are using fork. Fork is used to create new processes.
> You probably want clone instead, to create new threads and specify
> which resources to share between the two threads.
> 
> man 2 clone
> 
> sys_clone is 120 (0x78).

Is that a 64-bit sys_call number? I thought they'd changed that on us. 
AFAIK, __NR_clone is 56 these days... and "syscall" replaces int 0x80. A 
real "curve ball" to asm programmers! Well, they *told* us not to do it 
that way. :)

Best,
Frank

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

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

* Re: Appendix: multithreating HOWTO ? :)
  2009-08-23 15:04     ` Frank Kotler
@ 2009-08-23 15:23       ` Claudio Fontana
  2009-08-23 16:17         ` £ukasz
  2009-08-23 19:13         ` £ukasz
  0 siblings, 2 replies; 9+ messages in thread
From: Claudio Fontana @ 2009-08-23 15:23 UTC (permalink / raw)
  To: Frank Kotler; +Cc: £ukasz, linux-assembly

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="windows-1252", Size: 1125 bytes --]

On Sun, Aug 23, 2009 at 5:04 PM, Frank Kotler<fbkotler@zytor.com> wrote:
> Claudio Fontana wrote:
>>
>> Łukasz wrote:
>>>
>>> in appendix i want to just precize problem. Broblem is how to write,
>>> create etc, shared memory for parent and child proceses, where they could
>>> for example exchange wariables
>>
>> You are using fork. Fork is used to create new processes.
>> You probably want clone instead, to create new threads and specify
>> which resources to share between the two threads.
>>
>> man 2 clone
>>
>> sys_clone is 120 (0x78).
>
> Is that a 64-bit sys_call number? I thought they'd changed that on us.
> AFAIK, __NR_clone is 56 these days... and "syscall" replaces int 0x80. A
> real "curve ball" to asm programmers! Well, they *told* us not to do it that
> way. :)
>
> Best,
> Frank

I am still on good old 32bit. On 64 bit system call numbers are indeed
completely different.
--
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

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

* Re: Appendix: multithreating HOWTO ? :)
  2009-08-23 15:23       ` Claudio Fontana
@ 2009-08-23 16:17         ` £ukasz
  2009-08-23 19:18           ` Frank Kotler
  2009-08-23 21:02           ` linuxasm
  2009-08-23 19:13         ` £ukasz
  1 sibling, 2 replies; 9+ messages in thread
From: £ukasz @ 2009-08-23 16:17 UTC (permalink / raw)
  To: linux-assembly

thanks for answer, im experiment with system clone ( actualy fork i found in many manuals ) but i dont understand Frakns last sentence
"Well, they *told* us not to do it that way. :)", to program multiproceses with fork ? Any way is there any way to make share memory for proceses ?

Lukas

--- On Sun, 8/23/09, Claudio Fontana <claudio.fontana@gmail.com> wrote:

> From: Claudio Fontana <claudio.fontana@gmail.com>
> Subject: Re: Appendix: multithreating HOWTO ? :)
> To: "Frank Kotler" <fbkotler@zytor.com>
> Cc: "£ukasz" <blurrpp@yahoo.com>, linux-assembly@vger.kernel.org
> Date: Sunday, August 23, 2009, 5:23 PM
> On Sun, Aug 23, 2009 at 5:04 PM,
> Frank Kotler<fbkotler@zytor.com>
> wrote:
> > Claudio Fontana wrote:
> >>
> >> Łukasz wrote:
> >>>
> >>> in appendix i want to just precize problem.
> Broblem is how to write,
> >>> create etc, shared memory for parent and child
> proceses, where they could
> >>> for example exchange wariables
> >>
> >> You are using fork. Fork is used to create new
> processes.
> >> You probably want clone instead, to create new
> threads and specify
> >> which resources to share between the two threads.
> >>
> >> man 2 clone
> >>
> >> sys_clone is 120 (0x78).
> >
> > Is that a 64-bit sys_call number? I thought they'd
> changed that on us.
> > AFAIK, __NR_clone is 56 these days... and "syscall"
> replaces int 0x80. A
> > real "curve ball" to asm programmers! Well, they
> *told* us not to do it that
> > way. :)
> >
> > Best,
> > Frank
> 
> I am still on good old 32bit. On 64 bit system call numbers
> are indeed
> completely different.
> --
> 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
> 


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

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

* Re: Appendix: multithreating HOWTO ? :)
  2009-08-23 15:23       ` Claudio Fontana
  2009-08-23 16:17         ` £ukasz
@ 2009-08-23 19:13         ` £ukasz
  1 sibling, 0 replies; 9+ messages in thread
From: £ukasz @ 2009-08-23 19:13 UTC (permalink / raw)
  To: linux-assembly

more info for example.

http://stackoverflow.com/questions/807506/threads-vs-processes-in-linux

as u see with litle work i can create process (fork) from thread (clone), i have to make some changes in systme tables etc. now need to read little more kernel sys call source.

--- On Sun, 8/23/09, Claudio Fontana <claudio.fontana@gmail.com> wrote:

> From: Claudio Fontana <claudio.fontana@gmail.com>
> Subject: Re: Appendix: multithreating HOWTO ? :)
> To: "Frank Kotler" <fbkotler@zytor.com>
> Cc: "£ukasz" <blurrpp@yahoo.com>, linux-assembly@vger.kernel.org
> Date: Sunday, August 23, 2009, 5:23 PM
> On Sun, Aug 23, 2009 at 5:04 PM,
> Frank Kotler<fbkotler@zytor.com>
> wrote:
> > Claudio Fontana wrote:
> >>
> >> Łukasz wrote:
> >>>
> >>> in appendix i want to just precize problem.
> Broblem is how to write,
> >>> create etc, shared memory for parent and child
> proceses, where they could
> >>> for example exchange wariables
> >>
> >> You are using fork. Fork is used to create new
> processes.
> >> You probably want clone instead, to create new
> threads and specify
> >> which resources to share between the two threads.
> >>
> >> man 2 clone
> >>
> >> sys_clone is 120 (0x78).
> >
> > Is that a 64-bit sys_call number? I thought they'd
> changed that on us.
> > AFAIK, __NR_clone is 56 these days... and "syscall"
> replaces int 0x80. A
> > real "curve ball" to asm programmers! Well, they
> *told* us not to do it that
> > way. :)
> >
> > Best,
> > Frank
> 
> I am still on good old 32bit. On 64 bit system call numbers
> are indeed
> completely different.
> 


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

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

* Re: Appendix: multithreating HOWTO ? :)
  2009-08-23 16:17         ` £ukasz
@ 2009-08-23 19:18           ` Frank Kotler
  2009-08-23 21:02           ` linuxasm
  1 sibling, 0 replies; 9+ messages in thread
From: Frank Kotler @ 2009-08-23 19:18 UTC (permalink / raw)
  To: £ukasz; +Cc: linux-assembly

£ukasz wrote:
> thanks for answer, im experiment with system clone ( actualy fork i found in many manuals ) but i dont understand Frakns last sentence
> "Well, they *told* us not to do it that way. :)", to program multiproceses with fork ? Any way is there any way to make share memory for proceses ?

Just a "joke". I meant, using the int 80h (or syscall) interface. We're 
"supposed" to use the C interface.

Like Claudio, I'm a "32-bit guy" - and I don't know how to do 
multithreading even on 32-bit. But I think if you're going to use 64-bit 
registers, you'll have to use 64-bit sys_call numbers, put the 
parameters in regs compatible with the 64-bit ABI, and use "syscall" 
instead of "int 0x80". I don't think what you're doing will work, but 
I'm not equipped to try it.

You can share memory between processes by memmapping a region and 
flagging it MAP_SHARED (sys_fork or even separate executables). I 
*think* for threads, you'd want sys_clone, and all memory would be 
shared(?). I think your issue is going to be keeping your threads from 
read/writing the same memory at the same time. I'm a "single CPU guy", 
too, but I think multi-CPU is going to be "even worse".

I made an example using pthread_create - not that I want to use it 
either, but to see what it does for us. Easier to read the source, I'm 
sure, but no, I ran it in strace. Doing a bunch of "signal related" 
stuff I don't understand. I'm afraid I'm going to have to learn more 
about "signals" before I can proceed with this...

For those of us who need to learn about 64-bit programming, there's a 
tutorial:

<http://www.vikaskumar.org/wiki/index.php?title=X86-64_Tutorial>

Came across this, that looked interesting, too:

<http://milw0rm.org/papers/110>

Best,
Frank




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

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

* Re: Appendix: multithreating HOWTO ? :)
  2009-08-23 16:17         ` £ukasz
  2009-08-23 19:18           ` Frank Kotler
@ 2009-08-23 21:02           ` linuxasm
  1 sibling, 0 replies; 9+ messages in thread
From: linuxasm @ 2009-08-23 21:02 UTC (permalink / raw)
  To: linux-assembly

On Sunday 23 August 2009 09:17:59 £ukasz wrote:
> thanks for answer, im experiment with system clone ( actualy fork i found
> in many manuals ) but i dont understand Frakns last sentence "Well, they
> *told* us not to do it that way. :)", to program multiproceses with fork ?
> Any way is there any way to make share memory for proceses ?i

There are several ways to do shared memory.  If using a "fork"
the asmlib functions:
  shared_open
  shared_attach
  shared_close
provide some example code.
see http://linuxasmtools.net  download asmlib and look memory funcitons.
all the best, jeff
--
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

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

end of thread, other threads:[~2009-08-23 21:02 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-08-22 16:48 multithreating HOWTO ? :) £ukasz
2009-08-23 10:29 ` Appendix: " £ukasz
2009-08-23 13:35   ` Claudio Fontana
2009-08-23 15:04     ` Frank Kotler
2009-08-23 15:23       ` Claudio Fontana
2009-08-23 16:17         ` £ukasz
2009-08-23 19:18           ` Frank Kotler
2009-08-23 21:02           ` linuxasm
2009-08-23 19:13         ` £ukasz

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).