5 Replies - 10891 Views - Last Post: 20 October 2011 - 10:09 AM Rate Topic: -----

#1 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

How to run C programs in Linux terminal

Posted 11 April 2011 - 01:16 PM

Was not exactly sure where to post this so since the code is in C, thought I would place it here.

I wrote this program in C using the Linux terminal. I used the built in VI editor to write it and the built in gcc compiler to see if it would compile. But how would I go about actually running it in terminal? It needs input files.

I will provide my code if you want to look at it and see what I need to run it.
#include<stdio.h>
#include<stdlib.h> 
#include<string.h>
#include<sys/types.h>
#include<fcntl.h>

const int QUEUE_END = 1;
const int IDLING   = -1;

struct PBR {
	int 	processID;		//Process ID.
	int 	processArrivalTime;	//Process arrival time.
	int 	remainingRunTime;     	//Decreases as execution time unit increases.  Used to find execution termination (i.e.: 0 -> terminate).
	int 	executionTime;		//Execution time.
	int 	startTime;		//The process start time.
	int 	blockIO;		//the percetage for IO inturuption for given process id.
	double 	currentPriority;	//The priority calcaluation on each re-scheduele. 
	struct 	PBR *next; 		//A pointer to the next PBR in the queue (i.e. linked list).
};

/*
Two queues are maintained.  The first one reads all input from file to this queue so that it can simulate the arrival of PID.
*/
struct 	PBR *INTURUPT_QUEUEEND		= NULL;
struct	PBR *INTURUPT_QUEUESTART	= NULL;
struct 	PBR *INTURUPT_CURRENT_PROCESS	= NULL;

/*
This queue is the actual queue intended to simulate the actual schedueling process.  Will be known as 'process queue'.
*/
struct 	PBR *QUEUEEND			= NULL;
struct 	PBR *QUEUESTART			= NULL;
struct 	PBR *CURRENT_PROCESS		= NULL;
struct	PBR *CURRENT_NEXT		= NULL;

//Global variables.
char 	szSourceFile[256];			//Input file.
int 	gSeed				= 0;	//Seed.
char 	gSchedule			= 0;	//Scheduele type.
int 	gTimeQuantum			= 0;	//Time slice.
int 	gTotalwaitTime			= 0;	//Total wait.
int 	gTotalturnAroundTime		= 0;	//Total turn around time.
int 	gTotalExecutionTime		= 0;	//Total execution time.

/*
Forward referencing.
*/
void 	CreateInturupt(int nPID, int nArrivalTime, int nExecutionDelay, int nIOBlocking);
void 	CreateProcess(int nPID, int nArrivalTime, int nremainTime, int nblockIO, double currentPriority);
void 	JoinProcessQueue();
int 	Process(int *PID, int *AT, int *RT, int *ST, int);
int 	ProcessStatus();
int 	IsNotEmpty();
int 	LastProcessArivedAt();
int 	ReadLine(FILE *,char *);
int 	FileExist(char *);
int 	ReadOk(FILE *fp);
int 	Do_f_scheduling();
int		Do_rr_scheduling();
int		Do_p_scheduling();
int 	IsResceduele();
int 	Rescheduele();
int 	IsioInturupt();
int 	ReadFirst(FILE *fp, int *nSeed, char *cSceduel, int *nTimeQuantum);
int 	LoadQueue();
int 	ReadNew(FILE *fp, int *nPID, int *nArrivalTime, int *ExecutionDelay, int *nIOBlocking);

int main(int argc, char *argv[]) {
	if(argc != 2) {
		printf("You did not input a file as an argument.\n");  //Prints if no file was given.
	} else {
		strcpy(szSourceFile,argv[1]);
		//Check if file exists.
		if(FileExist(szSourceFile)) {
			//Load file into first queue.
			LoadQueue(szSourceFile);
			//If first queue is not empty.
			if(IsNotEmpty() == 1) {
				//Seed used for generating same results.
				srand(gSeed);
				switch(gSchedule) {
					case 'r':	//Round robin.
						Do_rr_scheduling();
						break;
					case 'f':	//FCFS.
						Do_f_scheduling();
						break;
					case 'p':	//Priority.
						Do_p_scheduling();
						break;
				}
			}
		} else {
			printf("could not open file : %s",szSourceFile);
		}
	}
	return 0;
}

