ou are provided a skeleton code in the file student_code.c. Some functions are completely implemented, and some are partially implemented. Additionally, you can add your own functions if required. Complete the program as per following details so that we can have functionality as described in the Synopsis above. All the code in single C file. REMEMBER DO NOT USE sleep(). 1. The given code reads the file's content for you and populates a dynamic array of type struct thread with information about the threads. The threads have filled in this list of threads for you. Additionally, you can check the threadCount variable to see how many threads were created using the input file. If more members are needed, you can add them. You might be able to initialise those extra members during file read if you wish to. 2. The main() already contains the code to read threads from input file. However, there is no code to initialize, execute and synchronize threads. You have to perform these tasks in a suitable way there. startClock() invocation as given in main() is required to initiate the program's internal clock, so do not remove it. 3. The threadRun() function also contains the code that a thread must run. However, the synchronization logic (entry/exit section) is missing. Add the suitable code before and after the critical section. 4. You will need to make and use POSIX pthreads and semaphore(s) to implement the required logic. 5. The image shows the expected output for the sample input file provided. In this output when there are multiple threads finishing (or starting) at the same time. However, the critical section order, e.g. in t=5 both t02 and t07 perform their critical section, must always be as per our synchronization requirement. Also, you have to make sure that a thread must be started at its creation time as per the input file
Description
You are provided a skeleton code in the file student_code.c. Some functions are completely implemented, and some are partially implemented. Additionally, you can add your own functions if required. Complete the program as per following details so that we can have functionality as described in the Synopsis above. All the code in single C file. REMEMBER DO NOT USE sleep().
1. The given code reads the file's content for you and populates a dynamic array of type struct thread with information about the threads. The threads have filled in this list of threads for you. Additionally, you can check the threadCount variable to see how many threads were created using the input file. If more members are needed, you can add them. You might be able to initialise those extra members during file read if you wish to.
2. The main() already contains the code to read threads from input file. However, there is no code to initialize, execute and synchronize threads. You have to perform these tasks in a suitable way there. startClock() invocation as given in main() is required to initiate the program's internal clock, so do not remove it.
3. The threadRun() function also contains the code that a thread must run. However, the synchronization logic (entry/exit section) is missing. Add the suitable code before and after the critical section.
4. You will need to make and use POSIX pthreads and semaphore(s) to implement the required logic.
5. The image shows the expected output for the sample input file provided. In this output when there are multiple threads finishing (or starting) at the same time. However, the critical section order, e.g. in t=5 both t02 and t07 perform their critical section, must always be as per our synchronization requirement. Also, you have to make sure that a thread must be started at its creation time as per the input file
Code given:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h>
#include <time.h>
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, you can add more members if required
{ char tid[4];//id of the thread as read from file
unsigned int startTime;
int state;
pthread_t handle;
int retVal;
} Thread; //you can add more functions here if required
void* threadRun(void* t);//the thread function, the code executed by each thread
int readFile(char* fileName, Thread** threads);
int main(int argc, char *argv[]) {
if(argc<2) { printf("Input file name missing...exiting with error code -1\n"); return -1; }
Thread* threads = NULL;
int threadCount = readFile(argv[1],&threads);
startClock();
//write some suitable code here to initiate, progress and terminate the threads following the requirements
return threadCount;
}
int readFile(char* fileName, Thread** threads)//do not modify this method
{ FILE *in = fopen(fileName, "r"); if(!in) { printf("Child A: Error in opening input file...exiting with error code -1\n"); 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,"\r\n"); while(command!=NULL) { threadCount++; command = strtok(NULL,"\r\n"); } *threads = (Thread*) malloc(sizeof(Thread)*threadCount); char* lines[threadCount]; command = NULL; int i=0; command = strtok(fileContent,"\r\n"); while(command!=NULL) { lines[i] = malloc(sizeof(command)*sizeof(char)); strcpy(lines[i],command); i++; command = strtok(NULL,"\r\n"); } for(int k=0; k<threadCount; k++) { char* token = NULL; int j = 0; token = strtok(lines[k],";"); while(token!=NULL) { (*threads)[k].state=0; if(j==0) strcpy((*threads)[k].tid,token); if(j==1) (*threads)[k].startTime=atoi(token); j++; token = strtok(NULL,";"); } } return threadCount; }
void logStart(char* tID) { printf("[%ld] New Thread with ID %s is started.\n", getCurrentTime(), tID); }
void logFinish(char* tID) { printf("[%ld] Thread with ID %s is finished.\n", getCurrentTime(), tID); }
void* threadRun(void* t) {
logStart(((Thread*)t)->tid);
printf statement printf("[%ld] Thread %s is in its critical section\n",getCurrentTime(), ((Thread*)t)->tid);
logFinish(((Thread*)t)->tid); ((Thread*)t)->state = -1; pthread_exit(0);
} void startClock() { programClock = time(NULL); }
long getCurrentTime(){ time_t now; now = time(NULL); return now-programClock; }
Sample_in.txt
t00;1
t01;20
t03;2
t07;3
t05;4
t02;5
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images