1. Write a multithreaded routine that creates 10 threads and each thread reads a
ID: 3548396 • Letter: 1
Question
1. Write a multithreaded routine that creates 10 threads and each thread reads a different file. Have the main program concatinate all of the read files into one output file on the disk
2. Write a multithreaded routine that uses semaphores to communicate between one thread that reads a large file and passes the result in a buffer to another thread.
3. Write your own ls routine that reads a directory, file names, size, user id, group id, permissions, size and inode number for each file in a designated directory.
4. Write a multithreaded routine that uses condition codes to keep track of the time and have another thread put out the time every second.
In C not C++
Explanation / Answer
2)
#include <stdio.h> /* standard I/O routines */
#include <pthread.h> /* pthread functions and data structures */
/* function to be executed by the new thread */
void* PrintHello(void* data)
{
int my_data = (int)data; /* data received by thread */
pthread_detach(pthread_self());
printf("Hello from new thread - got %d ", my_data);
pthread_exit(NULL); /* terminate the thread */
}
/* like any C program, program's execution begins in main */
int main(int argc, char* argv[])
{
int rc; /* return value */
pthread_t thread_id; /* thread's ID (just an integer) */
int t = 11; /* data passed to the new thread */
/* create a new thread that will execute 'PrintHello' */
rc = pthread_create(&thread_id, NULL, PrintHello, (void*)t);
if(rc) /* could not create thread */
{
printf(" ERROR: return code from pthread_create is %d ", rc);
exit(1);
}
printf(" Created new thread (%d) ... ", thread_id);
pthread_exit(NULL); /* terminate the thread */
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.