/*
For simulation process only, we maintain two queues.
	1. First queue loads data from file.
	2. Second queue simulates timing of next arrival.
*/
int Do_f_scheduling() {
	int PID = 0;					//Process ID.
	int AT  = 0;					//Arrive time.
	int ET  = 0; 					//Original execute time.
	int ST  = 0;	 				//Process start time.
	int Idling = 0; 				//Flag indicating the last status 'Idling'.
	int nCurrentTime    	= 1;	//Time units spent; execution time + idle time (Note: interruptions are not considered).
	int nTotalProcessCounts = 0;	//Total processes.
	
	int totalwaitTime		= 0;	//Total wait time.
	int totalturnAroundTime = 0;	//Total turn around time.
	int totalExecutionTime  = 0;	//Total execution time.
	int waitTime		= 0;		//Wait time.
	int turnAroundTime	= 0;		//Turn around time.
	int status			= 0;		//Status of process execution; [-1]->Idling, [0]-> New process, [1]-> any PID execution.
	waitTime = 0;	//Init.
	printf("___________________________________________________________________\n");
	printf("PID\tAT\tET\tTET\tTO\tCT\tST\tWT\tTWT\n");
	printf("___________________________________________________________________\n");	
	CURRENT_PROCESS = QUEUESTART;
	while(!((IsQueueEnd() == QUEUE_END) && (ProcessStatus() == IDLING))) {
		//Find if a new task is found and put them into the process queue.
		if(IsFoundNewTask(nCurrentTime) == 1) {
			JoinProcessQueue();		//Simulating an interruption of a new task.
			nTotalProcessCounts++;	//Add one process ID.
		}
		//Simulate a process execution.
		status = Process(&PID, &AT, &ET, &ST, nCurrentTime);
		switch(status) {
			case -1:									//Idling.
				waitTime = 0;							//Since idling, anybody joining queue will have no need to wait.
				Idling = 1;
				break;
			case 0:				 						//New process.
				totalExecutionTime += ET;				//Total.
				turnAroundTime = nCurrentTime - ST;		//Periodd from submission (arrive time) to the end of process execution.
				waitTime = ST - AT;						//The time taken from arrival to the start of a process.
				if(Idling == 1) {						//In the case of idling.
					waitTime = 0;						//In the case that the last action was 'Idling', wait time is zero.
					Idling = 0;							//Reset flag.
				}
				totalturnAroundTime += turnAroundTime;
				totalwaitTime += waitTime;
				printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", PID, AT, ET, totalExecutionTime, turnAroundTime, nCurrentTime, ST, waitTime,totalwaitTime  );	//Display.
				break;
			case 1:		//Continue to execute current process.
				break;
		}
		nCurrentTime++;		//Add one time unit.
	}
	//Print totals.
	if(nTotalProcessCounts > 0) {
		printf("___________________________________________________________________\n");
		printf("Schedueling technique used is %s\n", gSchedule == 'f' ? "FCFS (non-preemptive)":gSchedule == 'r'? "Round robin (preemptive)":"Priority (preemptive)");
		printf("The seed is %d\n", gSeed);
		printf("The time quantum is %d\n", gTimeQuantum);
		printf("___________________________________________________________________\n");
		printf("Total Proceses executed:\t%10.3f\n", (float)nTotalProcessCounts);
		printf("Average wait time is:\t\t%10.3f\n",(float) totalwaitTime / nTotalProcessCounts);
		printf("Average turn around time is:\t%10.3f\n",(float) totalturnAroundTime / nTotalProcessCounts);
		printf("Average execution time is:\t%10.3f\n",(float) totalExecutionTime / nTotalProcessCounts);
		printf("___________________________________________________________________\n");
	}
	return 0;
}
/*
For simulation process only, we maintain two queues.
	1. First queue loads data from file.
	2. Second queue simulates timing of next arrival.
*/
int Do_rr_scheduling() {
	int PID = 0;			//Process ID.
	int AT  = 0;			//Arrive time.
	int ET  = 0;			//Orginal execute time.
	int ST  = 0;			//Process start time.

	int Idling = 0;			//Flag indicating the last staus 'Idling'.

	int nCurrentTime        = 1;	//Time units spent; execution time + Idletime (Note: inturuptions are not considered).
	int nTotalProcessCounts = 0;	//Total processes.

	int totalwaitTime       = 0;	//Total wait time.
	int totalturnAroundTime = 0;	//Total turn around time.
	int totalExecutionTime  = 0;	//Total execution time.

	int waitTime       = 0;		//Wait time.
	int turnAroundTime = 0;		//Turn around time.
	int status         = 0;		//Status of process exection; [-1]->Idling, [0]->New process, [1]-> any PID execution.

	waitTime = 0;	//Init.
	printf("___________________________________________________________________\n");
	printf("PID\tAT\tET\tTET\tTO\tCT\tST\tWT\tTWT\n");
	printf("___________________________________________________________________\n");

	CURRENT_PROCESS = QUEUESTART;
	while(!((IsQueueEnd() == QUEUE_END) && (ProcessStatus() == IDLING))) {
		//Find if a new task is found and put them into the process queue.
		if(IsFoundNewTask(nCurrentTime) == 1) {
			JoinProcessQueue();		//Simulating an inturuption of a new task.
			nTotalProcessCounts++;		//Add one process ID.
		}

		//Simulate a process execution.
		status = Process(&PID, &AT, &ET, &ST, nCurrentTime);

		switch(status) {
			case -1:									//Idling.
				waitTime = 0;							//Since idling, anybody joining queue will have no need to wait.
				Idling = 1;
				break;
			case 0:		 								//New process.
				totalExecutionTime += ET;				//Total.
				turnAroundTime = nCurrentTime - ST;		//Period from submission (arrive time) to the end of process execution.
				waitTime = ST - AT;						//The time taken from arrival to the start of a process.
				if(Idling == 1) {						//In the case of idling.
					waitTime = 0;						//In the case that the last action was 'Idling', wait time is zero.
					Idling = 0;							//Reset flag.
				}

				totalturnAroundTime += turnAroundTime;
				totalwaitTime += waitTime;

				printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", PID, AT, ET, totalExecutionTime, turnAroundTime, nCurrentTime, ST, waitTime,totalwaitTime  );	//Display.

				break;
			case  1:	//Continue to execute current process.
				break;
		}
		nCurrentTime++;		//Add one time unit.
	}
	//Print totals.
	if(nTotalProcessCounts > 0) {
		printf("___________________________________________________________________\n");
		printf("Schedueling technique used is %s\n",gSchedule == 'f' ? "FCFS(Non-preemptive)":gSchedule == 'r'? "Round Robin (preemptive)":"Priority (preemptive)");
		printf("The seed is %d\n",gSeed);
		printf("The time quantum is %d\n",gTimeQuantum);
		printf("___________________________________________________________________\n");
		printf("Total Proceses executed:\t%10.3f\n", (float)nTotalProcessCounts);
		printf("Average wait time is:\t\t%10.3f\n",(float) totalwaitTime / nTotalProcessCounts);
		printf("Average turn around time is:\t%10.3f\n",(float) totalturnAroundTime / nTotalProcessCounts);
		printf("Average execution time is:\t%10.3f\n",(float) totalExecutionTime / nTotalProcessCounts);
		printf("___________________________________________________________________\n");
	}
	return 0;
}



