public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* RE: system call sched_yield() doesn't work on Linux 2.2
@ 2001-02-05  5:29 Matt
  2001-02-05  6:03 ` Davide Libenzi
  0 siblings, 1 reply; 11+ messages in thread
From: Matt @ 2001-02-05  5:29 UTC (permalink / raw)
  To: aron; +Cc: linux-kernel

mohit,
	to expect perfect alternation is not reasonable. the scheduler
(or one of its subsidiary and/or supporting functions) decides what
should run and what shouldn't. the linux scheduler did have problems
in 2.2 (and still does in some places). however last i checked
sched_yield() is at best a hint to the scheduler not a command. the
man page even suggests this. it says that if the process (or thread)
yields and if it is the highest priority task at the time it will be
re-run. so you can not guarantee that it will not re-run. this i think
was the point david was trying to make (albiet with some possibly
misplaced "fervour").

	however i did notice one small change wrt to SCHED_YIELD
semantics from 2.2.18 and 2.4.1-ac1 (one would assume that this change
happened during the schedule() re-writes during 2.3.x).

xref line 119 of kernel/sched.c in 2.2.18

 and

xref line 148 of kernel/sched.c in 2.4.1-ac1

in this case you will see that in 2.2.18 a SCHED_YIELD process will
get a "goodness" value of 0, however in 2.4.1-ac1 you will find that
it gets a value of -1 (and hence a lower scheduling priority). i dont
have a machine handy that is running 2.2.18 that i can patch and
reboot, how ever you may wish to change the return value on line 119
of kernel/sched.c in 2.2.18 to -1 and you may find that it might give
the behaviour you are looking for. it may also cause other
problems. caveat emptor and all that..

	matt

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 11+ messages in thread
* RE: system call sched_yield() doesn't work on Linux 2.2
@ 2001-02-05  7:27 Mohit Aron
  0 siblings, 0 replies; 11+ messages in thread
From: Mohit Aron @ 2001-02-05  7:27 UTC (permalink / raw)
  To: 'Matt'; +Cc: linux-kernel


Thanks for a reasonable/thoughtful reply.

>	to expect perfect alternation is not reasonable. the scheduler
> (or one of its subsidiary and/or supporting functions) decides what
> should run and what shouldn't. the linux scheduler did have problems
> in 2.2 (and still does in some places). however last i checked
> sched_yield() is at best a hint to the scheduler not a command. the
> man page even suggests this. it says that if the process (or thread)
> yields and if it is the highest priority task at the time it will be
> re-run. so you can not guarantee that it will not re-run. this i think
> was the point david was trying to make (albiet with some possibly
> misplaced "fervour").

It looks like what you're saying above is that the scheduling 
priority of a thread might be changed dynamically by the scheduler. 
Hence, even though it called sched_yield(), its resulting priority might 
still be higher than the other thread and hence there may not be 
perfect alternation. This makes sense.

However, I just had a chance to run my program on a Linux 2.4 kernel. 
I got almost perfect alternation every time I ran it. The output was:

	Thread1
	Thread1
	Thread2
	Thread1
	Thread2
	Thread1
	Thread2
	Thread1
	Thread2
	Thread2

Basically, the first thread prints twice in the beginning but after
that there's perfect alternation. This might however be because of
startup delays in Thread2.

I might add that I've tested my program on two other operating systems - 
Solaris 2.7 and DUNIX V4.0D. In both I got perfect alternation every
time I ran the program. And with Linux 2.4 there was "almost" perfect
alternation.


- Mohit
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 11+ messages in thread
* RE: system call sched_yield() doesn't work on Linux 2.2
@ 2001-02-05  4:33 Robert Guerra
  2001-02-05 20:03 ` David Schwartz
  0 siblings, 1 reply; 11+ messages in thread
From: Robert Guerra @ 2001-02-05  4:33 UTC (permalink / raw)
  To: davids; +Cc: linux-kernel

David,
    please try to reply courteously to queries by other people. And specially
when you're the one who's wrong. Mohit is right - Linux had a 
long standing problem where sched_yield() system call didn't work. It 
was only fixed in Linux 2.4. 

