Finish Lab 3

This commit is contained in:
2023-10-04 18:07:09 -03:00
parent 7fc0b79b52
commit 9ba4c8ebc9
8 changed files with 217 additions and 6 deletions

View 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;
}

View 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;
}

View 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");
}

View 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");
}

View 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;
}

View 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;
}