/*
For simulation process only, we maintain two queues.
	1. First queue loads data from file.
	2. Second queue simulates timing of next arrival.
*/
int Do_p_scheduling() {
	int PID = 0;			//Process ID.
	int AT  = 0;			//Arrive time.
	int ET  = 0;			//Orginal execute time.
	int ST  = 0;			//Process start time.

	int Idling = 0;			//Flag indicating the last staus 'Idling'.

	int nCurrentTime        = 1;	//Time units spent; execution time + Idletime (Note: inturuptions are not considered).
	int nTotalProcessCounts = 0;	//Total processes.

	int totalwaitTime       = 0;	//Total wait time.
	int totalturnAroundTime = 0;	//Total turn around time.
	int totalExecutionTime  = 0;	//Total execution time.

	int waitTime       = 0;		//Wait time.
	int turnAroundTime = 0;		//Turn around time.
	int status         = 0;		//Status of process exection; [-1]->Idling, [0]->New process, [1]-> any PID execution.

	waitTime = 0;	//Init.
	printf("___________________________________________________________________\n");
	printf("PID\tAT\tET\tTET\tTO\tCT\tST\tWT\tTWT\n");
	printf("___________________________________________________________________\n");

	CURRENT_PROCESS = QUEUESTART;
	while(!((IsQueueEnd() == QUEUE_END) && (ProcessStatus() == IDLING))) {
		//Find if a new task is found and put them into the process queue.
		if(IsFoundNewTask(nCurrentTime) == 1) {
			JoinProcessQueue();		//Simulating an inturuption of a new task.
			nTotalProcessCounts++;		//Add one process ID.
		}

		//Simulate a process execution.
		status = Process(&PID,&AT,&ET,&ST,nCurrentTime);

		switch (status) {
			case -1:					//Idling.
				waitTime = 0;				//Since idling, anybody joining queue will have no need to wait.
				Idling = 1;
				break;
			case  0:					//New process.
				totalExecutionTime += ET;		//Total.
				turnAroundTime = nCurrentTime - ST;	//Period from submition (arrive time) to the end of process execution.
				waitTime = ST - AT;			//The time taken from arrival to the start of a process.
				if (Idling == 1) {			//In the case of idling.
					waitTime = 0;			//In the case that the last action was 'Idling', wait time is zero.
					Idling = 0;			//Reset flag.
				}

				totalturnAroundTime += turnAroundTime;
				totalwaitTime += waitTime;
		
				printf("%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\n", PID, AT, ET, totalExecutionTime, turnAroundTime, nCurrentTime, ST, waitTime,totalwaitTime  );	//Display.

				break;
			case  1:	//Continue to execute current process.
				break;
		}
		nCurrentTime++;		//Add one time unit.
	}
	//Print totals.
	if(nTotalProcessCounts > 0) {
		printf("___________________________________________________________________\n");
		printf("Schedueling technique used is %s\n",gSchedule == 'f' ? "FCFS(Non-preemptive)":gSchedule == 'r'? "Round Robin (preemptive)":"Priority (preemptive)");
		printf("The seed is %d\n",gSeed);
		printf("The time quantum is %d\n",gTimeQuantum);
		printf("___________________________________________________________________\n");
		printf("Total Proceses executed:\t%10.3f\n", (float)nTotalProcessCounts);
		printf("Average wait time is:\t\t%10.3f\n",(float) totalwaitTime / nTotalProcessCounts);
		printf("Average turn around time is:\t%10.3f\n",(float) totalturnAroundTime / nTotalProcessCounts);
		printf("Average execution time is:\t%10.3f\n",(float) totalExecutionTime / nTotalProcessCounts);
		printf("___________________________________________________________________\n");
	}
	return 0;
}




