Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

segmenation fault in threading simulation

Status
Not open for further replies.

saphiroth

Programmer
Dec 15, 2004
34
US
Hi,
I wrote a program that simulates threading. I have 4 "threads" and all they do is write 1's and 0's into an array. I wrote a scheduler that creates a round robin run of the threads. Thread T1 writes 1's into the Buffer and if incouters a 0 then it blocks 'B' and if the goal of writing a certain amount of 1's is complete is sends a 'C' to the scheduler. T2 writes 0's and does that same thing. T3 is just like T1 and T4 is just like T2. If a thread finishes all of its writes it sends a 'D' to scheduler. For some reason I get a segmenation fault and can't find the reason. Please help.

Thank you.

#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <setjmp.h>

//initialize TurnArray
char TurnArray[4] = {'R','R','R','R'};
jmp_buf env0,env1,env2,env3;
int Buffer[50];
int Buffer2[100];
void Scheduler(char status,int ThreadID);

int main(){

void T1(void);
void T2(void);
void T3(void);
void T4(void);


Scheduler('*',-1);

}//main



void T1(){

int GoalArray[3] = {10,20,30};
int temp = (int)((rand()*3)%3);
int Goal = GoalArray[temp];
int Total, GoalCount, position, x, a, envReturn = 0;

for(a;a<sizeof(Buffer);a++){
Buffer[a] = 0;
}//for

while(Total <= 1000){
GoalCount = 0;
for(x;GoalCount<=Goal;x++){
if(x < 50){
if(Buffer[x] == 0){
Buffer[x] = 1;
GoalCount++;
}//if
else{
envReturn = setjmp(env0);
if(envReturn == 0){
Scheduler('B',0);
}//if
}//else
}
else{
x = 0;
if(Buffer[x] == 0){
Buffer[x] = 1;
GoalCount++;
}//if
else{
envReturn = setjmp(env0);
if(envReturn == 0){
Scheduler('B',0);
}//if
}//else
}
}//for

envReturn = setjmp(env0);
if(envReturn == 0){
Scheduler('C',0);
}//if
else{
Total = Total + Goal;
}//else
}//while

envReturn = setjmp(env0);
if(envReturn == 0){
printf("Thread 1 is done");
Scheduler('D',0);
}//if
else{
Scheduler('D',0);
}
}//T1

void T2(){

int GoalArray[3] = {10,20,30};
int temp = (int)((rand()*3)%3);
int Goal = GoalArray[temp];
int Total, GoalCount, position, x = 0;
int envReturn;

while(Total <= 1000){
GoalCount = 0;
for(x;GoalCount<=Goal;x++){
if(x < 50){
if(Buffer[x] == 1){
Buffer[x] = 0;
GoalCount++;
}//if
else{
envReturn = setjmp(env1);
if(envReturn == 0){
Scheduler('B',1);
}//if
}//else
}
else{
x = 0;
if(Buffer[x] == 1){
Buffer[x] = 0;
GoalCount++;
}//if
else{
envReturn = setjmp(env1);
if(envReturn == 0){
Scheduler('B',1);
}//if
}//else
}
}//for

envReturn = setjmp(env1);
if(envReturn == 0){
Scheduler('C',1);
}//if
else{
Total = Total + Goal;
}//else
}//while

envReturn = setjmp(env1);
if(envReturn == 0){
printf("Thread 2 is done");
Scheduler('D',1);
}//if
else{
Scheduler('D',1);
}
}//T2

void T3(){

int GoalArray[3] = {15,25,35};
int temp = (int)((rand()*3)%3);
int Goal = GoalArray[temp];
int Total, GoalCount, position, x = 0;
int envReturn;
int a = 0;

for(a;a<sizeof(Buffer2);a++){
Buffer2[a] = 0;
}//for

while(Total <= 1500){
GoalCount = 0;
for(x;GoalCount<=Goal;x++){
if(x < 50){
if(Buffer2[x] == 0){
Buffer2[x] = 1;
GoalCount++;
}//if
else{
envReturn = setjmp(env2);
if(envReturn == 0){
Scheduler('B',2);
}//if
}//else
}
else{
x = 0;
if(Buffer2[x] == 0){
Buffer2[x] = 1;
GoalCount++;
}//if
else{
envReturn = setjmp(env2);
if(envReturn == 0){
Scheduler('B',2);
}//if
}//else
}
}//for

envReturn = setjmp(env2);
if(envReturn == 0){
Scheduler('C',2);
}//if
else{
Total = Total + Goal;
}//else
}//while

envReturn = setjmp(env2);
if(envReturn == 0){
printf("Thread 3 is done");
Scheduler('D',2);
}//if
else{
Scheduler('D',2);
}
}//T3

