[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: fork(3am) confusion
From: |
Andrew J. Schorr |
Subject: |
Re: fork(3am) confusion |
Date: |
Thu, 25 Jul 2024 09:13:50 -0400 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
Hi,
The fork function does not take an argument.
Something like this might give results closer to
what you expect:
@load "fork"
function Child(N) {
printf Fmt, "Child", N, PROCINFO["pid"], PROCINFO["ppid"]
wait()
}
BEGIN {
Fmt = "%8s #%d: PID=%8d PPID=%8d\n"
for (i=1; i<=4; i++) {
if ((pid = fork()) == 0) {
# child process
printf Fmt,"Child",i,PROCINFO["pid"],PROCINFO["ppid"]
exit 0
}
else {
# parent process
child_pids[pid]
}
}
for (pid in child_pids)
printf "waitpid(%s) = %s\n", pid, waitpid(pid)
}
As for creating a network service, that may depend on whether
you intend to use TCP or UDP. The TCP API is fairly limited
when it comes to server functionality. In theory, rather
than forking, you could use the select(3am) extension to
create a multiplexing server, but nobody has yet written
the necessary BSD sockets library to make this possible.
There have also been thoughts in the past about how to
extend the existing socket implementation to get TCP servers
to work, but nobody has done the work.
If you're interested in working on extending gawk's TCP implementation or
writing a socket library, I'd be happy to offer some pointers.
Regards,
Andy
On Tue, Jul 23, 2024 at 10:22:39AM -0600, b3ak3r via Help-gawk wrote:
> Hello,
> I recently stumbled upon the Gawk fork(3am) extension and have been
> trying to use it in a loop without much success.
>
> # --
> @load "fork"
>
> function Child(N) {
> printf Fmt, "Child", N, PROCINFO["pid"], PROCINFO["ppid"]
> wait()
> }
>
> BEGIN {
> Fmt = "%8s #%d: PID=%8d PPID=%8d\n"
> # forks prior fork(s)..
> for (i=1; i<=4; i++) {
> fork(Child(i))
> }
> }
> # --
>
> Per the comment in the BEGIN section, this code appears to fork prior
> forks, and not all output gets displayed: piping the output to sort(1)
> shows 64 lines of output though only 15 lines are displayed
> otherwise. I'd like for the code to *not* do that and produce a
> single line out output per child, 4 lines total in this case.
>
> Probably I'm miss-understanding how fork(3am) is intended to be
> used. Also doing "fork(Child(i))" is likely wrong; the manpage does
> not indicate that fork() can take an argument though it appears to.
>
> My end goal in all this is to couple fork() with Gawk's Internet
> socket capability to create multi-threading in a simple network
> service. Is that a possibility with fork(3am) ? Also can someone
> point me towards examples of working gawk code utilizing fork(3am)?
>
> Regards,
> -B