Thank you mario !!
if I use this code:
#include <stdlib.h> /* EXIT_SUCESS, EXIT_FAILURE */
int main(void)
{
pid_t npid=0;
npid= fork();
if(npid) {
for (i=0;i<11;i++)
printf(“process1\n”);
return (EXIT_SUCCESS);
}else{
for (i=0;i<11;i++)
printf(“process2\n”);
return (EXIT_SUCCESS);
}
return (EXIT_SUCCESS);
}
result in shell is :
process1
process2
process1
process2
process1
process2
process1
process2
process1
process2
process1
process2
process1
process2
process1
process2
process1
process2
process1
process2
process1
process2
if I use this code:
#include <stdlib.h> /* EXIT_SUCESS und EXIT_FAILURE */
void *thread2( void *not_used )
{
int i;
for ( i = 0; i < 11; i++ )
{
printf("thread2\n");
sleep(1);
}
return (EXIT_SUCCESS);
}
int main( void )
{
int i;
pthread_create(NULL, NULL, thread2, NULL);
for ( i = 0; i < 11; i++ )
{
printf("Thread1\n");
sleep(1);
}
sleep(20);
return (EXIT_SUCCESS);
}
result in shell is:
thread2
Thread1
thread2
Thread1
thread2
Thread1
thread2
Thread1
thread2
Thread1
thread2
Thread1
thread2
Thread1
thread2
Thread1
thread2
Thread1
thread2
Thread1
thread2
Thread1
and If I use this code without sleep(1)
#include <stdlib.h> /* EXIT_SUCESS und EXIT_FAILURE */
void *thread2( void *not_used )
{
int i;
for ( i = 0; i < 11; i++ )
{
printf("thread2\n");
}
return (EXIT_SUCCESS);
}
int main( void )
{
int i;
pthread_create(NULL, NULL, thread2, NULL);
for ( i = 0; i < 11; i++ )
{
printf("Thread1\n");
}
sleep(20);
return (EXIT_SUCCESS);
}
result in shell is:
thread2
thread2
thread2
thread2
thread2
thread2
thread2
thread2
thread2
thread2
thread2
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
Thread1
and this is not equal with fork() and working with process
because there is no alternate.
only if i use sleep(1), or delay(1) with thread it will alternate.
so whats different by using Threads and using process to make
this conclusion.
with fork() I dont need sleep(1) or delay(1) to alternate.
and working with sleep(1) or delay(1) is reschedulingm I belief.
I hope you understand me!
Thank you !!!