> > Also, it is NOT unrealistic to expect perfect alternation.
>
>	Find one pthreads expert who agrees with this claim. Post it to
> comp.programming.threads and let the guys who created the standard 
> laugh at you. Scheduling is not a substitute for synchronization, ever.

I don't claim mastery over threads. But I have been programming with threads
for a very long time and am well aware of the way OS schedulers
work. In the example that Mohit posted, it is reasonable to expect 
perfect alternation once both threads have started. And it certainly isn't
something to laugh at (even if it were wrong).


- Robert Guerra




____________________________________________________________________
Get free email and a permanent address at http://www.netaddress.com/?N=1
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 11+ messages in thread
* RE: system call sched_yield() doesn't work on Linux 2.2
@ 2001-02-05  4:02 Mohit Aron
  2001-02-05 16:49 ` Rik van Riel
  0 siblings, 1 reply; 11+ messages in thread
From: Mohit Aron @ 2001-02-05  4:02 UTC (permalink / raw)
  To: 'David Schwartz', linux-kernel


>	I'm using 2.4.1-pre10, glibc 2.1.3.

And I'm using Linux 2.2. And the sched_yield bug exists in Linux 2.2. I
found 
a huge number of posted bug reports on linux-kernel regarding this issue. 
Check http://boudicca.tux.org/hypermail/linux-kernel/2000week21/0858.html
for one.


> 	Thread1
> 	Thread1
> 	Thread1
> 	Thread1
> 	Thread1
> 	Thread2
> 	Thread2
> 	Thread2
> 	Thread2
> 	Thread2
>
>	That's totally reasonable, although you probably just forgot to
fflush.

Maybe to you. Not to anyone else who knows anything about systems. FYI,
stdout
is line buffered - so a printf ending in a "\n" is an automatic flush.


>	It's also possible Thread1 did all its work, yields and all, before
Thread2
> even got started. Thus Thread1 had no 'ready to run' thread to yield to.
> (The main thread may not have been ready to run, it may still have been
> waiting to synchronize to the new thread it created.)

What synchronization ? Even if thread1 started before main thread has a
chance to
create thread2, the first sched_yield would give main thread a chance to
create 
thread2. 


>	Find one pthreads expert who agrees with this claim. Post it to
> comp.programming.threads and let the guys who created the standard laugh
at
> you. Scheduling is not a substitute for synchronization, ever.

I for one am one. And I find your statement about laughable. Try posting
your
stuff on comp.programming.threads and see if you don't get the laughter that

you're expecting is lying in wait for me. Here is simple 
logic for you to figure out - if you have one run queue, and two threads
calling sched_yield() (and hence theoretically putting themselves at the end
of run queue), perfect alternation should be seen. Also, who's said I'm
trying to
synchronize based on scheduling - sched_yield() just isn't working, that's
all.


>	Your reasoning is totally invalid and ignores so many possible other
>factors. For example, who says that the writes that your threads are doing
> don't block?

If the printfs are blocking, then there's even more reason that the other
thread should run. And print its stuff. Put some pressure on your brain and
it might come to you.

>	I'm not sure I understand what this has to do with anything. If you
think
>so, then I don't think you appreciate with a standard actually is. Perfect
>alternation is reasonable, expecting perfect alternation is not. Probably
>the reason you got perfect alternation in Solaris is because you only had
>one kernel execution vehicle. Try it with system contention scope threads.


My Linux system is also idle. Even if one assumes that a sched_yield causes 
some other process to run on the CPU, the thread that caused sched_yield is
theoretically the last on the run queue. Hence no matter what other system
process is run first, the other thread should always run before the thread
that called sched_queue. Again my point of putting some pressure on your
brain ...

Also, I won't reply any further to anything you post. If you don't
understand
systems, kindly stay out of this discussion.


- Mohit

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 11+ messages in thread
* RE: system call sched_yield() doesn't work on Linux 2.2
@ 2001-02-04 17:45 Mohit Aron
  2001-02-05  0:21 ` David Schwartz
  0 siblings, 1 reply; 11+ messages in thread
From: Mohit Aron @ 2001-02-04 17:45 UTC (permalink / raw)
  To: 'David Schwartz', linux-kernel


