MedVision ad

Semester 1 Chatter Thread (2010) (8 Viewers)

vanush

kdslkf
Joined
Oct 10, 2004
Messages
547
Location
Sydney, Australia
Gender
Male
HSC
2006
OMG
I finally finished it
Now to beautify

:monkey:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h> 
#include "pcb.h"
#include "res.h"

void execute(PcbPtr n);
void printRes(int * systemRes);

int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("Please specifiy a job list.\n");
  } else { 
    int dispatcherTimer = 0;
    PcbPtr runningProcess = NULL;
    pid_t prev;
    int status;
    pcbQueue jobQueue = (pcbQueue) initQueue((char *)argv[1]);
    PcbPtr jobHead = jobQueue->head;
		MabPtr pending;
		mabList memList = createMemList();
		MabPtr rt = memSplit(memList->head, 64, memList);
		//rt->locked = 1;
		
		int * systemRes = createRes();
		
		printf("Resources available : %d %d %d %d\n", systemRes[0], systemRes[1], systemRes[2], systemRes[3]);
    
    while (jobHead) {
      printf("%d %d %d %d (%d %d %d %d)\n", jobHead->arrivaltime, jobHead->priority, jobHead->remainingcputime, jobHead->mbytes, jobHead->res[0], jobHead->res[1], jobHead->res[2], jobHead->res[3]);
      jobHead = jobHead->next;
    }
		
		pcbQueue userJobQueue = (pcbQueue) createQueue();
			
    pcbQueue * feedbackQueue = (pcbQueue *) malloc(4 * sizeof(pcbQueue));
    feedbackQueue[0] = createQueue();
    feedbackQueue[1] = createQueue();
    feedbackQueue[2] = createQueue();
    feedbackQueue[3] = createQueue();	

		//pcbQueue freeList = createQueue();
		
		
    while (queueSize(feedbackQueue[0]) || queueSize(feedbackQueue[1]) || queueSize(feedbackQueue[2]) || queueSize(feedbackQueue[3]) ||
	  queueSize(jobQueue) || queueSize(userJobQueue) || runningProcess) 
		{		
			while (getNodeArrivalTime(jobQueue->head) <= dispatcherTimer) {		
				fprintf(stderr, "Dispatcher TImer is %d\n", dispatcherTimer);
				PcbPtr job = copyNode(deQueue(jobQueue));
				fprintf(stderr, "[init que to userjob queue] Getting job [%d] [%d] from the init queue\n", job->priority, job->mbytes);
				assert(job != NULL);					
				if (getNodePriority(job) == 0) {
					fprintf(stderr, "[init que to userjob queue] Adding priority job [%d] to real time queue\n", job->mbytes);
					job->mem = rt;
					enQueue(feedbackQueue[0], job);
					
				} else {
					fprintf(stderr, "[init que to userjob queue] Adding job [%d] to User Job Queue\n", job->mbytes);
					enQueue(userJobQueue, job);
				}
			}
			while (queueSize(userJobQueue) && ((pending = memChk(memList->head, userJobQueue->head->mbytes)) != NULL) && resChk(systemRes, userJobQueue->head->res)) {				
				fprintf(stderr, "Size of user job queue is: %d\n", queueSize(userJobQueue));
				PcbPtr userjob = deQueue(userJobQueue);
				assert(userjob != NULL);
				
				fprintf(stderr, "Getting job [%d] [%d] from the user job queue\n", userjob->priority, userjob->mbytes);
				
				MabPtr usermem = memAlloc(pending, userjob->mbytes, memList);
				assert(usermem != NULL);							
				
				userjob->mem = usermem;							
				
				resAlloc(systemRes, userjob->res);
				
				enQueue(feedbackQueue[getNodePriority(userjob)], userjob);
				fprintf(stderr, "Adding job [%d] [%d] to feedback quue %d \n", userjob->priority, userjob->mbytes, getNodePriority(userjob));			
			}
			if (runningProcess != NULL) 
			{					
				prev = getNodePid(runningProcess);

				runningProcess->remainingcputime--;
				fprintf(stderr,"Process CPU TIME : %d\n", runningProcess->remainingcputime);
				if (runningProcess->remainingcputime == 0) {
					kill(getNodePid(runningProcess), SIGINT);
					fprintf(stderr, "Killing [%d] [%d] job\n", runningProcess->priority, runningProcess->mbytes);
					memFree(runningProcess->mem, memList);
					if (runningProcess->priority != 0) {
						resFree(systemRes, runningProcess->res);
					}
					printMemory(memList);
					free(runningProcess);
					runningProcess = NULL;
					assert(!runningProcess);
				} else if ((getNodePriority(runningProcess) != 0) && 	(queueSize(feedbackQueue[0]) || queueSize(userJobQueue) || queueSize(feedbackQueue[1]) ||	queueSize(feedbackQueue[2]) ||  queueSize(feedbackQueue[3])))
				{
					kill(getNodePid(runningProcess), SIGTSTP);
					runningProcess->suspend = 1;
					if (getNodePriority(runningProcess) != 3) {
						runningProcess->priority++;
						enQueue(feedbackQueue[getNodePriority(runningProcess)], runningProcess);
						fprintf(stderr, "(lower) Suspending job [%d] [%d] on queue %d\n", runningProcess->priority, runningProcess->mbytes, runningProcess->priority);
					} else {
						enQueue(feedbackQueue[getNodePriority(runningProcess)], runningProcess);
						fprintf(stderr, "(own) Suspending job [%d] [%d] on queue %d\n", runningProcess->priority, runningProcess->mbytes, runningProcess->priority);
					}
					runningProcess = NULL;
					assert(runningProcess == NULL);
				}	
			}
			if (!runningProcess && ((queueSize(feedbackQueue[0]) || queueSize(feedbackQueue[1]) || queueSize(feedbackQueue[2]) || queueSize(feedbackQueue[3])))) {					
				waitpid(prev, &status, WUNTRACED);
				int doneDeq = 0;
				int i = 0;
				while (i < (4) && !doneDeq) {
					if (queueSize(feedbackQueue[i])) {
						runningProcess = deQueue(feedbackQueue[i]);
						fprintf(stderr, "This process is from Queue %d with priority %d and pid %d\n", i, runningProcess->priority, runningProcess->pid);
						assert(runningProcess->remainingcputime != NULL);
						assert(runningProcess != NULL);
						doneDeq = 1;
					}
					i++;
				}
				if (runningProcess->suspend == 1) {								
					kill(getNodePid(runningProcess), SIGCONT);
					runningProcess->suspend = 0;								
				} else {		
				
					execute(runningProcess);								
				}
			}
			sleep(1);
			fprintf(stdout, "========= DISPATCHER TIMER %d ==========\n", dispatcherTimer);
			dispatcherTimer++;			
		}
	}
  return 0;
}

