1

我尝试等待 main 函数,直到线程完成它们的工作。但是主函数完成它的工作并退出。我认为因为线程在变量中没有正确的指针/值。(计数和步骤)

有人知道在这种情况下如何正确使用 waitpid/wait 吗?

我的代码:

#define _GNU_SOURCE
#include <stdio.h>
#include <inttypes.h> /* for PRIu64 and uint64_t */
/* you'll need further includes */
#include <sched.h>
#include <stdlib.h>
#include "tally.h"
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define STACK_SIZE 32768
#define THREADS 2
struct clone_args{
        uint64_t steps;
        uint64_t* tally;

};
int incHelper(void* arg){
        struct clone_args *args = (struct clone_args*)arg;
        uint64_t steps= args->steps;
        uint64_t* tally = args->tally;
        increment(tally,steps);
        (void) arg;
        (void) tally;
        (void) steps;
        exit(2);
        return 0;
}

int main ()
{
        uint64_t N = 10000000;
        uint64_t tally = 1;
        tally_init();
        int thread_pid[THREADS];
        int status;
        for(int i= 0; i<THREADS;i++){
                void *child_stack = malloc(STACK_SIZE);
                struct clone_args *arguments = malloc(sizeof(struct clone_args));
                arguments->steps = N;
                arguments->tally = &tally;
                void* arg = (void*) &arguments;
                thread_pid[i] = clone(incHelper, child_stack+STACK_SIZE,CLONE_VM, arg);
                pid_t pid = waitpid(thread_pid[i],&status,SIGCHLD);
                printf("C-PID [%d]\n", thread_pid[i]);
                (void) pid;
        }

        tally_clean();
        printf( "\nTally is %" PRIu64 "\n", tally );
        (void) N;
        (void) thread_pid;
        (void) tally;
        (void) status;
        printf("\n MAIN PROGRAMM END\n");
        return 0;
}

增量函数:

/* modify this function to achieve correct parallel incrementation */
void  increment ( uint64_t *tally, uint64_t steps )
{
        printf("\nTALLY: %"PRIu64"\n",*tally);
        printf("STEPS: %"PRIu64"\n",steps);
        for( uint64_t i = 0; i < steps; i++ )
        {
                *tally += 1;
        }
        return;

}

运行代码后得到的结果:

C-PID [29819]
C-PID [29820]

Tally is 1

 MAIN PROGRAMM END
root@...(~/Downloads/asst3/asst3-clone)$
TALLY: 0
STEPS: 140714329004032

TALLY: 888309
STEPS: 140714329004032

代码应该用两个线程增加一个变量。为了避免临界区问题,我应该使用信号量。但那是另一个练习。首先练习它使用 clone() 函数创建两个线程。我不明白,如果 clone() 的标志是错误的,或者我的代码是完全错误的。我是编程语言 C 的新手。

我花了过去 12 个小时用谷歌搜索。

我感谢每一个答案:)。

抱歉英语不好。

问候

4

1 回答 1

3

默认情况下,clone()ed 进程不会向父进程发出关于其结束的信号。

如果您希望父母收到关于孩子结束的信号,您需要在对clone()传递的第三个参数进行 ORed 时显式传递要在其结束时发送的信号。

如果您使用,SIGCHLDwaitpid()照常使用。

在后一种情况下,克隆并等待如下:

  thread_pid[i] = clone(incHelper, child_stack+STACK_SIZE, CLONE_VM | SIGCHLD, arg);
  pid_t pid = waitpid(thread_pid[i], &status, 0);

如果您想使用另一个信号在孩子端发送,例如SIGUSR1,您需要告诉它waitpid()使用选项__WCLONE

  thread_pid[i] = clone(incHelper, child_stack+STACK_SIZE, CLONE_VM | SIGUSR1, arg);
  pid_t pid = waitpid(thread_pid[i], &status, __WCLONE);

这个

void* arg = (void*) &arguments;

采用 的地址arguments。由于arguments已经是所需结构的地址,它应该是:

void * arg = arguments;

注意:由于ed线程在下一次调用之前完成的主线程waitpid()s ,因此没有并行处理clone()clone()increment()

于 2013-12-15T16:42:46.590 回答