Delete lab questions
parent
3968765c35
commit
81fc21676b
@ -1,38 +0,0 @@
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define NUM_THREADS 20
|
||||
|
||||
void *fizzbuzz(void *arg) {
|
||||
int *number = (int *) arg;
|
||||
if (*number % 3 == 0) {
|
||||
printf("%d -> %s\n", *number, "fizz");
|
||||
} else if (*number % 5 == 0) {
|
||||
printf("%d -> %s\n", *number, "buzz");
|
||||
} else {
|
||||
printf("%d -> %s\n", *number, "no div");
|
||||
}
|
||||
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
pthread_t threads[20];
|
||||
int nums[20];
|
||||
|
||||
int i;
|
||||
for (i = 0; i < NUM_THREADS; i++) {
|
||||
nums[i] = i + 1;
|
||||
pthread_create(&threads[i], NULL, fizzbuzz, &nums[i]);
|
||||
}
|
||||
|
||||
for (i = 0; i < NUM_THREADS; i++) {
|
||||
char *returnedStr;
|
||||
pthread_join(threads[i], (void **) &returnedStr);
|
||||
}
|
||||
|
||||
pthread_exit(NULL);
|
||||
|
||||
}
|
@ -1,44 +0,0 @@
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdbool.h>
|
||||
#include <unistd.h> // gives us sleep(). Replace with windows.h for compiling on windows!
|
||||
|
||||
#define NUM_THREADS 20
|
||||
|
||||
typedef struct _threadinfo {
|
||||
bool * keepRunning;
|
||||
int * total;
|
||||
} threadinfo_t;
|
||||
|
||||
void * getInput(void * arg) {
|
||||
threadinfo_t * args = (threadinfo_t *) arg;
|
||||
|
||||
int currInput;
|
||||
printf("Input as much as you can --- you have 10 seconds!\n");
|
||||
while (*args->keepRunning) {
|
||||
printf("Add another number! : ");
|
||||
scanf("%d", &currInput);
|
||||
*args->total += currInput;
|
||||
}
|
||||
pthread_exit(NULL);
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
pthread_t thread;
|
||||
bool keepRunning = true;
|
||||
int total = 0;
|
||||
threadinfo_t args = {&keepRunning, &total};
|
||||
|
||||
pthread_create(&thread, NULL, getInput, &args);
|
||||
|
||||
sleep(10); // sleep for 10 secs
|
||||
keepRunning = false;
|
||||
pthread_join(thread, NULL);
|
||||
printf("\n\nYou managed to enter a total of: %d!\n", total);
|
||||
|
||||
pthread_exit(NULL);
|
||||
|
||||
}
|
||||
|
@ -1,8 +0,0 @@
|
||||
#include <unistd.h>
|
||||
|
||||
// the rest is up to you! Implement a program which starts _two_ pthreads.
|
||||
// One reads input from the user, and adds numbers to a total.
|
||||
// the other prints the current total value every 3 seconds.
|
||||
// After 30 seconds, the main thread sets a bool to false that signals
|
||||
// to both threads that they should join, just like in 3.c.
|
||||
// You can use sleep(int seconds) from unistd.h to make your threads sleep.
|
Binary file not shown.
Loading…
Reference in New Issue