/*
Prerequisite: 	If any process from first queue is sceduled to arrive at this movement.
Result:		That process will join the process 'queue'.
*/
void JoinProcessQueue() {	
	//Declare init.
	int PID			= 0;
	int AT			= 0;
	int RT			= 0;
	int blockIO		= 0;
	double currentPriority	= 0;

	if(INTURUPT_CURRENT_PROCESS != NULL) {
		//Get values for a deep copy.
		PID 		= INTURUPT_CURRENT_PROCESS->processID;
		AT 		= INTURUPT_CURRENT_PROCESS->processArrivalTime;
		RT 		= INTURUPT_CURRENT_PROCESS->remainingRunTime;
	 	blockIO		= INTURUPT_CURRENT_PROCESS->blockIO;
	 	currentPriority	= INTURUPT_CURRENT_PROCESS->currentPriority;

		INTURUPT_CURRENT_PROCESS = INTURUPT_CURRENT_PROCESS->next;  //Move next.

		//Free used up node.
		free(INTURUPT_QUEUESTART);	//current queues are the same.
		INTURUPT_QUEUESTART = INTURUPT_CURRENT_PROCESS;

		//Initalize anyway.
		if(INTURUPT_CURRENT_PROCESS == NULL) {
			INTURUPT_QUEUEEND=NULL;
			INTURUPT_CURRENT_PROCESS = NULL;
			INTURUPT_QUEUESTART = NULL;
		}

		CreateProcess(PID, AT, RT, blockIO, currentPriority);		//Create a node in the process execution queue.
	}
}




