Bug 30781

Summary: msgrcv call blocks other pthread's jobs
Product: Base System Reporter: Choi, Dong-won <dwchoe>
Component: kernAssignee: freebsd-bugs (Nobody) <bugs>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Unspecified   
Hardware: Any   
OS: Any   

Description Choi, Dong-won 2001-09-24 10:00:02 UTC
A program using msgrcv call and pthread do not run as expected.
One thread calls msgrcv and wait until some messages are arrived.
Other thread does its given jobs.
And some other threads can call msgsnd to send messages.
All threads need to run concurruntly.
It's just ok under Linux or SunOS.

How-To-Repeat: ----- test.c ------------- 
#include <stdio.h> 
#include <stdlib.h> 
#include <unistd.h> 
#include <pthread.h> 
#include <sys/ipc.h> 
#include <sys/msg.h> 

void *Thread(void *vArg); 
struct stmq { 
long id; 
}; 
struct stmq mqSt; 

main(int argc, char *argv[]) 
{ 
int i; 
int mqid; 
pthread_t thSelect; 
pthread_attr_t taAttr; 

if( (mqid = msgget(0x20, 0666 | IPC_CREAT)) == -1 ) exit(0); 

pthread_attr_init (&taAttr); 
pthread_attr_setscope (&taAttr, PTHREAD_SCOPE_SYSTEM); 
pthread_attr_setdetachstate(&taAttr, PTHREAD_CREATE_DETACHED); 

for(i=0; i<2; i++) { 
if(pthread_create(&thSelect, &taAttr, Thread, NULL) != 0) exit(0); 
} 

while(1) { 
printf("Main...\n"); fflush(stdout); 
msgrcv(mqid, &mqSt, sizeof(mqSt)-sizeof(long), 0, 0); 
} 
} 

void *Thread(void *vArg) 
{ 
printf("Thread...\n"); fflush(stdout); 
while(1); 
} 
------- End of test.c 

Under FreeBSD
$ gcc test.c -pthread 
$ a.out 
Main... 

Under Linux or SunOS
$ a.out
Main...
Thread...
Thread...
Comment 1 Peter Pentchev 2001-09-24 14:14:03 UTC
On Mon, Sep 24, 2001 at 01:58:54AM -0700, dwchoe@naver.com wrote:
> 
> >Number:         30781
> >Category:       kern
> >Synopsis:       msgrcv call blocks other pthread's jobs
> >Originator:     Choi, Dong-won
> >Release:        FreeBSD 4.2-20010108-STABLE
> >Organization:
> Naver.COM
> >Environment:
> FreeBSD 4.2-20010108-STABLE #0: Thu Jan 11 09:23:26 KST 2001     /usr/src/sys/compile/MYKERNEL  i386
> >Description:
> A program using msgrcv call and pthread do not run as expected.
> One thread calls msgrcv and wait until some messages are arrived.
> Other thread does its given jobs.
> And some other threads can call msgsnd to send messages.
> All threads need to run concurruntly.
> It's just ok under Linux or SunOS.

In FreeBSD, all threads live within one process.  This means that if
one thread should make a blocking system call, the whole process blocks
until the call returns.  The way around this is to make your system calls
not block.  For syscalls dealing with file descriptors, the fd's need to
be set into non-blocking mode.  Other syscalls have their own ways of
specifying non-blocking operation.
	
For msgrcv(3), it is the msgflg parameter - read the msgrcv(3) manual page
and set the IPC_NOWAIT flag.  Then have your main thread call msgrcv(3)
in non-blocking mode and if it returns nothing, sleep for a while before
the next call.

G'luck,
Peter

-- 
What would this sentence be like if it weren't self-referential?
Comment 2 yuri.viktarovich 2001-10-11 10:52:28 UTC
 > For msgrcv(3), it is the msgflg parameter - read the msgrcv(3) manual page
 > and set the IPC_NOWAIT flag.  Then have your main thread call msgrcv(3)
 > in non-blocking mode and if it returns nothing, sleep for a while before
 > the next call.

That's not really "message waiting" architecture than. Sending process should
trigger the event and listening process should awake on it. Just like with sockets.
Looks like that's currently impossible in FreeBSD with msgXXX functions without
interrupting the other threads.

Anyway shouldn't it be some generic uniform way to wakeup processes/threads
on events working the same way on msgXXX, FDs, process start/end, other events?

Yuri.
Comment 3 Maxim Konovalov 2002-01-23 09:09:12 UTC
This PR is duplicated with kern/21783:

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=kern/21783

-- 
Maxim Konovalov, MAcomnet, Internet-Intranet Dept., system engineer
phone: +7 (095) 796-9079, mailto: maxim@macomnet.ru
Comment 4 Sheldon Hearn freebsd_committer freebsd_triage 2002-01-31 10:26:40 UTC
State Changed
From-To: open->closed

Duplicate of kern/30781.