Exemplo n.º 1
0
unsigned int Sleep(unsigned int secs) {
  unsigned int rc;

  if ((rc = sleep(secs)) < 0) unix_error("Sleep error");
  return rc;
}
Exemplo n.º 2
0
Arquivo: tsh.c Projeto: fffxj/csapp
void Execve(const char *filename, char *const argv[], char *const envp[]) 
{
    if (execve(filename, argv, envp) < 0)
	unix_error("Execve error");
}
Exemplo n.º 3
0
Arquivo: tsh.c Projeto: fffxj/csapp
void Sigfillset(sigset_t *set)
{ 
    if (sigfillset(set) < 0)
	unix_error("Sigfillset error");
    return;
}
Exemplo n.º 4
0
void Sem_init(sem_t *sem, int pshared, unsigned int value) 
{
    if (sem_init(sem, pshared, value) < 0)
	unix_error("Sem_init error");
}
Exemplo n.º 5
0
void V(sem_t *sem) 
{
    if (sem_post(sem) < 0)
	unix_error("V error");
}
Exemplo n.º 6
0
void Munmap(void *start, size_t length) 
{
    if (munmap(start, length) < 0)
	unix_error("munmap error");
}
Exemplo n.º 7
0
void Fputs(const char *ptr, FILE *stream) 
{
    if (fputs(ptr, stream) == EOF)
	unix_error("Fputs error");
}
Exemplo n.º 8
0
void Sigprocmask(int method, sigset_t *mask, sigset_t *oldmask) {
    if (sigprocmask(method, mask, oldmask)) {
        unix_error("Sigprocmask error");
    }
}
Exemplo n.º 9
0
void Kill(pid_t pid, int signum) {
    if (kill(pid, signum) < 0) {
        unix_error("Kill error");
    }
}
Exemplo n.º 10
0
void Sigemptyset(sigset_t *mask) {
    if (sigemptyset(mask) < 0) {
        unix_error("Sigemptyset error");
    }
}
Exemplo n.º 11
0
void Sigaddset(sigset_t *mask, int signum) {
    if (sigaddset(mask, signum) < 0) {
        unix_error("Sigaddset error");
    }
}
Exemplo n.º 12
0
/*
 * sigtstp_handler - The kernel sends a SIGTSTP to the shell whenever
 *     the user types ctrl-z at the keyboard. Catch it and suspend the
 *     foreground job by sending it a SIGTSTP.  
 */
void sigtstp_handler(int sig) 
{
    if (kill(-fgpid(jobs), SIGSTOP) == -1) unix_error("kill failed");
    return;
}
Exemplo n.º 13
0
/* 
 * sigint_handler - The kernel sends a SIGINT to the shell whenver the
 *    user types ctrl-c at the keyboard.  Catch it and send it along
 *    to the foreground job.  
 */
void sigint_handler(int sig) 
{
    if (kill(-fgpid(jobs), SIGINT) == -1) unix_error("kill failed");
    return;
}
Exemplo n.º 14
0
void Setpgid(pid_t pid, pid_t pgid) {
  int rc;

  if ((rc = setpgid(pid, pgid)) < 0) unix_error("Setpgid error");
  return;
}
Exemplo n.º 15
0
void Stat(const char *filename, struct stat *buf) 
{
    if (stat(filename, buf) < 0)
	unix_error("Stat error");
}
Exemplo n.º 16
0
Arquivo: tsh.c Projeto: vvv214/icsLab
/* 
 * do_bgfg - Execute the builtin bg and fg commands
 */
void do_bgfg(char **argv) 
{
	int jid;
	pid_t pid;
	struct job_t* jobp = NULL;
	
	//check for condition "bg " or "fg "
	if(argv[1] == NULL){
		printf("%s command requires PID or %%jobid argument\n", argv[0]);
		return;
	}
	


	//jid %x
	if(argv[1][0] == '%'){
		jid = atoi(&argv[1][1]);
		jobp = getjobjid(jobs, jid);
		//check for the condition for invalid jid %x
		if(jobp == NULL){
			printf("%%%d: No such job\n", jid);
			return;
		}
	}
	//pid x
	else if(isdigit(argv[1][0])){
		pid = atoi(argv[1]);
		jobp = getjobpid(jobs, pid);
		//check for the condition for invalid pid x
		if(jobp == NULL){
			printf("(%d): No such process\n", pid);
			return;
		}
	}
	//check for the condition "bg a" or "fg a"
	else{
		printf("%s: argument must be a PID or %%jobid\n", argv[0]);
		return;
	}

	//send signal to all the processes in the process group
	if(kill(-(jobp->pid), SIGCONT) < 0){
		unix_error("kill error");
	}
	


	//do work for background
	if(!strcmp(argv[0], "bg")){
		jobp->state = BG;
		printf("[%d] (%d) %s", jobp->jid, jobp->pid, jobp->cmdline);
	}

	//do work for foreground
	else if(!strcmp(argv[0], "fg")){
		jobp->state = FG;
		waitfg(jobp->pid);
	}

	//check for the condition "x 1"
	else
		unix_error("must be bg or fg");

    return;
}
Exemplo n.º 17
0
void Fstat(int fd, struct stat *buf) 
{
    if (fstat(fd, buf) < 0)
	unix_error("Fstat error");
}
Exemplo n.º 18
0
/* split a net file */
static void split_net(void) {
	FILE *input;
	FILE *output;
	int filename_len;
	char *filename;
	int open = 0;

	input = Fopen(opt.in_file, "r");

	/* make the output directory */
	if ((mkdir(opt.out_dir, S_IRWXU | S_IRWXG | S_IRWXO) == -1)) {
		if (errno != EEXIST) {
			unix_error("mkdir failed");
		}
	}

	/* very simple splitter */
	while (Fgets(line, LINE_MAX, input) != NULL) {
		/* find the net line */
		if (strstr(line, "net") == line) {
			/* get the chrom name */
			if (sscanf(line, "net %s ", chrom) != 1) {
				fprintf(stderr, "%s: Can't parse the following line:\n", prog);
				fprintf(stderr, "%s", line);
				exit(EXIT_FAILURE);
			}

			/* build the output filename */
			filename_len = opt.out_dir_len + (int) strlen(chrom) + (int) strlen(opt.suffix) + 2;
			filename = (char *) Malloc (filename_len * sizeof(char));
			
			strcpy(filename, opt.out_dir);
			strcat(filename, "/");
			strcat(filename, chrom);
			strcat(filename, opt.suffix);

			/* close if necessary */
			if (open) {
				Fclose(output);
			}

			output = Fopen(filename, "w");
			free(filename);

			fprintf(output, "%s", line);

			open = 1;

			continue;
		}

		if (! open) {
			fprintf(stderr, "Out of synch (didn't find net line?)\n");
			exit(EXIT_FAILURE);
		}

		fprintf(output, "%s", line);
	}

	Fclose(input);
}
Exemplo n.º 19
0
/******************************************
 * Wrappers for the Standard I/O functions.
 ******************************************/
void Fclose(FILE *fp) 
{
    if (fclose(fp) != 0)
	unix_error("Fclose error");
}
Exemplo n.º 20
0
/* fclose, checking for erros */
void Fclose(FILE *stream) {
	if (fclose(stream) == EOF) {
		unix_error("fclose failed");
	}
}
Exemplo n.º 21
0
void Fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream) 
{
    if (fwrite(ptr, size, nmemb, stream) < nmemb)
	unix_error("Fwrite error");
}
Exemplo n.º 22
0
CAMLprim value unix_execvpe(value path, value args, value env)
{
  unix_error(ENOSYS, "execvpe", path);
  return Val_unit;
}
Exemplo n.º 23
0
void P(sem_t *sem) 
{
    if (sem_wait(sem) < 0)
	unix_error("P error");
}
Exemplo n.º 24
0
int main(int argc, char *argv[])
{
	int listenfd, connfd;
	char hostname[MAXLINE], port[MAXLINE];
	socklen_t clientlen;
	struct sockaddr_storage clientaddr;
	int acc_count = 0;

	//if (strcmp(argv[2], "compute-0-29.local") == 0)
	//{
	printf("%s listening\n", argv[2]);
	listenfd = Open_listenfd ("15618");
	//}
	sleep(5);

	//int send_val = 5;
	printf("Hostname is %s\n", argv[2]);
	printf("Int got as %d\n", atoi(argv[1]));

	if (strcmp(argv[2], "compute-0-29.local") == 0)
	{
		while (1)
		{
			clientlen = sizeof (clientaddr);

			// accept connections 
			connfd = Accept (listenfd, (SA *) & clientaddr, &clientlen);
			Getnameinfo ((SA *) & clientaddr, clientlen, hostname, MAXLINE,
					port, MAXLINE, 0);
			printf ("Accepted connection from (%s, %s). Connfd is %d\n", hostname, port, connfd);

			//newfd = (int *) malloc (sizeof (int));
			//newfd = connfd;

			// go serve this client! 
			// pthread_create (&tid, NULL, doit, newfd);
			acc_count++;

			double send_double = 232.23;
			int retval = Rio_writen (connfd, (void *)&send_double, sizeof(double));
			if (retval < 0)
			{   
				printf("Rio_writen to %d encountered a problem.\n", connfd);

				unix_error ("Rio_writen error");
			}   

			retval = Rio_writen (connfd, (void *)&send_double, sizeof(double));
			if (retval < 0)
			{   
				printf("Rio_writen to %d encountered a problem.\n", connfd);

				unix_error ("Rio_writen error");
			}
			int len = Rio_readn (connfd, (void *)&send_double, sizeof(double));

			if (len < 0)
			{
				unix_error ("Rio_readlineb error");
			}
			printf("Host %s got len as %d and receive_val as %lf\n", argv[2], len, send_double);

			len = Rio_readn (connfd, (void *)&send_double, sizeof(double));

			if (len < 0)
			{
				unix_error ("Rio_readlineb error");
			}
			printf("Host %s got len as %d and receive_val as %lf\n", argv[2], len, send_double);

			if (acc_count == 3)
			{
				printf("Accepted 3 connections.\n");
				break;
			}
		}
	}
	else
	{
		int serverfd = Open_clientfd ("10.22.1.241", "15618");
		printf("In host %s, serverfd is %d\n", argv[2], serverfd);

		double buf;
		int len = Rio_readn (serverfd, (void *)&buf, sizeof(double));

		if (len < 0)
		{
			unix_error ("Rio_readlineb error");
		}
		printf("Host %s got len as %d and buf as %lf\n", argv[2], len, buf);

		len = Rio_readn (serverfd, (void *)&buf, sizeof(double));

		if (len < 0)
		{
			unix_error ("Rio_readlineb error");
		}
		printf("Host %s got len as %d and buf as %lf\n", argv[2], len, buf);

		buf = 99.104;
		int retval = Rio_writen (serverfd, (void *)&buf, sizeof(double));
		if (retval < 0)
		{   
			printf("Rio_writen to %d encountered a problem.\n", serverfd);

			unix_error ("Rio_writen error");
		}   

		retval = Rio_writen (serverfd, (void *)&buf, sizeof(double));

		if (retval < 0)
		{   
			printf("Rio_writen to %d encountered a problem.\n", serverfd);

			unix_error ("Rio_writen error");
		}   
	}
	return 0;
}
Exemplo n.º 25
0
void Rio_writen(int fd, void *usrbuf, size_t n) 
{
    if (rio_writen(fd, usrbuf, n) != n)
	unix_error("Rio_writen error");
}
Exemplo n.º 26
0
Arquivo: mdriver.c Projeto: gz/cslab
/**************
 * Main routine
 **************/
int main(int argc, char **argv)
{
    int i;
    char c;
    char **tracefiles = NULL;  /* null-terminated array of trace file names */
    int num_tracefiles = 0;    /* the number of traces in that array */
    trace_t *trace = NULL;     /* stores a single trace file in memory */
    range_t *ranges = NULL;    /* keeps track of block extents for one trace */
    stats_t *libc_stats = NULL;/* libc stats for each trace */
    stats_t *mm_stats = NULL;  /* mm (i.e. student) stats for each trace */
    speed_t speed_params;      /* input parameters to the xx_speed routines */ 

    int team_check = 1;  /* If set, check team structure (reset by -a) */
    int run_libc = 0;    /* If set, run libc malloc (set by -l) */
    int autograder = 0;  /* If set, emit summary info for autograder (-g) */

    /* temporaries used to compute the performance index */
    double secs, ops, util, avg_mm_util, avg_mm_throughput, p1, p2, perfindex;
    int numcorrect;
    
    /* 
     * Read and interpret the command line arguments 
     */
    while ((c = getopt(argc, argv, "f:t:hvVgal")) != EOF) {
        switch (c) {
	case 'g': /* Generate summary info for the autograder */
	    autograder = 1;
	    break;
        case 'f': /* Use one specific trace file only (relative to curr dir) */
            num_tracefiles = 1;
            if ((tracefiles = realloc(tracefiles, 2*sizeof(char *))) == NULL)
		unix_error("ERROR: realloc failed in main");
	    strcpy(tracedir, "./"); 
            tracefiles[0] = strdup(optarg);
            tracefiles[1] = NULL;
            break;
	case 't': /* Directory where the traces are located */
	    if (num_tracefiles == 1) /* ignore if -f already encountered */
		break;
	    strcpy(tracedir, optarg);
	    if (tracedir[strlen(tracedir)-1] != '/') 
		strcat(tracedir, "/"); /* path always ends with "/" */
	    break;
        case 'a': /* Don't check team structure */
            team_check = 0;
            break;
        case 'l': /* Run libc malloc */
            run_libc = 1;
            break;
        case 'v': /* Print per-trace performance breakdown */
            verbose = 1;
            break;
        case 'V': /* Be more verbose than -v */
            verbose = 2;
            break;
        case 'h': /* Print this message */
	    usage();
            exit(0);
        default:
	    usage();
            exit(1);
        }
    }
	
    /* 
     * Check and print team info 
     */
    if (team_check) {
	/* Students must fill in their team information */
	if (!strcmp(team.teamname, "")) {
	    printf("ERROR: Please provide the information about your team in mm.c.\n");
	    exit(1);
	} else
	    printf("Team Name:%s\n", team.teamname);
	if ((*team.name1 == '\0') || (*team.id1 == '\0')) {
	    printf("ERROR.  You must fill in all team member 1 fields!\n");
	    exit(1);
	} 
	else
	    printf("Member 1 :%s:%s\n", team.name1, team.id1);

	if (((*team.name2 != '\0') && (*team.id2 == '\0')) ||
	    ((*team.name2 == '\0') && (*team.id2 != '\0'))) { 
	    printf("ERROR.  You must fill in all or none of the team member 2 ID fields!\n");
	    exit(1);
	}
	else if (*team.name2 != '\0')
	    printf("Member 2 :%s:%s\n", team.name2, team.id2);
    }

    /* 
     * If no -f command line arg, then use the entire set of tracefiles 
     * defined in default_traces[]
     */
    if (tracefiles == NULL) {
        tracefiles = default_tracefiles;
        num_tracefiles = sizeof(default_tracefiles) / sizeof(char *) - 1;
	printf("Using default tracefiles in %s\n", tracedir);
    }

    /* Initialize the timing package */
    init_fsecs();

    /*
     * Optionally run and evaluate the libc malloc package 
     */
    if (run_libc) {
	if (verbose > 1)
	    printf("\nTesting libc malloc\n");
	
	/* Allocate libc stats array, with one stats_t struct per tracefile */
	libc_stats = (stats_t *)calloc(num_tracefiles, sizeof(stats_t));
	if (libc_stats == NULL)
	    unix_error("libc_stats calloc in main failed");
	
	/* Evaluate the libc malloc package using the K-best scheme */
	for (i=0; i < num_tracefiles; i++) {
	    trace = read_trace(tracedir, tracefiles[i]);
	    libc_stats[i].ops = trace->num_ops;
	    if (verbose > 1)
		printf("Checking libc malloc for correctness, ");
	    libc_stats[i].valid = eval_libc_valid(trace, i);
	    if (libc_stats[i].valid) {
		speed_params.trace = trace;
		if (verbose > 1)
		    printf("and performance.\n");
		libc_stats[i].secs = fsecs(eval_libc_speed, &speed_params);
	    }
	    free_trace(trace);
	}

	/* Display the libc results in a compact table */
	if (verbose) {
	    printf("\nResults for libc malloc:\n");
	    printresults(num_tracefiles, libc_stats);
	}
    }

    /*
     * Always run and evaluate the student's mm package
     */
    if (verbose > 1)
	printf("\nTesting mm malloc\n");

    /* Allocate the mm stats array, with one stats_t struct per tracefile */
    mm_stats = (stats_t *)calloc(num_tracefiles, sizeof(stats_t));
    if (mm_stats == NULL)
	unix_error("mm_stats calloc in main failed");
    
    /* Initialize the simulated memory system in memlib.c */
    mem_init(); 

    /* Evaluate student's mm malloc package using the K-best scheme */
    for (i=0; i < num_tracefiles; i++) {
	trace = read_trace(tracedir, tracefiles[i]);
	mm_stats[i].ops = trace->num_ops;
	if (verbose > 1)
	    printf("Checking mm_malloc for correctness, ");
	mm_stats[i].valid = eval_mm_valid(trace, i, &ranges);
	if (mm_stats[i].valid) {
	    if (verbose > 1)
		printf("efficiency, ");
	    mm_stats[i].util = eval_mm_util(trace, i, &ranges);
	    speed_params.trace = trace;
	    speed_params.ranges = ranges;
	    if (verbose > 1)
		printf("and performance.\n");
	    mm_stats[i].secs = fsecs(eval_mm_speed, &speed_params);
	}
	free_trace(trace);
    }

    /* Display the mm results in a compact table */
    if (verbose) {
	printf("\nResults for mm malloc:\n");
	printresults(num_tracefiles, mm_stats);
	printf("\n");
    }

    /* 
     * Accumulate the aggregate statistics for the student's mm package 
     */
    secs = 0;
    ops = 0;
    util = 0;
    numcorrect = 0;
    for (i=0; i < num_tracefiles; i++) {
	secs += mm_stats[i].secs;
	ops += mm_stats[i].ops;
	util += mm_stats[i].util;
	if (mm_stats[i].valid)
	    numcorrect++;
    }
    avg_mm_util = util/num_tracefiles;

    /* 
     * Compute and print the performance index 
     */
    if (errors == 0) {
	avg_mm_throughput = ops/secs;

	p1 = UTIL_WEIGHT * avg_mm_util;
	if (avg_mm_throughput > AVG_LIBC_THRUPUT) {
	    p2 = (double)(1.0 - UTIL_WEIGHT);
	} 
	else {
	    p2 = ((double) (1.0 - UTIL_WEIGHT)) * 
		(avg_mm_throughput/AVG_LIBC_THRUPUT);
	}
	
	perfindex = (p1 + p2)*100.0;
	printf("Perf index = %.0f (util) + %.0f (thru) = %.0f/100\n",
	       p1*100, 
	       p2*100, 
	       perfindex);
	
    }
    else { /* There were errors */
	perfindex = 0.0;
	printf("Terminated with %d errors\n", errors);
    }

    if (autograder) {
	printf("correct:%d\n", numcorrect);
	printf("perfidx:%.0f\n", perfindex);
    }

    exit(0);
}
Exemplo n.º 27
0
Arquivo: tsh.c Projeto: fffxj/csapp
void Sigprocmask(int how, const sigset_t *set, sigset_t *oldset)
{
    if (sigprocmask(how, set, oldset) < 0)
	unix_error("Sigprocmask error");
    return;
}
Exemplo n.º 28
0
Arquivo: mdriver.c Projeto: gz/cslab
/*
 * read_trace - read a trace file and store it in memory
 */
static trace_t *read_trace(char *tracedir, char *filename)
{
    FILE *tracefile;
    trace_t *trace;
    char type[MAXLINE];
    char path[MAXLINE];
    unsigned index, size;
    unsigned max_index = 0;
    unsigned op_index;

    if (verbose > 1)
	printf("Reading tracefile: %s\n", filename);

    /* Allocate the trace record */
    if ((trace = (trace_t *) malloc(sizeof(trace_t))) == NULL)
	unix_error("malloc 1 failed in read_trance");
	
    /* Read the trace file header */
    strcpy(path, tracedir);
    strcat(path, filename);
    if ((tracefile = fopen(path, "r")) == NULL) {
	sprintf(msg, "Could not open %s in read_trace", path);
	unix_error(msg);
    }
    fscanf(tracefile, "%d", &(trace->sugg_heapsize)); /* not used */
    fscanf(tracefile, "%d", &(trace->num_ids));     
    fscanf(tracefile, "%d", &(trace->num_ops));     
    fscanf(tracefile, "%d", &(trace->weight));        /* not used */
    
    /* We'll store each request line in the trace in this array */
    if ((trace->ops = 
	 (traceop_t *)malloc(trace->num_ops * sizeof(traceop_t))) == NULL)
	unix_error("malloc 2 failed in read_trace");

    /* We'll keep an array of pointers to the allocated blocks here... */
    if ((trace->blocks = 
	 (char **)malloc(trace->num_ids * sizeof(char *))) == NULL)
	unix_error("malloc 3 failed in read_trace");

    /* ... along with the corresponding byte sizes of each block */
    if ((trace->block_sizes = 
	 (size_t *)malloc(trace->num_ids * sizeof(size_t))) == NULL)
	unix_error("malloc 4 failed in read_trace");
    
    /* read every request line in the trace file */
    index = 0;
    op_index = 0;
    while (fscanf(tracefile, "%s", type) != EOF) {
	switch(type[0]) {
	case 'a':
	    fscanf(tracefile, "%u %u", &index, &size);
	    trace->ops[op_index].type = ALLOC;
	    trace->ops[op_index].index = index;
	    trace->ops[op_index].size = size;
	    max_index = (index > max_index) ? index : max_index;
	    break;
	case 'r':
	    fscanf(tracefile, "%u %u", &index, &size);
	    trace->ops[op_index].type = REALLOC;
	    trace->ops[op_index].index = index;
	    trace->ops[op_index].size = size;
	    max_index = (index > max_index) ? index : max_index;
	    break;
	case 'f':
	    fscanf(tracefile, "%ud", &index);
	    trace->ops[op_index].type = FREE;
	    trace->ops[op_index].index = index;
	    break;
	default:
	    printf("Bogus type character (%c) in tracefile %s\n", 
		   type[0], path);
	    exit(1);
	}
	op_index++;
	
    }
    fclose(tracefile);
    assert(max_index == trace->num_ids - 1);
    assert(trace->num_ops == op_index);
    
    return trace;
}
Exemplo n.º 29
0
Arquivo: tsh.c Projeto: fffxj/csapp
void Sigaddset(sigset_t *set, int signum)
{
    if (sigaddset(set, signum) < 0)
	unix_error("Sigaddset error");
    return;
}
Exemplo n.º 30
0
int Poll(struct pollfd *fdarray, unsigned long nfds, int timeout) {
  int rc;
  if ((rc = poll(fdarray, nfds, timeout)) == -1) unix_error("poll Unix error");
  return rc;
}