int LastProcessArivedAt() {
	return QUEUEEND->processArrivalTime;
}

/*
Adds a new node to the 'process queue'.
*/
void CreateProcess(int nPID, int nArrivalTime, int nremainTime, int nblockIO, double currentPriority) {
	struct PBR *newPID = (struct PBR *)malloc(sizeof(struct PBR));
	if(newPID != NULL) {
		//Add node to empty queue.
		if(QUEUESTART == NULL) {
			QUEUEEND	= newPID;
			QUEUESTART	= newPID;
			CURRENT_PROCESS	= newPID;
		} else {
			//Add node to the end of queue.
			QUEUEEND->next  = newPID;
			QUEUEEND        = newPID;
		}
		newPID->next = NULL;
	} else {
		printf("Memory allocation error. \n");
		exit(0);
	}
	
	//Assign values.
	newPID->processID          = nPID;
	newPID->processArrivalTime = nArrivalTime;
	newPID->remainingRunTime   = nremainTime;
	newPID->executionTime      = nremainTime;
	newPID->startTime          = 0;
	newPID->blockIO            = nblockIO;
	newPID->currentPriority    = currentPriority;
}

/*
Simulate process execution for all three types.
*/
int Process(int *PID, int *AT, int *RT,int *ST, int nCurrentTime) {
	int status = 0;
	if(CURRENT_PROCESS != NULL) {
		//Get current values.
		*PID 	= CURRENT_PROCESS->processID;
		*AT 	= CURRENT_PROCESS->processArrivalTime;
		*RT 	= CURRENT_PROCESS->executionTime;
		//Record start time of the first of exection.
		if(CURRENT_PROCESS->startTime == 0 && CURRENT_PROCESS->processArrivalTime != 0)
			CURRENT_PROCESS->startTime = nCurrentTime;
		*ST = CURRENT_PROCESS->startTime;
		CURRENT_PROCESS->remainingRunTime--;	//One execution time unit reduced.
		status = Rescheduele();		//Reschedule if necessary.
		switch(status) {
			case 0:		//New PID; the old one has completed processing.
				return 0;
			case 1:		//Same PID; continue processing.
				return 1;
			case 2:		//New PID; the old one has been inturupted.
				return 1;
		}		
		return 1;//current PID.
	}
	return -1;	//"Hang". No process found idling.
}

int IsResceduele() {
	switch(gSchedule) {
		case 'f':
			if(CURRENT_PROCESS->remainingRunTime == 0) {	//Free memory up.
				return 1;
			}
			break;
		case 'p':
			if(((CURRENT_PROCESS->executionTime - CURRENT_PROCESS->remainingRunTime) % gTimeQuantum == 0)) {	//Equal time slice checked.
				return 1;
			}
			break;
		case 'r':
			if(((CURRENT_PROCESS->executionTime - CURRENT_PROCESS->remainingRunTime) % gTimeQuantum == 0)) {	//Equal time slice checked.
				return 1;
			}
			break;
	}
	return 0;
}

