Skip to contentSkip to conversation

system call sched_yield() doesn't work on Linux 2.2

DISCUSSION Mohit Aron 2 participants 2 messages Feb 3, 2001 – Feb 4, 2001

lore
Share & tools
lore

Mohit Aron

author
#0 lore raw

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

#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;
}

#1 lore raw

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

Quoted 13 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]]On Behalf Of Mohit Aron
Sent: Saturday, February 03, 2001 2:53 PM
To: [email protected]
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 ?

Quoted 9 lines

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 [email protected]
Please read the FAQ at http://www.tux.org/lkml/