void execute(PcbPtr n) {
  pid_t pid; 
  if ((pid = fork()) < 0) {
    printf("Error");
    exit(1);
  } 
  else if (pid == 0) {
    execvp(n->args[0], n->args);
  }
	fprintf(stderr, "Executing process %d with priority %d\n", pid, n->priority);
  n->pid = pid;
}

void printRes(int * systemRes) {
	fprintf(stderr, "[%d %d %d %d]\n", systemRes[0], systemRes[1], systemRes[2], systemRes[3]);
}
 

withoutaface

Premium Member
Joined
Jul 14, 2004
Messages
15,098
Gender
Male
HSC
2004
OMG
I finally finished it
Now to beautify

:monkey:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h> 
#include "pcb.h"
#include "res.h"

void execute(PcbPtr n);
void printRes(int * systemRes);

int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("Please specifiy a job list.\n");
  } else { 
    int dispatcherTimer = 0;
    PcbPtr runningProcess = NULL;
    pid_t prev;
    int status;
    pcbQueue jobQueue = (pcbQueue) initQueue((char *)argv[1]);
    PcbPtr jobHead = jobQueue->head;
		MabPtr pending;
		mabList memList = createMemList();
		MabPtr rt = memSplit(memList->head, 64, memList);
		//rt->locked = 1;
		
		int * systemRes = createRes();
		
		printf("Resources available : %d %d %d %d\n", systemRes[0], systemRes[1], systemRes[2], systemRes[3]);
    
    while (jobHead) {
      printf("%d %d %d %d (%d %d %d %d)\n", jobHead->arrivaltime, jobHead->priority, jobHead->remainingcputime, jobHead->mbytes, jobHead->res[0], jobHead->res[1], jobHead->res[2], jobHead->res[3]);
      jobHead = jobHead->next;
    }
		
		pcbQueue userJobQueue = (pcbQueue) createQueue();
			
    pcbQueue * feedbackQueue = (pcbQueue *) malloc(4 * sizeof(pcbQueue));
    feedbackQueue[0] = createQueue();
    feedbackQueue[1] = createQueue();
    feedbackQueue[2] = createQueue();
    feedbackQueue[3] = createQueue();	

		//pcbQueue freeList = createQueue();
		
		
    while (queueSize(feedbackQueue[0]) || queueSize(feedbackQueue[1]) || queueSize(feedbackQueue[2]) || queueSize(feedbackQueue[3]) ||
	  queueSize(jobQueue) || queueSize(userJobQueue) || runningProcess) 
		{		
			while (getNodeArrivalTime(jobQueue->head) <= dispatcherTimer) {		
				fprintf(stderr, "Dispatcher TImer is %d\n", dispatcherTimer);
				PcbPtr job = copyNode(deQueue(jobQueue));
				fprintf(stderr, "[init que to userjob queue] Getting job [%d] [%d] from the init queue\n", job->priority, job->mbytes);
				assert(job != NULL);					
				if (getNodePriority(job) == 0) {
					fprintf(stderr, "[init que to userjob queue] Adding priority job [%d] to real time queue\n", job->mbytes);
					job->mem = rt;
					enQueue(feedbackQueue[0], job);
					
				} else {
					fprintf(stderr, "[init que to userjob queue] Adding job [%d] to User Job Queue\n", job->mbytes);
					enQueue(userJobQueue, job);
				}
			}
			while (queueSize(userJobQueue) && ((pending = memChk(memList->head, userJobQueue->head->mbytes)) != NULL) && resChk(systemRes, userJobQueue->head->res)) {				
				fprintf(stderr, "Size of user job queue is: %d\n", queueSize(userJobQueue));
				PcbPtr userjob = deQueue(userJobQueue);
				assert(userjob != NULL);
				
				fprintf(stderr, "Getting job [%d] [%d] from the user job queue\n", userjob->priority, userjob->mbytes);
				
				MabPtr usermem = memAlloc(pending, userjob->mbytes, memList);
				assert(usermem != NULL);							
				
				userjob->mem = usermem;							
				
				resAlloc(systemRes, userjob->res);
				
				enQueue(feedbackQueue[getNodePriority(userjob)], userjob);
				fprintf(stderr, "Adding job [%d] [%d] to feedback quue %d \n", userjob->priority, userjob->mbytes, getNodePriority(userjob));			
			}
			if (runningProcess != NULL) 
			{					
				prev = getNodePid(runningProcess);

				runningProcess->remainingcputime--;
				fprintf(stderr,"Process CPU TIME : %d\n", runningProcess->remainingcputime);
				if (runningProcess->remainingcputime == 0) {
					kill(getNodePid(runningProcess), SIGINT);
					fprintf(stderr, "Killing [%d] [%d] job\n", runningProcess->priority, runningProcess->mbytes);
					memFree(runningProcess->mem, memList);
					if (runningProcess->priority != 0) {
						resFree(systemRes, runningProcess->res);
					}
					printMemory(memList);
					free(runningProcess);
					runningProcess = NULL;
					assert(!runningProcess);
				} else if ((getNodePriority(runningProcess) != 0) && 	(queueSize(feedbackQueue[0]) || queueSize(userJobQueue) || queueSize(feedbackQueue[1]) ||	queueSize(feedbackQueue[2]) ||  queueSize(feedbackQueue[3])))
				{
					kill(getNodePid(runningProcess), SIGTSTP);
					runningProcess->suspend = 1;
					if (getNodePriority(runningProcess) != 3) {
						runningProcess->priority++;
						enQueue(feedbackQueue[getNodePriority(runningProcess)], runningProcess);
						fprintf(stderr, "(lower) Suspending job [%d] [%d] on queue %d\n", runningProcess->priority, runningProcess->mbytes, runningProcess->priority);
					} else {
						enQueue(feedbackQueue[getNodePriority(runningProcess)], runningProcess);
						fprintf(stderr, "(own) Suspending job [%d] [%d] on queue %d\n", runningProcess->priority, runningProcess->mbytes, runningProcess->priority);
					}
					runningProcess = NULL;
					assert(runningProcess == NULL);
				}	
			}
			if (!runningProcess && ((queueSize(feedbackQueue[0]) || queueSize(feedbackQueue[1]) || queueSize(feedbackQueue[2]) || queueSize(feedbackQueue[3])))) {					
				waitpid(prev, &status, WUNTRACED);
				int doneDeq = 0;
				int i = 0;
				while (i < (4) && !doneDeq) {
					if (queueSize(feedbackQueue[i])) {
						runningProcess = deQueue(feedbackQueue[i]);
						fprintf(stderr, "This process is from Queue %d with priority %d and pid %d\n", i, runningProcess->priority, runningProcess->pid);
						assert(runningProcess->remainingcputime != NULL);
						assert(runningProcess != NULL);
						doneDeq = 1;
					}
					i++;
				}
				if (runningProcess->suspend == 1) {								
					kill(getNodePid(runningProcess), SIGCONT);
					runningProcess->suspend = 0;								
				} else {		
				
					execute(runningProcess);								
				}
			}
			sleep(1);
			fprintf(stdout, "========= DISPATCHER TIMER %d ==========\n", dispatcherTimer);
			dispatcherTimer++;			
		}
	}
  return 0;
}

