Finish Lab 3
This commit is contained in:
17
Lab3/examples/1-no_join-fixed.c
Normal file
17
Lab3/examples/1-no_join-fixed.c
Normal file
@ -0,0 +1,17 @@
|
||||
/**To compile, don't forget to add -lpthread. Might not work without that */
|
||||
#include<pthread.h>
|
||||
#include<stdio.h>
|
||||
|
||||
void* run (void* arg){
|
||||
printf("Hello from run\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(){
|
||||
pthread_t thread; // variable to store the reference to the thread
|
||||
pthread_create(&thread, NULL, &run, NULL);
|
||||
printf("In main"); // This section will be executed in parallel
|
||||
pthread_join(thread,NULL); // necessary for waiting for the thread to finish
|
||||
printf("In main 2");
|
||||
return 0;
|
||||
}
|
23
Lab3/examples/2-sleep no_join-fixed.c
Normal file
23
Lab3/examples/2-sleep no_join-fixed.c
Normal file
@ -0,0 +1,23 @@
|
||||
#include<pthread.h>
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<unistd.h>
|
||||
#include<time.h>
|
||||
#include<sys/time.h>
|
||||
|
||||
void* run (void* arg){
|
||||
printf("Hello from run\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(){
|
||||
pthread_t thread;
|
||||
pthread_create(&thread, NULL, &run, NULL);
|
||||
// While the sleep() function will appear to provide synchronization,
|
||||
// it is incorrect, and if used for this purposes instead of join
|
||||
// or synchronization mechanism - the grade for the assignment will be
|
||||
// reduced
|
||||
sleep(2);
|
||||
printf("Back in main");
|
||||
return 0;
|
||||
}
|
24
Lab3/examples/3-losing_track_of_threads-fixed.c
Normal file
24
Lab3/examples/3-losing_track_of_threads-fixed.c
Normal file
@ -0,0 +1,24 @@
|
||||
#include<pthread.h>
|
||||
#include<stdio.h>
|
||||
#include<stdlib.h>
|
||||
#include<unistd.h>
|
||||
#include<time.h>
|
||||
#include<sys/time.h>
|
||||
|
||||
void* run (void* arg){
|
||||
sleep(10-(int)arg);
|
||||
printf("Hello from run\n");
|
||||
}
|
||||
|
||||
int main(){
|
||||
pthread_t thread[10];
|
||||
int i = 0;
|
||||
for (i = 0 ; i < 10; ++i)
|
||||
pthread_create(&thread[i], NULL, &run,(void*)i);
|
||||
|
||||
// If you plan to use the results of all the threads, consider
|
||||
// using join for all of the threads
|
||||
for (i = 0 ; i < 10; ++i)
|
||||
pthread_join(thread[i],NULL);
|
||||
printf("In main");
|
||||
}
|
21
Lab3/examples/4-passing_data_wrongly.c
Normal file
21
Lab3/examples/4-passing_data_wrongly.c
Normal file
@ -0,0 +1,21 @@
|
||||
#include<pthread.h>
|
||||
#include<stdio.h>
|
||||
|
||||
void* run (void* arg){
|
||||
int i = (int) arg;
|
||||
printf("Hello from run, arg is %i\n",i);
|
||||
}
|
||||
|
||||
int main(){
|
||||
pthread_t thread[10];
|
||||
int i = 0;
|
||||
for (i = 0 ; i < 10; ++i)
|
||||
pthread_create(&thread, NULL, &run,(void*)i);
|
||||
// Notice how warnings are generated. This can be resolved by properly
|
||||
// allocating space for the thread parameters.
|
||||
// Note, that if you are using stack variable, the values might be corrupted
|
||||
for (i = 0 ; i < 10; i++){
|
||||
pthread_join(thread[i],NULL);
|
||||
}
|
||||
printf("In main");
|
||||
}
|
31
Lab3/examples/4-passing_data_wrongly_fixed.c
Normal file
31
Lab3/examples/4-passing_data_wrongly_fixed.c
Normal file
@ -0,0 +1,31 @@
|
||||
#include<pthread.h>
|
||||
#include<stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct thread_args{
|
||||
char letter;
|
||||
int id;
|
||||
}Args;
|
||||
void* run (void* arg){
|
||||
Args* argg = (Args*) arg;
|
||||
int i = argg->id;
|
||||
printf("Hello from run %d\n", i);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv){
|
||||
pthread_t thread[10];
|
||||
int i = 0;
|
||||
for (i = 0 ; i < 10; i++){
|
||||
Args* argg = (Args*) (malloc(sizeof(Args)));
|
||||
argg->letter = 'q';
|
||||
argg->id = i;
|
||||
pthread_create(&(thread[i]), NULL, &run,(void*)argg);
|
||||
}
|
||||
for (i = 0 ; i < 10; i++){
|
||||
pthread_join(thread[i],NULL);
|
||||
}
|
||||
// Free the resources somewhere, for long running program. Or let the OS handle that
|
||||
printf("In main");
|
||||
return 0;
|
||||
}
|
19
Lab3/examples/5-pthread_detach.c
Normal file
19
Lab3/examples/5-pthread_detach.c
Normal file
@ -0,0 +1,19 @@
|
||||
#include<pthread.h>
|
||||
#include<stdio.h>
|
||||
|
||||
void* run (void* arg){
|
||||
printf("Hello from run\n");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int main(){
|
||||
pthread_t thread;
|
||||
pthread_create(&thread, NULL, &run, NULL);
|
||||
pthread_detach(thread);
|
||||
// This command will make thread detached. This means the resources will be released
|
||||
// upon the thread's completion - calling return
|
||||
// However, detached threads cannot be joined, which means if you care about the result
|
||||
// of the thread execution - you should not be using pthread_detach
|
||||
printf("In main");
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user