简体   繁体   中英

use fork (process) instead of pthread to acieve the same

I have achieved concurrency with the following code by creating parallel threads. However, how should I go about using fork() to create child processes and do the same job as done by the main() below? I want to avoid creating any thread.

struct function_args
    {
        pthread_t thread;
        char *command;
    };
    
    int main()
    {
        while (1)
            {
                if (mode == 1)
                    printf("$> ");
    
                if ((nread = getline(&line, &linecap, in)) > 0)
                {
                    char *command;
                    int commands_num = 0;
                    struct function_args args[BUFF_SIZE];
    
                    // remove newline character
                    if (line[nread - 1] == '\n')
                        line[nread - 1] = '\0';
    
                    char *temp = line;
    
                    while ((command = strsep(&temp, "&")) != NULL)
                        if (command[0] != '\0')
                        {
                            args[commands_num++].command = strdup(command);
                            if (commands_num >= BUFF_SIZE)
                                break;
                        }
    
                    for (size_t i = 0; i < commands_num; i++)
                        if (pthread_create(&args[i].thread, NULL, &parseInput, &args[i]) != 0)
                            printError();
    
                    for (size_t i = 0; i < commands_num; i++)
                    {
                        if (pthread_join(args[i].thread, NULL) != 0)
                            printError();
                        if (args[i].command != NULL)
                            free(args[i].command);
                    }
                }
                else if (feof(in) != 0)
                {
                    atexit(clean);
                    exit(EXIT_SUCCESS);    // EOF
                }
            }
    
            return 0;
    }

I am aware that pid = fork() returns a processid which is 0 for the child and >0 for the parent. However, in the for loops below where I am handling multiple argv , I am not sure how to translate my main() to do so.

That really depends on what you're doing. In general terms you could do something like:

for (size_t i = 0; i < commands_num; i++)
{
    pid_t pid = fork();
    if (pid == 0)
    {
        parseInput(&args[i]);
        exit(0);
    }
    else if (pid == -1)
        printError();
    else
        args[i].thread = pid;
}

The children processes work independently of the parent and go about completing their task, so probably there's will be no need to do the equivalent of pthread_join() here in which case would be waitpid() , unless the parent process has to wait for their product to do something with it.

And speaking of which, once the processes are forked, they no longer share the same memory space, so transiting information between the children and the parent might be a challenge in itself. If you're just printing stuff to stdout you're set to go, otherwise you'll have to figure out pipes to make parent and children communicate.

Another alternative to using system native threads (or pthreads in particular) is to use some green thread library such as libdill , theoretically it will enable multi-threading even in systems that don't support native multi-threading.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM