Exemplo n.º 1
0
int main(int argc, char *argv[])
{
	pid_t pid;
	TELL_WAIT();
	pid = Fork();
	if (pid == 0)
	{	
		while ( n < 1000)
		{
			WAIT_PARENT();
			printf(" c%d ", n);
			n += 2;
			TELL_PARENT(getppid());
			//sleep(1);
		}
		
	}
	else if(pid > 0)
	{
		while (n < 1000)
		{
			
			printf(" p%d ", n);
			n += 2;
			TELL_CHILD(pid);
		//	sleep(1);
			WAIT_CHILD();
			
		}
		
	}
	printf("Hello, world\n");
	
	return 0;
}
Exemplo n.º 2
0
int main(void) {
	pid_t	pid, mypid;

	if((pid = fork()) < 0)
		err_sys("fork error");
	else if(pid > 0) {	// parent
		mypid = getpid();
		TELL_WAIT();

		printf("%d: WAIT_CHILD()\n", mypid);
		WAIT_CHILD();
		printf("%d: wake up\n", mypid);

		sleep(2);
		printf("%d: TELL_CHILD()\n", mypid);
		TELL_CHILD(pid);
	} else {			// child
		mypid = getpid();
		TELL_WAIT();

		sleep(2);
		printf("%d: TELL_PARENT()\n", mypid);
		TELL_PARENT(getppid());

		printf("%d: WAIT_PARENT()\n", mypid);
		WAIT_PARENT();

		printf("%d: wake up\n", mypid);
	}

	return 0;
}
Exemplo n.º 3
0
/* gcc apue.h apue_err.c figure-14.5.c figure-10.24.c figure-14.7.c */
int
main(void)
{
    int     fd;
    pid_t   pid;

    /*
     * Create a file and write two bytes to it.
     */
    if ((fd = creat("templock", FILE_MODE)) < 0)
        err_sys("creat error");
    if (write(fd, "ab", 2) != 2)
        err_sys("write error");

    TELL_WAIT();        /* figure-10.24.c */
    if ((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid == 0) {  /* child */
        lockabyte("child", fd, 0);
        TELL_PARENT(getppid());
        WAIT_PARENT();
        lockabyte("child", fd, 1);
    } else {                /* parent */ 
        lockabyte("parent", fd, 1);
        TELL_CHILD(pid);
        WAIT_CHILD();
        lockabyte("parent", fd, 0);
    }
    exit(0);
}
Exemplo n.º 4
0
int main(int argc, char *argv[])
{
	int		fd;
	pid_t	pid;

	/*Create a file and write two bytes to it*/
	fd = Open("templock", O_RDWR | O_CREAT | O_TRUNC, FILE_MODE) ;

	if(write(fd, "ab", 2) != 2)
		err_sys("write error");

	TELL_WAIT();
	pid = Fork();

	if(pid == 0)
	{
		lockabyte("child", fd, 0);
		TELL_PARENT(getppid());
		WAIT_PARENT();
		lockabyte("child", fd, 1);
	}else 
	{
		lockabyte("parent", fd, 1);
		TELL_CHILD(pid);
		WAIT_CHILD();
		lockabyte("parent", fd, 0);
	}
	return 0;
}
Exemplo n.º 5
0
/* increment counter in shared memory segment; mmap /dev/zero version */ 
int main(void)
{
	int fd, i, counter;
	pid_t pid;
	void *area;

	if ((fd = open("/dev/zero", O_RDWR)) < 0)
		err_sys("open error");
	if ((area = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
			 fd, 0)) == MAP_FAILED)
		err_sys("mmap error");
	close(fd);		/* can close /dev/zero now that it is mapped */

	TELL_WAIT();

	if ((pid = Fork()) > 0) { /* parent */
		for (i = 0; i < NLOOPS; i += 2) {
			if ((counter = update((long *)area)) != i)
				err_quit("parent: expected %d, got %d", i, counter);
			TELL_CHILD(pid);
			WAIT_CHILD();
		}
	} else {		/* child */
		for (i = 1; i < NLOOPS + 1; i += 2) {
			WAIT_PARENT();
			if ((counter = update((long *)area)) != i)
				err_quit("child: expected %d, got %d", i, counter);
			TELL_PARENT();
		}
	}
	exit(0);
}
Exemplo n.º 6
0
int main (void)
{
	pid_t	pid = 0, ppid = 0, childpid = 0;
	int		i = 0;

	init ();

	if ((pid = fork ()) < 0) {
		err_sys ("fork error");
	} else if (pid == 0) { /* child block */
		pid = getpid ();
		ppid = getppid ();
		for (i = 0; i < 100; i ++) {
			WAIT_PARENT ();
			increment (pid);
			TELL_PARENT (ppid);
		}
	} else { /* parent block */
		childpid = pid;
		pid = getpid ();
		for (i = 0; i < 100; i ++) {
			increment (pid);
			TELL_CHILD (childpid);
			WAIT_CHILD ();
		}
	}
	exit (0);
}
Exemplo n.º 7
0
int main(void)
{
    int fd;
    pid_t pid;

    if ((fd = creat("templock", FILE_MODE)) < 0)
        err_sys("creat error");

    if (write(fd, "ab", 2) != 2)
        err_sys("write error");

    TELL_WAIT();

    if ((pid = fork()) < 0){
        err_sys("fork error");
    }else if(pid == 0){/*child*/
        lockabyte("child", fd, 0);

        TELL_PARENT(getppid());
        WAIT_PARENT();

        lockabyte("child", fd, 1);
    }else{
        lockabyte("parent", fd, 1);
        TELL_CHILD(pid);
        WAIT_CHILD();
        lockabyte("parent", fd, 0);
    }

    exit(0);
}
Exemplo n.º 8
0
int
main(int argc, char *argv[])
{
	int					fd;
	pid_t				pid, cpid;

	if ((fd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, FILE_MODE)) < 0)
		err_sys("open error");
	if (write(fd, "abcdef", 6) != 6)
		err_sys("write error");

	if ((pid = fork()) < 0) {
		err_sys("fork error");
	} else if (pid > 0) {
		WAIT_CHILD();
		if (write_lock(fd, 0, SEEK_SET, 1) < 0)
			err_sys("write_lock error");
		printf("gain write lock\n");
	} else {
		if (read_lock(fd, 0, SEEK_SET, 1) < 0)
			err_sys("read_lock error");
		printf("gain read lock 1\n");
		TELL_PARENT(getppid());
		if ((cpid = fork()) < 0) {
			err_sys("fork error");
		} else if (cpid > 0) {
			if (read_lock(fd, 0, SEEK_SET, 1) < 0)
				err_sys("read_lock error");
			printf("gain read lock 2\n");
		}
	}
	return 0;
}
Exemplo n.º 9
0
int main(int argc, char* argv[])
{
	int fd;
	pid_t pid;

	if((fd = creat("templock", FILEMODE)) < 0)
		printf("creat error");
	if(write(fd, "ab", 2) != 2)
		printf("write error");

	TELL_WAIT();
	if((pid = fork()) < 0)
		printf("fork error");
	else if(pid == 0)
	{
		lockabyte("child:", fd, 0);
		TELL_PARENT(getpid());
		WAIT_PARENT();
		lockabyte("child:", fd, 1);
	}
	else
	{
		lockabyte("parent:", fd, 1);
		TELL_CHILD(pid);
		WAIT_CHILD();
		lockabyte("parent:", fd, 0);
	}
	exit(0);
}
Exemplo n.º 10
0
Arquivo: a.c Projeto: klion26/APUE
int main(void)
{
    pid_t pid;
    FILE *fp;
    int fd;
    int i = 0;
    char *child = "in child\n";
    char *parent = "in parent\n";

    if ((fp = fopen("tmp", "a")) == NULL) {
        perror("fopen error");
        exit(1);
    }

    fd = fileno(fp);
    /* if (write(fd, pi, 1) != 1) { */
    /*     perror("write error"); */
    /*     exit(1); */
    /* } */
    fprintf(fp, "%d\n", i);
    fflush(fp);

    TELL_WAIT();
    if ((pid = fork()) < 0) { /* pid = fork()要用括号括起来 */
        perror("fork error");
        exit(1);
    } else if (pid == 0) {
        while (i < 100) {
            WAIT_PARENT();
            /* if (write(fd, pi, 1) != 1) { */
            /*     perror("write error"); */
            /*     exit(1); */
            /* } */
            i += 2;
            fprintf(fp, "%d\t", i);
            fflush(fp);

            write(fd, child, strlen(child));   
            TELL_PARENT(getppid());
        }
    } else {
        i++;
        while (i < 100) {
            /* if (write(fd, pi, 1) != 1) { */
            /*     perror("write error"); */
            /*     exit(1); */
            /* } */
            
            fprintf(fp, "%d\t", i);
            fflush(fp);
            i += 2;
            write(fd, parent, strlen(parent));
            TELL_CHILD(pid);
            WAIT_CHILD();
        }
    }

    return 0;
}
Exemplo n.º 11
0
int main(int argc,char *argv[]){
    pid_t pid;
    //set up for TELL** and WAIT**
    TELL_WAIT();
    if((pid=fork())<0){
        perror("fork error:");
        return -1;
    }else if(pid>0){
        WAIT_CHILD();
        charatatime("output from parent\n");
    }else{
        charatatime("output from child\n");
        TELL_PARENT(pid);
    }
    return 0;
}
int main()
{
    int fd = open(path, O_CREAT|O_TRUNC|O_RDWR, 0644);
    write(fd, "0", 1);
    char inbuf[20];
    char outbuf[20];
    int num;
    TELL_WAIT();
    pid_t pid;
    pid = fork();
    if(pid<0) {
        perror("fork error");
        return 1;
    } else if(pid == 0) { //child
        for(int i=0;i<10;++i) {
            lseek(fd, 0, SEEK_SET);
            ssize_t len = read(fd, inbuf, 20);
            if(len<0) break;
            inbuf[len] = 0;
            fprintf(stderr, "CHILD read %s\n",inbuf);//debug
            sscanf(inbuf,"%d",&num);
            ++num;
            lseek(fd, 0, SEEK_SET);
            sprintf(outbuf, "%d", num);
            write(fd, outbuf, strlen(outbuf));
            fprintf(stderr, "CHILD write %s\n",outbuf);//debug
            TELL_PARENT(getppid());
            WAIT_PARENT();
        }
    } else { //parent
        for(int i=0;i<10;++i) {
            WAIT_CHILD();
            lseek(fd, 0, SEEK_SET);
            ssize_t len = read(fd, inbuf, 20);
            if(len<0) break;
            inbuf[len] = 0;
            fprintf(stderr, "PARENT read %s\n",inbuf);//debug
            sscanf(inbuf,"%d",&num);
            ++num;
            lseek(fd, 0, SEEK_SET);
            sprintf(outbuf, "%d", num);
            write(fd, outbuf, strlen(outbuf));
            fprintf(stderr, "PARENT write %s\n",outbuf);//debug
            TELL_CHILD(pid);
        }
    }
}
Exemplo n.º 13
0
int main(void)
{
    pid_t pid;

    TELL_WAIT();

    if ((pid = fork()) < 0) {
        err_sys("fork error");
    } else if (pid == 0) {
        charactatime("output from child\n");
        TELL_PARENT(getppid());      /* child goes first  */
    } else {
        WAIT_CHILD();
        charactatime("output from parent\n");
    }
    exit(0);
}
Exemplo n.º 14
0
int main(int argc, char const *argv[])
{
    pid_t pid;

    TELL_WAIT();

    if ((pid = fork()) < 0) {
        fprintf(stderr, "fork error\n");
        exit(1);
    } else if (pid == 0) {
        charatatime("output from child\n");
        TELL_PARENT(getppid());
    } else {
        WAIT_CHILD();        /* child goes first */
        charatatime("output from parent\n");
    }
    return 0;
}
Exemplo n.º 15
0
int main()
{
	int fid;
	pid_t pid;
	if((fid = open("number.txt", O_WRONLY|O_CREAT|O_TRUNC))<0)
		err_sys("create file error...\n");
	
	sprintf(str,"%d",number);
	write(fid,str,strlen(str));
	close(fid);
	
	TELL_WAIT();
	
	if((pid = fork())<0)
		err_sys("fork error");
	else if(pid==0)
	{
		while((getNumber("number.txt")+1)<1000)
		{
			WAIT_PARENT();
			number  = getNumber("number.txt");
			setNumber("number.txt",number+1);
			printf("Pid = %d,Number = %d\n",getpid(),number+1);
			TELL_PARENT(getppid());
		}
	}
	else
	{
		while((getNumber("number.txt")+1)<1000)
		{
			number  = getNumber("number.txt");
			setNumber("number.txt",number+1);
			printf("Pid = %d,Number = %d\n",getpid(),number+1);
			TELL_CHILD(pid);
			WAIT_CHILD();
		}
		
		
	}
	
	
	close(fid);
	return 0;
}
Exemplo n.º 16
0
int main(void) {
	pid_t	pid;

	TELL_WAIT();

	if((pid = fork()) < 0)
		err_sys("fork error");
	else if (pid == 0) {
		WAIT_PARENT();		/* parent goes first */
		charactatime("output from child\n");
		TELL_PARENT(getppid());
	}
	else {
		charactatime("output from parent\n");
		TELL_CHILD(pid);
		WAIT_CHILD();
	}

	return 0;
}
Exemplo n.º 17
0
/* child goes first program*/
int main()
{
	pid_t pid;
	
	TELL_WAIT();
	
	pid = fork();
	if(pid < 0) {
		printf("fork error\n");
	}
	else if(pid == 0) {
		do_task("child task\n");
		TELL_PARENT(getppid());
	}
	else {
		WAIT_CHILD();
		do_task("parent task\n");
	}
	
	return 0;
}
Exemplo n.º 18
0
int main(int argc, char *argv[])
{
	char line[MAXLINE];
	int n;
	pid_t pid;
	int fd;

	if (argc != 2)
		err_sys("usage: a.out <pathname>");
	if ((fd = open(argv[1], O_RDWR)) < 0)
		err_sys("open %s file failed", argv[1]);

	memset(line, '\0', MAXLINE);
	TELL_WAIT();
	pid = fork();
	switch (pid) {
		case -1://error
			err_sys("fork error");
		case 0://child
			WAIT_PARENT();
			lseek(fd, SEEK_SET, 0);
			if ((n = read(fd, line, MAXLINE)) < 0)
				err_sys("read error form %s", argv[1]);
			printf("read %d bytes:", n);
			fflush(stdout);
			write(STDOUT_FILENO, line, n);
			TELL_PARENT(getppid());
			break;
		default://parent
			sleep(2);
			n = write(fd, "hello child\n", 13);
			printf("write %d bytes\n", n);
			fflush(stdout);
			TELL_CHILD(pid);
			WAIT_CHILD();
			break;
	}
	close(fd);
	exit(0);
}
Exemplo n.º 19
0
int test_record_lock()
{
	int ret;
	int fd;
	pid_t pid;
	char *name;

	name = tmpnam(NULL);
	if((fd = creat(name, FILE_MODE)) < 0) {
		err_sys("create");
	}

	if (write(fd, "ab", 2) != 2)
		err_sys("write");

	TELL_WAIT();
	if ((pid = fork()) < 0)
		err_sys("fork");
	else if (pid == 0) {
		lockbyte("child", fd, 0);
		TELL_PARENT(getpid());
		WAIT_PARENT();
		lockbyte("child", fd, 1);
		sleep(2);
		exit(0);
	} else {
		lockbyte("parent", fd, 1);
		TELL_CHILD(getpid());
		WAIT_CHILD();
		lockbyte("parent", fd, 0);

		close(fd);
		sleep(1);
		unlink(name);
	}

	return 0;
}
Exemplo n.º 20
0
int main(int argc, char *argv[])
{
    int fd;
    pid_t pid;
    void *addr = NULL;

    fd = open("/dev/zero", O_RDWR);
    if (fd < 0)
    {
        perror("open failure: ");
        return EXIT_FAILURE;
    }
    
    addr = mmap(NULL, sizeof(int), PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_SHARED, fd, 0);
    if (addr == NULL)
    {
        perror("mmap failure: ");
        return EXIT_FAILURE;
    }

    close(fd); // mmap success and close fd

    TELL_WAIT();

    if ((pid = fork()) < 0)
    {
        perror("fork failure: ");
        return EXIT_FAILURE;
    }
    else if (pid == 0)
    {
        int i = 0;
        pid = getpid();

        for (i = 0; i < 1000; i+=2)
        {
            if (update((int *)addr) != i)
            {
                fprintf(stderr, "child process sync failure, i = %d\n", i);
                return EXIT_FAILURE;
            }

            printf("child start ##############\n");
            TELL_PARENT(pid);
            WAIT_PARENT();
        }
    }
    else
    {
        int i = 0;
        pid = getpid();

        for (i = 1; i < 1000; i+=2)
        {
            WAIT_CHILD();
            printf("parent start   @@@@@@@@@@@@@@\n");

            if (update((int *)addr) != i)
            {
                fprintf(stderr, "parent process sync failure, i = %d\n", i);
                return EXIT_FAILURE;
            }

            TELL_CHILD(pid);
        }
    }

    munmap(addr, sizeof(int));

    return EXIT_SUCCESS;
}
Exemplo n.º 21
0
int servopen(char* host, char* port)
{
    int fd, newfd, i, on, pid;
    const char* protocol;
    struct in_addr inaddr;
    struct servent* sp;

    protocol = udp ? "udp" : "tcp";

    /* Initialize the socket address structure */
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;

    /* Caller normally wildcards the local Internet address, meaning
           a connection will be accepted on any connected interface.
           We only allow an IP address for the "host", not a name. */
    if (host == NULL)
        servaddr.sin_addr.s_addr = htonl(INADDR_ANY); /* wildcard */
    else {
        if (inet_aton(host, &inaddr) == 0)
            err_quit("invalid host name for server: %s", host);
        servaddr.sin_addr = inaddr;
    }

    /* See if "port" is a service name or number */
    if ((i = atoi(port)) == 0) {
        if ((sp = getservbyname(port, protocol)) == NULL)
            err_ret("getservbyname() error for: %s/%s", port, protocol);

        servaddr.sin_port = sp->s_port;
    } else
        servaddr.sin_port = htons(i);

    if ((fd = socket(AF_INET, udp ? SOCK_DGRAM : SOCK_STREAM, 0)) < 0)
        err_sys("socket() error");

    if (reuseaddr) {
        on = 1;
        if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
            err_sys("setsockopt of SO_REUSEADDR error");
    }

#ifdef SO_REUSEPORT
    if (reuseport) {
        on = 1;
        if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &on, sizeof(on)) < 0)
            err_sys("setsockopt of SO_REUSEPORT error");
    }
#endif

    /* Bind our well-known port so the client can connect to us. */
    if (bind(fd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0)
        err_sys("can't bind local address");

    join_mcast(fd, &servaddr);

    if (udp) {
        buffers(fd);

        if (foreignip[0] != 0) { /* connect to foreignip/port# */
            bzero(&cliaddr, sizeof(cliaddr));
            if (inet_aton(foreignip, &cliaddr.sin_addr) == 0)
                err_quit("invalid IP address: %s", foreignip);
            cliaddr.sin_family = AF_INET;
            cliaddr.sin_port = htons(foreignport);
            /* connect() for datagram socket doesn't appear to allow
                   wildcarding of either IP address or port number */

            if (connect(fd, (struct sockaddr*)&cliaddr, sizeof(cliaddr)) < 0)
                err_sys("connect() error");
        }

        sockopts(fd, 1);

        return (fd); /* nothing else to do */
    }

    buffers(fd);     /* may set receive buffer size; must do here to get
                               correct window advertised on SYN */
    sockopts(fd, 0); /* only set some socket options for fd */

    listen(fd, listenq);

    if (pauselisten)
        sleep_us(pauselisten * 1000); /* lets connection queue build up */

    if (dofork) TELL_WAIT(); /* initialize synchronization primitives */

    for (;;) {
        i = sizeof(cliaddr);
        if ((newfd = accept(fd, (struct sockaddr*)&cliaddr, &i)) < 0)
            err_sys("accept() error");

        if (dofork) {
            if ((pid = fork()) < 0) err_sys("fork error");

            if (pid > 0) {
                close(newfd); /* parent closes connected socket */
                WAIT_CHILD(); /* wait for child to output to terminal */
                continue;     /* and back to for(;;) for another accept() */
            } else {
                close(fd); /* child closes listening socket */
            }
        }

        /* child (or iterative server) continues here */
        if (verbose) {
            /* Call getsockname() to find local address bound to socket:
                   local internet address is now determined (if multihomed). */
            i = sizeof(servaddr);
            if (getsockname(newfd, (struct sockaddr*)&servaddr, &i) < 0)
                err_sys("getsockname() error");

            /* Can't do one fprintf() since inet_ntoa() stores
                           the result in a static location. */
            fprintf(stderr, "connection on %s.%d ",
                    INET_NTOA(servaddr.sin_addr), ntohs(servaddr.sin_port));
            fprintf(stderr, "from %s.%d\n", INET_NTOA(cliaddr.sin_addr),
                    ntohs(cliaddr.sin_port));
        }

        buffers(newfd);     /* setsockopt() again, in case it didn't propagate
                                   from listening socket to connected socket */
        sockopts(newfd, 1); /* can set all socket options for this socket */

        if (dofork)
            TELL_PARENT(getppid()); /* tell parent we're done with terminal */

        return (newfd);
    }
}