Hi !
I'm just playing around with learning about threads. Somewhere in the internet I found a little example and changed it for my mind and it seems to work correct for now.
; Fork
; nasm -f elf64
; ld fork.o -o fork
; Run with: ./fork
[bits 64]
SECTION .data
childMsg db 'This is the child process', 0h ; a message string
clength equ $-childMsg
parentMsg db 'This is the parent process', 0h ; a message string
plength equ $-parentMsg
crlf db 0xA,0xD ; newline, length is 2
SECTION .text
global _start
_start:
mov rax, 57 ; SYS_FORK
syscall
cmp rax, 0 ; if eax is zero we are in the child process
jz child ; jump if eax is zero to child label
parent:
mov rax, 1 ; sys_write
mov rdi, 1 ; stdout
mov rsi, parentMsg ; message address
mov rdx, plength ; message string length
syscall
mov rax, 1 ; sys_write
mov rdi, 1 ; stdout
mov rsi, crlf ; message address
mov rdx, 2 ; message string length
syscall
call workalot
jmp exit
child:
mov rax, 1 ; sys_write
mov rdi, 1 ; stdout
mov rsi, childMsg ; message address
mov rdx, clength ; message string length
syscall
mov rax, 1 ; sys_write
mov rdi, 1 ; stdout
mov rsi, crlf ; message address
mov rdx, 2 ; message string length
syscall
call workalot
jmp exit
exit:
mov rax, 60 ; sys_exit
mov rdi, 0 ; return 0 (success)
syscall
; -------- unten nur proceduren ---------------------
workalot:
mov rcx, 65000
loop1:
push rcx
mov rcx, 65000
loop2:
push rcx
nop
pop rcx
loop loop2
pop rcx
loop loop1
ret
As you see, it just throws out two little messages in a fork and runs down a delay and stops.
Now, my question is, if I have dependencies in the threads at each other, maybe some calculations with needed sums at the end, than I have to wait for this.
I know, there exists something called
"SYSCALL_WAIT4"
but to be honest, I don't have a clue, how to realize that. :-/
Does anyone habe a nice example or can explain a little bit?
Greetings, Andy :-)