[849] in Pthreads mailing list archive
Get help
daemon@ATHENA.MIT.EDU (Zhanggaoqing)
Wed Aug 11 02:27:31 1999
From: "Zhanggaoqing" <gqzhang@cenpok.net>
To: <pthreads@MIT.EDU>
Date: Wed, 11 Aug 1999 14:12:43 +0800
This is a multi-part message in MIME format.
------=_NextPart_000_0034_01BEE403.93F822E0
Content-Type: text/plain;
charset="gb2312"
Content-Transfer-Encoding: quoted-printable
Dear Sr.
How can I send signal between threads in a process?
My developing platform is DEC Unix 4.0, I use pthread_kill(pthread_t =
id,int sig_id) to send signal to a thread, but a message "Hangup" will =
emerge when call the function above and all program will exit, why?
My program is:
#include <pthread.h>
pthread_t timer_id;
pthread_t scanner_id;
pthread_t runner_id;
pthread_attr_t attr;
sigset_t new;
void *timer_func(void *vp);
void *scanner_func(void *vp);
void *runner_func(void *vp);
main()
{
sigemptyset(&new);
sigaddset(&new,SIGUSR1);
pthread_sigmask(SIG_BLOCK, &new,NULL);*/
if(pthread_create(&timer_id,attr,timer_func,NULL) !=3D 0)
perror("Create timer");
if(pthread_create(&scanner_id,attr,scanner_func,NULL) !=3D 0)
perror("Create scanner");
if(pthread_create(&runner_id,attr,runner_func,NULL) !=3D 0)
perror("Create runner");
while(1)
sleep(10);
} /* end main */
void* timer_func(void *vp)
{
printf("in timer_func\n");
while(1)
{
if(pthread_kill(scanner_id,SIGUSR1) !=3D 0)
perror("timer");
sleep(8);
}
}
void *scanner_func(void *vp)
{
int sig_id;
printf("in scanner_func\n");
while(1)
{
sigwait(&new,&sig_id);
printf("scanner wait for %d\n",sig_id);
pthread_kill(runner_id,SIGUSR1);
}
}
void *runner_func(void *vp)
{
int sig_id;
printf("in runner_func\n");
while(1)
{
sigwait(&new,&sig_id);
printf("runner wait for %d\n",sig_id);
}
}
My another question is about "pthread_attr_t attr" in program above.
pthread_attr_t attr variable must be declared in global and can not be =
initialized(call "pthread_attr_init()"), otherwise pthread_create will =
abort(error message "Invalid parameters").=20
Compiler will report errors if call pthread_create as
pthread_create(&thread_id, NULL, timer_func, NULL), compiler's error is
""0" is of type "long", and may not be converted to "struct =
CMA_T_HANDLE". pthread_create(&t, NULL, sigint, NULL);".
Thanks lot!
My e-mail: teng_mt@188.net
Teng mintang, from Hangzhou P.R.China
1999/08/11
------=_NextPart_000_0034_01BEE403.93F822E0
Content-Type: text/html;
charset="gb2312"
Content-Transfer-Encoding: quoted-printable
<!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML//EN">
<HTML>
<HEAD>
<META content=3Dtext/html;charset=3Dgb2312 http-equiv=3DContent-Type>
<META content=3D'"MSHTML 4.72.3110.7"' name=3DGENERATOR>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV>Dear Sr.<BR><BR>How can I send signal between threads in a =
process?<BR>My=20
developing platform is DEC Unix 4.0, I use pthread_kill(pthread_t id,int =
sig_id)=20
to send signal to a thread, but a message "Hangup" will emerge =
when=20
call the function above and all program will exit, why?<BR><BR>My =
program=20
is:<BR><BR>#include <pthread.h><BR><BR>pthread_t =
timer_id;<BR>pthread_t=20
scanner_id;<BR>pthread_t runner_id;<BR><BR>pthread_attr_t =
attr;<BR>sigset_t=20
new;<BR><BR>void *timer_func(void *vp);<BR>void *scanner_func(void =
*vp);<BR>void=20
*runner_func(void=20
*vp);<BR><BR>main()<BR>{<BR>sigemptyset(&new);<BR>sigaddset(&new,=
SIGUSR1);<BR><BR>pthread_sigmask(SIG_BLOCK,=20
&new,NULL);*/<BR><BR>if(pthread_create(&timer_id,attr,timer_func,=
NULL)=20
!=3D 0)<BR>perror("Create=20
timer");<BR><BR>if(pthread_create(&scanner_id,attr,scanner_func,=
NULL)=20
!=3D 0)<BR>perror("Create=20
scanner");<BR><BR>if(pthread_create(&runner_id,attr,runner_func,=
NULL)=20
!=3D 0)<BR>perror("Create =
runner");<BR><BR>while(1)<BR>sleep(10);<BR>}=20
/* end main */<BR><BR>void* timer_func(void *vp)<BR>{<BR>printf("in =
timer_func\n");<BR>while(1)<BR>{<BR>if(pthread_kill(scanner_id,SIGUS=
R1) !=3D=20
0)<BR>perror("timer");<BR><BR>sleep(8);<BR>}<BR>}<BR><BR>void=20
*scanner_func(void *vp)<BR>{<BR>int sig_id;<BR><BR>printf("in=20
scanner_func\n");<BR>while(1)<BR>{<BR>sigwait(&new,&sig_id);=
<BR>printf("scanner=20
wait for=20
%d\n",sig_id);<BR>pthread_kill(runner_id,SIGUSR1);<BR>}<BR>}<BR><BR>=
void=20
*runner_func(void *vp)<BR>{<BR>int sig_id;<BR><BR>printf("in=20
runner_func\n");<BR>while(1)<BR>{<BR>sigwait(&new,&sig_id);<=
BR>printf("runner=20
wait for %d\n",sig_id);<BR>}<BR>}<BR><BR>My another question is =
about=20
"pthread_attr_t attr" in program above.<BR><BR>pthread_attr_t =
attr=20
variable must be declared in global and can not be initialized(call=20
"pthread_attr_init()"), otherwise pthread_create will =
abort(error=20
message "Invalid parameters"). <BR><BR>Compiler will report =
errors if=20
call pthread_create as<BR>pthread_create(&thread_id, NULL, =
timer_func,=20
NULL), compiler's error is<BR>""0" is of type =
"long",=20
and may not be converted to "struct CMA_T_HANDLE". =20
pthread_create(&t, NULL, sigint, NULL);".<BR><BR>Thanks =
lot!<BR>My=20
e-mail: <A =
href=3D"mailto:teng_mt@188.net">teng_mt@188.net</A><BR><BR>Teng=20
mintang, from Hangzhou P.R.China<BR>1999/08/11</DIV></BODY></HTML>
------=_NextPart_000_0034_01BEE403.93F822E0--