I try to wait the main function, till the threads finish their work. But the main function finish its work and exit. I think because of that the threads has not the correct pointers/values in the variables.(tally and steps)
Does someone know how to use waitpid/wait properly in this case?
my Code:
#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;
}
The increment function:
/* 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;
}
The Result i got after running the code:
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
The code should increment a variable with two threads. To avoid criticalsection Problem i should use semaphores. But thats an other exercise. First exercise it to use clone() function to create two threads. I dont understand, if the flags of clone() are wrong or my code is completely wrong. I am new at programming language C.
I spent last 12 hours of searching with google.
I thank for every answer :).
Sorry for bad english.
Regards
Per default a
clone()
ed process does not signal the parent about its end.If you want the parent to be signaled about the child's end you need to explictily pass the signal to be sent on its end when
clone()
ing ORed to the 3rd parameter passed.If you use
SIGCHLD
then usewaitpid()
as usual.In this latter case clone and wait like so:
If you want to use another signal to be sent on the child's end like for example
SIGUSR1
you need to tell this towaitpid()
using the option__WCLONE
:This
takes the address of
arguments
. Asarguments
already is an address of the required structure, it should be:Note: As the main thread
waitpid()
s for theclone()
ed thread to finish before the next call toclone()
, there is no parallel processing ofincrement()