void execute(PcbPtr n) {
  pid_t pid; 
  if ((pid = fork()) < 0) {
    printf("Error");
    exit(1);
  } 
  else if (pid == 0) {
    execvp(n->args[0], n->args);
  }
	fprintf(stderr, "Executing process %d with priority %d\n", pid, n->priority);
  n->pid = pid;
}

void printRes(int * systemRes) {
	fprintf(stderr, "[%d %d %d %d]\n", systemRes[0], systemRes[1], systemRes[2], systemRes[3]);
}
Massive ouch on having to do it in C. Ours was in Java and was some of the easiest shit ever.
 

postnatal

Banned
Joined
Jan 6, 2010
Messages
524
Gender
Undisclosed
HSC
N/A
redfern station please upgrade your quality of toilet paper to something thicker then 1 ply and that tears instead of rips. also please get some toilet seats for your toilets
 

postnatal

Banned
Joined
Jan 6, 2010
Messages
524
Gender
Undisclosed
HSC
N/A
do people no longer flush? i am not happy with the amount of shit i have seen floating in toilets lately
 

SeCKSiiMiNh

i'm a fireball in bed
Joined
Mar 6, 2008
Messages
2,618
Location
island of screaming orgasms
Gender
Male
HSC
2009
do people no longer flush? i am not happy with the amount of shit i have seen floating in toilets lately
ahaha this reminds me..