int Rescheduele() {
	int ReturnVal    	= 1;
	struct PBR *temp 	= NULL;
	struct PBR *prev 	= NULL;
	struct PBR *high 	= NULL;
	struct PBR *garbage 	= NULL;
	switch(gSchedule) {
		case 'f':
			//Current is next in queue.
			if(CURRENT_PROCESS->remainingRunTime == 0) {
				temp = CURRENT_PROCESS;
				
				CURRENT_PROCESS = CURRENT_PROCESS->next;	//Make current node piont to the next node.

				free(temp);

				QUEUESTART = CURRENT_PROCESS;	//Make queue start point to the next node.
				
				ReturnVal = 0;
			}
			break;
		case 'p':
		case 'r':
			prev = CURRENT_PROCESS;
			//Clean up used up memory.
			if(CURRENT_PROCESS->remainingRunTime == 0) {
				temp = QUEUESTART;
				prev = QUEUESTART;
				while(temp != NULL && temp != CURRENT_PROCESS) {
					prev = temp;
					temp = temp->next;
				}
				if(QUEUESTART == CURRENT_PROCESS) {
					QUEUESTART = CURRENT_PROCESS->next;
				}
				if( QUEUEEND == CURRENT_PROCESS) {
					QUEUEEND = prev;
				}
				if(temp == CURRENT_PROCESS) {
					garbage 	= CURRENT_PROCESS;
					prev->next 	= CURRENT_PROCESS->next;
					free(garbage);
					CURRENT_PROCESS = NULL;
				}

				ReturnVal = 0;
			}
			if(IsResceduele() == 1 || ReturnVal == 0) {
				if(gSchedule == 'p') {
					//To get high priority, process as current.
					temp = QUEUESTART;
					high = temp;
					while(temp != NULL) {
						if(temp->currentPriority > high->currentPriority) {
							high =temp;
						}
						temp = temp->next;	
					}
					//printf("PID[%d] ---> Priority %f\n",high->processID,high->currentPriority ); @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@2
					CURRENT_PROCESS = high;
				} else {
					if(prev->next == NULL) {
						CURRENT_PROCESS = QUEUESTART;
					} else {
						CURRENT_PROCESS = prev->next;// move to next Round robin
					}
				}
				if(ReturnVal == 0) {
					//One process has completed and the new process priority recalculated for the first time.
					CURRENT_PROCESS->currentPriority = ((double)1/((double)CURRENT_PROCESS->executionTime/gTimeQuantum));
					return 0;	//Next PID.
				} else {
					//Process not executed to the end but if round robin or priority, rescheduling and priority recalculated for old process just termianted.
					if(prev->currentPriority > 0) {
						prev->currentPriority  = 1/(double)(prev->currentPriority)/((double)((double)prev->executionTime / (double)prev->currentPriority));
					}
					ReturnVal = 2;	
				}	
			}
			break;
	}
	//If current process is null then the queue is empty.
	if(CURRENT_PROCESS == NULL) {
		QUEUEEND 	= NULL;
		CURRENT_PROCESS = NULL;
		QUEUESTART 	= NULL;
	}
	return ReturnVal;
}

/*
Simulate IO inturuption.
*/
int IsioInturupt() {
	if(CURRENT_PROCESS != NULL) {
		int result = 0;
		result = rand() % 100;
		if(result < CURRENT_PROCESS->blockIO) {		//Free memory.
			return 1; 	//Reschedule.
		}
		return 0;	//Keep running.	
	}
	return 0;	//Keep running.
}

/*
Process status.
*/
int ProcessStatus() {
	if(CURRENT_PROCESS != NULL) {
		return 1; //Found process id.
	} else {
		return -1;	//Hang up; there are no new process ids currently.
	}
}

