student_code.c skeleton below to complete in programming language C
#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 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; }
Synopsis In this assignment, you will have a process that will create multiple threads at different times. These ve 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 start Clock() is invoked, the getCurrent Time() 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; li fetime. It is not necessary that the individual thread lines are in any specific or der 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: hartaA B ON Icnt 1 sampicz_tu.txt 10 MON Thread with ID 110 is started IZ) Me Thread with ID LZ is started. 15) Thed with ID 1 is started 191 Thread with ID tZ is finished. 1) Thread with ID 1 is finished 1161 Thread with ID 19 is finished 1201 Neu Thrend uith ID t22 is started 1761 Thrond uith ID t%2 is finished. Curta pova2 Synopsis In this assignment, you will have a process that will create multiple threads at different times. These ve 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 start Clock() is invoked, the getCurrent Time() 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; li fetime. It is not necessary that the individual thread lines are in any specific or der 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: hartaA B ON Icnt 1 sampicz_tu.txt 10 MON Thread with ID 110 is started IZ) Me Thread with ID LZ is started. 15) Thed with ID 1 is started 191 Thread with ID tZ is finished. 1) Thread with ID 1 is finished 1161 Thread with ID 19 is finished 1201 Neu Thrend uith ID t22 is started 1761 Thrond uith ID t%2 is finished. Curta pova2