Question

I have a homework to write First Come First Serve and Round Robin simulaion and compare them. I started of creating a linked list for making an event list. I have made insert and print_list functions, but couldn't make it work. My friend told me to use Doubly Linked List so I reacreated the linked list and am trying to make insert function work. I fixed most of the problems, but I can't find what's wrong with my insert function now. I get segmentation fault on my While loop statement, when I compare if inserting pointer data and the current pointer data. I have looked hrought my C book, which has the similar code as me and also through the Doubly Linked List Segmentation Fault -C questions here, but still couldn't get my error.

this is My Output

-bash-3.2$ ./main 3 100
please enter the process-id, arrival-time and cpu-time.
1 2 3 4
something
something
DONE1
DONE2
DONE3
DONE4
Segmentation fault

as the test4 (DONE4) is printed and the next test is not, it means that I have an error in this part

while((currentptr->arrivaltime < newpointer->arrivaltime) && currentptr != NULL)

can you please help me? What is wrong with my code?

struct event {
   struct event *next;
   int processid;
   int arrivaltime;
   int cputime;
};
typedef struct event Event;
typedef Event *eventPtr;

Event create_node(int processid, int arrivaltime, int cputime);
void add_event(Event *newpointer, eventPtr *eventlist);
void print_node(Event node);
void print_eventlist(Event *eventlist);
Event get_nextevent(Event *eventlist);

int main(int argc, char* argv[])
{
int sourcenum,a,b,c,i;
Event tempevent;
eventPtr eventlist = NULL;
print_eventlist(&(*eventlist));
char* sources,timeSlice;
if(argc != 3)
printf("Proper usage is: main.c sources time-slice\n");
else
{
    sourcenum = atoi(argv[1]);
    timeSlice = atoi(argv[2]);
    for(i = 0; i < sourcenum ; i++)
    {
          //print_eventlist(&(*eventlist));
          printf("please enter the process-id, arrival-time and cpu-time.\n");
          scanf("%d %d %d",&a, &b, &c);
          printf("something\n");
          tempevent = create_node(a,b,c);
          print
          printf("something\n");
          add_event(&tempevent, &eventlist);
    }

}
return 0;
}


void add_event(Event *newpointer, eventPtr *eventlist)     //insert
{
 printf("DONE1\n");
 eventPtr currentptr, prevptr;
 printf("DONE2\n");
 prevptr = NULL;
 printf("DONE3\n");
 currentptr = *eventlist;
 printf("DONE4\n");
 while((currentptr->arrivaltime < newpointer->arrivaltime) && currentptr != NULL)                  /*on every loop previous and current pointer gets upgraded*/
 {
         printf("DONEW\n");
         prevptr = currentptr;
         currentptr = currentptr->next;   
 }
 printf("DONEW\n");
 if(prevptr != NULL)
 {
     printf("DONEW\n");
     prevptr->next = newpointer;
     newpointer->next = currentptr;
 }
 else
 {
     printf("DONEW\n");
     newpointer->next = *eventlist;                      //?????
     *eventlist = newpointer;
 }
 printf("DONE\n");
}
Was it helpful?

Solution

You state that the problem is most likely in

while((currentptr->arrivaltime < newpointer->arrivaltime) && currentptr != NULL)

The fact that you want to test for currentptr!=NULL implies that it might be NULL. But if it is then the earlier part of your condition will fail. Test for NULL first, THEN make the second half of the comparison...:

while(currentptr != NULL && (currentptr->arrivaltime < newpointer->arrivaltime))

OTHER TIPS

eventPtr eventlist = NULL;

add_event(&tempevent, &eventlist);
{
    currentptr = *eventlist;  //currentptr == NULL at this point
    ...............
    while(currentptr->arrivaltime ....)  // Segmentation
}

In your code, the "eventlist = NULL" passed into "add_event". This is the reason of the Segmentation.

I think you should do "malloc()" to get a entry for "eventlist" before use it in "add_event".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top