From nobody@FreeBSD.org  Mon Sep 24 01:58:54 2001
Return-Path: <nobody@FreeBSD.org>
Received: from freefall.freebsd.org (freefall.FreeBSD.org [216.136.204.21])
	by hub.freebsd.org (Postfix) with ESMTP id 1881F37B415
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 24 Sep 2001 01:58:54 -0700 (PDT)
Received: (from nobody@localhost)
	by freefall.freebsd.org (8.11.4/8.11.4) id f8O8wsM18213;
	Mon, 24 Sep 2001 01:58:54 -0700 (PDT)
	(envelope-from nobody)
Message-Id: <200109240858.f8O8wsM18213@freefall.freebsd.org>
Date: Mon, 24 Sep 2001 01:58:54 -0700 (PDT)
From: "Choi, Dong-won" <dwchoe@naver.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: msgrcv call blocks other pthread's jobs
X-Send-Pr-Version: www-1.0

>Number:         30781
>Category:       kern
>Synopsis:       msgrcv call blocks other pthread's jobs
>Confidential:   no
>Severity:       critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Sep 24 02:00:02 PDT 2001
>Closed-Date:    Thu Jan 31 02:26:40 PST 2002
>Last-Modified:  Thu Jan 31 02:26:57 PST 2002
>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.

>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...

>Fix:

>Release-Note:
>Audit-Trail:

From: Peter Pentchev <roam@ringlet.net>
To: "Choi, Dong-won" <dwchoe@naver.com>
Cc: freebsd-gnats-submit@FreeBSD.org
Subject: Re: kern/30781: msgrcv call blocks other pthread's jobs
Date: Mon, 24 Sep 2001 16:14:03 +0300

 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?

From: yuri.viktarovich@philips.com
To: freebsd-gnats-submit@FreeBSD.org, dwchoe@naver.com
Cc:  
Subject: Re: kern/30781: msgrcv call blocks other pthread's jobs
Date: Thu, 11 Oct 2001 11:52:28 +0200

  > 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.
 
 

From: Maxim Konovalov <maxim@macomnet.ru>
To: freebsd-gnats-submit@FreeBSD.org, <dwchoe@naver.com>
Cc:  
Subject: Re: kern/30781: msgrcv call blocks other pthread's jobs
Date: Wed, 23 Jan 2002 12:09:12 +0300 (MSK)

 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
 
State-Changed-From-To: open->closed 
State-Changed-By: sheldonh 
State-Changed-When: Thu Jan 31 02:26:40 PST 2002 
State-Changed-Why:  
Duplicate of kern/30781. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=30781 
>Unformatted:
