From: adr <adr@SDF.ORG>
To: 9fans <9fans@9fans.net>
Subject: Re: [9fans] syscall silently kill processes
Date: Mon, 20 Jun 2022 05:59:58 +0000 (UTC) [thread overview]
Message-ID: <2b857fe-c6df-a6b8-40ec-566c85b7377d@SDF.ORG> (raw)
In-Reply-To: <ac1bc465-40c9-1f6b-3254-3ea6376e349@SDF.ORG>
On Mon, 20 Jun 2022, adr wrote:
> But I have something in mind for a case like
> this, when all the processes are going to use the same handler
> (that's why I was asking). Let me play with it a litle before I
> share it.
Ok, the idea is this: If in is bigger than zero in
threadnotify(int (*f)(void*, char*), int in), the handler is register
for the calling process. If in is 0, then the handler is cleared
for the calling process. If in is -1, the handler is register for
all processes and if in is less than -1, it is cleared for all
processes (expect for those who have already registered it for themselves).
Now back to your example, as all the processes are going to use the same handler,
you just have to register it once in threadmain:
#include <u.h>
#include <libc.h>
#include <thread.h>
static int
handler_alarm(void *, char *msg)
{
if(strstr(msg, "alarm"))
return 1;
return 0;
}
static void
proc_udp(void *)
{
char resp[512];
char req[] = "request";
int fd;
if((fd = dial("udp!185.157.221.201!5678", nil, nil, nil)) >= 0){
if(write(fd, req, strlen(req)) == strlen(req)){
fprint(1, "start\n");
alarm(2000);
read(fd, resp, sizeof(resp));
alarm(0);
fprint(1, "end\n");
}
close(fd);
}
threadexits(nil);
}
int mainstacksize = 5242880;
void
threadmain(int argc, char *argv[])
{
threadnotify(handler_alarm, -1);
for(int i = 0; i < 80; i++)
proccreate(proc_udp, nil, 10240);
sleep(5000);
threadexitsall(nil);
}
Now,
; ./5.out | grep end | wc -l
80
Are you happy Andrej?
adr.
/sys/src/libthread/sched.c:
[...]
if(t == nil){
_threaddebug(DBGSCHED, "all threads gone; exiting");
cancelnotes(p->pid);
_schedexit(p);
}
[...]
/sys/src/libthread/note.c
[...]
int
threadnotify(int (*f)(void*, char*), int in)
{
int i, frompid, topid;
int (*from)(void*, char*), (*to)(void*, char*);
if(in && in>-2){
from = nil;
frompid = 0;
to = f;
topid = (in == -1)? -1 : _threadgetproc()->pid;
lock(&onnotelock);
for(i=0; i<NFN; i++)
if(onnote[i]==to && onnotepid[i]==topid){
unlock(&onnotelock);
return i<NFN;
}
unlock(&onnotelock);
}else{
from = f;
frompid = (in < -1)? -1 : _threadgetproc()->pid;
to = nil;
topid = 0;
}
lock(&onnotelock);
for(i=0; i<NFN; i++)
if(onnote[i]==from && onnotepid[i]==frompid){
onnote[i] = to;
onnotepid[i] = topid;
break;
}
unlock(&onnotelock);
return i<NFN;
}
void
cancelnotes(int pid)
{
int i;
lock(&onnotelock);
for(i=0; i<NFN; i++)
if(onnotepid[i] == pid){
onnote[i] = nil;
onnotepid[i] = 0;
}
unlock(&onnotelock);
return;
}
static void
delayednotes(Proc *p, void *v)
{
int i;
Note *n;
int (*fn)(void*, char*);
if(!p->pending)
return;
p->pending = 0;
for(n=notes; n<enotes; n++){
if(n->proc == p){
for(i=0; i<NFN; i++){
if((onnotepid[i]!=p->pid && onnotepid[i]!=-1) || (fn = onnote[i])==nil)
continue;
if((*fn)(v, n->s))
break;
[...]
/sys/include/thread.h
[...]
void cancelnotes(int pid);
[...]
------------------------------------------
9fans: 9fans
Permalink: https://9fans.topicbox.com/groups/9fans/Tfa6823048ad90a21-Meda7cacd5cce9cc74c1ddaf8
Delivery options: https://9fans.topicbox.com/groups/9fans/subscription
next prev parent reply other threads:[~2022-06-20 6:00 UTC|newest]
Thread overview: 50+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-17 9:37 andrey100100100
2022-06-17 13:46 ` Thaddeus Woskowiak
2022-06-17 14:11 ` Jacob Moody
2022-06-17 14:39 ` Thaddeus Woskowiak
2022-06-17 15:06 ` andrey100100100
2022-06-17 16:08 ` Skip Tavakkolian
2022-06-17 16:11 ` Skip Tavakkolian
2022-06-17 16:16 ` Skip Tavakkolian
2022-06-17 17:42 ` adr
2022-06-17 16:11 ` Jacob Moody
2022-06-17 18:48 ` andrey100100100
2022-06-17 19:28 ` Jacob Moody
2022-06-17 21:15 ` adr
2022-06-18 6:40 ` andrey100100100
2022-06-18 8:37 ` adr
2022-06-18 9:22 ` adr
2022-06-18 12:53 ` Jacob Moody
2022-06-18 22:03 ` andrey100100100
2022-06-19 5:54 ` adr
2022-06-19 6:13 ` Jacob Moody
2022-06-18 22:22 ` andrey100100100
2022-06-18 16:57 ` andrey100100100
2022-06-19 2:40 ` adr
2022-06-19 5:01 ` adr
2022-06-19 8:52 ` andrey100100100
2022-06-19 10:32 ` adr
2022-06-19 11:40 ` andrey100100100
2022-06-19 12:01 ` andrey100100100
2022-06-19 15:10 ` andrey100100100
2022-06-19 16:41 ` adr
2022-06-19 21:22 ` andrey100100100
2022-06-19 21:26 ` andrey100100100
2022-06-20 4:41 ` adr
2022-06-20 5:39 ` andrey100100100
2022-06-20 5:59 ` adr [this message]
2022-06-20 15:56 ` andrey100100100
2022-06-20 22:29 ` Skip Tavakkolian
2022-06-21 7:07 ` andrey100100100
2022-06-21 11:26 ` adr
2022-06-21 13:03 ` andrey100100100
2022-06-21 13:22 ` adr
2022-06-28 15:28 ` adr
2022-06-28 16:43 ` ori
2022-06-28 18:19 ` adr
2022-06-28 18:28 ` adr
2022-06-28 19:09 ` andrey100100100
2022-06-28 19:42 ` adr
2022-06-29 13:14 ` adr
2022-06-21 13:47 ` andrey100100100
2022-06-21 7:22 ` adr
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2b857fe-c6df-a6b8-40ec-566c85b7377d@SDF.ORG \
--to=adr@sdf.org \
--cc=9fans@9fans.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).