c - segmentation fault after waking a process -
hey i'm writing simple shell on getting sigstsp pause process , continue on getting more commands. saves suspended process in job list. when try waking suspended process segmentation fault, after (ex)suspended process deals sigcont signal.
this signalhandler
void signalhandle(int sig) { int i,pid,id; sigset_t mask_set; sigset_t old_set; signal(sigtstp,signalhandle); sigfillset(&mask_set); sigprocmask(sig_setmask,&mask_set,&old_set); //handle sigtstp **if (sig == sigtstp)**{ gpid=getpid(); pid=fork(); if (pid==-1){ perror("(ctrl-z)(signal error)(fork)"); } else if (pid==0){ susp_bg_pid=gpid; i=0; while (getpid(jobslist,i)!=-1){ i=i+1; } id=getid(&jobslist,gpid); if (id==-1){ insertelem(&jobslist, l_fg_cmd, i, gpid, 1); } else{ delpid(&jobslist,gpid); insertelem(&jobslist, l_fg_cmd, id, gpid, 1); } gpid=getpid(); } else{ susp = 1; pause(); } } **if (sig == sigcont){** printf("process running again\n"); } }
}
and how try wake process
else if (!strcmp(cmd, "bg")) { int pid , new_pid=0; int id=0; if (num_arg > 1) { illegal_cmd = 1; } else{ if (num_arg==1) { id = atoi(args[1]); new_pid= getpid(*pjobslist,id); if (new_pid!=-1){ pelem=*pjobslist; if (pelem->varvalue!=null){ while (pelem!=null){ if (pelem->pid==new_pid){ printf("%s \n", pelem->varvalue); } pelem=pelem->pnext; } } **kill(new_pid,sigcont);** printf("now finished\n"); } else{ printf("smash error\n"); } } else{ } } }
my console output
smash > sleep 30
^zsmash > bg 0
sleep 30
now finished
smash > process running again
segmentation fault
press [enter] close terminal ...
int insertelem(list_element** plist, char* value, int id, int pid, int susp){ list_element *list; list_element *temp; list = *plist; if (value == null) return -1; if (list == null) { list = (list_element*)malloc(sizeof(list_element)); if (list == null) exit (-1); list->varvalue=(char*)malloc(sizeof(char)*(strlen(value)+1)); if (list->varvalue == null) exit (-1); strcpy(list->varvalue, value); list->varname = null; // varname doesn't mean job list->id = id; list->pid = pid; list->suspended = susp; list->pnext = null; *plist = list; return 0; } else { if (list->id == id) return insertelem(&list->pnext, value, id+1, pid, susp); else { temp = (list_element*)malloc(sizeof(list_element)); if (temp == null) exit (-1); temp->varvalue =(char*) malloc(sizeof(char)*(strlen(list->varvalue)+1)); if (temp->varvalue == null) exit (-1); strcpy(temp->varvalue, list->varvalue); temp->varname = null; temp->pid = list->pid; temp->id = list->id; temp->suspended = list->suspended; temp->pnext = list->pnext; free(list->varvalue); list->varvalue=(char*)malloc(sizeof(char)*(strlen(value)+1)); if (list->varvalue == null) exit (-1); strcpy(list->varvalue, value); list->id = id; list->pid = pid; list->suspended = susp; list->pnext = temp; return 0; } }
Comments
Post a Comment