in one of the toilets in fisher, there was some writing on the wall which goes something like:
"i fucked my girlfriend where you're sitting"<--"eww you had sex in the presence of poo particles"<-- "we all have sex in the pressence of poo particles" <-- "rimmies?"
 

jet

Banned
Joined
Jan 4, 2007
Messages
3,148
Gender
Male
HSC
2009
So this week has only reaffirmed my hate for group work.
 

jayadore

She was a hurricane.
Joined
Apr 3, 2007
Messages
2,010
Gender
Female
HSC
2007
So this week has only reaffirmed my hate for uni.



:eek:
 

spence

Active Member
Joined
Mar 9, 2006
Messages
1,640
Location
Sydney
Gender
Male
HSC
2007
Today was the first bit of group work I've done so far at USyd. Its going well so far
 

postnatal

Banned
Joined
Jan 6, 2010
Messages
524
Gender
Undisclosed
HSC
N/A
so basically today i fucked over my group presentation. and they were pretty pissed at me so i tried to guilt trip them by saying i have fear of public speaking and am alcohol dependent, and so i was drunk before i got to class (partly true), sadly no one cried for me :(
guys i hope to god none of you here end up in a group with me
 

jet

Banned
Joined
Jan 4, 2007
Messages
3,148
Gender
Male
HSC
2009
I just realised that we don't actually get marks on the content of our presentation, so I'm gonna wing it lol.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 8)

Top