Example #1
0
END_TEST

START_TEST(find_job_by_array_with_removed_record_test)
  {
  int result;
  all_jobs alljobs;

  struct job *test_job1 = job_alloc();
  strcpy(test_job1->ji_qs.ji_jobid, "test_job1");
  result = insert_job(&alljobs,test_job1);
  fail_unless(result == PBSE_NONE, "job insert fail1");

  struct job *test_job2 = job_alloc();
  strcpy(test_job2->ji_qs.ji_jobid, "test_job2");
  result = insert_job(&alljobs,test_job2);
  fail_unless(result == PBSE_NONE, "job insert fail2");
  
  struct job *test_job3 = job_alloc();
  strcpy(test_job3->ji_qs.ji_jobid, "test_job3");
  result = insert_job(&alljobs,test_job3);
  fail_unless(result == PBSE_NONE, "job insert fai3");

  struct job *test_job4 = job_alloc();
  strcpy(test_job4->ji_qs.ji_jobid, "test_job4");
  result = insert_job(&alljobs,test_job4);
  fail_unless(result == PBSE_NONE, "job insert fail4");

  struct job *test_job5 = job_alloc();
  strcpy(test_job5->ji_qs.ji_jobid, "test_job5");
  result = insert_job(&alljobs,test_job5);
  fail_unless(result == PBSE_NONE, "job insert fail5");
  }
Example #2
0
END_TEST

START_TEST(swap_jobs_test)
  {
  struct all_jobs alljobs;
  struct job *test_job;
  struct job *second_test_job;

  int result;
  initialize_all_jobs_array(&alljobs);

  test_job = job_alloc();
  second_test_job = job_alloc();

  result = swap_jobs(&alljobs,NULL,test_job);
  fail_unless(result != PBSE_NONE, "NULL first input job fail");

  result = insert_job_after(&alljobs,test_job,NULL);
  fail_unless(result != PBSE_NONE, "NULL second input job fail");

  insert_job(&alljobs, test_job);
  insert_job(&alljobs, second_test_job);
  result = swap_jobs(&alljobs, test_job,second_test_job);
  fail_unless(result == PBSE_NONE, "swap jobs fail");
  }
Example #3
0
END_TEST

START_TEST(swap_jobs_test)
  {
  all_jobs alljobs;
  struct job *test_job;
  struct job *second_test_job;

  int result;

  test_job = job_alloc();
  second_test_job = job_alloc();
  strcpy(test_job->ji_qs.ji_jobid,"test");
  strcpy(second_test_job->ji_qs.ji_jobid,"second_test");

  result = swap_jobs(&alljobs,NULL,test_job);
  fail_unless(result != PBSE_NONE, "NULL first input job fail");

  result = insert_job_after(&alljobs,test_job,NULL);
  fail_unless(result != PBSE_NONE, "NULL second input job fail");

  insert_job(&alljobs, test_job);
  insert_job(&alljobs, second_test_job);
  result = swap_jobs(&alljobs, test_job,second_test_job);
  fail_unless(result == PBSE_NONE, "swap jobs fail");
  }
Example #4
0
END_TEST

START_TEST(insert_job_after_test)
  {
  struct all_jobs alljobs;
  struct job *test_job;

  int result;
  initialize_all_jobs_array(&alljobs);

  test_job = job_alloc();

  result = insert_job_after(NULL,test_job,test_job);
  fail_unless(result != PBSE_NONE, "insert into null array fail");

  result = insert_job_after(&alljobs,NULL,test_job);
  fail_unless(result != PBSE_NONE, "NULL job after insert fail");

  result = insert_job_after(&alljobs,test_job,NULL);
  fail_unless(result != PBSE_NONE, "NULL job to insert fail");

  insert_job(&alljobs,test_job);
  result = insert_job_after(&alljobs,test_job,test_job);
  fail_unless(result == PBSE_NONE, "job insert fail");
  }
Example #5
0
int insert_into_recycler(

  job *pjob)

  {
  int              rc;
  char             log_buf[LOCAL_LOG_BUF_SIZE];

  pthread_mutex_lock(recycler.rc_mutex);

  if (pjob->ji_being_recycled == TRUE)
    {
    pthread_mutex_unlock(recycler.rc_mutex);
    return(PBSE_NONE);
    }
  
  if (LOGLEVEL >= 7)
    {
    snprintf(log_buf, sizeof(log_buf),
      "Adding job %s to the recycler", pjob->ji_qs.ji_jobid);
    log_event(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, __func__, log_buf);
    }

  sprintf(pjob->ji_qs.ji_jobid,"%016lx",(long)pjob);
  pjob->ji_being_recycled = TRUE;
    
  rc = insert_job(&recycler.rc_jobs, pjob);
  pjob->ji_momstat = time(NULL);
    
  pthread_mutex_unlock(recycler.rc_mutex);

  return(rc);
  } /* END insert_into_recycler() */
Example #6
0
int preempt(job_t* job)
{
	int cmp_n, cmp = 0;
	int i, x = -1;
	for ( i=0; i < cores.cnt; i++ )
	{
		cmp_n = sch_time(job, cores.jobs[i]);
		if ( cmp_n < cmp )
		{
			cmp = cmp_n;
			x = i;
		}//if
		else if ( cmp_n == cmp )
		{
			if ( x != -1 && cores.jobs[x]->arr_t < cores.jobs[i]->arr_t )
				x = i;
		}//else if
	}//for

	// insert job into core list.
	if ( x >= 0 )
	{
		delete_job(x,cores.jobs[x]->jid);
		insert_job(x,job);
	}//if

	return x;
}//preempt
Example #7
0
END_TEST

START_TEST(insert_job_after_test)
  {
  all_jobs alljobs;
  struct job *test_job;

  int result;

  test_job = job_alloc();
  strcpy(test_job->ji_qs.ji_jobid,"mylittlejob");

  result = insert_job_after(NULL,test_job,test_job);
  fail_unless(result != PBSE_NONE, "insert into null array fail");

  result = insert_job_after(&alljobs,(char *)NULL,test_job);
  fail_unless(result != PBSE_NONE, "NULL job after insert fail");

  result = insert_job_after(&alljobs,test_job,NULL);
  fail_unless(result != PBSE_NONE, "NULL job to insert fail");

  insert_job(&alljobs,test_job);
  result = insert_job_after(&alljobs,test_job,test_job);
  fail_unless(result == PBSE_NONE, "job insert fail");
  }
Example #8
0
void *remove_some_recycle_jobs(
    
  void *vp)

  {
  job    *pjob = NULL;
  time_t  time_now = time(NULL);

  pthread_mutex_lock(recycler.rc_mutex);

  for (int i = 0; i < JOBS_TO_REMOVE; i++)
    {
    pjob = pop_job_from_recycler(&recycler.rc_jobs);
    
    if (pjob == NULL)
      break;

    if (time_now - pjob->ji_momstat < MINIMUM_RECYCLE_TIME)
      {
      insert_job(&recycler.rc_jobs, pjob);
      unlock_ji_mutex(pjob, __func__, "1", LOGLEVEL);
      break;
      }

    if (LOGLEVEL >= 10)
      log_event(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, __func__, pjob->ji_qs.ji_jobid);

    unlock_ji_mutex(pjob, __func__, "1", LOGLEVEL);
    free_all_of_job(pjob);
    }

  pthread_mutex_unlock(recycler.rc_mutex);

  return(NULL);
  } /* END remove_some_recycle_jobs() */
Example #9
0
END_TEST

START_TEST(insert_job_test)
  {
  all_jobs alljobs;
  struct job *test_job;

  int result;

  test_job = job_alloc();

  result = insert_job(NULL, test_job);
  fail_unless(result != PBSE_NONE, "insert into null array fail");

  result = insert_job(&alljobs, NULL);
  fail_unless(result != PBSE_NONE, "NULL job insert fail");

  result = insert_job(&alljobs, test_job);
  fail_unless(result == PBSE_NONE, "job insert fail: %d",result);
  }
Example #10
0
/**
	When the scheme is set to RR, called when the quantum timer has expired
	on a core.
 
	If any job should be scheduled to run on the core free'd up by
	the quantum expiration, return the job_number of the job that should be
	scheduled to run on core core_id.
	@param core_id the zero-based index of the core where the quantum has expired.
	@param time the current time of the simulator. 
	@return job_number of the job that should be scheduled on core cord_id
	@return -1 if core should remain idle
 */
int scheduler_quantum_expired(int core_id, int time)
{
	// Increment Time
	inc_time(time);

	// Process quantumn rollover
	delete_job(core_id,cores.jobs[core_id]->jid);
	
	// Schedule new job
	job_t* p = priqueue_poll(jobs);
	if ( p != NULL )
	{
		insert_job(core_id,p);
		return p->jid;
	}//if
	return -1;
}//scheduler_quantum_expired
Example #11
0
int insert_into_recycler(

  job *pjob)

  {
  int              rc;
  pthread_mutex_t *tmp = pjob->ji_mutex;
  char             log_buf[LOCAL_LOG_BUF_SIZE];

  if (LOGLEVEL >= 7)
    {
    snprintf(log_buf, sizeof(log_buf),
      "Adding job %s to the recycler", pjob->ji_qs.ji_jobid);
    log_event(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, __func__, log_buf);
    }
  if (pjob->ji_being_recycled == TRUE)
    {
    return PBSE_NONE;
    }


  memset(pjob, 0, sizeof(job));
  pjob->ji_mutex = tmp;

  pthread_mutex_lock(recycler.rc_mutex);


  sprintf(pjob->ji_qs.ji_jobid,"%d",recycler.rc_next_id);
  pjob->ji_being_recycled = TRUE;

  recycler.rc_jobs.lock();
  if (recycler.rc_jobs.count() >= MAX_RECYCLE_JOBS)
    {
    enqueue_threadpool_request(remove_some_recycle_jobs,NULL);
    }
  recycler.rc_jobs.unlock();
    
  rc = insert_job(&recycler.rc_jobs, pjob);
    
  update_recycler_next_id();

  pthread_mutex_unlock(recycler.rc_mutex);

  return(rc);
  } /* END insert_into_recycler() */
Example #12
0
//**************************************************************************************
// function name: BgCmd
// Description: if command is in background, insert the command to jobs
// Parameters: command string, pointer to jobs
// Returns: 0- BG command -1- if not
//**************************************************************************************
int BgCmd(char* lineSize, pJob* job_list_head_pointer)
{
	int pID;
	char* command;
	char *args[MAX_ARG];
	char* delimiters = " \t\n";
	int i;

	if (lineSize[strlen(lineSize)-2] == '&')
	{
		lineSize[strlen(lineSize)-2] = '\0';
    	command = strtok(lineSize, delimiters);
		if (command == NULL)
			return -1;
		args[0] = command;
		for (i=1; i<MAX_ARG; i++)
		{
			args[i] = strtok(NULL, delimiters);
		}
		fflush(stdout);
		switch(pID=fork())
		{
			case -1:
				perror("error: process creation failed in BgCmd\n");
				return -1;
				break;
			case 0:
				// Child process
				setpgrp();
				fflush(stdout);
				execvp(args[0],args);
				/* if we got here, that means execv has faild */
				perror("error: process execution failed\n");
				exit(-1);
				break;
			default:
				command=strtok(lineSize,delimiters);
				Last_Bg_Pid=pID;
				/* we are the proud father */
				insert_job(job_list_head_pointer,pID,command,NOT_SUSPENDED);
				return 0;
		}
	}
	return -1;
}
Example #13
0
END_TEST

START_TEST(remove_job_test)
  {
  all_jobs alljobs;
  int result;
  struct job *test_job = job_alloc();

  result = remove_job(NULL,test_job);
  fail_unless(result != PBSE_NONE, "remove from null array fail");

  result = remove_job(&alljobs,NULL);
  fail_unless(result != PBSE_NONE, "NULL job remove fail");

  insert_job(&alljobs,test_job);
  result = remove_job(&alljobs,test_job);
  fail_unless(result == PBSE_NONE, "job remove fail");
  }
Example #14
0
END_TEST

START_TEST(get_jobs_index_test)
  {
  struct all_jobs alljobs;
  struct job *test_job = job_alloc();
  int result;
  initialize_all_jobs_array(&alljobs);

  result = get_jobs_index(NULL,test_job);
  fail_unless(result == -1 * PBSE_BAD_PARAMETER, "null input array fail");

  result = get_jobs_index(&alljobs,NULL);
  fail_unless(result == -1 * PBSE_BAD_PARAMETER, "NULL input job fail");

  insert_job(&alljobs, test_job);
  result = get_jobs_index(&alljobs, test_job);
  fail_unless(result == PBSE_NONE, "get_jobs_index fail");
  }
Example #15
0
END_TEST

START_TEST(has_job_test)
  {
  all_jobs alljobs;
  struct job *test_job = job_alloc();
  int result;


  result = has_job(NULL,test_job);
  fail_unless(result != PBSE_NONE, "null input array fail");

  result = has_job(&alljobs,NULL);
  fail_unless(result != PBSE_NONE, "NULL input job fail");

  insert_job(&alljobs, test_job);
  result = has_job(&alljobs, test_job);
  fail_unless(result == TRUE, "has_job fail");
  }
Example #16
0
/**
	Called when a new job arrives.
 
	If multiple cores are idle, the job should be assigned to the core with the
	lowest id.
	If the job arriving should be scheduled to run during the next
	time cycle, return the zero-based index of the core the job should be
	scheduled on. If another job is already running on the core specified,
	this will preempt the currently running job.
	Assumptions:
		- You may assume that every job wil have a unique arrival time.
	@param job_number a globally unique identification number of the job arriving.
	@param time the current time of the simulator.
	@param running_time the total number of time units this job will run before it will be finished.
	@param priority the priority of the job. (The lower the value, the higher the priority.)
	@return index of core job should be scheduled on
	@return -1 if no scheduling changes should be made. 
 
 */
int scheduler_new_job(int job_number, int time, int running_time, int priority)
{
	int i;
	inc_time(time);
	job_t* job = create_job(job_number, time, running_time, priority);

	if ( (i = get_core()) != -1 )
	{
		insert_job(i,job);
		return i;
	}//if
	else if ( is_prempt() )
	{
		i = preempt(job);
		if ( i == -1 )
			priqueue_offer(jobs,job);
		return i;
	}//else if
	priqueue_offer(jobs,job);
	return -1;
}//scheduler_new_job
Example #17
0
/**
	Called when a job has completed execution.
 
	The core_id, job_number and time parameters are provided for convenience. You may be able to calculate the values with your own data structure.
	If any job should be scheduled to run on the core free'd up by the
	finished job, return the job_number of the job that should be scheduled to
	run on core core_id.
 
	@param core_id the zero-based index of the core where the job was located.
	@param job_number a globally unique identification number of the job.
	@param time the current time of the simulator.
	@return job_number of the job that should be scheduled to run on core core_id
	@return -1 if core should remain idle.
 */
int scheduler_job_finished(int core_id, int job_number, int time)
{
	// Increment Time
	inc_time(time);

	// Process job termination
	job_t* p = delete_job(core_id,job_number);
	priqueue_remove(jobs,p);
	inc_wait(cur_t - p->arr_t - p->run_t);
	inc_turn(cur_t - p->arr_t);
	free_job(p);
	
	// Schedule new job
	p = priqueue_poll(jobs);
	if ( p != NULL )
	{
		insert_job(core_id,p);
		return p->jid;
	}//if
	return -1;
}//scheduler_job_finished
Example #18
0
END_TEST

START_TEST(next_job_test)
  {
  all_jobs alljobs;
  struct job *result;
  result = next_job(NULL,NULL);

  fail_unless(result == NULL, "null input parameters fail");

  result = next_job(&alljobs,NULL);
  fail_unless(result == NULL, "NULL input iterator fail");

  struct job *test_job1 = job_alloc();
  strcpy(test_job1->ji_qs.ji_jobid, "test_job1");
  int rc = insert_job(&alljobs,test_job1);
  fail_unless(rc == PBSE_NONE, "job insert fail1");

  struct job *test_job2 = job_alloc();
  strcpy(test_job2->ji_qs.ji_jobid, "test_job2");
  rc = insert_job(&alljobs,test_job2);
  fail_unless(rc == PBSE_NONE, "job insert fail2");

  struct job *test_job3 = job_alloc();
  strcpy(test_job3->ji_qs.ji_jobid, "test_job3");
  rc = insert_job(&alljobs,test_job3);
  fail_unless(rc == PBSE_NONE, "job insert fai3");

  struct job *test_job4 = job_alloc();
  strcpy(test_job4->ji_qs.ji_jobid, "test_job4");
  rc = insert_job(&alljobs,test_job4);
  fail_unless(rc == PBSE_NONE, "job insert fail4");

  struct job *test_job5 = job_alloc();
  strcpy(test_job5->ji_qs.ji_jobid, "test_job5");
  rc = insert_job(&alljobs,test_job5);
  fail_unless(rc == PBSE_NONE, "job insert fail5");

  /* first transverse to see if we get all 5 jobs */
  all_jobs_iterator *iter;
  alljobs.lock();
  iter = alljobs.get_iterator();
  alljobs.unlock();

  job *pjob = next_job(&alljobs,iter);
  int jobcount = 0;

  while(pjob != NULL)
    {
    jobcount++;
    pjob = next_job(&alljobs,iter);
    }

  fail_unless(jobcount == 5, "Expected job counts to be 5, but it was %d",
    jobcount);

  all_jobs_iterator *iter2;
  alljobs.lock();
  iter2 = alljobs.get_iterator();
  alljobs.unlock();

  /* simulate another thread had added more jobs to the alljobs */
  struct job *test_job6 = job_alloc();
  strcpy(test_job6->ji_qs.ji_jobid, "test_job6");
  rc = insert_job(&alljobs,test_job6);
  fail_unless(rc == PBSE_NONE, "job insert fail6");

  pjob = next_job(&alljobs,iter2);
  jobcount = 0;

  while(pjob != NULL)
    {
    jobcount++;
    fail_unless(pjob->ji_qs.ji_jobid[0] != (char)254, 
      "get_next returned a deleted job");
    pjob = next_job(&alljobs,iter2);
    }

  fail_unless(jobcount == 6, "Expected job counts to be 6, but it was %d",
    jobcount);
  }
Example #19
0
//********************************************
// function name: ExeCmd
// Description: interperts and executes built-in commands
// Parameters: pointer to jobs, command string
// Returns: 0 - success,1 - failure
//**************************************************************************************
int ExeCmd(pJob* head, char* lineSize, char* cmdString)
{
	char* cmd; 
	char* args[MAX_ARG];
	char pwd[MAX_LINE_SIZE];
	static char last_dir[MAX_LINE_SIZE];
	char* delimiters = " \t\n";  
	int i = 0;
	/*========used in kill command=======*/
	int signal_number, Job_number, signal_sent;
	int found_job_pid;
	char found_job_name[MAX_LINE_SIZE];
	/*===================================*/
	/*========used in fg command=========*/
	int child_done_id, fg_runner_pid, fg_req_JID;
	/*===================================*/
	/*========used in bg command=========*/
	int bg_runner_pid;
	/*===================================*/

	bool illegal_cmd = FALSE; // illegal command
    	cmd = strtok(lineSize, delimiters);
	if (cmd == NULL)
		return 0; 
   	args[0] = cmd;
   	L_Fg_Cmd = cmd;
	for (i=1; i<MAX_ARG; i++)
	{
		args[i] = strtok(NULL, delimiters);
	}
/*************************************************/
// Built in Commands
/*************************************************/
	if (!strcmp(cmd, "cd") ) 
	{
		getcwd(pwd,sizeof(pwd));
		if(!strcmp(args[1], "-"))
		{
			if(last_dir[0]!=0)
			{
				chdir(last_dir);
			}
			else
			{
				perror("smash error: > no previous path exists\n");
			}
		}
		else
		{

			if(!chdir(args[1]))
			{
				strcpy(last_dir,pwd);
			}
			else
			{
				printf("smash error: > \"%s\" -path not found\n",args[1]);
				illegal_cmd = TRUE;
			}
		}
	} 
	
	/*************************************************/
	else if (!strcmp(cmd, "pwd")) 
	{
		if(getcwd(pwd,sizeof(pwd))!= NULL)
		{
			fprintf(stdout,"%s\n",pwd);
		}
		else
		{
			perror("smash error: > pwd error\n");
		}
	}
	/*************************************************/
	else if (!strcmp(cmd, "mkdir"))
	{

		if(mkdir(args[1], 0755))
			{
				if(EEXIST == errno)
				{
					printf("smash error: > \"%s\" -directory already exists\n",args[1]);
				}
				else
				{
					printf("smash error: > \"%s\" -cannot create directory\n",args[1]);
				}
			}
	}
	/*************************************************/
	else if (!strcmp(cmd, "jobs")) 
	{
		print_jobs(*head);
	}
	/*************************************************/
	else if (!strcmp(cmd, "kill"))
	{
		if(args[1][0] != '-')
		{
			printf("smash error: > \"%s\"",cmdString);
			return -1;
		}
		signal_number=atoi(args[1]+1);
		Job_number=atoi(args[2]);

		//search requested job
		found_job_pid=find_job_PID(*head,Job_number);

		if(found_job_pid==-1)
		{
			printf("smash error: > kill %d - job does not exist\n",Job_number);
			return -1;
		}
		else
		{

			switch (signal_number)
			{
				case SIGTSTP:
							signal_sent=kill(found_job_pid,SIGTSTP);

							printf("signal SIGTSTP was sent to pid %d\n",found_job_pid);
							if(signal_sent!=0)
							{
								printf("smash error: > kill %d - cannot send signal\n",Job_number);
							}
							strcpy(found_job_name,find_job_name(*head,found_job_pid));
							remove_job(head,found_job_pid);
							insert_job(head, found_job_pid, found_job_name, SUSPENDED);
							break;
				case SIGINT:
							signal_sent=kill(found_job_pid,SIGINT);

							printf("signal SIGINT was sent to pid %d\n",found_job_pid);
							if(signal_sent!=0)
							{
								printf("smash error: > kill %d - cannot send signal\n",Job_number);
							}
							remove_job(head,found_job_pid);
							break;
				case SIGTERM:
							signal_sent=kill(found_job_pid,SIGTERM);

							printf("signal SIGTERM was sent to pid %d\n",found_job_pid);
							if(signal_sent!=0)
							{
								printf("smash error: > kill %d - cannot send signal\n",Job_number);
							}
							remove_job(head,found_job_pid);
							break;
				default:
							signal_sent=kill(found_job_pid,signal_number);
							if(signal_sent!=0)
							{
								printf("smash error: > kill %d - cannot send signal\n",Job_number);
							}
							printf("signal %d was sent to pid %d\n",signal_number,found_job_pid);
			}
		}
	}
	/*************************************************/
	else if (!strcmp(cmd, "showpid")) 
	{
		printf("smash pid is %d\n",getpid());
	}
	/*************************************************/
	else if (!strcmp(cmd, "fg")) 
	{
		if(args[1]==NULL)
		{
			Last_Bg_Pid=get_last_job_pid(*head);
			fg_runner_pid=Last_Bg_Pid;
		}
		else
		{
			fg_req_JID=atoi(args[1]);
			fg_runner_pid=find_job_PID(*head,fg_req_JID);
		}
		if (fg_runner_pid== -1)// No jobs in the Bg
		{
				perror("smash error: requested process not found\n");
		}
		else if(is_suspended(*head,fg_runner_pid)== SUSPENDED)
		{
			kill(fg_runner_pid,SIGCONT);
			printf("signal SIGCONT was sent to pid %d\n",fg_runner_pid);
			unsuspend(*head,fg_runner_pid);
			Susp_Bg_Pid=get_last_suspended_job_pid(*head);
		}
		GPid=fg_runner_pid;
		L_Fg_Cmd = find_job_name(*head,fg_runner_pid);
		do
		{
			child_done_id = wait(NULL);
			if((child_done_id==-1)&&(errno==EINTR)) //child is gone due to inerrupt
			{
				break;
			}
		}
		while((child_done_id != fg_runner_pid) && (fg_runner_pid != Susp_Bg_Pid));
		remove_job(head,fg_runner_pid);
		GPid= -1;

	} 
	/*************************************************/
	else if (!strcmp(cmd, "bg")) 
	{

		if(args[1]==NULL)
			{
				bg_runner_pid=get_last_suspended_job_pid(*head);
			}
		else
			{
				fg_req_JID=atoi(args[1]);
				bg_runner_pid=find_job_PID(*head, fg_req_JID);
				if(is_suspended(*head,bg_runner_pid)==NOT_SUSPENDED)
				{
					perror("smash error: > bg called on un-suspended job\n");
					return -1;
				}
			}
		strcpy(found_job_name,find_job_name(*head,bg_runner_pid));
		printf("%s",found_job_name);
		kill(bg_runner_pid,SIGCONT);
		printf("signal SIGCONT was sent to pid %d\n",bg_runner_pid);
		unsuspend(*head,bg_runner_pid);
		Susp_Bg_Pid=get_last_suspended_job_pid(*head);
	}
	/*************************************************/
	else if (!strcmp(cmd, "quit"))
	{
		if(args[1]==NULL)
		{
			destroy_list(head);
	   		exit(0);
		}
		else if (!strcmp(args[1], "kill"))
		{
			if (kill_jobs(*head) != -1) {
				destroy_list(head);
				exit(0);
			}
			else {
				exit (-1);
			}
		}
		else
			perror("smash error: > quit called with illegal argument\n");
			return -1;
	} 
	/*************************************************/
	else // external command
	{
 		ExeExternal(args, cmdString);
	 	return 0;
	}
	if (illegal_cmd == TRUE)
	{
		printf("smash error: > \"%s\"\n", cmdString);
		return 1;
	}
    return 0;
}
Example #20
0
int main()
{
    int bg;
    char *args[ARGS_SIZE];
    int cnt;
    char new_dir[MAX_PATH + 1];
    // new_dir[0] = '\0';

    char buf[MAX_PATH + 1];
    
    memcpy(curdir, getcwd(buf, MAX_PATH + 1), MAX_PATH + 1);
    
    signal(SIGCHLD, handle_child_death);

    // free(shell_command_history);
    shell_command_history = (struct history_item **) malloc(HISTORY_SIZE*sizeof(struct history_item *));
    for(int i = 0; i < HISTORY_SIZE; i++) {
      shell_command_history[i] = (struct history_item *) malloc(sizeof(struct history_item));
    }

    while(1) {
      bg = 0;
      cnt = getcmd("\n>>  ", args, &bg);
      args[cnt] = NULL;

      /*
      for (int i = 0; i < cnt; i++)
        printf("\nArg[%d] = %s", i, args[i]);
        */

      printf("\n");
    
      int history_element_to_execute;
      if ( (history_element_to_execute = atoi(args[0])) > 0) {
        printf("history_element_to_execute: %d\n", history_element_to_execute);
        memcpy(args, shell_command_history[history_element_to_execute-1], ARGS_SIZE);
      }
      
      save_to_history(args);

      if (strcmp(args[0],"history") == 0) {
        if (args[1] != NULL) {
          printf("history does not require any arguments.\n");
          exit(-1);
        }
        
        show_history();
        
        continue;
      }

      if (strcmp(args[0], "cd") == 0) {
        new_dir[0] = '\0';
        strcat(new_dir, curdir);
        printf("%s\n", new_dir);

        if (args[1][0] == '/') { 
          new_dir[0] = '\0';
        }
        char slash[] = {'/', '\0'};
        strcat(new_dir, slash);
        strcat(new_dir, args[1]);
        strcat(new_dir, slash);

        curdir[0] = '\0';
        strcat(curdir, new_dir);
  
        chdir(new_dir);
        
        continue;
      }
      

      if (strcmp(args[0], "pwd") == 0) {
        char* cwd;
        cwd = getcwd(buf, MAX_PATH + 1);
        printf("%s", cwd);

        continue;
      }
  
      if (strcmp(args[0], "exit") == 0) {
        exit(0);
      }

      if (strcmp(args[0], "jobs") == 0) {
        print_jobs();

        continue;
      }
      
      if (strcmp(args[0], "fg") == 0) {
        // waitpid();
        int pos = strtol(args[1], NULL, 10);

        struct job * selected_job = get_job_at_pos(pos);
        if (selected_job == NULL) {
          printf("There was an issue. The selected job, %d, has not been found.\n", pos);
          continue;
        } 

        int status = 0;

        waitpid(selected_job->pid, &status, 0);

        continue;
      } 
  
      // char * file = malloc(sizeof);
      int child_pid = 0;
      if((child_pid = fork()) == 0) {
        int output_filename_index = cnt - 1;
        if (bg) {
          output_filename_index--;
        }

        if (output_filename_index < 1) {
          output_filename_index = 1;
        }
        printf("cnt: %d.\n", cnt);

        char* output_filename = malloc((MAX_PATH+1)*sizeof(char));
        if (strcmp(args[output_filename_index - 1], ">") == 0) {
          char* arg = args[output_filename_index];
          memcpy(output_filename, arg, strlen(arg)+1);
          printf("%s\n", output_filename);
          close(1);
          open(output_filename, O_WRONLY | O_CREAT, 0666);
          // int fd[2] = {0,1};
          // pipe(fd);
          args[output_filename_index - 1] = NULL;
        }
      
        int result = execvp(args[0], args);
        if (result < 0) {
          printf("We got the errno %d", errno);
        } 
      } else {
        // continue execution
      }

      if (bg) {
          // printf("\nBackground enabled..\n");
          int inserted_job_pos = insert_job(child_pid, args);
          printf("Created job with pid: %d, at pos: %d", child_pid, inserted_job_pos);
      } else {
          // printf("\nBackground not enabled \n");
          int status = 0;
          waitpid(child_pid, &status, 0);
      }
      
      printf("\n\n");
    }
}