Commit 861e92f8 authored by Zhaozhong Liu's avatar Zhaozhong Liu
Browse files

Lab 3.4 complete

parent 1b7cf2b8
......@@ -19,6 +19,9 @@ summation: summation.c
summation3: summation3.c
$(CC) $(CFLAGS) -o $@ $< $(LFLAGS)
summation4: summation4.c
$(CC) $(CFLAGS) -o $@ $< $(LFLAGS)
test: test.c
$(CC) $(CFLAGS) -o $@ $< $(LFLAGS)
......
1.
[zl011@acet116-lnx-13 Lab3]$ make summation
gcc -std=gnu99 -Wall summation.c -o summation
summation.c: In function ‘main’:
summation.c:75:8: warning: unused variable ‘status’ [-Wunused-variable]
void *status;
^
/tmp/ccdJULCt.o: In function `SumExp':
summation.c:(.text+0x90): undefined reference to `pow'
/tmp/ccdJULCt.o: In function `main':
summation.c:(.text+0x2cd): undefined reference to `pthread_create'
summation.c:(.text+0x355): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
make: *** [summation] Error 1
Problem 1
1)
summation.c:(.text+0x90): undefined reference to `pow'
summation.c:(.text+0x2cd): undefined reference to `pthread_create'
summation.c:(.text+0x355): undefined reference to `pthread_join'
2. The flag is: -lm and -lpthread
2) The flag for pow is "-lm"
3) After I had "-lm" appended, there were undefined references :
- pthread_create
- pthread_join
Problem 2:
1)Explain the mechanism by which the main thread passes arguments to the other threads it creates.
......@@ -41,4 +35,6 @@ Problem 2:
6)What would happen if one of the child threads should decide to call execl(3) to run “/bin/ls” in the executable’s current working directory? (Again, modify your source code to include this change and experiment with it. When you are done, leave this line commented out.)
When execl is called, all chid threads executes except the main thread does not perform the thread join and also there is no summation at the end.
Problem 4:
1)Yes, because I have got the time before each thread ends in the sum function.
2)I did not have any other problems.
/*
* Copyright (c) 2012 Bucknell University
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: L. Felipe Perrone (perrone@bucknell.edu)
*/
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/time.h>
// structure for thread arguments
struct thread_args {
int tid;
int a;
int b;
double x;
double result;
};
int shared;
//function that each thread will execute
void *SumExp(void *args_ptr) {
struct timeval stTime,edTime;
gettimeofday(&stTime, NULL);
int i;
//define the argument structure
struct thread_args *myargs_ptr = (struct thread_args*) args_ptr;
myargs_ptr->result = 0;
shared = 123;
printf("Thread %d starting a= %d, b= %d, x= %lf\n",
myargs_ptr->tid,
myargs_ptr->a,
myargs_ptr->b,
myargs_ptr->x);
//if (myargs_ptr ->tid == 3){printf("thread 3 exits\n");exit(0);}
//if (myargs_ptr->tid == 3){printf("thread 3 runs execl\n"); execl("/bin/ls","ls",NULL);}
for (i=myargs_ptr->a; i<myargs_ptr->b; i++) {
myargs_ptr->result += pow(i, myargs_ptr->x);
}
printf("Thread %d done a= %d, b= %d, x= %lf -> result = %lf\n",
myargs_ptr->tid,
myargs_ptr->a,
myargs_ptr->b,
myargs_ptr->x,
myargs_ptr->result);
gettimeofday(&edTime, NULL);
long sec = edTime.tv_sec - stTime.tv_sec;
long usec = edTime.tv_usec - stTime.tv_usec;
char *ctime_str = ctime(&edTime.tv_sec);
printf("Thread %d used time: %ld secs / %ld micro secs\nTime readable: %s\n",
myargs_ptr->tid,
sec, usec, ctime_str);
pthread_exit((void*) args_ptr);
}
int main (int argc, char *argv[]) {
pthread_attr_t attr;
struct thread_args *targs; // array for thread argument structs
struct thread_args **tstatus; // array of pointers to returns from threads
pthread_t *tidp; // array for thread ids
//initiated the time_value struct
struct timeval tvS, tvE;
//get the time when this program starts
gettimeofday(&tvS, NULL);
int ret_val;
int i;
void *status;
double summation = 0;
int num_threads, increment; // command line parameters
if (argc < 3) {
printf("usage: summation [numthreads] [increment]\n");
exit(-1);
}
num_threads = atoi(argv[1]);
increment = atoi(argv[2]);
printf("numth is: %d, increment is: %d\n", num_threads, increment);
targs = (struct thread_args *) calloc(num_threads, sizeof(struct thread_args));
tstatus = (struct thread_args **) calloc(num_threads, sizeof(struct thread_args *));
tidp = (pthread_t *) calloc(num_threads, sizeof(pthread_t));//to have an array to store all the tids
// initialize attr variable
pthread_attr_init(&attr);
// initialize thread detached state to joinable
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
for (i=0; i<num_threads; i++) {
tstatus[i] = malloc(sizeof(struct thread_args));
targs[i].a = increment*i;
targs[i].b = increment*(i+1);
targs[i].x = 2;
targs[i].tid = i;
targs[i].result = 0;
ret_val = pthread_create(&tidp[i], &attr, SumExp, (void *) &targs[i]);
if (ret_val) {
printf("ERROR in pthread_create for thread %d: return value= %d\n",
i, ret_val);
exit(-1);
}
}
// free attribute variable
pthread_attr_destroy(&attr);
// main thread waits for spawned threads to terminate
for(i=0; i<num_threads; i++) {
//
ret_val = pthread_join(tidp[i], (void **) &tstatus[i]);
if (ret_val) {
printf("ERROR in pthread_join() for thread %d = return value= %d\n",
i, ret_val);
exit(-1);
}
summation += tstatus[i]->result;
printf("Joined thread %d; returned= %lf\n", i, tstatus[i]->result);
}
printf("Total value computed = %lf\n", summation);
//get the time when this program ends
gettimeofday(&tvE, NULL);
//get the difference bt two times
long interval = tvE.tv_sec - tvS.tv_sec;
long intervalMicro = tvE.tv_usec - tvS.tv_usec;
printf("The time of this program in sec: %ld, in micro sec: %ld \n", interval, intervalMicro);
pthread_exit(NULL);
printf("still here\n");
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment