On 4/26/06, Min-Hua Chen <orca.chen@xxxxxxxxx> wrote: > hi: > > here is an example I found on internet. > > 1 #include <stdio.h> > 2 #include <stdlib.h> > 3 #include <pthread.h> > 4 > 5 void *print_message_function( void *ptr ); > 6 > 7 main() > 8 { > 9 pthread_t thread1, thread2; > 10 char *message1 = "Thread 1"; > 11 char *message2 = "Thread 2"; > 12 int iret1, iret2; > 13 > 14 iret1 = pthread_create( &thread1, NULL, > print_message_function, (void*) message1); > 15 iret2 = pthread_create( &thread2, NULL, > print_message_function, (void*) message2); > 16 > 17 pthread_join( thread1, NULL); > 18 pthread_join( thread2, NULL); > 19 > 20 printf("Thread 1 returns: %d\n",iret1); > 21 printf("Thread 2 returns: %d\n",iret2); > 22 exit(0); > 23 } > 24 > 25 void *print_message_function( void *ptr ) > 26 { > 27 char *message; > 28 message = (char *) ptr; > 29 for (;;) > 30 printf("%s \n", message); > 31 } > > printf here does not need any lock and I don't know why > printk needs spin_lock or preempt_disable (in my case) > to get a correct output. Let me know according to you what should be the correct output ? I do not find the output to be wrong as given by you in the earlier part of this mailing thread. Are you expecting sequential printing by each thread, that means first thread 1 prints all and then thread2 and then thread3. Do you get taht type of printing when you use spinlock. Can you please post both outputs with and without the usage of spinlocks in your code. Gaurav > > regards, > Min-Hua > > > On 4/25/06, Mulyadi Santosa <mulyadi.santosa@xxxxxxxxx> wrote: > > Hi... > > > > > > > The kernel is preemptive and context did happen, right? > > Yep, it is (at least based on what you wrote on the e-mail) > > > > > why the same code (without spin lock/preempt_disable()) > > > works fine by pthread API and looks strange when in the kernel. > > > > Please elaborate on what you mean by "works fine by pthread API".... I > > really don't understand. Attaching source code is better for > > clarification too. > > > sorry :-) > > > regards > > > > Mulyadi > > > > > > -- -- -Gaurav Email: gauravd.chd@xxxxxxxxx --------------------------------- Read my blog at: http://lkdp.blogspot.com/ --------------------------------- -- Kernelnewbies: Help each other learn about the Linux kernel. Archive: http://mail.nl.linux.org/kernelnewbies/ FAQ: http://kernelnewbies.org/faq/