void T4(){

int GoalArray[3] = {15,25,35};
int temp = (int)((rand()*3)%3);
int Goal = GoalArray[temp];
int Total, GoalCount, position, x = 0;
int envReturn;


while(Total <= 1500){
GoalCount = 0;
for(x;GoalCount<=Goal;x++){
if(x < 50){
if(Buffer2[x] == 1){
Buffer[x] = 0;
GoalCount++;
}//if
else{
envReturn = setjmp(env3);
if(envReturn == 0){
Scheduler('B',3);
}//if
}//else
}
else{
x = 0;
if(Buffer[x] == 1){
Buffer[x] = 0;
GoalCount++;
}//if
else{
envReturn = setjmp(env3);
if(envReturn == 0){
Scheduler('B',3);
}//if
}//else
}
}//for

envReturn = setjmp(env3);
if(envReturn == 0){
Scheduler('C',3);
}//if
else{
Total = Total + Goal;
}//else
}//while

envReturn = setjmp(env3);
if(envReturn == 0){
printf("Thread 4 is done");
Scheduler('D',3);
}//if
else{
Scheduler('D',3);
}
}//T4



void Scheduler(char status,int ThreadID){

if(status == '*' && ThreadID == -1){
T1();
}//if

if(TurnArray[0] == 'D' && TurnArray[1] == 'D' && TurnArray[2] == 'D' && TurnArray[3] == 'D'){
exit(0);
}

if(status == 'C'){
TurnArray[ThreadID] = 'R'; //if C
printf("State: %c, ID: %d, Condition: %c",status, ThreadID, 'R');
if(ThreadID == 0){
if(env1 == NULL){
T2();
}
else{
longjmp(env1, 2);
}
}//if
else if(ThreadID == 1){
if(env2 == NULL){
T3();
}
else{
longjmp(env2, 2);
}
}//if
else if(ThreadID == 2){
if(env3 == NULL){
T4();
}
else{
longjmp(env3, 2);
}
}//if
else if(ThreadID == 3){
if(env0 == NULL){
T1();
}
else{
longjmp(env0, 2);
}
}//if

}//if
else if(status == 'B'){
TurnArray[ThreadID] = 'W'; //if B
printf("State: %c, ID: %d, Condition: %c",status, ThreadID, 'W');
if(ThreadID == 0){
if(env1 == NULL){
T2();
}
else{
TurnArray[1] = 'R';
//printf("State: %c, ID: %d, Condition: %c",'B', 1, 'R');
longjmp(env1, 2);
}
}//if
else if(ThreadID == 1){
if(env2 == NULL){
T3();
}
else{
TurnArray[0] = 'R';
//printf("State: %c, ID: %d, Condition: %c",'B', 0, 'R');
longjmp(env2, 2);
}
}//if
else if(ThreadID == 2){
if(env3 == NULL){
T4();
}
else{
TurnArray[3] = 'R';
//printf("State: %c, ID: %d, Condition: %c",'B', 3, 'R');
longjmp(env3, 2);
}
}//if
else if(ThreadID == 3){
TurnArray[2] = 'R';
//printf("State: %c, ID: %d, Condition: %c",'B', 2, 'R');
longjmp(env0, 2);
}//if

}//else if
else{ //if D
TurnArray[ThreadID] = 'D';
printf("State: %c, ID: %d, Condition: %c",status, ThreadID, 'F');
if(ThreadID == 0){
longjmp(env1, 2);
}//if
else if(ThreadID == 1){
longjmp(env2, 2);
}//if
else if(ThreadID == 2){
longjmp(env3, 2);
}//if
else if(ThreadID == 3){
longjmp(env0, 2);
}//if
}//else

}//Scheduler



 
Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

--
 
In all your T1 you have
Code:
int Total, GoalCount, position, x, a, envReturn = 0;

for(a;a<sizeof(Buffer);a++){
Buffer[a] = 0;
}//for
while(Total <= 1000){
GoalCount = 0;
for(x;GoalCount<=Goal;x++){
if(x < 50){ 
if(Buffer[x] == 0){
Buffer[x] = 1;
GoalCount++;
}//if
a and x are not initialized. T2, T3 and T4 do not have the same bug.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top