Themes or processes? Having data-independent tasks, which is better to use?

There are many N time-independent tasks. Each of the tasks processes a certain stream of digital data. The data stream for each task comes from the input port, and the resulting stream is then sent to the output port.

1) What is computationally less intense: tasks in the form of processes or threads?
2) The best choice depends on the number of available physical processor cores?

Thank you in advance!

+3
source share
7 answers

, , , . , , . .

, ; , .

+2

, , , . , , - , .

+3

. , Linux . -, . , , . Linux , .

Windows, , Windows NT. Windows NT OS/2, . , , , Linux. , Windows . , , .

+2

, , ( , Linux). , ​​.

, , , , .

, - : , , //. , :

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <string>

void termination_handler(int signum)
{
    printf("pid %d : signal %d caught in pid %d\n", getpid(), signum, getpid());
    kill(0, SIGTERM);
    exit(1);
}

int main(int argc ,char** argv) 
{
    if (argc > 1)
    {
        printf("pid %d : Worker code running\n", getpid());
        sleep(10);
    }
    else
    {
        printf("pid %d : manager started\n", getpid());
        // manager
        const int MAX_INSTANCES = 3;
        int numInstances = 0;

        struct sigaction new_action;
        /* Set up the structure to specify the new action. */
        new_action.sa_handler = termination_handler;
        sigemptyset(&new_action.sa_mask);
        new_action.sa_flags = 0;
        sigaction(SIGKILL, &new_action, NULL);
        sigaction(SIGTERM, &new_action, NULL);
        sigaction(SIGINT, &new_action, NULL);

        int status;
        int w;
        do
        {
            while (numInstances < MAX_INSTANCES)
            {
                int pid = fork();
                if (pid < 0)
                {
                    printf("fork failed\n");
                    exit(1);
                }
                else
                {
                    if (pid == 0)
                    {
                        char * const argv1[] = { (char*) argv[0], (char*)"worker",(char *) 0 };
                        char * const envp1[] = { (char *) 0 };
                        std::string prog = argv[0];
                        execve(prog.c_str(), argv1, envp1);
                    }
                    else
                    {
                        numInstances++;
                    }
                }
            }           

            w = waitpid(0, &status, WUNTRACED | WCONTINUED);
            if (w == -1)
            {
                perror("waitpid");
                exit(EXIT_FAILURE);
            }

            if (WIFEXITED(status))
            {
                printf("pid %d : child %d exited, status=%d\n",getpid(), w, WEXITSTATUS(status));
                numInstances--;
            }
            else if (WIFSIGNALED(status))
            {
                printf("pid %d : child %d killed by signal %d\n",getpid(), w, WTERMSIG(status));
                numInstances--;
            }
            else if (WIFSTOPPED(status))
            {
                printf("pid %d : child %d stopped by signal %d\n",getpid(), w, WSTOPSIG(status));
            }
            else if (WIFCONTINUED(status))
            {
                printf("pid %d : child %d continued\n", getpid(), w);
            }
        } 
        while (true);
        //! WIFEXITED(status) && !WIFSIGNALED(status));

        printf("pid %d : manager terminated\n", getpid());
    }
    return 0;
}
+1

, , "" . . , .

, . "" , . , , . , . 64 (/) 1,86 Xeon . 10 000 000. 333 .

, , , , ; / . , 100 Sleep (0), , , , (Win32). 343 350 . , 1,7%. .

+1

.

, , . , - , (, ).

0

1) : ?

. .

2) ?

, , MUCH MORE SLOWLY, . .

- 99,9%.

.

0
source

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


All Articles