L
L
LosGnidoS2020-11-09 01:00:49
linux
LosGnidoS, 2020-11-09 01:00:49

What is a thread in Linux (pthread_create())?

This program should output to the console "Hello World" "2007"

Created a structure for passing arguments to the stream function. Everything seems to be fine here. I do not see obvious obstacles to outputting text to the console, but, nevertheless, this does not happen. I would be grateful if you point out an error or shortcoming. I would appreciate any advice or clarification.

Also, I don't really understand thread abstraction. On the one hand, I understand that this is a tool that allows you to increase processor performance. Threads allow within one program to perform several actions simultaneously, using common data. In Linux, threads run just like processes, i.e. independently. Question: why in the same program perform several actions using threads, when in fact the program can do it all by itself without the help of threads. What is their real, applied meaning of threads ??? how do they work and how do they relate to the process? I have an idea about the standard output, input and error streams (stdout, stdin, stderr), but I don’t understand if these are the same thing, that I create a stream using a stream function and redirect the streams using (> >>, < < <). Are these threads different??? Once again: I will be glad to any explanation. Thanks in advance for your reply.

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

struct thread_arg
{
    char * str;
    int num;
};

void * function (void * arg)
{
    struct thread_arg targ = *(struct thread_arg *) arg;
    fprintf (stderr, "str=%s\n", targ.str);
    fprintf (stderr, "num=%d\n", targ.num);
    return NULL;
}

int main (void)
{
    pthread_t thread;
    int result;
    struct thread_arg targ;
    targ.str = "Hello World";
    targ.num = 2007;

    result = pthread_create (&thread, NULL, &function, &targ);

    
    return 0;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2020-11-09
@LosGnidoS

At you the program comes to the end before, than manages to fulfill a flow (to display the information on the screen). After the end of the main thread (the main function), the entire process is terminated, incl. and running threads.
To fix the error, you need to wait for the end of its execution in main() after starting the thread: pthread_join(). In general, you need to perform join for each "attached" thread, and by default they are all attached.
A process is a container for threads. Each process has at least one thread - the main ( main ()). The operating system scheduler schedules threads.
It is easier and cheaper (in terms of resources) for the OS to start a thread than it is to start a process.
Threads allow some tasks in your program to be solved in parallel. This is about the same as starting a second process to solve part of the problem. But two processes have different address spaces, while threads have one. Because of this, data exchange between threads is much easier than between processes. But, of course, there are pitfalls here too - data races and other related problems.
stdin and other input / output streams are a completely different entity - they are data streams.
pthread_create() - Creates a thread of execution. Feel the difference!

R
Rsa97, 2020-11-09
@Rsa97

https://habr.com/ru/post/326138/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question