/*
1) Read line by line.
2) Convert the line.
3) And then into a queue
*/
int LoadQueue() {
	FILE *fp  	    = NULL;
	int nPID	    = 0;
	int nArrivalTime    = 0;
	int nExecutionDelay = 0;
	int nIOBlocking     = 0;
	int ReadOk	    = 0;
	int nSeed;
	char cSceduel;
	int nTimeQuantum;

	fp = fopen(szSourceFile , "r");		//Open the input file.
	if(fp == NULL) {
		printf("Was unable to open input file.\n");
		return 0;	//Empty.
	}
	//Read one line.
	if((ReadOk = ReadFirst(fp,&gSeed, &gSchedule,&gTimeQuantum)) == 1) {
		ReadOk = ReadNew(fp,&nPID, &nArrivalTime, &nExecutionDelay, &nIOBlocking);
	}
	while(ReadOk == 1) {
		CreateInturupt(nPID, nArrivalTime, nExecutionDelay, nIOBlocking);		//Put it into a queue.
		ReadOk = ReadNew(fp,&nPID, &nArrivalTime,&nExecutionDelay,&nIOBlocking);	//Read the next line from file.
	}
	return IsNotEmpty();
}

/*
Convert the lines to PIDs, arrivalTimes, and executionTimes.
*/
int ReadFirst(FILE *fp, int *nSeed, char *cSceduel, int *nTimeQuantum) {
	char line[3][128];
	int count = 0;
	int row   = 0;
	int temp  = 0;
	line[row][count] = fgetc(fp);
	*nSeed		 = -1;
	*cSceduel	 = '\0';
	count++;
	*nTimeQuantum	 = -1;
	while((ReadOk(fp) == 1) && !((line[row][count] == 10) || (line[row][count] == 13)) && (count <255)) {
		line[row][count] = fgetc(fp);
		if(line[row][count] ==' ' || line[row][count] == '\t' || line[row][count] == '\n') {
			line[row][count] = '\0';
			if(row < 2) {
				row++;
			} else {
				break;
			}	
			temp = fgetc(fp);
			count = 0;
		}
		while ((ReadOk(fp) == 1) && (temp ==' ' || temp == '\t' || temp == '\n')) {
			temp = fgetc(fp);
		}
		if(temp != 0) {
			line[row][count] = temp;	
			temp  = 0;
		}
		count++;
	}
	line[row][count] = '\0';
	sscanf(line[0],"%d",nSeed);
	cSceduel[0] = line[1][0];
	sscanf(line[2],"%d", nTimeQuantum);
	if(*nSeed == -1 || cSceduel[0] == '\0' || *nTimeQuantum == -1) {
		return 0;	//Error reading.
	}
	return 1;
}

/*
Convert the lines to PIDs, arrivalTimes, and executionTimes.
*/
int ReadNew(FILE *fp,int *nPID, int *nArrivalTime, int *nExecutionTime, int *nIOBlocking) {
	char buffer[1024];
	ReadLine(fp,buffer);	//Read a line.
	if(ReadOk(fp)) {
		//Put it into the queue.
		*nPID 			= -1;
		*nArrivalTime 		= -1;
		*nExecutionTime 	= -1;
		*nIOBlocking		= -1;
		sscanf(buffer,"%d %d %d %d",nPID,nArrivalTime,nExecutionTime,nIOBlocking);
		if(*nPID == -1 || *nArrivalTime == -1 || *nExecutionTime == -1 || *nIOBlocking ==-1) {
			return 0;
		}
		return 1;
	}
	return 0;	//Error reading.
}

/*
Read input file one line at a time.
*/
int ReadLine(FILE *fp, char *line) {
	int count = 0;
	line[count] = fgetc(fp);
	while((ReadOk(fp) == 1) && !((line[count] == 10) || (line[count] == 13)) && (count <255)) {
		count++;
		line[count] = fgetc(fp);
	}
	count++;
	line[count] = '\0';
	return 0;
}

