Thread and forks

I'm relatively new to streams and forks. Therefore, to understand them a little better, I wrote simple programs. One of the small programs I wrote two programs: one for printing the counter on two processes, and the other with two threads. I noticed that the fork prints counters alternating when a stream prints one flow counter, and then the rest. So, the flow is not so parallel, but it behaves more serially. Why? Am I doing something wrong?

Also, what exactly does pthread_join do? Even when I do not execute pthread_join, the program works the same way.

Here is my code for the stream

void * thread1(void *a){ int i =0; for(i=0; i<100; i++) printf("Thread 1 %d\n",i); } void * thread2(void *b){ int i =0; for(i=0; i<100; i++) printf("Thread 2 %d\n", i); } int main() { pthread_t tid1,tid2; pthread_create(&tid1,NULL,thread1, NULL); pthread_create(&tid2,NULL,thread2, NULL); pthread_join(tid1,NULL); pthread_join(tid2,NULL); return 0; } 

And here is my fork code

 int main(void) { pid_t childPID; childPID = fork(); if(childPID >= 0) // fork was successful { if(childPID == 0) // child process { int i; for(i=0; i<100;i++) printf("\n Child Process Counter : %d\n",i); } else //Parent process { int i; for(i=0; i<100;i++) printf("\n Parent Process Counter : %d\n",i); } } else // fork failed { printf("\n Fork failed, quitting!!!!!!\n"); return 1; } return 0; } 

EDIT: How can I make a threaded program behave like a fork program? that is, the counter prints an interlacing.

+4
source share
2 answers

You are driving on a bad road here. The lesson you should learn is not to think about the OS scheduler. No matter what you do β€” processing schedules, priorities, or any knobs you turn, you cannot do it reliably.

You have laid your way in the search for the need for synchronization mechanisms - mutexes, semaphores, variable conditions, flow barriers, etc. What you want to do is exactly why they exist and what you should use to achieve your goals.

In the last question, pthread_join restores some resources from dead, joinable (i.e. disconnected) threads and allows you to check any returned variable from an expired thread. In your program, they mainly serve as a blocking mechanism. That is, main will block these calls until the threads expire. Without pthread_joins your main will end and the process will die, including the threads you created. If you do not want to join threads and do not use anything in main , use pthread_exit in main , as this will allow main to exit, but the threads continue processing.

+1
source

First of all, fork creates a second process, creating a thread, creating a β€œdispatch unit of work” within the same process.

Getting two different processes to rotate is usually a simple matter of starting the operating system. However, in the process, you need to learn more about how the OS chooses which of several threads to run.

You could, artificially, get a way out of the flows alternating, causing a dream for different times from each stream. That is, create stream A (code to output one line and then sleep at 100), then create stream B (enter code to output one line and then sleep at 50, etc.).

I understand that I want to see how threads can work in parallel, like processes. But, is this a real requirement or just a zoo request?

0
source

Source: https://habr.com/ru/post/1501362/


All Articles