ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a wierd situation. When I add a new var to the main(){} of my program I get a seg fault error. So if I was to add this to the code below: int myNewVar = 0;
I would get a seg fault, otherwise program is fine. Any help would be awesome I have been fighting this for some time now. Thanks
Code:
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
struct Process{
int pid;
int arrival;
int burst;
};
float trn_avg;
int FCFS(queue<Process> q,float *ptr_wait, float *ptr_thr_put,int sim_time);
void sort_queue(queue<Process> q);
int SJF(queue<Process> q,float *ptr_wait, float *ptr_thr_put,int sim_time);
int find_lrg(queue<Process> q);
int main(int argc, char **argv){
int p_remain = 0;
float *ptr_wait;
*ptr_wait = 0;
float thr_put = 0;
float *ptr_thr_put;
*ptr_thr_put = 0;
bool first = true;
int sim_time;
//argument check
if(argc == 3 || argc == 4){
sim_time = strtol(argv[1],NULL,10);
string algorithm = argv[2];
if(argc == 4){
int time_slice = strtol(argv[3],NULL,10);
}
}else{//incorrect amount of arguments
fprintf(stderr,"usage:./hw4 sim_time algorithm [time_slice]\n");
exit(1);
}
queue<Process> proc_que;//queue to hold processes
Process proc;//struct to hold process data
int count = 1;//keep track of data for incoming process line
int i_proc;//input process
while( cin >> i_proc){
switch (count) {
case 1:
proc.pid = i_proc;//PID
count++;
break;
case 2:
proc.arrival = i_proc;//arrival time
count++;
break;
case 3:
proc.burst = i_proc;//CPU burst time
proc_que.push(proc);//struct data loaded,push to queue
count = 1;//process struct loaded,count = 1 to start new struct
break;
default:
cout << "Nothing entered into struct" << endl;
break;
}
}
p_remain = FCFS(proc_que,ptr_wait,ptr_thr_put,sim_time);//First come first serve
cout << endl << "Throughput = " << *ptr_thr_put << endl;
cout << "Avg wait time = " << *ptr_wait << endl;
cout << "Avg turnaround time = " << trn_avg << endl;
cout << "Remaining tasks = " << p_remain << endl;
//find_lrg(proc_que);
return 0;
}
//function for first come first serve algorithim.
int FCFS(queue<Process> q,float *ptr_wait, float *ptr_thr_put,int sim_time){
Process p;
float cmtv_wait = 0;
float cmtv_trn_time = 0;
float old_burst = 0;
float st_end = 0;
int q_size = q.size();
float time = 0;
float wait_time = 0;
float turn_time = 0;
for(int i = 0; i < q_size; i++){
p = q.front();
q.pop();
//ensure that there is enough time left
if(time + p.burst <= sim_time){
//schedule/terminate printout
cout << " " << *ptr_wait << ": scheduling PID " << p.pid << ", CPU = " << p.burst << endl;
cout << " " << *ptr_wait + p.burst << ": PID " << p.pid << ", terminated" << endl;
(*ptr_thr_put)++;
time = time + p.burst;
*ptr_wait = time;
//avg wait time calc
wait_time += time;
}else{
break;
}
}
//avg turn around time calc
turn_time = wait_time;
//do not need last time
wait_time -= time;
//clac avg turnaround time for final stats
trn_avg = turn_time/(*ptr_thr_put);
//calc avg wait time for final stats
*ptr_wait = wait_time/((*ptr_thr_put));
return q_size - (*ptr_thr_put);
}
int SJF(queue<Process> q,float *ptr_wait, float *ptr_thr_put,int sim_time){
Process p;
float cmtv_wait = 0;
float cmtv_trn_time = 0;
float old_burst = 0;
float st_end = 0;
int q_size = q.size();
float time = 0;
float wait_time = 0;
float turn_time = 0;
for(int i = 0; i < q_size; i++){
p = q.front();
q.pop();
//ensure that there is enough time left
if(time + p.burst <= sim_time){
//schedule/terminate printout
cout << " " << *ptr_wait << ": scheduling PID " << p.pid << ", CPU = " << p.burst << endl;
cout << " " << *ptr_wait + p.burst << ": PID " << p.pid << ", terminated" << endl;
(*ptr_thr_put)++;
time = time + p.burst;
*ptr_wait = time;
//avg wait time calc
wait_time += time;
}else{
break;
}
}
//avg turn around time calc
turn_time = wait_time;
//do not need last time
wait_time -= time;
//clac avg turnaround time for final stats
trn_avg = turn_time/(*ptr_thr_put);
//calc avg wait time for final stats
*ptr_wait = wait_time/((*ptr_thr_put));
return q_size - (*ptr_thr_put);
}
int find_lrg(queue<Process> q){
Process prc;
int brst = 0;
int temp = 0;
int q_size = q.size();
for(int i = 0; i < q_size; i++){
prc = q.front();
q.pop();
brst = prc.burst;
if(brst > temp){
temp = brst;
}
}
cout << "this is lrg " << temp;
return temp;
}
That is not enough of a correction. If you take that approach, you would need to correct all the uses of each of those two variables within main().
Correcting all those uses, so each variable is a float within main() but a float* everywhere else, is a bit more efficient than the alternative I'm about to suggest. But the following is less of a change and it keeps the usage consistent between main() and the rest of the code, which may make the program more maintainable.
This is a common source of confusion for beginners in C programming. You have a function (typically spec'ed by someone else) that must take an XYZ* as a parameter. So in the function that must call that, a beginner only declares a XYZ* and initializes only the value pointed to and then passes that. But unless you initialize the pointer itself, you don't have any value pointed to and you usually get a seg fault.
The efficient solution (in some more advanced cases the only good solution) is to use an XYZ (instead of an XYZ*) within the top level function and pass &name instead of name to any function that needs an XYZ*
The simplest solution, as I showed above, is to declare and initialize an XYZ and use its address to initialize the pointer.
The common solution is to use malloc. Sometime that is best, but it is more popular (as compared to the simpler solution) than it ought to be.
Those non-initialized variables have usually got a something like a random value. The memory area of those variables were earlier in use (probably). Adding another variable will cause only one thing, the variables will be stored in another part of the memory, therefore will get another "initial" value. So in your example in the first case the memory contained something usable, and in the second case the memory (where those variables were stored) contained invalid values.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.