Exemplo n.º 1
0
void
child_main(int i, int listenfd, int addrlen)
{
	int				connfd;
	void			web_child(int);
	socklen_t		clilen;
	struct sockaddr	*cliaddr;

	if ((cliaddr = malloc(addrlen)) == NULL) {
		perror("malloc error");
		exit(1);
	}

	printf("child %ld starting\n", (long) getpid());
	for ( ; ; ) {
		clilen = addrlen;
		my_lock_wait();
		if ((connfd = accept(listenfd, cliaddr, &clilen)) < 0) {
			perror("accept error");
			exit(1);
		}
		my_lock_release();

		web_child(connfd);		/* process the request */
		if (close(connfd) == -1) {
			perror("close error");
			exit(1);
		}
	}
}
Exemplo n.º 2
0
int main(int argc, char **argv)
{
    int fd, zero = 0;
    int *ptr;
    pid_t pid;
    struct stat buf;

    if (argc != 2) {
        perror("usage: incr2 <pathname>"); 
        exit(1); 
    }

    /* open file, initialize to 0, map into memory */
    // stat() in <sys/stat.h>, check whether this is the first to open the file
    if (stat(argv[1], &buf) == -1 && errno == ENOENT) {
        /* first time, using O_CREAT */
        if ((fd = open(argv[1], O_RDWR | O_CREAT, FILE_MODE)) < 0) {
            perror("create failed"); 
            exit(1);
        }
        if (write(fd, &zero, sizeof(int)) <0) {
            perror("write failed"); 
            exit(1);
        }
    } else if ((fd = open(argv[1], O_RDWR, FILE_MODE)) < 0) {
        perror("open failed"); 
        exit(1);
    }
    
    ptr = (int *)mmap(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    if (ptr == MAP_FAILED) {
        perror("mmap failed"); 
        exit(1); 
    }

    /* create, initialize, and unlink file lock */
    my_lock_init(fd);

    setbuf(stdout, NULL);   /* stdout is unbuffered */
    
    my_lock_wait();
    printf("pid %d: get lock\n", getpid());
    sleep(5);
    printf("pid %d: %d\n", getpid(), (*ptr)++);
    my_lock_release();

    close(fd);
    unlink(argv[1]);
    exit(0);
}
Exemplo n.º 3
0
int
main(int argc, char **argv)
{
	int		i, j, fd, nchildren, nloops, nbytes;
	pid_t	pid;
	ssize_t	n;
	char	request[MAXLINE], reply[MAXN];
	int count = 0;

	if (argc != 6)
		err_quit("usage: client <hostname or IPaddr> <port> <#children> "
				 "<#loops/child> <#bytes/request>");

	nchildren = atoi(argv[3]);
	nloops = atoi(argv[4]);
	nbytes = atoi(argv[5]);
	snprintf(request, sizeof(request), "%d\n", nbytes); /* newline at end */
	my_lock_init(NULL);

	for (i = 0; i < nchildren; i++) {
		if ( (pid = Fork()) == 0) {		/* child */
			for (j = 0; j < nloops; j++) {
				fd = Tcp_connect(argv[1], argv[2]);

				/*printf("%d : %d\n", getpid(), j);
				Write(fd, request, strlen(request));

				if ( (n = Readn(fd, reply, nbytes)) != nbytes)
					err_quit("server returned %d bytes", n);

				Close(fd);*/		/* TIME_WAIT on client, not server */
			}
			my_lock_wait();
			printf("child %d done\n", i);
			my_lock_release();
			sleep(10);
			exit(0);
		}
		/* parent loops around to fork() again */
	}

	while (wait(NULL) > 0)	/* now parent waits for all children */
		;
	if (errno != ECHILD)
		err_sys("wait error");
	printf("all child: %d\n", count);
	exit(0);
}
Exemplo n.º 4
0
void
child_main(int i, int listenfd, int addrlen)
{
	int				connfd;
	void			web_child(int);
	socklen_t		clilen;
	struct sockaddr	*cliaddr;

	cliaddr = Malloc(addrlen);

	printf("child %ld starting\n", (long) getpid());
	for ( ; ; ) {
		clilen = addrlen;
		my_lock_wait();
		connfd = Accept(listenfd, cliaddr, &clilen);
		my_lock_release();

		web_child(connfd);		/* process the request */
		Close(connfd);
	}
}
Exemplo n.º 5
0
void child_main(int fd,socklen_t addrlen)
{
	socklen_t clilen;
	struct sockaddr *cliaddr;
	int	connfd;

	cliaddr = malloc(addrlen);
	for(;;)
	{
		clilen = addrlen;
		my_lock_wait();
		if((connfd = accept(listenfd,cliaddr,&clilen)) < 0)
		{
			if(errno = EINTR)
				continue;
			else
				err_quit("accept error!");
		}
		my_lock_release();
		web_child(connfd);
	}
}
Exemplo n.º 6
0
void
child_main(int i, int listenfd, int addrlen)
{
	int connfd, epollfd, n, nfds, nconn = 1;
	socklen_t		clilen;
	struct sockaddr	*cliaddr;
	struct epoll_event ev, events[MAX_EVENTS];
	pthread_t tid;

	cliaddr = Malloc(addrlen);

	setnonblocking(listenfd);
	epollfd = epoll_create(MAX_EVENTS);
	if (-1 == epollfd)
		err_sys("epoll_create");

	ev.events = EPOLLIN;
	ev.data.fd = listenfd;
	if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, listenfd, &ev))
		err_sys("epoll_ctr");
	printf("child %ld starting\n", (long) getpid());
	clilen = addrlen;
	/*if (0 != pthread_create(&tid, NULL, child_thread, &epollfd))
		err_sys("pthread_create");*/
	for ( ; ; ) {
		nfds = epoll_wait(epollfd, events, MAX_EVENTS, -1);
		printf("nfds = %d, connections = %d\n", nfds, nconn);
		if (-1 == nfds)
			err_sys("epoll_wait");
		for (n = 0; n < nfds; n++)
		{
			if (events[n].data.fd == listenfd && nconn <= MAX_EVENTS)
			{
				if (my_lock_wait())
					continue;
				connfd = accept(listenfd, cliaddr, &clilen);
				if (connfd == -1)
				{
					my_lock_release();
					continue;
				}
				my_lock_release();
				printf("%d:recevice one request\n", getpid());
				nconn++;
				setnonblocking(connfd);
				ev.events = EPOLLIN;
				ev.data.fd = connfd;
				if (-1 == epoll_ctl(epollfd, EPOLL_CTL_ADD, connfd, &ev))
					err_sys("epoll_ctl");	
			}
			else
			{
				if (events[n].data.fd < 0)	
					continue;
				web_child(events[n].data.fd);
				if (-1 == epoll_ctl(epollfd, EPOLL_CTL_DEL, events[n].data.fd, NULL))
					err_sys("epoll_ctl");
				Close(events[n].data.fd);
				nconn--;
			}
		}
		
	}
}
Exemplo n.º 7
0
int main(int argc, char **argv)
{
    int fd, i, num_loop, zero = 0;
    char c, lock_f[128];
    struct stat st;
    int *iptr;
    char *cptr;

    if(argc != 4) {
        printf("usage: %s <pathname> <loop number> <char>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if(is_int(argv[2]))
        num_loop = atoi(argv[2]);

    c = argv[3][0];

    i = stat(argv[1], &st); /* first one will get -1*/

    snprintf(lock_f, sizeof(lock_f), "%s.lock", argv[1]);

    if((fd = open(argv[1], O_RDWR | O_CREAT, F_MODE)) < 0) {
        perror("open() failed\n");
        exit(EXIT_FAILURE);
    }

    if(i == -1) { /* initialze */
        write(fd, &zero, sizeof(int)); /* int counter*/
        for(i = 0; i < SIZE; i++) {
            write(fd, &zero, 1);
        }
    }

    iptr = (int *)mmap(NULL, SIZE+sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);

    if(iptr == MAP_FAILED) {
        perror("mmap failed\n");
        exit(EXIT_FAILURE);
    }

    close(fd); /* POSIX recommendation */

    cptr = (char *)(&iptr[1]);

    my_lock_init(lock_f);
    setbuf(stdout, NULL); /* stdout unbuffer */

    for(i = 0; i < num_loop; i++) {
        my_lock_wait();

        if(*iptr >= (SIZE-1)) {
            my_lock_release();
            exit(EXIT_SUCCESS);
        }

        //
        for ( ; ; ) {
            if ((cptr[*iptr - 1]) == c && *iptr != 0) {
                my_lock_release();
                usleep(100);
                my_lock_wait();
            } else {
                break;
            }
        }

        cptr[*iptr] = c;
        printf("%c: %d\n", c, (*iptr)++);
        my_lock_release();
        // usleep(800000);
    }

    return 0;
}