Add A3, almost done
This commit is contained in:
parent
0e99d1045f
commit
39d628cad2
8
Assignment3/.idea/.gitignore
vendored
Normal file
8
Assignment3/.idea/.gitignore
vendored
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
|
# Editor-based HTTP Client requests
|
||||||
|
/httpRequests/
|
||||||
|
# Datasource local storage ignored files
|
||||||
|
/dataSources/
|
||||||
|
/dataSources.local.xml
|
2
Assignment3/.idea/Assignment3.iml
Normal file
2
Assignment3/.idea/Assignment3.iml
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module classpath="CMake" type="CPP_MODULE" version="4" />
|
4
Assignment3/.idea/misc.xml
Normal file
4
Assignment3/.idea/misc.xml
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
|
||||||
|
</project>
|
8
Assignment3/.idea/modules.xml
Normal file
8
Assignment3/.idea/modules.xml
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/Assignment3.iml" filepath="$PROJECT_DIR$/.idea/Assignment3.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
6
Assignment3/.idea/vcs.xml
Normal file
6
Assignment3/.idea/vcs.xml
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
6
Assignment3/CMakeLists.txt
Normal file
6
Assignment3/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.20)
|
||||||
|
project(Assignment3 C)
|
||||||
|
|
||||||
|
set(CMAKE_C_STANDARD 99)
|
||||||
|
|
||||||
|
add_executable(Assignment3 main.c)
|
BIN
Assignment3/documentation/Assignment 3.pdf
Normal file
BIN
Assignment3/documentation/Assignment 3.pdf
Normal file
Binary file not shown.
35
Assignment3/documentation/sample_code.txt
Normal file
35
Assignment3/documentation/sample_code.txt
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#include <pthread.h> // for pthread_create(), pthread_join(), etc.
|
||||||
|
#include <stdio.h> // for scanf(), printf(), etc.
|
||||||
|
#include <stdlib.h> // for malloc()
|
||||||
|
#include <unistd.h> // for sleep()
|
||||||
|
|
||||||
|
#define NUMBER_OF_BOXES_PER_DWELLER 5
|
||||||
|
#define ROOM_IN_TRUCK 10
|
||||||
|
#define MIN_BOX_WEIGHT 5
|
||||||
|
#define MAX_BOX_WEIGHT 50
|
||||||
|
#define MAX_TIME_FOR_HOUSE_DWELLER 7
|
||||||
|
#define MIN_TIME_FOR_HOUSE_DWELLER 1
|
||||||
|
#define MAX_TIME_FOR_MOVER 3
|
||||||
|
#define MIN_TIME_FOR_MOVER 1
|
||||||
|
#define MIN_TIME_FOR_TRUCKER 1
|
||||||
|
#define MAX_TIME_FOR_TRUCKER 3
|
||||||
|
#define MIN_TRIP_TIME 5
|
||||||
|
#define MAX_TRIP_TIME 10
|
||||||
|
#define RANDOM_WITHIN_RANGE(a,b,seed) (a+rand_r(&seed)%(b-a))
|
||||||
|
// For pipes
|
||||||
|
#define READ_END 0
|
||||||
|
#define WRITE_END 1
|
||||||
|
|
||||||
|
// Pipe between house dwellers and movers
|
||||||
|
int houseFloor[2];
|
||||||
|
// Pipe between movers and truckers
|
||||||
|
int nextToTrucks[2];
|
||||||
|
|
||||||
|
// some function definitions follow
|
||||||
|
|
||||||
|
int main(int argc, char** argv){
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
// rest of the program follows
|
||||||
|
return 0;
|
||||||
|
}
|
62
Assignment3/documentation/sample_execution.txt
Normal file
62
Assignment3/documentation/sample_execution.txt
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
Please input number of people living in the house, number of movers and number of truck drivers
|
||||||
|
3 2 1
|
||||||
|
Hello from house dweller 0
|
||||||
|
Hello from house dweller 1
|
||||||
|
Hello from house dweller 2
|
||||||
|
Hello from mover 1
|
||||||
|
Hello from mover 0
|
||||||
|
Hello from truck driver 0
|
||||||
|
House dweller 1 created a box that weighs 17 in 5 units of time
|
||||||
|
House dweller 0 created a box that weighs 19 in 6 units of time
|
||||||
|
House dweller 2 created a box that weighs 13 in 6 units of time
|
||||||
|
House dweller 1 created a box that weighs 30 in 1 units of time
|
||||||
|
Mover 0 brought down a box that weighs 17 in 1 units of time
|
||||||
|
Mover 0 brought down a box that weighs 13 in 1 units of time
|
||||||
|
Mover 1 brought down a box that weighs 19 in 2 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 17 to the truck, took 2 units of time, room left:9
|
||||||
|
Mover 0 brought down a box that weighs 30 in 1 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 13 to the truck, took 1 units of time, room left:8
|
||||||
|
House dweller 1 created a box that weighs 6 in 4 units of time
|
||||||
|
House dweller 0 created a box that weighs 20 in 4 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 19 to the truck, took 2 units of time, room left:7
|
||||||
|
House dweller 2 created a box that weighs 44 in 6 units of time
|
||||||
|
Mover 0 brought down a box that weighs 20 in 2 units of time
|
||||||
|
House dweller 0 created a box that weighs 17 in 2 units of time
|
||||||
|
Mover 1 brought down a box that weighs 6 in 2 units of time
|
||||||
|
House dweller 1 created a box that weighs 26 in 2 units of time
|
||||||
|
Mover 0 brought down a box that weighs 44 in 1 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 30 to the truck, took 2 units of time, room left:6
|
||||||
|
Mover 1 brought down a box that weighs 26 in 2 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 20 to the truck, took 1 units of time, room left:5
|
||||||
|
Mover 0 brought down a box that weighs 17 in 2 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 6 to the truck, took 1 units of time, room left:4
|
||||||
|
House dweller 0 created a box that weighs 25 in 4 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 44 to the truck, took 1 units of time, room left:3
|
||||||
|
House dweller 1 created a box that weighs 40 in 5 units of time
|
||||||
|
House dweller 1 is done packing
|
||||||
|
Trucker 0 loaded up a box that weighs 26 to the truck, took 1 units of time, room left:2
|
||||||
|
House dweller 2 created a box that weighs 49 in 6 units of time
|
||||||
|
Mover 0 brought down a box that weighs 25 in 2 units of time
|
||||||
|
Mover 1 brought down a box that weighs 40 in 2 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 17 to the truck, took 2 units of time, room left:1
|
||||||
|
Mover 0 brought down a box that weighs 49 in 2 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 25 to the truck, took 1 units of time, room left:0
|
||||||
|
Full truck 0 with load of 217 units of mass departed, round-trip will take 6
|
||||||
|
House dweller 0 created a box that weighs 14 in 6 units of time
|
||||||
|
House dweller 0 is done packing
|
||||||
|
House dweller 2 created a box that weighs 35 in 6 units of time
|
||||||
|
Mover 0 brought down a box that weighs 14 in 2 units of time
|
||||||
|
Mover 1 brought down a box that weighs 35 in 1 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 40 to the truck, took 2 units of time, room left:9
|
||||||
|
Trucker 0 loaded up a box that weighs 49 to the truck, took 1 units of time, room left:8
|
||||||
|
House dweller 2 created a box that weighs 11 in 6 units of time
|
||||||
|
House dweller 2 is done packing
|
||||||
|
Mover 0 is done moving boxes downstairs
|
||||||
|
Trucker 0 loaded up a box that weighs 14 to the truck, took 2 units of time, room left:7
|
||||||
|
Mover 1 brought down a box that weighs 11 in 2 units of time
|
||||||
|
Mover 1 is done moving boxes downstairs
|
||||||
|
Trucker 0 loaded up a box that weighs 35 to the truck, took 2 units of time, room left:6
|
||||||
|
Trucker 0 loaded up a box that weighs 11 to the truck, took 2 units of time, room left:5
|
||||||
|
Not full truck with load of 149 units of mass departed, one way trip will take 5
|
||||||
|
Trucker 0 is finished
|
||||||
|
Moving is finished!
|
100
Assignment3/documentation/sample_execution_2.txt
Normal file
100
Assignment3/documentation/sample_execution_2.txt
Normal file
@ -0,0 +1,100 @@
|
|||||||
|
Please input number of people living in the house, number of movers and number of truck drivers
|
||||||
|
5 2 2
|
||||||
|
Hello from house dweller 0
|
||||||
|
Hello from house dweller 1
|
||||||
|
Hello from house dweller 2
|
||||||
|
Hello from house dweller 3
|
||||||
|
Hello from mover 1
|
||||||
|
Hello from house dweller 4
|
||||||
|
Hello from truck driver 0
|
||||||
|
Hello from mover 0
|
||||||
|
Hello from truck driver 1
|
||||||
|
House dweller 0 created a box that weighs 11 in 1 units of time
|
||||||
|
House dweller 4 created a box that weighs 37 in 1 units of time
|
||||||
|
Mover 1 brought down a box that weighs 37 in 1 units of time
|
||||||
|
Mover 0 brought down a box that weighs 11 in 1 units of time
|
||||||
|
House dweller 3 created a box that weighs 40 in 3 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 37 to the truck, took 1 units of time, room left:9
|
||||||
|
House dweller 2 created a box that weighs 49 in 4 units of time
|
||||||
|
House dweller 4 created a box that weighs 22 in 3 units of time
|
||||||
|
House dweller 3 created a box that weighs 24 in 1 units of time
|
||||||
|
Trucker 1 loaded up a box that weighs 11 to the truck, took 2 units of time, room left:9
|
||||||
|
House dweller 0 created a box that weighs 24 in 4 units of time
|
||||||
|
Mover 0 brought down a box that weighs 40 in 2 units of time
|
||||||
|
House dweller 1 created a box that weighs 38 in 6 units of time
|
||||||
|
Mover 1 brought down a box that weighs 49 in 2 units of time
|
||||||
|
House dweller 4 created a box that weighs 44 in 2 units of time
|
||||||
|
Trucker 1 loaded up a box that weighs 40 to the truck, took 2 units of time, room left:8
|
||||||
|
House dweller 3 created a box that weighs 33 in 3 units of time
|
||||||
|
Mover 0 brought down a box that weighs 22 in 2 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 49 to the truck, took 2 units of time, room left:8
|
||||||
|
Mover 1 brought down a box that weighs 24 in 2 units of time
|
||||||
|
House dweller 2 created a box that weighs 6 in 4 units of time
|
||||||
|
House dweller 4 created a box that weighs 19 in 2 units of time
|
||||||
|
Mover 0 brought down a box that weighs 24 in 1 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 24 to the truck, took 1 units of time, room left:7
|
||||||
|
House dweller 0 created a box that weighs 19 in 4 units of time
|
||||||
|
Trucker 1 loaded up a box that weighs 22 to the truck, took 2 units of time, room left:7
|
||||||
|
Mover 0 brought down a box that weighs 44 in 1 units of time
|
||||||
|
House dweller 4 created a box that weighs 38 in 2 units of time
|
||||||
|
House dweller 4 is done packing
|
||||||
|
Mover 1 brought down a box that weighs 38 in 2 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 24 to the truck, took 1 units of time, room left:6
|
||||||
|
Mover 0 brought down a box that weighs 33 in 1 units of time
|
||||||
|
Mover 1 brought down a box that weighs 6 in 1 units of time
|
||||||
|
Trucker 1 loaded up a box that weighs 44 to the truck, took 2 units of time, room left:6
|
||||||
|
House dweller 1 created a box that weighs 26 in 6 units of time
|
||||||
|
House dweller 2 created a box that weighs 29 in 4 units of time
|
||||||
|
House dweller 3 created a box that weighs 8 in 5 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 38 to the truck, took 2 units of time, room left:5
|
||||||
|
Trucker 1 loaded up a box that weighs 33 to the truck, took 1 units of time, room left:5
|
||||||
|
Mover 0 brought down a box that weighs 19 in 2 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 6 to the truck, took 1 units of time, room left:4
|
||||||
|
House dweller 3 created a box that weighs 39 in 1 units of time
|
||||||
|
House dweller 3 is done packing
|
||||||
|
Mover 1 brought down a box that weighs 19 in 2 units of time
|
||||||
|
House dweller 0 created a box that weighs 44 in 5 units of time
|
||||||
|
Mover 1 brought down a box that weighs 26 in 1 units of time
|
||||||
|
Trucker 1 loaded up a box that weighs 19 to the truck, took 2 units of time, room left:4
|
||||||
|
Mover 0 brought down a box that weighs 38 in 2 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 19 to the truck, took 2 units of time, room left:3
|
||||||
|
Mover 1 brought down a box that weighs 29 in 1 units of time
|
||||||
|
Mover 0 brought down a box that weighs 8 in 1 units of time
|
||||||
|
House dweller 2 created a box that weighs 33 in 4 units of time
|
||||||
|
Trucker 1 loaded up a box that weighs 26 to the truck, took 2 units of time, room left:3
|
||||||
|
Mover 1 brought down a box that weighs 39 in 1 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 38 to the truck, took 2 units of time, room left:2
|
||||||
|
Mover 0 brought down a box that weighs 44 in 2 units of time
|
||||||
|
House dweller 1 created a box that weighs 17 in 6 units of time
|
||||||
|
Trucker 1 loaded up a box that weighs 29 to the truck, took 2 units of time, room left:2
|
||||||
|
Mover 1 brought down a box that weighs 33 in 2 units of time
|
||||||
|
Trucker 0 loaded up a box that weighs 8 to the truck, took 1 units of time, room left:1
|
||||||
|
House dweller 2 created a box that weighs 13 in 3 units of time
|
||||||
|
House dweller 2 is done packing
|
||||||
|
Trucker 1 loaded up a box that weighs 39 to the truck, took 1 units of time, room left:1
|
||||||
|
Trucker 0 loaded up a box that weighs 44 to the truck, took 1 units of time, room left:0
|
||||||
|
Full truck 0 with load of 287 units of mass departed, round-trip will take 8
|
||||||
|
Mover 0 brought down a box that weighs 17 in 2 units of time
|
||||||
|
House dweller 0 created a box that weighs 42 in 6 units of time
|
||||||
|
House dweller 0 is done packing
|
||||||
|
Mover 1 brought down a box that weighs 13 in 1 units of time
|
||||||
|
Trucker 1 loaded up a box that weighs 33 to the truck, took 1 units of time, room left:0
|
||||||
|
Full truck 1 with load of 296 units of mass departed, round-trip will take 8
|
||||||
|
House dweller 1 created a box that weighs 39 in 4 units of time
|
||||||
|
Mover 0 brought down a box that weighs 42 in 2 units of time
|
||||||
|
Mover 1 brought down a box that weighs 39 in 2 units of time
|
||||||
|
House dweller 1 created a box that weighs 5 in 5 units of time
|
||||||
|
House dweller 1 is done packing
|
||||||
|
Mover 0 is done moving boxes downstairs
|
||||||
|
Mover 1 brought down a box that weighs 5 in 2 units of time
|
||||||
|
Mover 1 is done moving boxes downstairs
|
||||||
|
Trucker 0 loaded up a box that weighs 17 to the truck, took 2 units of time, room left:9
|
||||||
|
Trucker 1 loaded up a box that weighs 13 to the truck, took 2 units of time, room left:9
|
||||||
|
Trucker 0 loaded up a box that weighs 42 to the truck, took 1 units of time, room left:8
|
||||||
|
Trucker 1 loaded up a box that weighs 39 to the truck, took 1 units of time, room left:8
|
||||||
|
Not full truck with load of 52 units of mass departed, one way trip will take 9
|
||||||
|
Trucker 0 loaded up a box that weighs 5 to the truck, took 1 units of time, room left:7
|
||||||
|
Not full truck with load of 64 units of mass departed, one way trip will take 8
|
||||||
|
Trucker 0 is finished
|
||||||
|
Trucker 1 is finished
|
||||||
|
Moving is finished!
|
17
Assignment3/examples/1-no_join-fixed.c
Normal file
17
Assignment3/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
Assignment3/examples/2-sleep no_join-fixed.c
Normal file
23
Assignment3/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
Assignment3/examples/3-losing_track_of_threads-fixed.c
Normal file
24
Assignment3/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
Assignment3/examples/4-passing_data_wrongly.c
Normal file
21
Assignment3/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
Assignment3/examples/4-passing_data_wrongly_fixed.c
Normal file
31
Assignment3/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
Assignment3/examples/5-pthread_detach.c
Normal file
19
Assignment3/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;
|
||||||
|
}
|
151
Assignment3/main.c
Normal file
151
Assignment3/main.c
Normal file
@ -0,0 +1,151 @@
|
|||||||
|
#include <pthread.h> // for pthread_create(), pthread_join(), etc.
|
||||||
|
#include <stdio.h> // for scanf(), printf(), etc.
|
||||||
|
#include <stdlib.h> // for malloc()
|
||||||
|
#include <unistd.h> // for sleep()
|
||||||
|
|
||||||
|
#define NUMBER_OF_BOXES_PER_DWELLER 5
|
||||||
|
#define ROOM_IN_TRUCK 10
|
||||||
|
#define MIN_BOX_WEIGHT 5
|
||||||
|
#define MAX_BOX_WEIGHT 50
|
||||||
|
#define MAX_TIME_FOR_HOUSE_DWELLER 7
|
||||||
|
#define MIN_TIME_FOR_HOUSE_DWELLER 1
|
||||||
|
#define MAX_TIME_FOR_MOVER 3
|
||||||
|
#define MIN_TIME_FOR_MOVER 1
|
||||||
|
#define MIN_TIME_FOR_TRUCKER 1
|
||||||
|
#define MAX_TIME_FOR_TRUCKER 3
|
||||||
|
#define MIN_TRIP_TIME 5
|
||||||
|
#define MAX_TRIP_TIME 10
|
||||||
|
#define RANDOM_WITHIN_RANGE(a,b,seed) (a+rand_r(&seed)%(b-a))
|
||||||
|
// For pipes
|
||||||
|
#define READ_END 0
|
||||||
|
#define WRITE_END 1
|
||||||
|
|
||||||
|
// Pipe between house dwellers and movers
|
||||||
|
int houseFloor[2];
|
||||||
|
// Pipe between movers and truckers
|
||||||
|
int nextToTrucks[2];
|
||||||
|
|
||||||
|
typedef struct args {
|
||||||
|
int id;
|
||||||
|
unsigned int seed;
|
||||||
|
} Args;
|
||||||
|
|
||||||
|
void* dweller(void* arg) {
|
||||||
|
Args *args = (Args*) arg;
|
||||||
|
int id = args->id;
|
||||||
|
unsigned int seed = args->seed;
|
||||||
|
printf("Hello from house dweller %d\n", id);
|
||||||
|
|
||||||
|
for(int i = 0; i < NUMBER_OF_BOXES_PER_DWELLER; i++) {
|
||||||
|
int weight = RANDOM_WITHIN_RANGE(MIN_BOX_WEIGHT, MAX_BOX_WEIGHT, seed);
|
||||||
|
int time = RANDOM_WITHIN_RANGE(MIN_TIME_FOR_HOUSE_DWELLER, MAX_TIME_FOR_HOUSE_DWELLER, seed);
|
||||||
|
sleep(time);
|
||||||
|
write(houseFloor[WRITE_END], &weight, sizeof(int));
|
||||||
|
printf("House dweller %d produced a box that weights %d in %d units of time\n", id, weight, time);
|
||||||
|
}
|
||||||
|
printf("House dweller %d is done packing\n", id);
|
||||||
|
free(args);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* mover(void* arg) {
|
||||||
|
Args *args = (Args*) arg;
|
||||||
|
int id = args->id;
|
||||||
|
unsigned int seed = args->seed;
|
||||||
|
int weight;
|
||||||
|
|
||||||
|
printf("Hello from mover %d\n", id);
|
||||||
|
while (read(houseFloor[READ_END], &weight, sizeof(int)) > 0) {
|
||||||
|
int time = RANDOM_WITHIN_RANGE(MIN_TIME_FOR_MOVER, MAX_TIME_FOR_MOVER, seed);
|
||||||
|
sleep(time);
|
||||||
|
printf("Mover %d brought down a box of weight %d in %d units of time\n", id, weight, time);
|
||||||
|
write(nextToTrucks[WRITE_END], &weight, sizeof(int));
|
||||||
|
}
|
||||||
|
printf("Mover %d is done moving\n", id);
|
||||||
|
free(args);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* trucker(void* arg) {
|
||||||
|
Args *args = (Args*) arg;
|
||||||
|
int id = args->id;
|
||||||
|
unsigned int seed = args->seed;
|
||||||
|
int capacity = ROOM_IN_TRUCK;
|
||||||
|
int truckWeight = 0;
|
||||||
|
int weight;
|
||||||
|
|
||||||
|
printf("Hello from trucker %d\n", id);
|
||||||
|
while (read(nextToTrucks[READ_END], &weight, sizeof(int)) > 0) {
|
||||||
|
capacity--;
|
||||||
|
truckWeight += weight;
|
||||||
|
int time = RANDOM_WITHIN_RANGE(MIN_TIME_FOR_TRUCKER, MAX_TIME_FOR_TRUCKER, seed);
|
||||||
|
sleep(time);
|
||||||
|
printf("Trucker %d loaded up box of weight %d in %d units of time, room left: %d\n", id, weight, time, capacity);
|
||||||
|
|
||||||
|
if (capacity == 0) {
|
||||||
|
time = RANDOM_WITHIN_RANGE(MIN_TRIP_TIME, MAX_TRIP_TIME, seed);
|
||||||
|
printf("Full truck %d with load of %d units of mass departed. Round trip will take %d units of time\n", id, truckWeight, time);
|
||||||
|
sleep(time);
|
||||||
|
capacity = ROOM_IN_TRUCK;
|
||||||
|
truckWeight = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("Truck %d is not full\n", id);
|
||||||
|
int time = RANDOM_WITHIN_RANGE(MIN_TRIP_TIME/2, MAX_TRIP_TIME/2, seed); // One way trip, so half the time
|
||||||
|
sleep(time);
|
||||||
|
printf("Not full truck %d with load of %d units of mass departed. One way will take %d units of time\n", id, truckWeight, time);
|
||||||
|
printf("Truck %d is finished\n", id);
|
||||||
|
free(args);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(int argc, char **argv) {
|
||||||
|
srand(time(NULL));
|
||||||
|
|
||||||
|
pipe(houseFloor);
|
||||||
|
pipe(nextToTrucks);
|
||||||
|
|
||||||
|
int numberOfHouseDwellers, numberOfMovers, numberOfTruckers;
|
||||||
|
printf("Please input number of people living in the house, number of movers and number of truck drivers\n");
|
||||||
|
scanf("%d %d %d", &numberOfHouseDwellers, &numberOfMovers, &numberOfTruckers);
|
||||||
|
|
||||||
|
pthread_t dwellerThreads[numberOfHouseDwellers];
|
||||||
|
pthread_t moverThreads[numberOfMovers];
|
||||||
|
pthread_t truckerThreads[numberOfTruckers];
|
||||||
|
|
||||||
|
for(int i = 0; i < numberOfHouseDwellers; i++) {
|
||||||
|
Args *args = (Args*) malloc(sizeof(Args));
|
||||||
|
args->id = i;
|
||||||
|
args->seed = rand();
|
||||||
|
pthread_create(&dwellerThreads[i], NULL, &dweller, (void*) args);
|
||||||
|
}
|
||||||
|
for(int i = 0; i < numberOfMovers; i++) {
|
||||||
|
Args *args = (Args*) malloc(sizeof(Args));
|
||||||
|
args->id = i;
|
||||||
|
args->seed = rand();
|
||||||
|
pthread_create(&moverThreads[i], NULL, &mover, (void*) args);
|
||||||
|
}
|
||||||
|
for(int i = 0; i < numberOfTruckers; i++) {
|
||||||
|
Args *args = (Args*) malloc(sizeof(Args));
|
||||||
|
args->id = i;
|
||||||
|
args->seed = rand();
|
||||||
|
pthread_create(&truckerThreads[i], NULL, &trucker, (void*) args);
|
||||||
|
}
|
||||||
|
for (int i = 0; i < numberOfHouseDwellers; i++) {
|
||||||
|
pthread_join(dwellerThreads[i], NULL);
|
||||||
|
}
|
||||||
|
close(houseFloor[WRITE_END]);
|
||||||
|
for (int i = 0; i < numberOfMovers; i++) {
|
||||||
|
pthread_join(moverThreads[i], NULL);
|
||||||
|
}
|
||||||
|
close(houseFloor[READ_END]);
|
||||||
|
close(nextToTrucks[WRITE_END]);
|
||||||
|
for (int i = 0; i < numberOfTruckers; i++) {
|
||||||
|
pthread_join(truckerThreads[i], NULL);
|
||||||
|
}
|
||||||
|
close(nextToTrucks[READ_END]);
|
||||||
|
|
||||||
|
printf("Moving is finished!\n");
|
||||||
|
return 0;
|
||||||
|
}
|
63
Assignment3/outputs/3 2 1.txt
Normal file
63
Assignment3/outputs/3 2 1.txt
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
Please input number of people living in the house, number of movers and number of truck drivers
|
||||||
|
3 2 1
|
||||||
|
Hello from house dweller 0
|
||||||
|
Hello from house dweller 1
|
||||||
|
Hello from house dweller 2
|
||||||
|
Hello from mover 0
|
||||||
|
Hello from mover 1
|
||||||
|
Hello from trucker 0
|
||||||
|
House dweller 0 produced a box that weights 22 in 1 units of time
|
||||||
|
House dweller 2 produced a box that weights 24 in 2 units of time
|
||||||
|
House dweller 0 produced a box that weights 42 in 1 units of time
|
||||||
|
Mover 0 picked up a box of weight 22 in 2 units of time
|
||||||
|
Mover 1 picked up a box of weight 24 in 1 units of time
|
||||||
|
House dweller 1 produced a box that weights 42 in 4 units of time
|
||||||
|
House dweller 2 produced a box that weights 47 in 2 units of time
|
||||||
|
Mover 0 picked up a box of weight 42 in 1 units of time
|
||||||
|
Trucker 0 loaded a box of weight 22 in 2 units of time, room left: 9
|
||||||
|
Mover 0 picked up a box of weight 47 in 1 units of time
|
||||||
|
Mover 1 picked up a box of weight 42 in 2 units of time
|
||||||
|
House dweller 0 produced a box that weights 26 in 5 units of time
|
||||||
|
Trucker 0 loaded a box of weight 24 in 2 units of time, room left: 8
|
||||||
|
Mover 0 picked up a box of weight 26 in 1 units of time
|
||||||
|
Trucker 0 loaded a box of weight 42 in 1 units of time, room left: 7
|
||||||
|
House dweller 2 produced a box that weights 19 in 5 units of time
|
||||||
|
House dweller 0 produced a box that weights 45 in 2 units of time
|
||||||
|
House dweller 1 produced a box that weights 40 in 6 units of time
|
||||||
|
Mover 1 picked up a box of weight 19 in 1 units of time
|
||||||
|
Trucker 0 loaded a box of weight 47 in 2 units of time, room left: 6
|
||||||
|
Mover 0 picked up a box of weight 45 in 2 units of time
|
||||||
|
Mover 1 picked up a box of weight 40 in 1 units of time
|
||||||
|
Trucker 0 loaded a box of weight 42 in 1 units of time, room left: 5
|
||||||
|
House dweller 0 produced a box that weights 37 in 3 units of time
|
||||||
|
House dweller 0 is done packing
|
||||||
|
Mover 0 picked up a box of weight 37 in 1 units of time
|
||||||
|
Trucker 0 loaded a box of weight 26 in 2 units of time, room left: 4
|
||||||
|
House dweller 1 produced a box that weights 31 in 4 units of time
|
||||||
|
Trucker 0 loaded a box of weight 19 in 1 units of time, room left: 3
|
||||||
|
Mover 1 picked up a box of weight 31 in 1 units of time
|
||||||
|
House dweller 1 produced a box that weights 17 in 1 units of time
|
||||||
|
House dweller 2 produced a box that weights 27 in 6 units of time
|
||||||
|
Trucker 0 loaded a box of weight 40 in 2 units of time, room left: 2
|
||||||
|
Mover 0 picked up a box of weight 27 in 2 units of time
|
||||||
|
Mover 1 picked up a box of weight 17 in 2 units of time
|
||||||
|
Trucker 0 loaded a box of weight 45 in 2 units of time, room left: 1
|
||||||
|
House dweller 2 produced a box that weights 33 in 4 units of time
|
||||||
|
House dweller 2 is done packing
|
||||||
|
Trucker 0 loaded a box of weight 37 in 1 units of time, room left: 0
|
||||||
|
Full truck 0 with load of 344 units of mass departed. Round trip will take 7 units of time
|
||||||
|
House dweller 1 produced a box that weights 14 in 5 units of time
|
||||||
|
House dweller 1 is done packing
|
||||||
|
Mover 0 picked up a box of weight 33 in 1 units of time
|
||||||
|
Mover 0 is done moving
|
||||||
|
Mover 1 picked up a box of weight 14 in 1 units of time
|
||||||
|
Mover 1 is done moving
|
||||||
|
Trucker 0 loaded a box of weight 31 in 2 units of time, room left: 9
|
||||||
|
Trucker 0 loaded a box of weight 27 in 2 units of time, room left: 8
|
||||||
|
Trucker 0 loaded a box of weight 17 in 2 units of time, room left: 7
|
||||||
|
Trucker 0 loaded a box of weight 33 in 2 units of time, room left: 6
|
||||||
|
Trucker 0 loaded a box of weight 14 in 2 units of time, room left: 5
|
||||||
|
Truck 0 is not full
|
||||||
|
Not full truck 0 with load of 122 units of mass departed. One way will take 3 units of time
|
||||||
|
Truck 0 is finished
|
||||||
|
Moving is finished!
|
Loading…
Reference in New Issue
Block a user