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

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

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

 

[1] New Thread with ID t00 is started.
[1] Thread t00 is in its critical section
[1] Thread with ID t00 is finished.
[2] New Thread with ID t03 is started.
[2] Thread t03 is in its critical section
[2] Thread with ID t03 is finished.
[3] New Thread with ID t07 is started.
[4] New Thread with ID t05 is started.
[5] New Thread with ID t02 is started.
[5] Thread t02 is in its critical section
[5] Thread t07 is in its critical section
[5] Thread with ID t07 is finished.
[5] Thread with ID t02 is finished.
[20] Thread t05 is in its critical section
[20] Thread with ID t05 is finished.
[20] New Thread with ID t01 is started.
[20] Thread t01 is in its critical section
[20] Thread with ID t01 is finished.
Transcribed Image Text:[1] New Thread with ID t00 is started. [1] Thread t00 is in its critical section [1] Thread with ID t00 is finished. [2] New Thread with ID t03 is started. [2] Thread t03 is in its critical section [2] Thread with ID t03 is finished. [3] New Thread with ID t07 is started. [4] New Thread with ID t05 is started. [5] New Thread with ID t02 is started. [5] Thread t02 is in its critical section [5] Thread t07 is in its critical section [5] Thread with ID t07 is finished. [5] Thread with ID t02 is finished. [20] Thread t05 is in its critical section [20] Thread with ID t05 is finished. [20] New Thread with ID t01 is started. [20] Thread t01 is in its critical section [20] Thread with ID t01 is finished.
Synopsis:
In this assignment, our process will create multiple threads at different times. These threads may have different start_time but there is no lifetime. Each
thread after its creation runs a small critical section and then terminates. All threads perform same action/code. Most of the code such as reading the
input file, creating the threads etc. is provided. Your task is to implement following synchronization logic with the help of POSIX pthreads and
semaphores:
• Only one thread can be in its critical section at any time in this process.
• The first thread, in terms of creation time, enters first in its critical section.
• After that threads are permitted to perform their critical section based on their ID.
o Threads are given IDs in the format txy where x and y are digits (0-9). Thread IDs are unique. Threads may have same or different start_time.
Thread entries in the input file can be in any order.
o The "y" in thread IDs thus will either be an even digit or odd digit.
o After the first thread, the next thread that will be permitted to perform its critical section must be the one in which "y" is different i.e. if "y" was even
in first thread then in the next it must be odd or vice versa.
o For the rest of the process, you must follow the same scheme i.e. two threads with odd "y" or even "y" can not perform critical section
simultaneously.
• Since synchronization may lead to deadlock or starvation, you have to make sure that your solution is deadlock free i.e. your program must terminate
successfully, and all the threads must perform their critical section.
• One extended form of starvation will be that towards the end, we have all odd or all even processes left, and they are all locked because there are no
other type (odd/even) of threads left. Once the process reaches to that stage, you must let them perform their critical section to avoid starvation and
progress towards the end of the process. In the screen shot on the next page, you will notice that t07, t05 and t01 perform their critical section without
any even number thread separating them because there are no more even number threads left. However, you must make sure that there are no other
threads coming in future which could help avoid this situation. If there is chance for more threads coming, then you will hold this till then. For example, in
the screenshot on the next page, this started happening at t=20 which is the creation time of the last thread in our input file.
Transcribed Image Text:Synopsis: In this assignment, our process will create multiple threads at different times. These threads may have different start_time but there is no lifetime. Each thread after its creation runs a small critical section and then terminates. All threads perform same action/code. Most of the code such as reading the input file, creating the threads etc. is provided. Your task is to implement following synchronization logic with the help of POSIX pthreads and semaphores: • Only one thread can be in its critical section at any time in this process. • The first thread, in terms of creation time, enters first in its critical section. • After that threads are permitted to perform their critical section based on their ID. o Threads are given IDs in the format txy where x and y are digits (0-9). Thread IDs are unique. Threads may have same or different start_time. Thread entries in the input file can be in any order. o The "y" in thread IDs thus will either be an even digit or odd digit. o After the first thread, the next thread that will be permitted to perform its critical section must be the one in which "y" is different i.e. if "y" was even in first thread then in the next it must be odd or vice versa. o For the rest of the process, you must follow the same scheme i.e. two threads with odd "y" or even "y" can not perform critical section simultaneously. • Since synchronization may lead to deadlock or starvation, you have to make sure that your solution is deadlock free i.e. your program must terminate successfully, and all the threads must perform their critical section. • One extended form of starvation will be that towards the end, we have all odd or all even processes left, and they are all locked because there are no other type (odd/even) of threads left. Once the process reaches to that stage, you must let them perform their critical section to avoid starvation and progress towards the end of the process. In the screen shot on the next page, you will notice that t07, t05 and t01 perform their critical section without any even number thread separating them because there are no more even number threads left. However, you must make sure that there are no other threads coming in future which could help avoid this situation. If there is chance for more threads coming, then you will hold this till then. For example, in the screenshot on the next page, this started happening at t=20 which is the creation time of the last thread in our input file.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY