;================================================================================= ; sys_fork demonstration. ; ; Forks new child process, child then sleeps for 4 seconds while parent in the ; meanwhile waits for it to wake up. When child wakes up and exits, parent ; terminates as well. ; ; The LSCR Project. ;================================================================================= format ELF include '../macros.inc' include '../../include/symbols.inc' include '../../include/structs.inc' extrn error_chk extrn dd2ascii_hex section '.text' executable public _start _start: mov eax, SYS_FORK int 0x80 ; sys_fork returns 0 to the child process, ; child's PID to the parent process, or ERRNO on error. ccall error_chk, <"SYS_FORK has failed: ">, exit test eax, eax jne @f ;-----------------------------------------------; this block will be executed by child mov eax, SYS_GETPID int 0x80 ccall dd2ascii_hex, eax, child1_s+19 mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, child1_s mov edx, child1_s_sz int 0x80 mov [stime+timespec.tv_sec], 4 mov [stime+timespec.tv_nsec], 0 mov eax, SYS_NANOSLEEP ; sleep for 4 seconds mov ebx, stime mov ecx, 0 int 0x80 jmp exit ;-----------------------------------------------; this block will be executed by parent @@: push eax ccall dd2ascii_hex, eax, parent2_s+32 mov eax, SYS_GETPID int 0x80 ccall dd2ascii_hex, eax, parent1_s+20 mov eax, SYS_WRITE mov ebx, STDOUT mov ecx, parent1_s mov edx, parent1_s_sz int 0x80 mov eax, SYS_WAITPID ; wait for the child to wake up pop ebx xor ecx, ecx xor edx, edx int 0x80 exit: mov eax, SYS_EXIT xor ebx, ebx int 0x80 section '.data' writeable stime timespec child1_s db "CHILD: My PID is 0x00000000",0xa db "CHILD: I will sleep now for 4 seconds...",0xa child1_s_sz = $-child1_s parent1_s db "PARENT: My PID is 0x00000000",0xa parent2_s db "PARENT: And my child's PID is 0x00000000",0xa db "PARENT: Waiting for child to wake up and exit...",0xa parent1_s_sz = $-parent1_s