Exemple #1
0
int main(int argc, char *argv[])
{
  int fd;
  long i, seqno;
  pid_t pid;
  ssize_t n;
  char line[MAXLINE + 1];
  
  pid = getpid();
  fd = Open(SEQFILE, O_RDWR, FILE_MODE);/*以读写模式打开文件*/
  
  for(i = 0; i < 20; ++i){
    my_lock(fd);/*对指定文件进行加锁*/
    
    Lseek(fd, 0L, SEEK_SET);
    n = Read(fd, line, MAXLINE);
    line[n] = '\0';
    
    n = sscanf(line, "%ld\n", &seqno);/*将line中的序列号保存到seqno中*/
    printf("%s: pid = %ld, seq# = %ld\n", argv[0], (long)pid, seqno);
    
    seqno++;/*序列号加1*/
   
    snprintf(line, sizeof(line), "%ld\n", seqno);/*将序列号保存到line缓冲区中*/
    Lseek(fd, 0L, SEEK_SET);
    Write(fd, line, strlen(line));/*将line缓冲区中的内容写到指定的文件中*/

    my_unlock(fd);/*释放锁*/
  }
n  exit(0);
}
Exemple #2
0
int main(int argc, char **argv)
{
    int fd;
    long i= 0;
    long seqno = 0;
    pid_t pid;
    ssize_t n;
    char line[MAX_LINE+1];
    pid =getpid();
    fd = open(SWQFILE, O_RDWR, "ab" );
    for(i = 0 ; i< 20 ; i++ )
    {
        my_lock(fd);
        lseek(fd, 0L, SEEK_SET);
        n = read(fd, line, MAX_LINE);
        line[n] = '\0';
        n = sscanf(line,  "%ld\n" , &seqno);
        printf("%s: pid = %ld, seq# = %ld\n", argv[0], (long)pid, seqno);
        seqno++;
        snprintf(line, sizeof(line), "%ld\n",seqno);
        lseek(fd, 0L, SEEK_SET);
        write(fd, line, strlen(line));
        my_unlock(fd);
    }
    exit(0);

}
Exemple #3
0
main()
{
  int  fd, i, n, pid, seqno;
  char buff[MAXBUFF + 1];

  pid = getpid();
  if ( (fd = open(SEQFILE, 2) ) < 0)
    err_sys("can't open %s", SEQFILE);

  for (i=0 ; i<20 ; i++) {
    my_lock(fd);                    /* lock the file */
    lseek(fd, 0L, 0);               /* rewind before read */
    if ( (n = read(fd, buff, MAXBUFF)) <= 0 )
      err_sys("read error");
    buff[n] = '\0';                 /* null terminate for sscanf */

    if ( (n = sscanf(buff, "%d\n", &seqno)) != 1)
      err_sys("sscanf error");
    printf("pid = %d, seq# = %d\n", pid, seqno);

    seqno++;                        /* increment the sequence number */

    sprintf(buff, "%03d\n", seqno);
    n = strlen(buff);
    lseek(fd, 0L, 0);               /* rewind before write */
    if (write(fd, buff, n) != n)
      err_sys("write error");
    my_unlock(fd);                  /* unlick the file */
  }
}
Exemple #4
0
int main(int argc, char*argv[]) {
	int fd;
	long i, seqno;
	pid_t pid;
	ssize_t n;
	char line[MAXLINE + 1];
	int result;

	pid = getpid();
	fd = open(SEQFILE, O_CREAT | O_RDWR, FILE_MODE);
	
	for (i = 0; i < 20; i++) {
		my_lock(fd);
		if ((result = lseek(fd, 0, SEEK_SET)) == -1) {
			printf("lseek error ...\n");
			return -1;
		}
		n = read(fd, line, MAXLINE);
		line[n] = '\0';

		n = sscanf(line, "%ld\n", &seqno);
		printf("%s: pid = %ld, seq# = %ld\n", argv[0], (long)pid, seqno);

		seqno++;
		snprintf(line, sizeof(line), "%ld\n", seqno);
		lseek(fd, 0L, SEEK_SET);
		write(fd, line, strlen(line));

		my_unlock(fd);
	}
}
Exemple #5
0
int
main(int argc, char **argv)
{
	int		fd;
	long	i, seqno;
	pid_t	pid;
	ssize_t	n;
	char	line[MAXLINE + 1];

	pid = getpid();
	fd = open(SEQFILE, O_RDWR, FILE_MODE);

	for (i = 0; i < 20; i++) {
		my_lock(fd);				/* lock the file */

		lseek(fd, 0L, SEEK_SET);	/* rewind before read */
		n = read(fd, line, MAXLINE);
		line[n] = '\0';				/* null terminate for sscanf */

		n = sscanf(line, "%ld\n", &seqno);
		printf("%s: pid = %ld, seq# = %ld\n", argv[0], (long) pid, seqno);

		seqno++;					/* increment sequence number */

		snprintf(line, sizeof(line), "%ld\n", seqno);
		lseek(fd, 0L, SEEK_SET);	/* rewind before write */
		write(fd, line, strlen(line));

		my_unlock(fd);				/* unlock the file */
	}
	exit(0);
}
Exemple #6
0
int
main(int argc, char **argv)
{
	int		fd;
	long	i, nloop, seqno;
	ssize_t	n;
	char	line[MAXLINE + 1];

	if (argc != 2)
		err_quit("usage: loopmain <loopcount>");
	nloop = atol(argv[1]);

	fd = Open(SEQFILE, O_RDWR, FILE_MODE);

	for (i = 0; i < nloop; i++) {
		my_lock(fd);				/* lock the file */

		Lseek(fd, 0L, SEEK_SET);	/* rewind before read */
		n = Read(fd, line, MAXLINE);
		line[n] = '\0';				/* null terminate for sscanf */

		n = sscanf(line, "%ld\n", &seqno);
		seqno++;					/* increment sequence number */

		snprintf(line, sizeof(line), "%ld\n", seqno);
		Lseek(fd, 0L, SEEK_SET);	/* rewind before write */
		Write(fd, line, strlen(line));

		my_unlock(fd);				/* unlock the file */
	}
	exit(0);
}
Exemple #7
0
int main(int argc, char **argv)
{
	int fd, i, seqno, c;
	pid_t pid;
	ssize_t	n;
	char line[MAXLINE + 1], *ptr;

	pid = getpid();
	if ((fd = open(SEQFILE, O_RDWR | O_CREAT, FILE_MODE)) == -1) {
		printf("open error for %s: %s\n", SEQFILE, strerror(errno));
		exit(1);
	}

	setvbuf(stdout, NULL, _IONBF, 0);	/* unbuffered */

	for (i = 0; i < 20; i++) {
		my_lock(fd);				/* lock the file */

		if (lseek(fd, 0L, SEEK_SET) == (off_t) -1) {	/* rewind before read */
			perror("lseek error");
			exit(1);
		}
		if ((n = read(fd, line, MAXLINE)) < 0) {
			perror("read error");
			exit(1);
		}
		line[n] = '\0';				/* null terminate for sscanf */

		n = sscanf(line, "%d\n", &seqno);
		snprintf(line, sizeof(line), "pid = %d, seq# = %d\n", pid, seqno);
		for (ptr = line; (c = *ptr++) != 0; )
			putchar(c);

		seqno++;					/* increment sequence number */

		sprintf(line, "%03d\n", seqno);
		if (lseek(fd, 0, SEEK_SET) == (off_t) -1) {		/* rewind before write */
			perror("lseek error");
			exit(1);
		}
		if (write(fd, line, strlen(line)) != strlen(line)) {
			perror("write error");
			exit(1);
		}

		my_unlock(fd);				/* unlock the file */
	}
	exit(0);
}
Exemple #8
0
int
main(int argc, char **argv)
{
	int		fd;
	long	i, nloop, seqno;
	ssize_t	n;
	char	line[MAXLINE + 1];

	if (argc != 2) {
		fprintf(stderr, "usage: loopmain <loopcount>\n");
		exit(1);
	}
	nloop = atol(argv[1]);

	if ((fd = open(SEQFILE, O_RDWR | O_NONBLOCK, FILE_MODE)) == -1) {
		fprintf(stderr, "open error for %s: %s\n", SEQFILE, strerror(errno));
		exit(1);
	}

	for (i = 0; i < nloop; i++) {
		my_lock(fd);				/* lock the file */

		if (lseek(fd, 0L, SEEK_SET) == (off_t) -1) {	/* rewind before read */
			perror("lseek error");
			exit(1);
		}
		if ((n = read(fd, line, MAXLINE)) == -1) {
			perror("read error");
			exit(1);
		}
		line[n] = '\0';				/* null terminate for sscanf */

		n = sscanf(line, "%ld\n", &seqno);
		seqno++;					/* increment sequence number */

		snprintf(line, sizeof(line), "%ld\n", seqno);
		if (lseek(fd, 0L, SEEK_SET) == (off_t) -1) {	/* rewind before write */
			perror("lseek error");
			exit(1);
		}
		if (write(fd, line, strlen(line)) != strlen(line)) {
			perror("write error");
			exit(1);
		}

		my_unlock(fd);				/* unlock the file */
	}
	exit(0);
}
Exemple #9
0
static void counter_func(struct work_struct *work)
{
	unsigned int cpu = smp_processor_id();
	int i;

	pr_info("Started job #%u\n", cpu);

	for (i = 0; i < TIMES_INC; i++) {
		my_lock(&count_lock);
		count++;
		my_unlock(&count_lock);
	}

	pr_info("finished job #%u\n", cpu);
}
Exemple #10
0
int
main(int argc, char **argv)
{
	int		fd = 0, stat, nconflicts;
	long	i, j, nproc;
	sem_t	*ptr;
	pid_t	pid;
	ssize_t	n;

	if (argc != 2)
		err_quit("usage: locksvsemrace1 <#processes>");
	nproc = atol(argv[1]);
	Pipe(pipefd);

	ptr = My_shm(sizeof(sem_t));	/* create memory-based semaphore */
	Sem_init(ptr, 1, 0);

	for (j = 0; j < nproc; j++) {
		if (Fork() == 0) {
				/* 4child */
			Sem_wait(ptr);		/* wait for parent to start children */
			for (i = 0; i < 10; i++) {
				my_lock(fd);		/* lock the file */
				my_unlock(fd);		/* unlock the file */
			}
			exit(0);
		}
		/* parent loops around, creating next child */
	}
	for (j = 0; j < nproc; j++)
		Sem_post(ptr);	/* start all the children */

	/* now just wait for all the children to finish */
	while ( (pid = waitpid(-1, &stat, WNOHANG)) > 0)
		;
	Close(pipefd[1]);
	nconflicts = 0;
	while ( (n = Read(pipefd[0], &stat, 1)) > 0)
		nconflicts += n;
	printf("%d conflicts\n", nconflicts);
	exit(0);
}
Exemple #11
0
static void* engine_thread(void* p) {

    while(!has_first_table) {}

    eng_ctx_t* eng_ctx = eng_context_init(p);

    while (is_thread_run) {
        eng_context_config(eng_ctx);
        travel_command_list(eng_ctx);

        eng_context_free_rwservers(eng_ctx);
    }

    my_lock(&total_request_lock);
    total_request += eng_ctx->stats.ninsert;
    total_request += eng_ctx->stats.nupdate;
    total_request += eng_ctx->stats.ndelete;
    total_request += eng_ctx->stats.nselect;
    my_unlock(&total_request_lock);

    eng_context_destroy(eng_ctx);

    return NULL;
}
void down(int me)
{
	my_unlock(chopstick[me]);
	my_unlock(chopstick[(me+1)%5]);

}