/*
Add one element to end of the queue.
*/
void CreateInturupt(int nPID, int nArrivalTime, int nExecutionDelay, int nIOBlocking) {
	//Allocate for one element/node.
	struct PBR *newPID = (struct PBR *)malloc(sizeof(struct PBR));
	if(newPID != NULL) {
		//Queue is empty.
		if(INTURUPT_QUEUESTART == NULL) {
			INTURUPT_QUEUEEND		= newPID;
			INTURUPT_QUEUESTART		= newPID;
			INTURUPT_CURRENT_PROCESS	= newPID;
		} else {
			//Join the end of the queue.
			INTURUPT_QUEUEEND->next  = newPID;
			INTURUPT_QUEUEEND        = newPID;
		}
		//Assign null to the field 'next', of new node.
		newPID->next = NULL;
	} else {
		printf("Memory allocation error. \n");
		exit(0);
	}
	//Assign values to the fields in node created.
	newPID->processID 		= nPID;
	newPID->processArrivalTime 	= nArrivalTime;
	newPID->remainingRunTime 	= nExecutionDelay;
	newPID->blockIO 		= nIOBlocking;
	newPID->currentPriority 	= gTimeQuantum;
	newPID->startTime 		= 0;
}

/*
Check if there is an inturuption/process at a given time.
*/
int IsFoundNewTask(int now) {
	if(INTURUPT_CURRENT_PROCESS != NULL) {
		if(INTURUPT_CURRENT_PROCESS->processArrivalTime == now) {
			return 1; 	//Found.
		}
	}
	return 0;	//Not found.
}

/*
check if queue is empty.
*/
int IsNotEmpty() {
	if(INTURUPT_CURRENT_PROCESS != NULL) {
		return 1;	//Queue is not empty.
	} else {
		return 0;	//Queue is empty.
	}
}

/*
check if queue is empty.
*/
int IsQueueEnd() {
	if(INTURUPT_QUEUEEND == NULL) {
		return 1;	//Queue end reached.
	} else {
		return 0;
	}
}

/*
Check if the file exists.
*/
int FileExist(char *FileName) {
	FILE *fp = fopen( FileName, "r" );
	int x = ReadOk(fp);
	fclose(fp);
	return x;
}

/*
Check for an input/output error on the file operations performed.
*/
int ReadOk(FILE *fp) {
	if(ferror(fp)) {
		clearerr(fp);
		return 0;
	} else {
		if(feof(fp)) {
			return 0;
		}
	}
	return 1;
}



Is This A Good Question/Topic? 0
  • +

Replies To: How to run C programs in Linux terminal

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: How to run C programs in Linux terminal

Posted 11 April 2011 - 01:20 PM

So you have compiled the code.

Then you should be able to just type in the programs compiled name.
If that doesn't run then try ./<program_name>.
Was This Post Helpful? 1
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: How to run C programs in Linux terminal

Posted 11 April 2011 - 01:41 PM

Put your code in .c file, then I expect you have gcc in your computer installed already. Then

$ gcc fileName.c
will create an executable called a.out which you can run with:
$ ./a.out

Also read this and this links
Was This Post Helpful? 1
  • +
  • -

#4 unbound  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 20
  • Joined: 01-March 11

Re: How to run C programs in Linux terminal

Posted 11 April 2011 - 01:49 PM

The way I run mine on linux is,

gcc -g filename.c -o filename
./filename

Then if you want to pass any args to your program you can do this: ./filename arg1 arg2
Was This Post Helpful? 2
  • +
  • -

#5 mattlyons  Icon User is offline

  • D.I.C Regular

Reputation: 6
  • View blog
  • Posts: 301
  • Joined: 10-September 09

Re: How to run C programs in Linux terminal

Posted 11 April 2011 - 03:19 PM

Thanks all.
Was This Post Helpful? 0
  • +
  • -

#6 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 61
  • View blog
  • Posts: 232
  • Joined: 19-September 11

Re: How to run C programs in Linux terminal

Posted 20 October 2011 - 10:09 AM

View Postunbound, on 11 April 2011 - 01:49 PM, said:

The way I run mine on linux is,

gcc -g filename.c -o filename
./filename

Then if you want to pass any args to your program you can do this: ./filename arg1 arg2


Thanks for this, I found it very helpful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1