Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this assignment, you will have a process that will create multiple threads at different times. These may have different start_time and lifetime. Once all

In this assignment, you will have a process that will create multiple threads at different times. These may have different start_time and lifetime. Once all threads are over, the process will terminate. There are three tasks that should be performed:

- Read the input file that contians thread properties: ID, start time and lifetime

- Create a thread whenever it is the start time of some thread

- All the threads implement/run same function. Implement this function so that the thread should terminate when it has consumed time units equal to its lifetime

Skeleton code below:

#include #include #include #include #include #include #include

void logStart(char* tID);//function to log that a new thread is started void logFinish(char* tID);//function to log that a thread has finished its time

void startClock();//function to start program clock long getCurrentTime();//function to check current time since clock was started time_t programClock;//the global timer/clock for the program

typedef struct thread //represents a single thread { char tid[4];//id of the thread as read from file //add more members here as per requirement } Thread;

void* threadRun(void* t);//the thread function, the code executed by each thread int readFile(char* fileName, Thread** threads);//function to read the file content and build array of threads

int main(int argc, char *argv[]) { if(argc

//you can add some suitable code here as per problem sepcification

while()//put a suitable condition here to run your program { //write suitable code here to run the threads } return 0; }

int readFile(char* fileName, Thread** threads)//use this method in a suitable way to read file { FILE *in = fopen(fileName, "r"); if(!in) { printf("Child A: Error in opening input file...exiting with error code -1 "); return -1; }

struct stat st; fstat(fileno(in), &st); char* fileContent = (char*)malloc(((int)st.st_size+1)* sizeof(char)); fileContent[0]='\0'; while(!feof(in)) { char line[100]; if(fgets(line,100,in)!=NULL) { strncat(fileContent,line,strlen(line)); } } fclose(in);

char* command = NULL; int threadCount = 0; char* fileCopy = (char*)malloc((strlen(fileContent)+1)*sizeof(char)); strcpy(fileCopy,fileContent); command = strtok(fileCopy," "); while(command!=NULL) { threadCount++; command = strtok(NULL," "); } *threads = (Thread*) malloc(sizeof(Thread)*threadCount);

char* lines[threadCount]; command = NULL; int i=0; command = strtok(fileContent," "); while(command!=NULL) { lines[i] = malloc(sizeof(command)*sizeof(char)); strcpy(lines[i],command); i++; command = strtok(NULL," "); }

for(int k=0; k { char* token = NULL; int j = 0; token = strtok(lines[k],";"); while(token!=NULL) { //this loop tokenizes each line of input file //write your code here to populate instances of Thread to build a collection } } return threadCount; }

void logStart(char* tID)//invoke this method when you start a thread { printf("[%ld] New Thread with ID %s is started. ", getCurrentTime(), tID); }

void logFinish(char* tID)//invoke this method when a thread is over { printf("[%ld] Thread with ID %s is finished. ", getCurrentTime(), tID); }

void* threadRun(void* t)//implement this function in a suitable way { }

void startClock()//invoke this method when you start servicing threads { programClock = time(NULL); }

long getCurrentTime()//invoke this method whenever you want to check how much time units passed //since you invoked startClock() { time_t now; now = time(NULL); return now-programClock; }

image text in transcribed

Synopsis In this assignment, you will have a process that will create multiple threads at different times. These threads may have different start_time and lifetime. Once all the threads are over, the process will terminate. There are three tasks that should be performed: Read the input file that contains thread properties: ID, start time and lifetime. Create a thread whenever it is the start time of some thread. All the threads implement run same function. Implement this function so that the thread should terminate when it has consumed time units equal to its lifetime. Description For this assignment, you are provided a skeleton code in the file student_code.c. Some functions are completely implemented, some are partially and for some only header is provided. Additionally, you can write your own functions if required. Complete the program as per the following details so that we can have functionality as described in Synopsis as above. Write all the code in single C file: 1. The program has its local clock mechanism. You must invoke startClock() when you are ready to service threads. The file reading should be performed before this. Once the startClock() is invoked, the getCurrentTime() can be invoked to see how much time units are passed since startClock() was invoked. This will provide a local clock ticks behavior to service your threads. 2. The thread information will be provided to this program in a text file. You must accept the name of this file as command line argument. In this file, there will be one thread information per line. Each thread information has three attributes provided in the order: thread_id;start_time; lifetime. It is not necessary that the individual thread lines are in any specific order.start_time indicates that at what time a thread should be created and started lifetime is the time units for which a thread must stay alive. Use the methods in step 1 in a suitable way to implement this functionality. 3. To read the file, readFile() function is provided. However, it has a small part missing that you must complete so that the thread attributes can be stored in memory in a suitable way. This also requires the completion of struct thread. Add suitable members to struct thread and the store the content of the file in a suitable data structure. 4. Once you have thread information in memory, start servicing the threads using the suitable POSIX library calls. main() is provided but you have to complete it in a suitable way. The program will keep running if there are some threads which are not yet created or if there are some running threads. 5. The threadRun() is the function that each thread must run. Implement this function so that each thread takes care of its lifetime and terminates when that is over. 6. The image below shows the expected output for the sample input file provided with this assignment: pscubuntu: cp386/a2$ .Assignment_1 samplez in.txt 111 Mew Thread with ID 118 is started. IZ1 Merw Thread uith ID t2 ix started 151 Mew Thread with ID ti is started 191 Thread with ID t2 is finished. [10] Thread with ID ti is finished. [16] Thread with ID tie is finished. (20) New Thread with ID t22 is started [25] Thread with ID t22 is finished. psc@ubuntu:cp386/a2$ Synopsis In this assignment, you will have a process that will create multiple threads at different times. These threads may have different start_time and lifetime. Once all the threads are over, the process will terminate. There are three tasks that should be performed: Read the input file that contains thread properties: ID, start time and lifetime. Create a thread whenever it is the start time of some thread. All the threads implement run same function. Implement this function so that the thread should terminate when it has consumed time units equal to its lifetime. Description For this assignment, you are provided a skeleton code in the file student_code.c. Some functions are completely implemented, some are partially and for some only header is provided. Additionally, you can write your own functions if required. Complete the program as per the following details so that we can have functionality as described in Synopsis as above. Write all the code in single C file: 1. The program has its local clock mechanism. You must invoke startClock() when you are ready to service threads. The file reading should be performed before this. Once the startClock() is invoked, the getCurrentTime() can be invoked to see how much time units are passed since startClock() was invoked. This will provide a local clock ticks behavior to service your threads. 2. The thread information will be provided to this program in a text file. You must accept the name of this file as command line argument. In this file, there will be one thread information per line. Each thread information has three attributes provided in the order: thread_id;start_time; lifetime. It is not necessary that the individual thread lines are in any specific order.start_time indicates that at what time a thread should be created and started lifetime is the time units for which a thread must stay alive. Use the methods in step 1 in a suitable way to implement this functionality. 3. To read the file, readFile() function is provided. However, it has a small part missing that you must complete so that the thread attributes can be stored in memory in a suitable way. This also requires the completion of struct thread. Add suitable members to struct thread and the store the content of the file in a suitable data structure. 4. Once you have thread information in memory, start servicing the threads using the suitable POSIX library calls. main() is provided but you have to complete it in a suitable way. The program will keep running if there are some threads which are not yet created or if there are some running threads. 5. The threadRun() is the function that each thread must run. Implement this function so that each thread takes care of its lifetime and terminates when that is over. 6. The image below shows the expected output for the sample input file provided with this assignment: pscubuntu: cp386/a2$ .Assignment_1 samplez in.txt 111 Mew Thread with ID 118 is started. IZ1 Merw Thread uith ID t2 ix started 151 Mew Thread with ID ti is started 191 Thread with ID t2 is finished. [10] Thread with ID ti is finished. [16] Thread with ID tie is finished. (20) New Thread with ID t22 is started [25] Thread with ID t22 is finished. psc@ubuntu:cp386/a2$

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

PostgreSQL Up And Running A Practical Guide To The Advanced Open Source Database

Authors: Regina Obe, Leo Hsu

3rd Edition

1491963417, 978-1491963418

More Books

Students also viewed these Databases questions

Question

2. Define identity.

Answered: 1 week ago

Question

1. Identify three communication approaches to identity.

Answered: 1 week ago

Question

4. Describe phases of majority identity development.

Answered: 1 week ago