What version of Linux are you using ? What I see is the following:
	Thread1
	Thread1
	Thread1
	Thread1
	Thread1
	Thread2
	Thread2
	Thread2
	Thread2
	Thread2

Also, it is NOT unrealistic to expect perfect alternation. The definition
of sched_yield in the manpage says that sched_yield() puts the thread under
question last in the run queue. So perfect alternation should have occurred.
I've also tested the code on Solaris - there is perfect alternation there.


- Mohit


-----Original Message-----
From: David Schwartz [mailto:davids@webmaster.com]
Sent: Sunday, February 04, 2001 3:09 AM
To: Mohit Aron; linux-kernel@vger.kernel.org
Subject: RE: system call sched_yield() doesn't work on Linux 2.2



	The program you attached worked perfectly for me. You need to
'fflush(stdout);' after each 'printf'. You didn't expect perfect alternation
did you? That's totally unrealistic. You cannot use the scheduler as a
synchronization mechanism.

--

Thread1
Thread1
Thread2
Thread1
Thread1
Thread2
Thread1
Thread2
Thread2
Thread2

--

	DS

> -----Original Message-----
> From: linux-kernel-owner@vger.kernel.org
> [mailto:linux-kernel-owner@vger.kernel.org]On Behalf Of Mohit Aron
> Sent: Saturday, February 03, 2001 2:53 PM
> To: linux-kernel@vger.kernel.org
> Subject: system call sched_yield() doesn't work on Linux 2.2
>
>
> Hi,
> 	the system call sched_yield() doesn't seem to work on Linux
> 2.2. Does
> anyone know of a kernel patch that fixes this ?
>
> Attached below is a small program that uses pthreads and demonstrates that
> sched_yield() doesn't work. Basically, the program creates two
> threads that
> alternatively try to yield CPU to each other.
>
>
> - Mohit
>
>
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/

^ permalink raw reply	[flat|nested] 11+ messages in thread
* system call sched_yield() doesn't work on Linux 2.2
@ 2001-02-03 22:53 Mohit Aron
  2001-02-04 11:08 ` David Schwartz
  0 siblings, 1 reply; 11+ messages in thread
From: Mohit Aron @ 2001-02-03 22:53 UTC (permalink / raw)
  To: linux-kernel

[-- Attachment #1: Type: text/plain, Size: 333 bytes --]

Hi,
	the system call sched_yield() doesn't seem to work on Linux 2.2. Does
anyone know of a kernel patch that fixes this ? 

Attached below is a small program that uses pthreads and demonstrates that
sched_yield() doesn't work. Basically, the program creates two threads that
alternatively try to yield CPU to each other.


- Mohit


[-- Attachment #2: sched_yield.c --]
[-- Type: text/plain, Size: 591 bytes --]

#include <stdio.h>
#include <sched.h>
#include <pthread.h>

static pthread_t thread1, thread2;


static void *thread1_func(void *arg)
{
  int i;

  for (i=0; i < 5 ;i++) {
    printf("Thread1\n");
    if (sched_yield()) printf("error in yielding\n");
  }
}

static void *thread2_func(void *arg)
{
  int i;

  for (i=0; i < 5 ;i++) {
    printf("Thread2\n");
    if (sched_yield()) printf("error in yielding\n");
  }
}


int main(int argc, char **argv)
{
  pthread_create(&thread1, NULL, thread1_func, NULL);
  pthread_create(&thread2, NULL, thread2_func, NULL);

  sleep(10);

  return 0;
}

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2001-02-05 20:03 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2001-02-05  5:29 system call sched_yield() doesn't work on Linux 2.2 Matt
2001-02-05  6:03 ` Davide Libenzi
  -- strict thread matches above, loose matches on Subject: below --
2001-02-05  7:27 Mohit Aron
2001-02-05  4:33 Robert Guerra
2001-02-05 20:03 ` David Schwartz
2001-02-05  4:02 Mohit Aron
2001-02-05 16:49 ` Rik van Riel
2001-02-04 17:45 Mohit Aron
2001-02-05  0:21 ` David Schwartz
2001-02-03 22:53 Mohit Aron
2001-02-04 11:08 ` David Schwartz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox